Skip to content

[UPDATE QUERY] Swift: report nil, CommonCrypto and default-parameter IVs that swift/static-initialization-vector misses - #351

Draft
felickz with Copilot wants to merge 5 commits into
mainfrom
copilot/swift-query-update-weak-crypto-randomness
Draft

felickz with Copilot wants to merge 5 commits into
mainfrom
copilot/swift-query-update-weak-crypto-randomness

Conversation

Copilot AI commented Oct 31, 2026 •

Copy link
Copy Markdown

📝 Query Update

  • Query File: languages/swift/custom/src/StaticInitializationVectorExtended/StaticInitializationVectorExtended.ql. It complements upstream swift/static-initialization-vector, which is reused unchanged.
  • Language: swift
  • Update Type: enhanced detection (missing sinks and sources)

🎯 Changes

What Changed

  • New query swift/custom/static-initialization-vector-extended. It reuses upstream's sources, sinks, barriers and extension points, and adds the following:
    • Sinks: the IV argument of the CommonCrypto functions CCCrypt and CCCryptorCreate (argument 5), CCCryptorCreateWithMode (argument 4) and CCCryptorReset (argument 1).
    • Sources:
      • nil literals. These are reported only at CommonCrypto sinks, because RNCryptor generates a random IV when given nil.
      • Call-site DefaultArgumentExpr (a call that omits an argument with a default value). Defaults that Swift fills in at the call site are excluded.
    • Flow steps:
      • From the receiver of withUnsafeBytes/withUnsafeMutableBytes to the closure's pointer parameter. This covers the deprecated generic Data overload.
      • From a parameter to reads of it inside nested closures. Closures passed to Foundation methods have no visible caller, so the dataflow library otherwise loses these values.
    • Exclusions:
      • Calls using ECB (kCCOptionECBMode, kCCModeECB) or RC4 (kCCAlgorithmRC4, kCCModeRC4), since these ignore the IV.
      • Results upstream already reports (an upstream source reaching an upstream sink).
  • New packs: languages-swift-custom-src and languages-swift-custom-test. The repo had no Swift directory before.
  • Docs: query help (.md) and a newQuery change note.

Why This Change

Both iGoat-Swift CWE-329 findings reproduce. Upstream reports neither:

  • nil IV: upstream has no CommonCrypto sinks, and nil is not one of its sources. CommonCrypto replaces a nil IV with all zero bytes.
  • Default-parameter IV: the Swift extractor does not store a parameter's default value; the schema has no default value on ParamDecl. The only thing that can be modelled is the call that omits the argument. In iGoat the IV then passes through nested withUnsafeBytes closures before it reaches CCCrypt.

The query is a custom one rather than an edit to upstream because upstream lives in codeql/swift-all.

Impact

New alerts where a constant, nil or default-parameter IV reaches a CommonCrypto call, or where a default-parameter IV reaches an upstream sink. Upstream results do not change, and this query does not repeat them.

🔍 Before vs. After

// WebkitCacheExerciseVC.aesCBCDecrypt
CCCrypt(op, alg, options, keyBytes, keyLength, nil, dataBytes, ...)

// Data+Extension.swift
extension Data {
    func aes(operation: Int = kCCEncrypt, keyData: Data,
             ivData: Data = "abcdefghijklmnop".data(using: .utf8)!) -> Data {
        ... ivData.withUnsafeBytes { ivBytes in keyData.withUnsafeBytes { keyBytes in
                CCCrypt(..., ivBytes, ...) } } ...
    }
}
plain.aes(keyData: key)

Previous Behavior

No alerts.

New Behavior

  • CCCrypt(..., nil, ...) is reported as "A nil initialization vector (all zero bytes) is used for encryption."
  • CCCrypt(..., ivBytes, ...) is reported with a path back to plain.aes(keyData: key), as "The default value of parameter 'ivData' is used as an initialization vector for encryption."

🧪 Testing

  • New/updated test cases added
  • All tests pass. They pass on Linux with the CodeQL 2.27.2 CLI, but against a database built with the 2.20.0 extractor (see notes below). A macOS codeql test run -- StaticInitializationVectorExtended/ is still needed.
  • Regression tests validated. Upstream swift/static-initialization-vector on the same database reports only the CryptoSwift CBC(iv:) case.
  • No unintended side effects

