Hostname validation disabled in an AFNetworking policy without pinning

Hostname validation disabled in an AFNetworking policy without pinning

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

  1. For default() or .none, keep validatesDomainName set to true. 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 avoid allowInvalidCertificates = true in an unpinned policy.
  2. If the service requires pinning, choose .certificate or .publicKey for the required hosts. Prepare backup pins, plan for key rotation, and block connections when a pin check fails.
  3. Migrate to the default trust handling in URLSession or a maintained library such as Alamofire (stable release 5.12.0 as of August 30, 2026). With a custom DefaultTrustEvaluator, keep validateHost: true unless peer-specific pinning explicitly verifies the service identity.
  4. Test the release configuration with a mismatched hostname, an expired certificate, and an untrusted issuer. Each case should fail.

Examples

Before

swift
import AFNetworking

func insecurePolicy() -> AFNetworking.AFSecurityPolicy {
    let policy = AFNetworking.AFSecurityPolicy.default()
    policy.validatesDomainName = false
    return policy
}

After

swift
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.

swift
import Foundation

func standardSession() -> URLSession {
    URLSession(configuration: .default)
}

References