Repository navigation
[Query Fix - githubsecuritylab/rust/iron-response-xss] - find Iron response XSS when the prelude calls have no target #264
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /** | ||
| * Provides a fallback that recognizes the `iron` crate by the names it uses, for databases in which | ||
| * the calls into it cannot be resolved. | ||
| * | ||
| * A database that is built without compiling the code, as a code scanning default setup does | ||
| * (`buildMode: none`), can leave every call that goes through a glob import of `iron::prelude` without | ||
| * a target, such as `Iron::new`, `Response::with` and the methods of the request. The data extension | ||
| * rows for Iron match canonical paths, and so do the sinks that are written as calls to a canonical | ||
| * path, so none of them applies to such code. The classes and predicates here match the same API by | ||
| * name, in files that use `iron`, and for calls that have no target. | ||
| */ | ||
|
|
||
| import rust | ||
| private import codeql.rust.dataflow.DataFlow | ||
|
|
||
| /** Gets the identifier of the first segment of `path`, such as `iron` for `iron::prelude`. */ | ||
| private string getRootSegment(Path path) { | ||
| not exists(path.getQualifier()) and result = path.getSegment().getIdentifier().getText() | ||
| or | ||
| result = getRootSegment(path.getQualifier()) | ||
| } | ||
|
|
||
| /** Holds if `file` uses the `iron` crate, with a `use` item or an `extern crate` item. */ | ||
| predicate isIronFile(File file) { | ||
| exists(Use use | | ||
| use.getLocation().getFile() = file and | ||
| getRootSegment(use.getUseTree().getPath()) = "iron" | ||
| ) | ||
| or | ||
| exists(ExternCrate crate | | ||
| crate.getLocation().getFile() = file and | ||
| crate.getIdentifier().getText() = "iron" | ||
| ) | ||
| } | ||
|
|
||
| /** Holds if `typeRepr` is a type that is called `Request`, or a reference to one. */ | ||
| private predicate isRequestType(TypeRepr typeRepr) { | ||
| typeRepr.(PathTypeRepr).getPath().getSegment().getIdentifier().getText() = "Request" | ||
| or | ||
| isRequestType(typeRepr.(RefTypeRepr).getTypeRepr()) | ||
| } | ||
|
|
||
| /** | ||
| * A parameter of type `Request`, `&Request` or `&mut Request` of a function or closure in a file that | ||
| * uses `iron`: the shape of an Iron handler or middleware, whether or not the type can be resolved. | ||
| */ | ||
| class IronRequestParameter extends DataFlow::Node { | ||
| IronRequestParameter() { | ||
| exists(Param param | | ||
| param = this.asParameter() and | ||
| isIronFile(param.getLocation().getFile()) and | ||
| isRequestType(param.getTypeRepr()) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** The fields of an Iron request that hold data that the client controls. */ | ||
| private predicate isRequestField(string name) { name = ["url", "headers", "body", "extensions"] } | ||
|
|
||
| /** Holds if the method called `name` only tells a property of its receiver, not its content. */ | ||
| private predicate isNonRevealingMethod(string name) { | ||
| name = | ||
| [ | ||
| "len", "is_empty", "capacity", "count", "contains", "starts_with", "ends_with", "is_some", | ||
| "is_none", "is_ok", "is_err" | ||
| ] | ||
| } | ||
|
|
||
| /** | ||
| * Holds if the method called `name` says that it escapes or encodes for HTML, such as `escape_html` or | ||
| * `html_escape`. A name that only says `escape_default` or `encode`, or that undoes the encoding | ||
| * (`unescape_html`), does not make a value safe for HTML. | ||
| */ | ||
| bindingset[name] | ||
| private predicate isHtmlEncodingMethod(string name) { | ||
| name.toLowerCase().regexpMatch("(escape|encode)_?html\\w*|html_?(escape|encode)\\w*") | ||
| } | ||
|
|
||
| /** | ||
| * Holds if the flow goes from `pred` to `succ` in a file that uses `iron`, where the data flow library | ||
| * cannot know it because the type of `pred` is not known: `succ` is the read of a field of the | ||
| * request that does not resolve to a field, or the result of a method that has no target, called on | ||
| * `pred`. A method that says that it encodes for HTML (`escape_html`) is taken to do so, so it does not | ||
| * pass the value on. | ||
| */ | ||
| predicate ironUnresolvedStep(DataFlow::Node pred, DataFlow::Node succ) { | ||
| exists(FieldExpr field | | ||
| isIronFile(field.getLocation().getFile()) and | ||
| isRequestField(field.getIdentifier().getText()) and | ||
| not exists(field.getStructField()) and | ||
| pred.asExpr() = field.getContainer() and | ||
| succ.asExpr() = field | ||
| ) | ||
| or | ||
| exists(MethodCallExpr call | | ||
| isIronFile(call.getLocation().getFile()) and | ||
| not exists(call.getStaticTarget()) and | ||
| not isNonRevealingMethod(call.getIdentifier().getText()) and | ||
| not isHtmlEncodingMethod(call.getIdentifier().getText()) and | ||
| pred.asExpr() = call.getReceiver() and | ||
| succ.asExpr() = call | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `call` is a call of `Response::with` in a file that uses `iron`, and the call has no target. | ||
| * The call may name the type with a longer path, such as `iron::Response::with`. | ||
| */ | ||
| predicate isUnresolvedIronResponseWith(Call call) { | ||
| isIronFile(call.getLocation().getFile()) and | ||
| not exists(call.getStaticTarget()) and | ||
| exists(Path path | path = call.(CallExpr).getFunction().(PathExpr).getPath() | | ||
| path.getSegment().getIdentifier().getText() = "with" and | ||
| path.getQualifier().getSegment().getIdentifier().getText() = "Response" | ||
| ) | ||
| } |
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
74 changes: 74 additions & 0 deletions
74
rust/test/security/CWE-079/IronResponseXss/GlobCalls.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,74 @@ | ||
| | 10 | ... .path() | | ||
| | 13 | ...::with(...) | | ||
| | 17 | ... .path() | | ||
| | 19 | ...::with(...) | | ||
| | 23 | ... .path() | | ||
| | 27 | ...::with(...) | | ||
| | 31 | ... .path() | | ||
| | 34 | ...::with(...) | | ||
| | 38 | ... .query() | | ||
| | 38 | ... .unwrap_or(...) | | ||
| | 41 | ...::with(...) | | ||
| | 45 | ... .path() | | ||
| | 46 | path.join(...) | | ||
| | 49 | ...::with(...) | | ||
| | 54 | ...::with(...) | | ||
| | 58 | ... .path() | | ||
| | 59 | res.len() | | ||
| | 61 | ...::with(...) | | ||
| | 65 | ... .path() | | ||
| | 69 | ...::with(...) | | ||
| | 75 | ...::with(...) | | ||
| | 79 | ... .escape_default() | | ||
| | 79 | ... .join(...) | | ||
| | 79 | ... .path() | | ||
| | 79 | ... .to_string() | | ||
| | 82 | ...::with(...) | | ||
| | 86 | ... .escape_html() | | ||
| | 86 | ... .join(...) | | ||
| | 86 | ... .path() | | ||
| | 89 | ...::with(...) | | ||
| | 95 | ... .path() | | ||
| | 99 | clean(...) | | ||
| | 100 | ...::with(...) | | ||
| | 104 | ... .http(...) | | ||
| | 104 | ... .unwrap() | | ||
| | 104 | ...::new(...) | | ||
| | 105 | ... .http(...) | | ||
| | 105 | ... .unwrap() | | ||
| | 105 | ...::new(...) | | ||
| | 106 | ... .http(...) | | ||
| | 106 | ... .unwrap() | | ||
| | 106 | ...::new(...) | | ||
| | 107 | ... .http(...) | | ||
| | 107 | ... .unwrap() | | ||
| | 107 | ...::new(...) | | ||
| | 108 | ... .http(...) | | ||
| | 108 | ... .unwrap() | | ||
| | 108 | ...::new(...) | | ||
| | 109 | ... .http(...) | | ||
| | 109 | ... .unwrap() | | ||
| | 109 | ...::new(...) | | ||
| | 110 | ... .http(...) | | ||
| | 110 | ... .unwrap() | | ||
| | 110 | ...::new(...) | | ||
| | 111 | ... .http(...) | | ||
| | 111 | ... .unwrap() | | ||
| | 111 | ...::new(...) | | ||
| | 112 | ... .http(...) | | ||
| | 112 | ... .unwrap() | | ||
| | 112 | ...::new(...) | | ||
| | 113 | ... .http(...) | | ||
| | 113 | ... .unwrap() | | ||
| | 113 | ...::new(...) | | ||
| | 114 | ... .http(...) | | ||
| | 114 | ... .unwrap() | | ||
| | 114 | ...::new(...) | | ||
| | 115 | ... .http(...) | | ||
| | 115 | ... .unwrap() | | ||
| | 115 | ...::new(...) | | ||
| | 116 | ... .http(...) | | ||
| | 116 | ... .unwrap() | | ||
| | 116 | ...::new(...) | | ||
| | 118 | ... .path() | | ||
| | 119 | ...::with(...) | |
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,13 @@ | ||
| /** | ||
| * Lists the calls of `glob_prelude.rs` that have no target. The queries recognize Iron by its names | ||
| * because of them: if the extraction resolves these calls some day, this test changes and the file no | ||
| * longer exercises the fallback. | ||
| */ | ||
|
|
||
| import rust | ||
|
|
||
| from Call call | ||
| where | ||
| call.getLocation().getFile().getBaseName() = "glob_prelude.rs" and | ||
| not exists(call.getStaticTarget()) | ||
| select call.getLocation().getStartLine(), call.toString() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.