test/StaticInitializationVectorExtended/test.swift is an inline-expectations test ($ Alert / $ Source):

  • Positive cases:
    • both iGoat reductions
    • a constant array or string literal passed to CCCrypt / CCCryptorCreate
    • nil passed to CCCryptorCreate, CCCryptorCreateWithMode (CBC) and CCCryptorReset
  • Negative cases:
    • random IVs
    • a default IV parameter that every caller overrides
    • ECB through an inline option or a variable, and the CCCryptorCreateWithMode ECB mode
    • RC4
    • nil passed to RNCryptor
    • CryptoSwift CBC(iv:), which upstream already reports

The withUnsafeBytes stubs deliberately never call body, to match Foundation, so the test depends on the custom flow steps.

Notes on the Linux run:

  • 2.27.2 ships only a placeholder Swift extractor on Linux. I built the database with the 2.20.0 extractor, upgraded it with codeql database upgrade, then ran codeql test run --reuse-dataset.
  • To stop the 2.20.0 Swift compiler crashing on the nested closures, I passed -disable-stack-protector. That option is set only in a temporary copy of the test file, not in the committed one.
  • I confirmed the ECB/RC4 negatives would alert without the exclusion.

📊 Impact Analysis

  • Compatibility: backward compatible. This is a new custom query, and upstream is not touched.
  • Performance: neutral. It is the same configuration as upstream. The extra steps are limited to parameters read in nested closures and to withUnsafe*Bytes calls.
  • Accuracy: improved recall; precision is set to medium. Known false-positive risks:
    • A default IV that is generated at random (for example iv: Data = randomIV()) is still reported, because the default value is not in the database. This is documented in the query help.
    • CommonCrypto functions and constants are matched by name only, as upstream does for CryptoSwift.
    • Decrypting with a nil IV is reported on purpose: it means the data was encrypted with an all-zero IV.

📋 Checklist

  • Query compiles without errors (--warnings=error)
  • Documentation updated
  • Tests updated with new expectations
  • Metadata updated if needed (@id swift/custom/static-initialization-vector-extended, CWE-329/1204)

🔗 References

…to, nil and default-parameter IVs

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot AI changed the title [WIP] Update query for enhanced detection of weak cryptography [UPDATE QUERY] Swift: report nil, CommonCrypto and default-parameter IVs that swift/static-initialization-vector misses Oct 31, 2026
Copilot AI requested a review from felickz October 31, 2026 05:24
@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 newly discovered Swift suite cannot run in the Linux-only workflow, and overly broad ECB/RC4 matching can suppress genuine static- or nil-IV findings.

2 open issues
What changed in this PR

Adds a custom Swift query to complement upstream static-IV detection, covering the CommonCrypto and default-parameter cases described in #301 without modifying the upstream query.

Changes:

  • Models CommonCrypto IV arguments, nil/default sources, and flow through nested closures.
  • Adds ECB/RC4 exclusions and filtering to avoid repeating upstream results.
  • Introduces Swift query/test packs, regression fixtures, query help, and a change note.
File Description
languages/​swift/​custom/​test/​StaticInitializationVectorExtended/​test.swift Adds positive and negative Swift fixtures.
languages/​swift/​custom/​test/​StaticInitializationVectorExtended/​StaticInitializationVectorExtended.qlref Connects the query to inline-expectation processing.
languages/​swift/​custom/​test/​StaticInitializationVectorExtended/​StaticInitializationVectorExtended.expected Records expected alerts and flow paths.
languages/​swift/​custom/​test/​qlpack.yml Defines the Swift test pack.
languages/​swift/​custom/​src/​StaticInitializationVectorExtended/​StaticInitializationVectorExtended.qll Implements extended detection and result filtering.
languages/​swift/​custom/​src/​StaticInitializationVectorExtended/​StaticInitializationVectorExtended.ql Defines query metadata and path-result selection.
languages/​swift/​custom/​src/​StaticInitializationVectorExtended/​StaticInitializationVectorExtended.md Documents coverage, remediation, and limitations.
languages/​swift/​custom/​src/​qlpack.yml Defines the Swift query pack.
languages/​swift/​custom/​src/​change-notes/​2026-10-31-static-initialization-vector-extended.md Announces the new query and its coverage.

Review effort: Balanced

Comment thread languages/swift/custom/test/qlpack.yml Resolved
Copilot AI and others added 3 commits November 2, 2026 20:39
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
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 Update]: CWE-329 MEDIUM - Swift: extend weak cryptography and randomness coverage (swift/static-initialization-vector)

3 participants