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
base: main
Are you sure you want to change the base?
Changes from all commits
3d25e3b
49680f7
2f75767
2e24f4e
fa76b00
e971c26
c9af512
647f7a2
ae1d929
e774a83
03bf875
3ccc8ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(arguments:format:)", "", "", "Argument[format:]", "predicate-injection", "manual"], | ||
| ["", "NSPredicate", true, "init(argumentArray:format:)", "", "", "Argument[format:]", "predicate-injection", "manual"], | ||
| ["", "NSPredicate", true, "init(fromMetadataQueryString:)", "", "", "Argument[fromMetadataQueryString:]", "predicate-injection", "manual"] | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| 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> |
| 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" |
| 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) |
| 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) |
| 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 |
| 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 |
| 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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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 | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.