Description
AFNetworking 4.0.1 checks the system trust chain and the requested hostname by default, even without certificate or public-key pinning. This applies to AFSecurityPolicy.default() and pinning mode .none.
If you set validatesDomainName to false on that policy, AFNetworking uses a basic X.509 policy without the requested hostname. A trusted certificate issued for a different host may then be accepted, allowing someone on the network path to impersonate the server.
For a named host, AFNetworking 4.0.1 rejects allowInvalidCertificates = true when hostname validation is enabled and pinning is off. Turning off hostname validation too can allow arbitrary certificates. Securely provisioned certificate or public-key pins can identify a specific peer.
AFNetworking was deprecated on January 17, 2023 and no longer releases updates.
Potential impact
- A man-in-the-middle attack using a certificate issued by a trusted certificate authority for a different hostname.
- Reading or modifying server responses and request data.
- Theft of sensitive information in transit, including session cookies, authentication tokens, and personal information.
Remediation
- For
default()or.none, keepvalidatesDomainNameset totrue. Use a publicly trusted certificate. If the server uses a self-signed certificate, securely provision its certificate or public-key pin before connecting. Keep hostname checks on and avoidallowInvalidCertificates = truein an unpinned policy. - If the service requires pinning, choose
.certificateor.publicKeyfor the required hosts. Prepare backup pins, plan for key rotation, and block connections when a pin check fails. - Migrate to the default trust handling in
URLSessionor a maintained library such as Alamofire (stable release 5.12.0 as of August 30, 2026). With a customDefaultTrustEvaluator, keepvalidateHost: trueunless peer-specific pinning explicitly verifies the service identity. - Test the release configuration with a mismatched hostname, an expired certificate, and an untrusted issuer. Each case should fail.
Examples
Before
import AFNetworking
func insecurePolicy() -> AFNetworking.AFSecurityPolicy {
let policy = AFNetworking.AFSecurityPolicy.default()
policy.validatesDomainName = false
return policy
}
After
import AFNetworking
func standardPolicy() -> AFNetworking.AFSecurityPolicy {
let policy = AFNetworking.AFSecurityPolicy.default()
policy.validatesDomainName = true
return policy
}
validatesDomainName = true explicitly retains the default hostname check alongside the system trust-chain check.
When migrating to URLSession, the platform can handle the server trust challenge by default.
import Foundation
func standardSession() -> URLSession {
URLSession(configuration: .default)
}
References
- AFNetworking 4.0.1 - AFSecurityPolicy.h
- AFNetworking 4.0.1 - AFSecurityPolicy.m
- AFNetworking deprecation notice
- Apple - Preventing Insecure Network Connections
- Apple - Performing Manual Server Trust Authentication
- Alamofire - DefaultTrustEvaluator
- RFC 9525 - Service Identity in TLS
- CWE-297: Improper Validation of Certificate with Host Mismatch
- OWASP Top 10:2025 A04 - Cryptographic Failures