Skip to content

[Query Update]: CWE-329 MEDIUM - Swift: extend weak cryptography and randomness coverage (swift/static-initialization-vector) #301

Open

Description

@felickz

Query File Path

swift/static-initialization-vector

Update Type

Enhanced detection coverage

Issue Description

AI Scan reported 2 medium-severity CWE-329 (weak crypto randomness) finding(s) in Swift public synthetic repositories that the baseline CodeQL default-setup scan did not report. Gap categories: missing-sink, missing-source. Existing queries considered: swift/static-initialization-vector.

Why CodeQL appears to have missed these (unverified until reproduced with a test database):

  • Passing nil as the IV to CCCrypt means a zero IV; the static IV query does not treat a nil literal as constant.
  • A hard-coded IV supplied as a default parameter value likely escapes swift/static-initialization-vector.

Linked findings (gap-key: swift/query-update/weak-crypto-randomness)

Repo CWE Severity AI scan alert Summary
iGoat-Swift CWE-329 medium https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/35 WebkitCacheExerciseVC.aesCBCDecrypt(data:keyData:) passes nil as the IV argument to CCCrypt(), which CommonCrypto interprets as an all
iGoat-Swift CWE-329 medium https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/20 The shared AES helper defaults every encryption to the same hard-coded IV and uses CBC with padding but no authentication tag. Reusing an IV

Findings come from AI Scan on public synthetic apps. Hypotheses above are unverified until reproduced with a test database. Verify first whether CodeQL already covers a case (for example via a different query or threat model) before adding code.

Handling decision

  • Type: Query Update
  • Decision: Update existing CodeQL queries. The queries named above already target this weakness class; the findings show missing flow steps, sinks, sources, barriers or scope handling. Extend the smallest existing query or model instead of adding a new query.
  • Recommended actions across the findings: extend-existing-query (2)

Patterns in this gap

  • static-iv-nil (1 finding), for example iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Exercises/InsecureLocalDataStorage/WebKit Cache/WebkitCacheExerciseVC.swift
  • static-iv-default-parameter (1 finding), for example iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Extensions/Data+Extension.swift

Finding details

Each finding below has its own context so the agent can work from this issue alone.

1. iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Exercises/InsecureLocalDataStorage/WebKit Cache/WebkitCacheExerciseVC.swift:141 (CWE-329, medium)
  • AI Scan alert: https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/35
  • AI Scan message: WebkitCacheExerciseVC.aesCBCDecrypt(data:keyData:) passes nil as the IV argument to CCCrypt(), which CommonCrypto interprets as an all
  • Context for the agent: A Swift decrypt helper calls CCCrypt with a nil IV argument. CodeQL would need nil literals at the IV parameter to count as a static IV.
  • Why CodeQL appears to miss it: missing-sink: Passing nil as the IV to CCCrypt means a zero IV; the static IV query does not treat a nil literal as constant.
  • Existing CodeQL queries considered: swift/static-initialization-vector
  • Recommended action from the comparison: extend-existing-query (classification confidence: medium)
2. iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Extensions/Data+Extension.swift:5 (CWE-329, medium)
  • AI Scan alert: https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/20
  • AI Scan message: The shared AES helper defaults every encryption to the same hard-coded IV and uses CBC with padding but no authentication tag. Reusing an IV
  • Context for the agent: Data+Extension.swift:5 defines an AES helper whose IV parameter defaults to a constant literal used with CBC. CodeQL would need to treat default argument literals as constant sources reaching the IV sink.
  • Why CodeQL appears to miss it: missing-source: A hard-coded IV supplied as a default parameter value likely escapes swift/static-initialization-vector.
  • Existing CodeQL queries considered: swift/static-initialization-vector
  • Recommended action from the comparison: extend-existing-query (classification confidence: medium)

Proposed Solution

  1. Reproduce each linked finding with a minimal synthetic test case and confirm the baseline result.
  2. Identify the missing source, sink, summary or scope condition and extend the smallest model or query that fixes it.
  3. Add positive and negative tests and a change note.
  4. Check the false-positive rate on the other linked findings before widening.

Code Examples

See the linked alerts above for the vulnerable pattern; reduce each to a minimal test case with a safe counterpart that should not alert.

Breaking Change?

No - backward compatible

References (Optional)

