The rule detects insecure configurations of AFNetworking’s AFSecurityPolicy that disable or weaken certificate pinning. Certificate pinning is a critical security mechanism that prevents Man-in-the-Middle (MitM) attacks by ensuring the application only communicates with servers presenting a known, trusted certificate or public key.
This rule flags the following insecure patterns:
Use of AFSecurityPolicy.default(): The default policy has certificate pinning disabled.
Explicitly disabling pinning: The policy is initialized with pinningMode set to AFSSLPinningMode.none.
Allowing invalid certificates: The allowInvalidCertificates property is set to true, which bypasses certificate chain validation and effectively disables pinning.
Disabling domain name validation: The validatesDomainName property is set to false, which means the certificate’s common name is not checked against the server’s domain, weakening security.
An attacker could exploit this vulnerability to intercept and tamper with traffic between the mobile application and its backend servers. It is strongly recommended to enable certificate pinning by setting the pinningMode to AFSSLPinningMode.certificate or AFSSLPinningMode.publicKey and ensuring that certificate validation checks are not disabled.
Non-Compliant Code Examples
importAFNetworkingclassNetworkManager{funccreateInsecureSessionWithDefaultPolicy()->AFHTTPSessionManager{letmanager=AFHTTPSessionManager()// NON-COMPLIANT: The default policy has pinning disabled (AFSSLPinningMode.none).// This will be flagged by the rule.manager.securityPolicy=AFSecurityPolicy.default()returnmanager}funccreateInsecureSessionWithInvalidCerts()->AFHTTPSessionManager{letmanager=AFHTTPSessionManager()// This policy seems secure at first glance...letpolicy=AFSecurityPolicy(pinningMode:.publicKey)// NON-COMPLIANT: ...but setting allowInvalidCertificates to true bypasses all// certificate validation, making pinning useless. This will be flagged.policy.allowInvalidCertificates=truemanager.securityPolicy=policyreturnmanager}funccreateInsecureSessionWithNoDomainValidation()->AFHTTPSessionManager{letmanager=AFHTTPSessionManager()letpolicy=AFSecurityPolicy(pinningMode:.publicKey)// NON-COMPLIANT: Disabling domain name validation is a security risk,// as it allows a certificate for a different domain to be accepted.// This will be flagged by the rule.policy.validatesDomainName=falsemanager.securityPolicy=policyreturnmanager}}
Compliant Code Examples
importAFNetworkingclassSecureNetworkManager{funccreateSecureSessionManager()->AFHTTPSessionManager{letmanager=AFHTTPSessionManager()// COMPLIANT: Initialize the policy with a secure pinning mode,// such as .publicKey or .certificate.letpolicy=AFSecurityPolicy(pinningMode:.publicKey)// COMPLIANT: Ensure that self-signed or otherwise invalid certificates are not allowed.// This is the default, but it is good practice to be explicit.policy.allowInvalidCertificates=false// COMPLIANT: Ensure that the certificate's domain name is validated against the server's domain.// This is also the default.policy.validatesDomainName=true// To complete the implementation, you must provide the public keys or certificates// of the servers you want to trust.// For example, load certificates from your app's bundle.ifletcertificateData=loadPinnedCertificates(){policy.pinnedCertificates=NSSet(array:certificateData)as?Set<Data>}else{// Handle the error case where certificates could not be loaded.// For security, you might want to prevent the manager from being used.fatalError("Could not load pinned certificates.")}manager.securityPolicy=policyreturnmanager}privatefuncloadPinnedCertificates()->[Data]?{varcertificates:[Data]=[]// Assuming you have .cer files (in DER format) in your project bundle.ifletpaths=Bundle.main.paths(forResourcesOfType:"cer",inDirectory:".")as[String]?{forpathinpaths{ifletcertificateData=NSData(contentsOfFile:path){certificates.append(certificateDataasData)}}}returncertificates.isEmpty?nil:certificates}}
Seamless integrations. Try Datadog Code Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
How to use this rule
1
2
rulesets:- swift-security # Rules to enforce Unknown security.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Security scans to your CI pipelines