Repository navigation
[New Query - githubsecuritylab/rust/ssrf-reflection-injection] - detect Rust request forgery through Iron handlers and async closures #258
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
Draft
felickz
wants to merge
2
commits into
main
Choose a base branch
from
rust/ssrf-reflection-injection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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,42 @@ | ||
| # Server-side request forgery (extended framework coverage) | ||
|
|
||
| Directly incorporating user input into the URL of an outgoing HTTP request can make the server send requests to destinations chosen by an attacker, for example internal services or cloud metadata endpoints. The response may then be reflected back to the attacker. | ||
|
|
||
| This query extends the standard `rust/request-forgery` query with: | ||
|
|
||
| - `iron` request sources: reads of the `url`, `headers` and `body` fields of an inbound `iron::request::Request`. The authority of `req.url` is derived from the attacker-controlled `Host` header. | ||
| - Flow of captured variables into `async` blocks, such as a `warp` `Filter::then` closure parameter used inside `async move { ... }`. | ||
| - A barrier for `format!` templates that start with a fixed scheme and host (for example `"https://my.app/{}"`), because interpolated values cannot change the destination host. | ||
|
|
||
| ## Recommendation | ||
|
|
||
| Do not let user input select the scheme, host or port of an outgoing request. Use a fixed base URL and only append validated path or query components, or check the destination against an allowlist of permitted hosts. | ||
|
|
||
| ## Example | ||
|
|
||
| In the following example, a URL path parameter is used as the host of an outgoing request: | ||
|
|
||
| ```rust | ||
| let routes = warp::path::param().then(|tainted_param: String| async move { | ||
| reqwest::Client::new() | ||
| .post(format!("https://{}", tainted_param)) // BAD | ||
| .send() | ||
| .await | ||
| }); | ||
| ``` | ||
|
|
||
| The following example only uses the parameter in the path of a request to a fixed host: | ||
|
|
||
| ```rust | ||
| let routes = warp::path::param().then(|tainted_param: String| async move { | ||
| reqwest::Client::new() | ||
| .post(format!("https://my.app/{}", tainted_param)) // GOOD | ||
| .send() | ||
| .await | ||
| }); | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| - OWASP: [Server Side Request Forgery](https://owasp.org/www-community/attacks/Server_Side_Request_Forgery). | ||
| - Common Weakness Enumeration: [CWE-918](https://cwe.mitre.org/data/definitions/918.html). |
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,167 @@ | ||
| /** | ||
| * @name Server-side request forgery (extended framework coverage) | ||
| * @description Making a network request with a URL whose host is derived from | ||
| * user-controlled data allows an attacker to make the server send | ||
| * requests to arbitrary (including internal) destinations. | ||
| * @kind path-problem | ||
| * @problem.severity error | ||
| * @security-severity 9.1 | ||
| * @precision medium | ||
| * @id githubsecuritylab/rust/ssrf-reflection-injection | ||
| * @tags security | ||
| * experimental | ||
| * external/cwe/cwe-918 | ||
| */ | ||
|
|
||
| private import rust | ||
| private import codeql.rust.controlflow.ControlFlowGraph | ||
| private import codeql.rust.dataflow.DataFlow | ||
| private import codeql.rust.dataflow.Ssa | ||
| private import codeql.rust.dataflow.TaintTracking | ||
| private import codeql.rust.security.RequestForgeryExtensions | ||
|
|
||
| /** | ||
| * A read of an attacker-controlled field of an inbound `iron` HTTP request, | ||
| * such as `req.url` (whose authority comes from the `Host` header), | ||
| * `req.headers` or `req.body`. | ||
| */ | ||
| class IronRequestFieldSource extends RequestForgery::Source { | ||
| IronRequestFieldSource() { | ||
| exists(Struct s, string name | | ||
| s.getCanonicalPath() = "iron::request::Request" and | ||
| name = ["url", "headers", "body"] and | ||
| this.asExpr().(FieldExpr).getStructField() = s.getStructField(name) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `template` is the string literal of a format template that starts | ||
| * with a fixed scheme and host, followed by a path, query or fragment | ||
| * separator, for example `"https://my.app/{}"`. Values interpolated into such | ||
| * a template cannot change the destination host of the request. | ||
| */ | ||
| bindingset[template] | ||
| private predicate isFixedHostTemplate(string template) { | ||
| template.regexpMatch("(?s)r?#*\"[a-zA-Z][a-zA-Z0-9+.\\-]*://[^/{}?#@\\s\"\\\\]+[/?#].*") | ||
| } | ||
|
|
||
| /** | ||
| * A `format!(...)` call whose template starts with a fixed scheme and host, | ||
| * which is considered to sanitize request forgery. | ||
| */ | ||
| class FixedHostFormatBarrier extends RequestForgery::Barrier { | ||
| FixedHostFormatBarrier() { | ||
| exists(MacroExpr me, FormatArgsExpr fa | | ||
| this.asExpr() = me and | ||
| me.getMacroCall().getPath().getText() = "format" and | ||
| fa.getParentNode+() = me and | ||
| not exists(MacroExpr nested | | ||
| nested != me and | ||
| nested.getMacroCall().getPath().getText() = "format" and | ||
| fa.getParentNode+() = nested and | ||
| nested.getParentNode+() = me | ||
| ) and | ||
| isFixedHostTemplate(fa.getTemplate().(LiteralExpr).getTextValue()) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if the variable `v` is read in an `async` block that does not declare it. | ||
| */ | ||
| private predicate isCapturedByAsyncBlock(Variable v) { | ||
| exists(BlockExpr b, VariableReadAccess read | | ||
| b.isAsync() and | ||
| read = v.getAnAccess() and | ||
| read.getParentNode+() = b and | ||
| not v.getPat().getParentNode+() = b | ||
| ) | ||
| } | ||
|
|
||
| /** Holds if the control flow node `n` assigns to the variable `v`. */ | ||
| private predicate assignsTo(CfgNode n, Variable v) { | ||
| exists(VariableWriteAccess write | write.getVariable() = v | | ||
| n.getAstNode() = write or n.getAstNode() = write.getAssignmentExpr() | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Holds if the value that `source` gives to the variable `v` can still be its value at the control | ||
| * flow node `n`. The `source` is the pattern that declares `v`, or an assignment to `v`, and no | ||
| * other assignment to `v` lies on the path from it to `n`. | ||
| */ | ||
| private predicate valueLiveAt(Variable v, AstNode source, CfgNode n) { | ||
| isCapturedByAsyncBlock(v) and | ||
| ( | ||
| source = v.getPat() | ||
| or | ||
| source = any(VariableWriteAccess write | write.getVariable() = v).getAssignmentExpr() | ||
| ) and | ||
| n.getAstNode() = source | ||
| or | ||
| exists(CfgNode pred | | ||
| valueLiveAt(v, source, pred) and | ||
| n = pred.getASuccessor() and | ||
| not assignsTo(n, v) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Holds if a captured value from `node1` reaches a read by `node2` in an | ||
| * `async` block, for example a closure parameter used in `async move { ... }` | ||
| * (as with `warp` `Filter::then`), without being overwritten first. | ||
| * | ||
| * The captured value is the one that the variable has when the block is created: the pattern that | ||
| * declares it, or the right-hand side of an assignment that is not overwritten before the block. When | ||
| * the block is created in a closure that is nested in the scope of the variable, the pattern is used. | ||
| */ | ||
| private predicate asyncBlockCaptureStep(DataFlow::Node node1, DataFlow::Node node2) { | ||
| exists(Variable v, VariableReadAccess va, BlockExpr b, Ssa::Definition def | | ||
| b.isAsync() and | ||
| va = v.getAnAccess() and | ||
| node2.asExpr() = va and | ||
| def.getARead() = va and | ||
| def.getAnUltimateDefinition() instanceof Ssa::CapturedEntryDefinition and | ||
| va.getParentNode+() = b and | ||
| not v.getPat().getParentNode+() = b and | ||
| ( | ||
| exists(CfgNode creation, AstNode source | | ||
| creation.getAstNode() = b and | ||
| valueLiveAt(v, source, creation) and | ||
| ( | ||
| node1.asPat() = source | ||
| or | ||
| node1.asExpr() = source.(AssignmentExpr).getRhs() | ||
| ) | ||
| ) | ||
| or | ||
| b.getEnclosingCfgScope() != v.getEnclosingCfgScope() and | ||
| node1.asPat() = v.getPat() | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * A taint-tracking configuration for detecting request forgery vulnerabilities. | ||
| */ | ||
| module SsrfReflectionInjectionConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node source) { source instanceof RequestForgery::Source } | ||
|
|
||
| predicate isSink(DataFlow::Node sink) { sink instanceof RequestForgery::Sink } | ||
|
|
||
| predicate isBarrier(DataFlow::Node barrier) { barrier instanceof RequestForgery::Barrier } | ||
|
|
||
| predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { | ||
| asyncBlockCaptureStep(node1, node2) | ||
| } | ||
| } | ||
|
|
||
| module SsrfReflectionInjectionFlow = TaintTracking::Global<SsrfReflectionInjectionConfig>; | ||
|
|
||
| import SsrfReflectionInjectionFlow::PathGraph | ||
|
|
||
| from SsrfReflectionInjectionFlow::PathNode source, SsrfReflectionInjectionFlow::PathNode sink | ||
| where SsrfReflectionInjectionFlow::flowPath(source, sink) | ||
| select sink.getNode(), source, sink, "The URL of this request depends on a $@.", source.getNode(), | ||
| "user-provided value" | ||
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.