v0.14 is out: a mobile auth pack for Swift/iOS and Android, catching insecure token storage, cleartext traffic, and OAuth in embedded WebViews. Read more →
MEDIUM AI PREVALENCE: HIGH auth.swift.storage.token-in-appstorage

A token, secret, or credential is bound to @AppStorage.

Why AI tools produce this: AI coding tools generate this anti-pattern by default, it appears in a large share of AI-written auth code.

Why this matters

@AppStorage is a thin SwiftUI wrapper over UserDefaults, so the value lands in an unencrypted on-disk plist that is readable from a device backup, a jailbroken device, or the simulator container (CWE-312). AI-generated SwiftUI code reaches for @AppStorage for persistence and unknowingly stores authentication material in the clear.

Keep @AppStorage for non-sensitive UI preferences. Store secrets in the Keychain (Security framework or a wrapper) and read them into memory when needed instead of persisting them through @AppStorage.

VULNERABLE
vulnerable.swift
import SwiftUI

struct SettingsView: View {
    // ruleid: auth.swift.storage.token-in-appstorage
    @AppStorage("accessToken") var accessToken: String = ""

    // ruleid: auth.swift.storage.token-in-appstorage
    @AppStorage("refresh_token") private var refreshToken = ""

    // ruleid: auth.swift.storage.token-in-appstorage
    @AppStorage("apiSecret") var apiSecret = ""

    var body: some View {
        Text(accessToken)
    }
}
SAFE
safe.swift
import SwiftUI

struct SettingsView: View {
    // Non-sensitive UI preferences are the intended use of @AppStorage.
    @AppStorage("username") var username: String = ""
    @AppStorage("prefersDark") var prefersDark = false
    @AppStorage("launchCount") private var launchCount = 0

    var body: some View {
        Text(username)
    }
}

Suppressing this rule

If a finding is a genuine false positive, scope the suppression to the exact line and leave a reason, never disable the rule project-wide. Disable directives are line-scoped by design.

// oauthlint-disable-next-line auth.swift.storage.token-in-appstorage -- <reason>

References

https://developer.apple.com/documentation/swiftui/appstorage ↗https://cwe.mitre.org/data/definitions/312.html ↗