Skip to content

[NEW QUERY] Swift: credential and sensitive data exposure (swift/credential-and-data-exposure) - #352

Draft
felickz with Copilot wants to merge 3 commits into
mainfrom
copilot/swift-query-create-credential-data-exposure
Draft

felickz with Copilot wants to merge 3 commits into
mainfrom
copilot/swift-query-create-credential-data-exposure

Conversation

Copilot AI commented Oct 31, 2026 •

Copy link
Copy Markdown

📝 Query Information

  • Language: swift
  • Query ID: swift/credential-and-data-exposure
  • Category: security
  • Severity: warning (@security-severity 5.5, @precision low)
  • CWE/CVE (if applicable): CWE-200, CWE-316, CWE-522, CWE-922

🎯 Description

AI Scan found 7 Swift credential and data exposure findings that the default CodeQL scan does not report, and no existing Swift query covers them. This PR adds one query for the six patterns behind those findings, along with the first Swift custom packs in this repo.

What This Query Detects

Pattern CWE Logic
Pasteboard exposure 200 Taint from SensitiveExpr, or from names matching decrypt|plaintext|cleartext, into UIPasteboard setters or setValue/setData/addItems/setItems/setObjects. The write is safe if the same callable references .localOnly or .expirationDate.
Credential-derived session token 522 A password flows into an *encrypt* call (not decrypt) whose result is stored in a variable or property named token, session or cookie.
Missing background snapshot protection 200 A UIApplicationDelegate (including its extensions) does not implement applicationDidEnterBackground(_:) or applicationWillResignActive(_:), the app has no scene-delegate or notification equivalent, and it declares a sensitive UITextField/UITextView.
Weak keychain accessibility 922 SecItemAdd in a file that never references kSecAttrAccessible or kSecAttrAccessControl, plus any use of kSecAttrAccessibleAlways*.
Sensitive response caching 922 .returnCacheDataElseLoad or .returnCacheDataDontLoad inside a function that touches a SensitiveExpr or a credential-like string literal.
Sensitive data retained in memory 316 A sensitive-named String/Data property of a class is assigned, but never set to nil, "" or an empty value, and never removeAll'd.

Notes:

  • Why the extra source names: the standard SensitiveExprs heuristic treats any name containing crypt as non-sensitive. That misses the Swift.nV decryptedVal case, so decrypted-plaintext names are added as pasteboard sources.
  • Compiler-generated code: results at UnknownLocation, such as compiler-generated property accessors, are dropped.
  • Not covered: the static key in the OversecuredVulnerableiOSApp finding is left to swift/hardcoded-key. iGoat-Swift alert 56 relies on storyboard-only fields, so the app delegate alert covers it only indirectly.

Example Vulnerable Code

UIPasteboard.general.string = self.decryptedValue

let token = Crypto.encrypt("\(info.email):\(info.password)")

urlRequest.cachePolicy = .returnCacheDataElseLoad   // in a function sending "token=key"

self.cardNumberString = cardNoTextField.text ?? ""  // never cleared

Example Safe Code

UIPasteboard.general.setItems([["public.text": password]],
    options: [.localOnly: true, .expirationDate: Date(timeIntervalSinceNow: 60)])

let item: [String: Any] = [kSecAttrAccessible: kSecAttrAccessibleWhenUnlockedThisDeviceOnly, ...]

func applicationDidEnterBackground(_ application: UIApplication) { /* obscure window */ }

🧪 Testing

  • Positive test cases included
  • Negative test cases included
  • Edge cases covered
  • All tests pass

The tests use inline expectations (// $ Alert[swift/credential-and-data-exposure]). CodeQL 2.27.2 ships only a macOS Swift extractor, so the test database was built with the v2.15.5 Linux extractor and then upgraded with codeql database upgrade. codeql test run --reuse-dataset passes on that database. The .expected file may need a --learn on macOS. The Swift files are compiled one at a time during test extraction, so keychain_accessible.swift has its own stubs. It is also a separate file because the keychain check works per file.

.github/workflows/codeql-qlt-unit-tests.yml now skips swift during test discovery on the ubuntu runner, like ql and actions.

📋 Checklist

  • Query compiles without errors
  • Documentation complete (.md and .qhelp): .md only, no .qhelp
  • Metadata properly set (@name, @id, @kind, etc.)
  • Tests validate query behavior
  • No false positives in test cases

🔗 References


Note: This query was developed using Test-Driven Development methodology.

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add CodeQL query for credential and data exposure [NEW QUERY] Swift: credential and sensitive data exposure (swift/credential-and-data-exposure) Oct 31, 2026
Copilot AI requested a review from felickz October 31, 2026 05:45
@felickz
felickz requested a balanced review from Copilot November 2, 2026 18:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The query can miss unsafe writes and retained secrets because protection checks are not write-specific and clearing logic incorrectly accepts arbitrary zero-argument calls and partial buffer resets.

4 open issues
What changed in this PR

Adds swift/credential-and-data-exposure, a low-precision security audit query addressing six credential and sensitive-data exposure patterns from issue #256, along with the repository’s first Swift custom packs.

Changes:

  • Detects pasteboard exposure, credential-derived tokens, missing snapshot protection, weak keychain accessibility, sensitive caching, and retained sensitive values.
  • Adds query documentation and positive/negative Swift fixtures with expected results.
  • Excludes Swift tests from discovery on the Ubuntu test runner.
File Description
languages/​swift/​custom/​test/​qlpack.yml Defines the Swift test pack.
languages/​swift/​custom/​test/​CredentialAndDataExposure/​test.swift Adds fixtures for all six detection patterns.
languages/​swift/​custom/​test/​CredentialAndDataExposure/​keychain_accessible.swift Adds separate keychain accessibility cases.
languages/​swift/​custom/​test/​CredentialAndDataExposure/​CredentialAndDataExposure.qlref Connects the query to inline expectations.
languages/​swift/​custom/​test/​CredentialAndDataExposure/​CredentialAndDataExposure.expected Records expected query results.
languages/​swift/​custom/​src/​qlpack.yml Defines the Swift query pack.
languages/​swift/​custom/​src/​CredentialAndDataExposure/​CredentialAndDataExposure.ql Implements the six detection heuristics.
languages/​swift/​custom/​src/​CredentialAndDataExposure/​CredentialAndDataExposure.md Documents coverage, recommendations, and examples.
.github/​workflows/​codeql-qlt-unit-tests.yml Skips Swift test discovery on Ubuntu.

Review effort: Balanced

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Query Create]: CWE-200, CWE-316, CWE-522, CWE-922 MEDIUM - Swift: new query for credential and sensitive data exposure

3 participants