- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
",t};e.buildCustomizationMenuUi=t;function n(e){let t='
",t}function s(e){let n=e.filter.currentValue||e.filter.defaultValue,t='${e.filter.label}
`,e.filter.options.forEach(s=>{let o=s.id===n;t+=``}),t+="${e.filter.label}
`,t+=`ID: swift-security/insecure-afnet-cert-config
Language: Unknown
Severity: Error
Category: Security
CWE: 295
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:
AFSecurityPolicy.default()
: The default policy has certificate pinning disabled.pinningMode
set to AFSSLPinningMode.none
.allowInvalidCertificates
property is set to true
, which bypasses certificate chain validation and effectively disables pinning.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.
import AFNetworking
class NetworkManager {
func createInsecureSessionWithDefaultPolicy() -> AFHTTPSessionManager {
let manager = AFHTTPSessionManager()
// NON-COMPLIANT: The default policy has pinning disabled (AFSSLPinningMode.none).
// This will be flagged by the rule.
manager.securityPolicy = AFSecurityPolicy.default()
return manager
}
func createInsecureSessionWithInvalidCerts() -> AFHTTPSessionManager {
let manager = AFHTTPSessionManager()
// This policy seems secure at first glance...
let policy = AFSecurityPolicy(pinningMode: .publicKey)
// NON-COMPLIANT: ...but setting allowInvalidCertificates to true bypasses all
// certificate validation, making pinning useless. This will be flagged.
policy.allowInvalidCertificates = true
manager.securityPolicy = policy
return manager
}
func createInsecureSessionWithNoDomainValidation() -> AFHTTPSessionManager {
let manager = AFHTTPSessionManager()
let policy = 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 = false
manager.securityPolicy = policy
return manager
}
}
import AFNetworking
class SecureNetworkManager {
func createSecureSessionManager() -> AFHTTPSessionManager {
let manager = AFHTTPSessionManager()
// COMPLIANT: Initialize the policy with a secure pinning mode,
// such as .publicKey or .certificate.
let policy = 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.
if let certificateData = 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 = policy
return manager
}
private func loadPinnedCertificates() -> [Data]? {
var certificates: [Data] = []
// Assuming you have .cer files (in DER format) in your project bundle.
if let paths = Bundle.main.paths(forResourcesOfType: "cer", inDirectory: ".") as [String]? {
for path in paths {
if let certificateData = NSData(contentsOfFile: path) {
certificates.append(certificateData as Data)
}
}
}
return certificates.isEmpty ? nil : certificates
}
}