Skip to content

[NEW DATA EXTENSION] Swift CWE-22: QL models for deep-link query parameters into file paths (swift/path-injection-extended) - #107

Draft
felickz with Copilot wants to merge 2 commits into
mainfrom
copilot/swift-data-extension-create-path-traversal
Draft

felickz with Copilot wants to merge 2 commits into
mainfrom
copilot/swift-data-extension-create-path-traversal

Conversation

Copilot AI commented Oct 16, 2026 •

Copy link
Copy Markdown

📝 Data Extension Information

  • Language: swift
  • Extension Name(s): no YAML file. Swift does not support data extensions, so the models are in QL: languages/swift/custom/src/PathInjectionExtended/PathInjectionModels.qll, wrapped by PathInjectionExtended.ql (swift/path-injection-extended).
  • Extension Types: summaryModel (via SummaryModelCsv), taint-inheriting fields (via TaintInheritingContent), and one extra flow step for path injection only (PathInjectionAdditionalFlowStep)
  • Target Library/Framework: Foundation (URLComponents, NSURLComponents, URLQueryItem, NSURLQueryItem), Swift stdlib (Sequence), UIKit (UIViewController)
  • Library Modules Covered: Foundation URL parsing, Sequence.first(where:), properties stored on UIViewController subclasses

🎯 Description

What This Data Extension Models

AI Scan found a deep-link "save" route (OversecuredVulnerableiOSApp) that writes attacker data to Documents/<name>. CodeQL default setup did not report it. The issue guessed the Documents URL helper was the missing link. It is not: the helper is app code, URL.appendingPathComponent is already modeled, and Data.write(to:options:) is already a sink. The flow actually breaks in three places:

Break Fix
No models for URLComponents/NSURLComponents initializers Summaries for init(url:resolvingAgainstBaseURL:) and init(string:): Argument[0] → ReturnValue.OptionalSome (taint)
queryItems, name, value have no taint inheritance TaintInheritingContent for all fields of URLComponents, NSURLComponents, URLQueryItem, NSURLQueryItem
Sequence.first(where:) only carries taint from collection elements, so a tainted queryItems array loses it Argument[-1] → ReturnValue (taint)
The deep link is stored on a view controller and read in viewDidAppear(_:), which UIKit calls, not app code Extra flow step: a write to a stored property of a UIViewController subclass flows to self.<prop> reads

The view-controller step only applies to path injection. It can over-report: any self read is flagged once any tainted value is assigned to the property, even if it was read before the write.

Swift has no extensible predicates in codeql/swift-all, so a .model.yml file cannot work. These QL models only apply to queries that import them, which is why this adds a wrapper query rather than relying on the stock swift/path-injection.

The qlt unit-test workflow now skips Swift explicitly: the Swift extractor only runs on macOS, so Linux runners cannot extract Swift tests.

Threat Model

remote. The source is the existing UIApplication.LaunchOptionsKey.url / application(_:open:options:) custom URL scheme source.

Example Vulnerable Code

override func viewDidAppear(_ animated: Bool) {
    if let deeplink = deeplink { handleDeeplink(deeplink) }   // stored via set(deeplink:)
}
func getQueryParameter(_ url: URL, _ name: String) -> String? {
    if let c = NSURLComponents(url: url, resolvingAgainstBaseURL: true), let params = c.queryItems {
        return params.first(where: { $0.name == name })?.value
    }
    return nil
}
// ...
data.write(to: FileManager.documentsUrlForFile(withName: name))   // $ hasPathInjection

Example Safe Code

data.write(to: FileManager.documentsUrlForFile(withName: "export.bin"))   // fixed file name
// SafeViewController: stored property only ever assigned constants
data.write(to: FileManager.documentsUrlForFile(withName: fileName))

📦 Extension Details

Extension YAML

Not applicable for Swift. These are the equivalent rows in UrlComponentsSummaries:

;URLComponents;true;init(url:resolvingAgainstBaseURL:);;;Argument[0];ReturnValue.OptionalSome;taint
;URLComponents;true;init(string:);;;Argument[0];ReturnValue.OptionalSome;taint
;NSURLComponents;true;init(url:resolvingAgainstBaseURL:);;;Argument[0];ReturnValue.OptionalSome;taint
;NSURLComponents;true;init(string:);;;Argument[0];ReturnValue.OptionalSome;taint
;Sequence;true;first(where:);;;Argument[-1];ReturnValue;taint

