Files
pruefwerk/compliance-agent/rules/cra_semgrep_swift.yaml
T
sharang 5b2deec9e8
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy MCP (push) Skipped
CI / Deploy Docs (push) Successful in 1m12s
CI / Deploy Dashboard (push) Successful in 5m51s
CI / Deploy Agent (push) Successful in 7m29s
feat(ios): secrets in UserDefaults, URL schemes and permissions of iOS apps (#479)
2026-10-05 23:35:23 +00:00

73 lines
3.5 KiB
YAML

# Prüfwerk's own semgrep rules for Swift (iOS apps; Objective-C is in
# cra_semgrep_objc.yaml). Written for Prüfwerk, not
# adapted from any third-party rule set (the Semgrep registry rules may not be
# used for a service offered to others). mobsfscan already covers ATS, the
# Keychain accessibility, weak hashes and ciphers, TLS and web views; these
# rules cover what it does not.
# Same conventions as cra_semgrep.yaml: each rule id is `cra-ai-<n>-<slug>`,
# keyed back to its control via the `control-map` LUT, with its CWE and
# control in the metadata. Keep patterns tight: a false positive that the
# judge refutes marks the whole finding a false positive.
# Test cases: cra_semgrep_swift.swift (`semgrep --test compliance-agent/rules`).
rules:
# --- cra-ai-14: data at rest -------------------------------------------------
- id: cra-ai-14-swift-secret-in-userdefaults
languages: [swift]
severity: WARNING
message: A password, token or key is stored in UserDefaults, an unencrypted plist in the app container that backups and device extraction read (CWE-312). Store it in the Keychain (kSecClassGenericPassword, with a ThisDeviceOnly accessibility).
metadata:
cwe: ["CWE-312: Cleartext Storage of Sensitive Information"]
control: cra-ai-14
patterns:
- pattern-either:
# The key names the secret ...
- patterns:
- pattern-either: &userdefaults-set
- pattern: 'UserDefaults.standard.set($V, forKey: $K)'
- pattern: 'UserDefaults(suiteName: $S)?.set($V, forKey: $K)'
- pattern: 'UserDefaults(suiteName: $S)!.set($V, forKey: $K)'
- patterns:
- pattern-either:
- pattern-inside: |
let $D = UserDefaults.standard
...
- pattern-inside: |
let $D = UserDefaults(suiteName: $S)
...
- pattern: '$D.set($V, forKey: $K)'
- metavariable-regex:
metavariable: $K
regex: &secret (?i).*(passw|passcode|token|secret|api_?key|credential|private_?key|session_?id)
# ... or the stored value does.
- patterns:
- pattern-either: *userdefaults-set
- metavariable-regex:
metavariable: $V
regex: *secret
# A flag or a count is not the secret, even if its key names one.
- metavariable-regex:
metavariable: $V
regex: ^(?!(true|false|nil|-?\d+(\.\d+)?|Date\(\))$)
- id: cra-ai-14-swift-secret-in-appstorage
languages: [swift]
severity: WARNING
message: A password, token or key is kept in @AppStorage, which stores it in UserDefaults, an unencrypted plist in the app container that backups and device extraction read (CWE-312). Store it in the Keychain instead.
metadata:
cwe: ["CWE-312: Cleartext Storage of Sensitive Information"]
control: cra-ai-14
pattern-either:
- patterns:
- pattern-either: &appstorage
- pattern: |
@AppStorage($K) var $X: $T = $E
- pattern: |
@AppStorage($K) var $X = $E
- metavariable-regex:
metavariable: $K
regex: *secret
- patterns:
- pattern-either: *appstorage
- metavariable-regex:
metavariable: $X
regex: *secret