https://cwe.mitre.org/data/definitions/329.html

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    enhancement
    severity:medium
    on Oct 31, 2026
  2. added
    precision:medium
    tags:experimental
    on Nov 3, 2026
  3. felickz commented on Nov 3, 2026

    @felickz
    Author

    Upstream triage

    precision:medium Relies on heuristics or accepted over-approximation. Community Packs, always tags:experimental. tags:experimental Goes to Community Packs: performance, false-positive or synthetic-only concerns.

    Community Packs, experimental. Recommended for the Community Packs repository, not github/codeql. The issue is labeled tags:experimental for this.

    Separable piece. CommonCrypto key and IV sink rows and nil IV. This part could be proposed to github/codeql on its own, see the split note below.

    Linked pull request: #351

    Possible overlap with #102 (the same CommonCrypto key and IV sink rows), so one upstream pull request may cover several issues.

    Why. Stock static-initialization-vector has no CommonCrypto sinks, so constant or nil IVs passed to CCCrypt and CCCryptorCreate* are missed, a real gap for a widely used iOS API. The sink positions, nil semantics and ECB/RC4 exclusion are sound. The default-argument source is unconstrained, so default nil or random IVs can alert. Keep experimental for now; a trimmed sink-and-nil patch could be proposed to github/codeql separately.

    Change Patch to an existing query or library
    Target swift/static-initialization-vector: add CommonCrypto IV sinks, nil IV and default-argument IV sources
    Precision assessed medium (declared medium in the pull request). CommonCrypto sinks are exact by API position, nil is a documented all-zero IV, ECB and RC4 are excluded and flow reuses stock sources (very-high to high). The default-argument source accepts any non-caller-side default without a constant check, which lowers the PR to medium.
    Real-world basis general. CommonCrypto (CCCrypt, CCCryptorCreate, CCCryptorCreateWithMode, CCCryptorReset) is Apple's standard C crypto API in many iOS apps; the stock static IV query covers only CryptoSwift and RNCryptor.
    Stock overlap partial. Reuses the stock IV flow. Stock models only CryptoSwift and RNCryptor sinks: no CCCrypt, nil or default-argument handling. Checked local swift-all 6.8.3 and the upstream main extensions file.
    Concerns, overlap and next steps

    False-positive concerns

    • A default argument such as iv: Data? = nil or a random generator call is reported, since the default value is never checked to be constant
    • iv: Data? = nil forwarded to RNCryptor alerts as a default-argument IV although the test treats a literal nil as safe there
    • Decrypting data whose first block is the IV with a nil IV, then dropping that block, is safe but reported (rare)
    • Options passed in a parameter cannot be inspected, so nil-IV calls whose caller selects ECB or RC4 alert

    Performance concerns

    • Every nil literal and non-caller-side default argument is a global taint source on top of the stock literal sources; unmeasured on large databases
    • CapturedParameterStep adds a global step from each parameter to its reads in nested closures

    Heuristics used

    • CommonCrypto constant names kCCOptionECBMode, kCCAlgorithmRC4, kCCModeECB, kCCModeRC4 matched by DeclRef name
    • value-preserving conversions recognized by result type name CCOptions or UInt32

    What an upstream change would look like. Community Packs swift/src/<category>/ query with tests in swift/test/. Separately, a github/codeql patch to StaticInitializationVectorExtensions.qll (CommonCrypto IV sinks, nil IV limited to them), tests under swift/ql/test/query-tests/Security/CWE-1204, library change note.

    Before opening an upstream pull request

    • Split the default-argument source out, or require the default value to be a constant IV source
    • Add negative tests: iv default nil forwarded to RNCryptor, a random default IV, and ECB options passed as a parameter
    • For github/codeql, add the CommonCrypto sink class to the stock extensions file and use $@ rather than toString in the message
    • Remove the internal codeql.swift.generated.ParentChild import and getImmediate accessors (Community Packs forbids internal libraries)
    • Measure run time of the global nil, default-argument and closure-capture steps on a large Swift database
    • Add a true positive from a real, non-training project (Community Packs requirement)

    Split note. The CommonCrypto sinks plus nil IV (with the ECB/RC4 exclusion) are a strong github/codeql patch candidate. The default-argument source is the over-approximate part; split it out or add a constant-default check before proposing upstream.

    Change from the initial guess. When this issue was opened the coarse destination guess was github/codeql. Reading the actual pull request changed it to Community Packs.

    This is a recommendation only. A human reviews it before any pull request is opened against github/codeql or the Community Packs repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CWE-329enhancementprecision:mediumquery-updateseverity:mediumtags:experimental

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions