+MASTG-BEST-0073 Properly Validate Server Trust in URLSessionDelegate and WKNavigationDelegate (iOS)

MASTG-BEST-0073 Properly Validate Server Trust in URLSessionDelegate and WKNavigationDelegate (iOS)

When an iOS app overrides the default certificate validation by implementing URLSessionDelegate.urlSession(_:didReceive:completionHandler:)) or WKNavigationDelegate.webView(_:didReceive:completionHandler:)), it takes full control of the server trust evaluation. An incorrect implementation that accepts credentials without calling SecTrustEvaluateWithError) bypasses certificate chain validation and hostname verification, leaving connections open to Machine-in-the-Middle (MITM) attacks.

See "Performing manual server trust authentication" in the Apple Developer Documentation for more information.

Prefer the Default ATS Trust Evaluation

The safest approach is to not implement urlSession(_:didReceive:completionHandler:) or webView(_:didReceive:completionHandler:) at all. When these methods are absent, the URL Loading System and WKWebView perform the full ATS-enforced server trust evaluation automatically. Override these methods only when the app has a specific justified requirement (for example, certificate pinning or connecting to a development server with a self-signed certificate). Even for certificate pinning, prefer Apple's declarative Identity Pinning, which lets you pin CA or leaf public keys directly in the Info.plist under NSPinnedDomains and is enforced automatically by ATS without any custom validation code (see MASTG-KNOW-0072). If a server presents an otherwise untrusted certificate, fix it server-side rather than weakening or replacing the system's trust evaluation in the app.

Perform Explicit Server Trust Evaluation

If you must handle the challenge, always:

  1. Confirm the challenge is of type NSURLAuthenticationMethodServerTrust.
  2. Obtain the serverTrust object from challenge.protectionSpace.serverTrust.
  3. Call SecTrustEvaluateWithError and verify it returns true.
  4. Call completionHandler(.useCredential, URLCredential(trust: serverTrust)) only when evaluation succeeds.
  5. Call completionHandler(.cancelAuthenticationChallenge, nil) on any other challenge type or when evaluation fails.

1. Übersicht

Bezeichnung Standard

1.1 Referenzen

1.2 Identifizierte Anforderungen

1.3 Zugehörige Regulierungen

2. Identifizierte Anforderungen

Anforderungen
Quelle Anforderung

3. Zugehörige Regulierungen

Regulierungen
Quelle Regulierung

Verknüpfte Vorgänge

  • OWASP -

    EN: Publications of the OWASP Foundation, https://owasp.org/. OWASP® is a registered trademark of the OWASP Foundation, Inc.; GRCA is not affiliated with, nor endorsed by, the OWASP Foundation. Only the versions published by OWASP are authoritative.

    DE: Veröffentlichungen der OWASP Foundation, https://owasp.org/. OWASP® ist eine eingetragene Marke der OWASP Foundation, Inc.; GRCA steht in keiner Verbindung zur OWASP Foundation und wird von ihr nicht unterstützt. Maßgeblich sind allein die von OWASP veröffentlichten Fassungen.