Hardcoded HTTP URLs constructed with Foundation

Hardcoded HTTP URLs constructed with Foundation

Description

Creating an http:// URL with Foundation.URL(string:) does not send a request. Data can be exposed if the app uses that URL and the connection succeeds without TLS.

App Transport Security (ATS) normally blocks HTTP requests made with URLSession. Check Info.plist for exceptions such as NSAllowsArbitraryLoads or NSExceptionAllowsInsecureHTTPLoads. Lower-level APIs such as Network or CFNetwork may not enforce ATS. Check the actual traffic to see whether the app connected over HTTP.

Potential impact

If the endpoint is reached without effective transport protection:

  • A network observer may read requests and responses.
  • An attacker may modify traffic or responses.
  • A peer may impersonate the server or redirect communication to the wrong endpoint.
  • Broad ATS exceptions may permit additional cleartext connections.

Remediation

  1. Use the https: URL for the resource and confirm the server serves it directly. An HTTP-to-HTTPS redirect leaves the first request unprotected.
  2. Keep certificate validation and ATS enabled. Remove broad exceptions such as NSAllowsArbitraryLoads. Limit unavoidable exceptions to the required domains and features, and remove them as soon as possible.
  3. Keep development and test endpoints out of production builds through separate environment configuration. Private and loopback addresses also need appropriate protection.
  4. Verify both the release Info.plist and actual network traffic for any remaining successful HTTP connections.

Examples

Before

swift
import Foundation

func loadProfile() {
    guard let url = Foundation.URL(string: "http://api.example.com/profile") else {
        return
    }

    URLSession.shared.dataTask(with: url) { _, _, _ in }.resume()
}

After

swift
import Foundation

func loadProfile() {
    guard let url = Foundation.URL(string: "https://api.example.com/profile") else {
        return
    }

    URLSession.shared.dataTask(with: url) { _, _, _ in }.resume()
}

The revised URL uses HTTPS from the first request.

References