Access Path Explanation

  • The initializers return optionals, so the output is ReturnValue.OptionalSome. This matches the stock URL.init(string:) rows.
  • first(where:) uses plain ReturnValue, alongside the stock CollectionElement value row. Swift dataflow already steps through ?., !, and if let on a tainted optional.
  • Field inheritance matches on getEnclosingDecl().asNominalTypeDecl().getFullName(), so properties declared in extensions are covered too.
  • The view-controller step goes from the right-hand side of x.<prop> = v to a MemberRefExpr read of <prop> whose base is the self parameter. Assignment targets are excluded. The property's type must have UIViewController as a base type (transitively).

🧪 Testing

Swift tests cannot be extracted on Linux, so nothing has run end to end. Someone on macOS needs to run codeql test run languages/swift/custom/test/PathInjectionExtended/.

  • Extension YAML resolves without errors (not applicable; the QL compiles, and the new rows parse through summaryModel on an empty dataset)
  • Database created with sample code (codeql database create or codeql test extract)
  • Single query verified with extension applied (codeql query run --additional-packs=<model-pack-dir>)
  • Unit tests pass with extension applied (codeql test run --additional-packs=<model-pack-dir>)
  • Positive test cases (vulnerable code detected): written in deeplink.swift, not yet run
  • Negative test cases (safe code not flagged): written in deeplink.swift, not yet run

The test uses an InlineExpectationsTest query with an empty .expected file, so the expectations live inline in the Swift file.

📋 Checklist

  • Extension YAML is valid and properly formatted (not applicable to Swift)
  • Extension placed in correct location (languages/[language]/custom/src/)
  • qlpack.yml includes dataExtensions configuration (not applicable to Swift)
  • Access paths verified via API graph queries
  • No false positives in test cases
  • Documentation/comments included in YAML (QLDoc in the .qll)

🔗 References


Note: This data extension was developed following CodeQL Models as Data best practices.

… models

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add data extension models for path traversal in Swift [NEW DATA EXTENSION] Swift CWE-22: QL models for deep-link query parameters into file paths (swift/path-injection-extended) Oct 16, 2026
Copilot AI requested a review from felickz October 16, 2026 16:03
@felickz
felickz requested a balanced review from Copilot October 30, 2026 18:16

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.

🔵 Needs a closer look

The new Swift models and lifecycle-property flow remain unverified against an extracted Swift database, requiring macOS tests to confirm source-to-sink flow and both positive and negative expectations.

No open issues 🎉

What changed in this PR

Adds swift/path-injection-extended to address the deep-link path-traversal detection gap described in #104. It uses QL models rather than YAML data extensions, which Swift does not support.

Changes:

  • Models URL query-parameter parsing and stored view-controller properties to extend path-injection tracking.
  • Adds Swift query/test packs and positive and negative regression cases.
  • Skips Swift tests in the Linux unit-test workflow; end-to-end validation remains pending on macOS.
File Description
languages/​swift/​custom/​test/​qlpack.yml Defines the Swift test pack and source-pack dependency.
languages/​swift/​custom/​test/​PathInjectionExtended/​PathInjectionExtendedTest.ql Checks path-injection results against inline expectations.
languages/​swift/​custom/​test/​PathInjectionExtended/​PathInjectionExtendedTest.expected Adds an empty baseline for inline expectation checks.
languages/​swift/​custom/​test/​PathInjectionExtended/​deeplink.swift Adds deep-link flow cases and safe constant-path counterparts.
languages/​swift/​custom/​src/​qlpack.yml Defines the custom Swift query pack.
languages/​swift/​custom/​src/​PathInjectionExtended/​PathInjectionModels.qll Adds parsing summaries, field taint inheritance, and view-controller flow modeling.
languages/​swift/​custom/​src/​PathInjectionExtended/​PathInjectionExtended.ql Wraps the stock path-injection analysis with the additional models.
.github/​workflows/​codeql-qlt-unit-tests.yml Excludes Swift extraction from the Linux test runner.

Review effort: Balanced

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.

[Data Extension Create]: CWE-22 HIGH - Swift: data extension models for path traversal

3 participants