Skip to content

[Query Create]: CWE-20 MEDIUM - Swift: new query for cwe 20 #300

Open

Description

@felickz

Target Language

swift

Query Name (Optional)

Cwe20

Query Type

Security

Query Description

AI Scan reported 2 medium-severity CWE-20 (cwe 20) finding(s) in Swift public synthetic repositories that the baseline CodeQL default-setup scan did not report. Gap categories: missing-source, query-absent. Existing queries considered: none.

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

  • Custom URL scheme query parameters in application(_:open:options:) are unvalidated input but no matching query or source for this use exists.
  • Unchecked substring ranges from a response can crash or misparse; no Swift query covers unvalidated delimiter lookups.

Linked findings (gap-key: swift/query-create/cwe-20)

Repo CWE Severity AI scan alert Summary
iGoat-Swift CWE-20 medium https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/40 AppDelegate.application(_:open:options:) handles the iGoat:// custom URL scheme and takes the contactNumber and message query parame
iGoat-Swift CWE-20 medium https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/58 The parser performs substring operations using ranges from an untrusted HTTP response without checking whether either delimiter was found. A

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 Create
  • Decision: Create a new CodeQL query. No existing query covers these patterns (most findings are gap type query-absent or missing-query). Because coverage of lower-severity patterns can be heuristic, target Community Packs unless precision is shown to be high enough for github/codeql.
  • Recommended actions across the findings: write-new-query (2)

Patterns in this gap

  • url-scheme-input-validation (1 finding), for example iGoat-Swift iGoat-Swift/iGoat-Swift/AppDelegate.swift
  • unchecked-substring-range (1 finding), for example iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Exercises/Key Management/Key Storage Server Side/KeyStorageServerSideVC.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/AppDelegate.swift:16 (CWE-20, medium)
  • AI Scan alert: https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/40
  • AI Scan message: AppDelegate.application(_:open:options:) handles the iGoat:// custom URL scheme and takes the contactNumber and message query parame
  • Context for the agent: A Swift app delegate reads query parameters from a custom URL scheme and uses them without validation. CodeQL would need the open-URL handler as a remote source and a sensitive action sink.
  • Why CodeQL appears to miss it: missing-source: Custom URL scheme query parameters in application(_:open:options:) are unvalidated input but no matching query or source for this use exists.
  • Recommended action from the comparison: write-new-query (classification confidence: low)
2. iGoat-Swift iGoat-Swift/iGoat-Swift/Source/Exercises/Key Management/Key Storage Server Side/KeyStorageServerSideVC.swift:70 (CWE-20, medium)
  • AI Scan alert: https://github.com/codeql-forks-felickz/iGoat-Swift/security/code-scanning/58
  • AI Scan message: The parser performs substring operations using ranges from an untrusted HTTP response without checking whether either delimiter was found. A
  • Context for the agent: A Swift parser takes ranges from range(of:) on an HTTP response and slices without checking for nil. CodeQL would need a query for unchecked optional ranges used in substring operations.
  • Why CodeQL appears to miss it: query-absent: Unchecked substring ranges from a response can crash or misparse; no Swift query covers unvalidated delimiter lookups.
  • Recommended action from the comparison: write-new-query (classification confidence: low)

Expected Severity

Medium

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.

CWE/CVE Reference (Optional)

CWE-20

References (Optional)

https://cwe.mitre.org/data/definitions/20.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.

    Linked pull request: #350

    Why. Two unrelated new queries. The unchecked range(of:) query has no untrusted source and misses common guards such as length > 0, so safe code alerts. The URL scheme query re-declares sources that the stock library already models as remote and adds low-impact sinks (alert text, message and mail composers, open) behind a loose any-Bool-call-validates barrier. Better as experimental Community Packs queries than github/codeql.

    Change New query
    Target Two new queries: unchecked NSString.range(of:) in substring bounds; deep link params reaching alerts, composers, open
    Precision assessed medium (declared medium in the pull request). Unchecked range(of:) has no untrusted source and only accepts NSNotFound or zero-length checks. The URL scheme query is flow based with narrow sinks and a broad barrier (any Bool call or comparison on the value). Both rely on mitigation-absent logic.
    Real-world basis partial. UIApplicationDelegate and UISceneDelegate deep links and NSString.range(of:) are public APIs, but the sinks mirror one training exercise (alert, SMS and mail composers) and the untrusted response premise is not modeled.
    Stock overlap partial. Stock CustomUrlSchemes.qll already models application(_:open:options:) and scene delegates as remote sources (local swift-all 6.8.3); no stock query uses these sinks or checks range(of:).
    Concerns, overlap and next steps

    False-positive concerns

    • range(of:) guarded by length > 0, location < length or NSEqualRanges is reported, as only NSNotFound and ==/!= 0 comparisons count as presence checks
    • Lookups on constant or app-controlled strings are reported; no untrusted input is required for the substring alert
    • Deep link text shown in an alert or prefilled in a composer needs user action, so impact is low yet every such use alerts
    • UIApplication.open of a URL derived from the deep link alerts although forwarding links is often by design

    Performance concerns

    • localTaint closures from every range(of:) call and a global validation barrier joining each condition with reads of the compared variable; unmeasured

    Heuristics used

    • any Bool-valued call or ==/!= on the value counts as validation
    • range(of: method prefix and NSString base type matched by name
    • only NSNotFound or zero length comparisons recognized as presence checks

    What an upstream change would look like. Two separate Community Packs queries under swift/src/<category>/ with tests in swift/test/. If kept, the deep link query should use the stock RemoteFlowSource instead of its own CustomUrlSchemeSource copy.

    Before opening an upstream pull request

    • Replace CustomUrlSchemeSource with the stock RemoteFlowSource so deep link modeling is not duplicated
    • Add a true positive from a real, non-training project for each query (Community Packs requirement)
    • Accept more presence checks (length > 0, location comparisons) or restrict the substring query to untrusted data via flow
    • Narrow the validated-variable barrier so any Bool call on the value does not count as validation

    Split note. Two independent queries. The URL scheme query is closer to upstream quality (flow based, public sources already modeled) but its sinks are low impact; neither meets the github/codeql bar. Keep both experimental.

    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-20enhancementprecision:mediumquery-createseverity:mediumtags:experimental

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions