Repository navigation
Unified: Add predicate injection query #23143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3d25e3b
unified: Copy predicate injection query and tests unchanged
geoffw0 49680f7
unified: Collect original predicate injection configuration and models
geoffw0 2f75767
unified: Port predicate injection to unified Swift models
geoffw0 2e24f4e
unified: Model predicate formats with no variadic arguments
geoffw0 fa76b00
unified: Reuse existing shared predicate injection models
geoffw0 e971c26
unified: Test constructed predicate formats and bound values
geoffw0 c9af512
unified: Clarify safe predicate binding in query help
geoffw0 647f7a2
Unified: Tiny bits of manual cleanup.
geoffw0 ae1d929
Unified: Convert the new model file from .yml to .json.
geoffw0 e774a83
Unified: Lets organize model files according to the library, not the …
geoffw0 03bf875
Revert "unified: Clarify safe predicate binding in query help".
geoffw0 3ccc8ba
Unified: Add test coverage for the multiple varargs issue pointed out…
geoffw0 a55424c
Merge branch 'main' into unifiedpredinjection
geoffw0 f11e9f4
Unified: Fix semantic merge conflict (model format).
geoffw0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "extensions": [ | ||
| { | ||
| "addsTo": { | ||
| "pack": "codeql/unified-all", | ||
| "extensible": "legacySinkModel" | ||
| }, | ||
| "data": [ | ||
| // namespace, type, subtypes, name, signature, ext, output, kind, provenance | ||
| ["", "NSPredicate", true, "init(format:)", "", "", "Argument[format:]", "predicate-injection", "manual"], | ||
| ["", "NSPredicate", true, "init(format:_:)", "", "", "Argument[format:]", "predicate-injection", "manual"], | ||
| ["", "NSPredicate", true, "init(format:arguments:)", "", "", "Argument[format:]", "predicate-injection", "manual"], | ||
| ["", "NSPredicate", true, "init(format:argumentArray:)", "", "", "Argument[format:]", "predicate-injection", "manual"], | ||
| ["", "NSPredicate", true, "init(fromMetadataQueryString:)", "", "", "Argument[fromMetadataQueryString:]", "predicate-injection", "manual"] | ||
| ] | ||
| } | ||
| ] | ||
| } |
28 changes: 28 additions & 0 deletions
28
unified/ql/src/queries/security/CWE-943/PredicateInjection.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p> | ||
| 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. | ||
| </p> | ||
| </overview> | ||
| <recommendation> | ||
| <p> | ||
| When building a predicate from untrusted data, you should either pass it to the appropriate <code>arguments</code> 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. | ||
| </p> | ||
| </recommendation> | ||
| <example> | ||
| <p> | ||
| In the following insecure example, <code>NSPredicate</code> 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: | ||
| </p> | ||
| <sample src="PredicateInjectionBad.swift" /> | ||
| <p> | ||
| A better way to do this is to use the <code>arguments</code> parameter of <code>NSPredicate</code>'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: | ||
| </p> | ||
| <sample src="PredicateInjectionGood.swift" /> | ||
| </example> | ||
| <references> | ||
| <li>Apple Developer Documentation: <a href="https://developer.apple.com/documentation/foundation/nspredicate">NSPredicate</a> </li> | ||
| </references> | ||
| </qhelp> |
39 changes: 39 additions & 0 deletions
39
unified/ql/src/queries/security/CWE-943/PredicateInjection.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * @name Predicate built from user-controlled sources | ||
| * @description Building an NSPredicate from user-controlled sources may lead to attackers | ||
| * changing the predicate's intended logic. | ||
| * @kind path-problem | ||
| * @problem.severity error | ||
| * @security-severity 8.8 | ||
| * @precision high | ||
| * @id unified/swift/predicate-injection | ||
| * @tags security | ||
| * external/cwe/cwe-943 | ||
| */ | ||
|
|
||
| import unified | ||
|
|
||
| /** | ||
| * A taint-tracking configuration for predicate injection vulnerabilities. | ||
| */ | ||
| module PredicateInjectionConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { Models::isSource(node, _) } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { Models::isSink(node, "predicate-injection") } | ||
|
|
||
| predicate isBarrier(DataFlow::Node node) { | ||
| // TODO: Port the numeric-type barrier from original Swift's PredicateInjectionExtensions.qll. | ||
| none() | ||
| } | ||
|
|
||
| predicate observeDiffInformedIncrementalMode() { any() } | ||
| } | ||
|
|
||
| module PredicateInjectionFlow = TaintTracking::Global<PredicateInjectionConfig>; | ||
|
|
||
| import PredicateInjectionFlow::PathGraph | ||
|
|
||
| from PredicateInjectionFlow::PathNode source, PredicateInjectionFlow::PathNode sink | ||
| where PredicateInjectionFlow::flowPath(source, sink) | ||
| select sink.getNode(), source, sink, "This predicate depends on a $@.", source.getNode(), | ||
| "user-provided value" |
9 changes: 9 additions & 0 deletions
9
unified/ql/src/queries/security/CWE-943/PredicateInjectionBad.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| 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) |
9 changes: 9 additions & 0 deletions
9
unified/ql/src/queries/security/CWE-943/PredicateInjectionGood.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| 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) |
38 changes: 38 additions & 0 deletions
38
...d/ql/test/query-tests/security/CWE-943/PredicateInjection/PredicateInjectionTest.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #select | ||
| | predicateInjection.swift:26:25:26:36 | remoteString | predicateInjection.swift:23:24:23:78 | String(...) | predicateInjection.swift:26:25:26:36 | remoteString | This predicate depends on a $@. | predicateInjection.swift:23:24:23:78 | String(...) | user-provided value | | ||
| | predicateInjection.swift:29:25:29:36 | remoteString | predicateInjection.swift:23:24:23:78 | String(...) | predicateInjection.swift:29:25:29:36 | remoteString | This predicate depends on a $@. | predicateInjection.swift:23:24:23:78 | String(...) | user-provided value | | ||
| | predicateInjection.swift:31:25:31:36 | remoteString | predicateInjection.swift:23:24:23:78 | String(...) | predicateInjection.swift:31:25:31:36 | remoteString | This predicate depends on a $@. | predicateInjection.swift:23:24:23:78 | String(...) | user-provided value | | ||
| | predicateInjection.swift:32:25:32:36 | remoteString | predicateInjection.swift:23:24:23:78 | String(...) | predicateInjection.swift:32:25:32:36 | remoteString | This predicate depends on a $@. | predicateInjection.swift:23:24:23:78 | String(...) | user-provided value | | ||
| | predicateInjection.swift:36:25:36:36 | remoteString | predicateInjection.swift:23:24:23:78 | String(...) | predicateInjection.swift:36:25:36:36 | remoteString | This predicate depends on a $@. | predicateInjection.swift:23:24:23:78 | String(...) | user-provided value | | ||
| | predicateInjection.swift:39:42:39:53 | remoteString | predicateInjection.swift:23:24:23:78 | String(...) | predicateInjection.swift:39:42:39:53 | remoteString | This predicate depends on a $@. | predicateInjection.swift:23:24:23:78 | String(...) | user-provided value | | ||
| | predicateInjection.swift:46:25:46:51 | StringInterpolationExpr | predicateInjection.swift:44:24:44:79 | String(...) | predicateInjection.swift:46:25:46:51 | StringInterpolationExpr | This predicate depends on a $@. | predicateInjection.swift:44:24:44:79 | String(...) | user-provided value | | ||
| | predicateInjection.swift:47:25:47:51 | ... + ... | predicateInjection.swift:44:24:44:79 | String(...) | predicateInjection.swift:47:25:47:51 | ... + ... | This predicate depends on a $@. | predicateInjection.swift:44:24:44:79 | String(...) | user-provided value | | ||
| edges | ||
| | predicateInjection.swift:23:9:23:20 | remoteString | predicateInjection.swift:26:25:26:36 | remoteString | provenance | | | ||
| | predicateInjection.swift:23:9:23:20 | remoteString | predicateInjection.swift:29:25:29:36 | remoteString | provenance | | | ||
| | predicateInjection.swift:23:9:23:20 | remoteString | predicateInjection.swift:31:25:31:36 | remoteString | provenance | | | ||
| | predicateInjection.swift:23:9:23:20 | remoteString | predicateInjection.swift:32:25:32:36 | remoteString | provenance | | | ||
| | predicateInjection.swift:23:9:23:20 | remoteString | predicateInjection.swift:36:25:36:36 | remoteString | provenance | | | ||
| | predicateInjection.swift:23:9:23:20 | remoteString | predicateInjection.swift:39:42:39:53 | remoteString | provenance | | | ||
| | predicateInjection.swift:23:24:23:78 | String(...) | predicateInjection.swift:23:9:23:20 | remoteString | provenance | | | ||
| | predicateInjection.swift:44:9:44:20 | remoteString | predicateInjection.swift:46:38:46:49 | remoteString | provenance | | | ||
| | predicateInjection.swift:44:9:44:20 | remoteString | predicateInjection.swift:47:40:47:51 | remoteString | provenance | | | ||
| | predicateInjection.swift:44:24:44:79 | String(...) | predicateInjection.swift:44:9:44:20 | remoteString | provenance | | | ||
| | predicateInjection.swift:46:38:46:49 | remoteString | predicateInjection.swift:46:25:46:51 | StringInterpolationExpr | provenance | | | ||
| | predicateInjection.swift:47:40:47:51 | remoteString | predicateInjection.swift:47:25:47:51 | ... + ... | provenance | | | ||
| nodes | ||
| | predicateInjection.swift:23:9:23:20 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:23:24:23:78 | String(...) | semmle.label | String(...) | | ||
| | predicateInjection.swift:26:25:26:36 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:29:25:29:36 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:31:25:31:36 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:32:25:32:36 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:36:25:36:36 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:39:42:39:53 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:44:9:44:20 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:44:24:44:79 | String(...) | semmle.label | String(...) | | ||
| | predicateInjection.swift:46:25:46:51 | StringInterpolationExpr | semmle.label | StringInterpolationExpr | | ||
| | predicateInjection.swift:46:38:46:49 | remoteString | semmle.label | remoteString | | ||
| | predicateInjection.swift:47:25:47:51 | ... + ... | semmle.label | ... + ... | | ||
| | predicateInjection.swift:47:40:47:51 | remoteString | semmle.label | remoteString | | ||
| subpaths |
3 changes: 3 additions & 0 deletions
3
unified/ql/test/query-tests/security/CWE-943/PredicateInjection/PredicateInjectionTest.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| query: queries/security/CWE-943/PredicateInjection.ql | ||
| postprocess: | ||
| - utils/test/InlineExpectationsTestQuery.ql |
50 changes: 50 additions & 0 deletions
50
unified/ql/test/query-tests/security/CWE-943/PredicateInjection/predicateInjection.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // --- stubs --- | ||
| struct URL { | ||
| init?(string: String) {} | ||
| } | ||
|
|
||
| extension String { | ||
| init(contentsOf: URL) { | ||
| let data = "" | ||
| self.init(data) | ||
| } | ||
| } | ||
|
|
||
| class NSPredicate { | ||
| init(format: String, argumentArray: [Any]?) {} | ||
| init(format: String, arguments: CVaListPointer) {} | ||
| init(format: String, _: CVarArg...) {} | ||
| init?(fromMetadataQueryString: String) {} | ||
| } | ||
|
|
||
| // --- tests --- | ||
|
|
||
| func test() { | ||
| let remoteString = String(contentsOf: URL(string: "http://example.com/")!) // $ Source | ||
| let safeString = "safe" | ||
|
|
||
| NSPredicate(format: remoteString, argumentArray: []) // $ Alert | ||
| NSPredicate(format: safeString, argumentArray: []) // Safe | ||
| NSPredicate(format: safeString, argumentArray: [remoteString]) // Safe | ||
| NSPredicate(format: remoteString, arguments: CVaListPointer(_fromUnsafeMutablePointer: UnsafeMutablePointer(bitPattern: 0)!)) // $ Alert | ||
| NSPredicate(format: safeString, arguments: CVaListPointer(_fromUnsafeMutablePointer: UnsafeMutablePointer(bitPattern: 0)!)) // Safe | ||
| NSPredicate(format: remoteString) // $ Alert | ||
| NSPredicate(format: remoteString, "") // $ Alert | ||
| NSPredicate(format: remoteString, "", "") // $ MISSING: Alert | ||
| NSPredicate(format: safeString, remoteString, remoteString) // Safe | ||
| NSPredicate(format: safeString) // Safe | ||
| NSPredicate(format: remoteString, "" as! CVarArg) // $ Alert | ||
| NSPredicate(format: safeString, "" as! CVarArg) // Safe | ||
| NSPredicate(format: safeString, remoteString as! CVarArg) // Safe | ||
| NSPredicate(fromMetadataQueryString: remoteString) // $ Alert | ||
| NSPredicate(fromMetadataQueryString: safeString) // Safe | ||
| } | ||
|
|
||
| func testConstructedFormats() { | ||
| let remoteString = String(contentsOf: URL(string: "https://example.com/")!) // $ Source | ||
|
|
||
| NSPredicate(format: "SELF LIKE \(remoteString)") // $ Alert | ||
| NSPredicate(format: "SELF LIKE " + remoteString) // $ Alert | ||
| NSPredicate(format: "SELF LIKE %@", remoteString) // Safe | ||
| NSPredicate(format: "SELF LIKE %@", argumentArray: [remoteString]) // Safe | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's actually targeting this one where the parameter is declared with the varargs syntax ("
..."). We have an issue for this, but it's good to be aware that it's affecting our current models.