Sensitive data sent through Swift NWConnection without TLS or DTLS

Sensitive data sent through Swift NWConnection without TLS or DTLS

Description

App Transport Security (ATS) in Apple’s URL Loading System does not cover NWConnection. With Network.framework, the app must configure transport encryption and authenticate the peer.

Network.NWParameters.tcp and Network.NWParameters.udp select plain TCP and UDP. Network.NWParameters(tls: nil, tcp: ...) and Network.NWParameters(dtls: nil, udp: ...) explicitly omit TLS and DTLS.

To assess exposure, check whether the connection succeeded, which protocol it used, how it authenticated the peer, and whether the application protocol protected the data.

Potential impact

If the application protocol also lacks authenticated encryption:

  • Network observers or intermediaries may read credentials and personal information.
  • An active attacker may modify transmitted data.
  • An attacker may impersonate the server or peer.
  • Captured messages may be replayed if separate replay controls are absent.

Remediation

  • For TCP, use Network.NWParameters.tls or supply non-nil TLS options. For UDP, use Network.NWParameters.dtls or supply non-nil DTLS options. Keep the system’s server-trust checks enabled and avoid callbacks that accept every certificate or peer.
  • For HTTP or other URL-based resources, use HTTPS with URLSession and limit ATS exceptions.
  • Send only the sensitive data you need. If you also need protection beyond the TLS endpoints, add application-layer authenticated encryption with independent key management, unique nonces, and replay protection. Keep authenticated transport in place.
  • Inspect release-build traffic for cleartext and unexpected protocols.

Examples

Before

swift
import Dispatch
import Foundation
import Network

func sendCredential(clientSecret: Data) {
    let connection = Network.NWConnection(
        host: "api.example.com",
        port: 443,
        using: Network.NWParameters.tcp
    )
    let queue = DispatchQueue(label: "cleartext-connection")
    connection.start(queue: queue)
    connection.send(content: clientSecret, completion: .contentProcessed { _ in })
}

After

swift
import Dispatch
import Foundation
import Network

func sendCredential(clientSecret: Data) {
    let connection = Network.NWConnection(
        host: "api.example.com",
        port: 443,
        using: Network.NWParameters.tls
    )
    let queue = DispatchQueue(label: "tls-connection")
    connection.start(queue: queue)
    connection.send(content: clientSecret, completion: .contentProcessed { _ in })
}

Port 443 alone does not enable TLS. The revised NWConnection uses Network.NWParameters.tls to protect clientSecret in transit. Keep server-trust checks enabled and handle connection states and send errors.

References