Skip to content

Unified: Add predicate injection query - #23143

Open
geoffw0 wants to merge 12 commits into
github:mainfrom
geoffw0:unifiedpredinjection
Open

geoffw0 wants to merge 12 commits into
github:mainfrom
geoffw0:unifiedpredinjection

Conversation

@geoffw0

@geoffw0 geoffw0 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Adds a unified/swift/predicate-injection query for unified Swift. Done with Copilot + GPT 6.1-Sol + the query porting skill. I checked things over and did a bit of fixup afterwards. Just one piece of follow-up work (barriers).

Results on tests LGTM. DCA run started.

geoffw0 and others added 11 commits November 16, 2026 12:07
Create the byte-for-byte baseline for the unified Swift port, including query help and samples. The original tests live under CWE-946; place the unified copies under CWE-943 to match the query.

Co-authored-by: Copilot <[email protected]>
Copy the data flow configuration and query-specific models from the original Swift libraries into the query, preserving declarations and comments unchanged. API adaptations follow separately.

Co-authored-by: Copilot <[email protected]>
Use unified taint tracking and declarative NSPredicate sinks. Select format arguments by label and order selector labels as required by the current model interpreter.

The focused test reports four paths with no inline expectation failures. Keep the zero-variadic-argument call marked MISSING and leave the original numeric-type barrier as a TODO.

Co-authored-by: Copilot <[email protected]>
Add the format-only call selector, since the model interpreter matches supplied argument labels exactly. The existing test now detects all five original unsafe cases and still excludes bound values and constant formats.

Co-authored-by: Copilot <[email protected]>
The legacy Swift model data already provides the four original sink selectors. Remove their duplicate rows and retain only the new format-only selector. Both affected query tests pass with unchanged results.

Co-authored-by: Copilot <[email protected]>
Cover interpolation and concatenation of remote data, including the query-help patterns. Keep variadic and array-bound values as safe comparisons. All five original paths remain, with two additional paths and no expectation failures.

Co-authored-by: Copilot <[email protected]>
Describe the separate variadic argument used by the secure sample instead of naming an arguments parameter that the sample does not use. Clarify that interpolation into the format is unsafe. Validate help generation, XML and sample paths; verify the Apple reference with the restricted link checker.

Co-authored-by: Copilot <[email protected]>
…query - as we do for other target languages.
I don't see anything clarified here, if anything it's slightly less readable.

This reverts commit c9af512.
@geoffw0
geoffw0 requested a review from a team as a code owner November 16, 2026 16:11
@geoffw0 geoffw0 added the no-change-note-required This PR does not need a change note label Nov 16, 2026
Copilot AI adaptive review requested due to automatic review settings November 16, 2026 16:11
@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

unified/ql/src/queries/security/CWE-943/PredicateInjection.qhelp

Predicate built from user-controlled sources

Predicates represent logical conditions that can be used to check whether an object matches them. If a predicate is built from user-provided data without sufficient sanitization, an attacker may be able to change the overall meaning of the predicate.

Recommendation

When building a predicate from untrusted data, you should either pass it to the appropriate arguments parameter during initialization, or as an array of substitution variables before evaluation. You should not append or concatenate it to the body of the predicate.

Example

In the following insecure example, NSPredicate is built directly from data obtained from an HTTP request. This is untrusted, and can be arbitrarily set by an attacker to alter the meaning of the predicate:

let remoteString = try String(contentsOf: URL(string: "https://example.com/")!)

let filenames: [String] = ["img1.png", "img2.png", "img3.png", "img.txt", "img.csv"]

let predicate = NSPredicate(format: "SELF LIKE \(remoteString)")
let filtered = filenames.filter(){ filename in
    predicate.evaluate(with: filename)
}
print(filtered)

A better way to do this is to use the arguments parameter of NSPredicate's initializer. This prevents attackers from altering the meaning of the predicate, even if they control the externally obtained data, as seen in the following secure example:

let remoteString = try String(contentsOf: URL(string: "https://example.com/")!)

let filenames: [String] = ["img1.png", "img2.png", "img3.png", "img.txt", "img.csv"]

let predicate = NSPredicate(format: "SELF LIKE %@", remoteString)
let filtered = filenames.filter(){ filename in
    predicate.evaluate(with: filename)
}
print(filtered)

References

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The query includes non-input source kinds that cause false positives, while the NSPredicate initializer model misses attacker-controlled formats with multiple substitution arguments.

2 open issues
What changed in this PR

Adds predicate-injection detection to unified Swift analysis, identifying flows into NSPredicate format strings.

Changes:

  • Adds the query, help text, and secure/insecure examples.
  • Moves predicate sink models into a dedicated file and adds format-only initializer support.
  • Adds regression tests for initializer overloads, interpolation, concatenation, and safe parameter substitution.
File Description
unified/​ql/​test/​query-tests/​security/​CWE-943/​PredicateInjection/​PredicateInjectionTest.qlref Connects the query to inline expectation checks.
unified/​ql/​test/​query-tests/​security/​CWE-943/​PredicateInjection/​PredicateInjectionTest.expected Records expected alerts and flow paths.
unified/​ql/​test/​query-tests/​security/​CWE-943/​PredicateInjection/​predicateInjection.swift Tests vulnerable formats and safe substitutions.
unified/​ql/​src/​queries/​security/​CWE-943/​PredicateInjectionGood.swift Demonstrates safe parameter substitution.
unified/​ql/​src/​queries/​security/​CWE-943/​PredicateInjectionBad.swift Demonstrates unsafe string interpolation.
unified/​ql/​src/​queries/​security/​CWE-943/​PredicateInjection.ql Defines taint tracking and path results; leaves numeric barriers for follow-up.
unified/​ql/​src/​queries/​security/​CWE-943/​PredicateInjection.qhelp Explains the vulnerability and remediation.
unified/​ql/​lib/​ext/​nspredicate.model.json Defines dedicated predicate-injection sink models.
unified/​ql/​lib/​ext/​legacy-swift.model.json Removes sink entries moved to the dedicated model file.

Review effort: Max (dynamic)

NSPredicate(format: safeString, arguments: CVaListPointer(_fromUnsafeMutablePointer: UnsafeMutablePointer(bitPattern: 0)!)) // Safe
NSPredicate(format: remoteString) // $ Alert
NSPredicate(format: remoteString, "") // $ Alert
NSPredicate(format: remoteString, "", "") // $ MISSING: Alert

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@asgerf this case is not matched with the model because there's more than one vararg (i.e. I think it should match the model init(format:arguments:) for https://developer.apple.com/documentation/foundation/nspredicate/init(format:arguments:)). Shall I write up an issue for this, or are you already aware, or have I missed something here perhaps?

This branch has not been deployed

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

Labels

documentation no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants