Repository navigation
Description
What to do
Fix the one open review finding on pull request 264 (branch rust/iron-response-xss-unresolved-prelude, head b88b9f6): the thread on IronResponseXss.ql.
This issue is assigned with the base branch set to the branch of that pull request. Open your pull request against rust/iron-response-xss-unresolved-prelude, not against main. Do not push to any other branch, and do not rebase or force-push the base branch.
The problem
rust/src/security/CWE-079/IronResponseXss.ql leaves some flows to the standard rust/xss query, because the data extension rows for Iron already make them a source and a sink there. Two clauses of its where do that:
- A result from a request parameter that the rows do not make a source (
IronRequestParameter) is dropped when a modelled source (Xss::Source) reaches the same sink. - A result from a modelled source into the body of a call of
Response::withthat resolves (IronResolvedResponseBody) is dropped, on the assumption that the standard query reports it.
The assumption of the second clause is wrong when a method call that has no target lies between the request and the body. The standard query cannot follow such a call. Only the extra step of this query (ironUnresolvedStep, in rust/src/common/IronFallback.qll) follows it. The second clause then drops the modelled-source result, the first clause drops the other one, and nobody reports the flow.
Reproduction
Append this to the end of rust/test/security/CWE-079/IronResponseXss/main.rs, after the last function and one blank line, so that no existing line number moves. Response::with(loud) lands on line 173.
fn bad_registered_unresolved_step_handler(req: &mut Request) -> IronResult<Response> {
let res = req.url.path();
// The method does not exist for the type, so the call has no target and the standard analysis does
// not follow the value through it.
let loud = res.shout();
// BAD: a body on its own, from a handler that is registered below, to a call that resolves. The data
// extension rows make it a flow, but the standard query cannot see it across the call that has no
// target, so this query has to report it.
Ok(Response::with(loud))
}
fn register_more() {
Iron::new(bad_registered_unresolved_step_handler).http("localhost:3015").unwrap();
}With the pushed query (head b88b9f6) the test reports 19 alerts and none at Response::with(loud). The standard query (XSS.qlref in the same directory) does not report it either.
The fix to try
Test the assumption instead of making it: drop a resolved-body result only when the standard query's own analysis reaches that body from the same source.
The standard query is, in full, source = Xss::Source, sink = Xss::Sink, barrier = Xss::Barrier, with no additional flow steps (module XssConfig in XSS.ql of codeql/rust-queries, which cannot be imported). A copy of it in this query has none of the steps of this query for calls that have no target, so it holds exactly when the standard query reports the flow.
Add, before import IronResponseXssFlow::PathGraph:
/**
* The configuration of the standard cross-site scripting query (`rust/xss`), copied from it. It has none
* of the steps of this query for calls that have no target, so a flow that it finds is a flow that the
* standard query reports.
*/
module StandardXssConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node node) { node instanceof Xss::Source }
predicate isSink(DataFlow::Node node) { node instanceof Xss::Sink }
predicate isBarrier(DataFlow::Node barrier) { barrier instanceof Xss::Barrier }
}
module StandardXssFlow = TaintTracking::Global<StandardXssConfig>;Keep the first conjunct of the where clause as it is, and replace its last conjunct (the comment and the not (...)) with:
// A body on its own that goes to a call that resolves is left to the standard query when its analysis
// reaches the body from the same source. When a call that has no target lies in between, it does not,
// and this query reports the flow.
not (
sinkNode.getNode() instanceof IronResolvedResponseBody and
exists(DataFlow::Node standardSink |
StandardXssFlow::flow(sourceNode.getNode(), standardSink) and
standardSink.getLocation() = sinkNode.getNode().getLocation()
)
)Why the location: the sink of a data extension row is a node that the library creates for the row, not the expression node of the argument. It is printed at the location of the argument (IronModels.expected in the test directory shows this), but node instanceof IronResolvedResponseBody and node instanceof Xss::Sink never holds for one node. An earlier attempt used that test as the sink of the copy, which made the sink set empty, so nothing was suppressed and every flow that the standard query reports was reported twice. Correlating by location is the plan that has not been tried yet.
If the location correlation does not work, a cheaper variant to try next is a copy whose sink is the argument expression itself (predicate isSink(DataFlow::Node node) { node instanceof IronResolvedResponseBody }, no Xss::Sink) and a where conjunct StandardXssFlow::flow(sourceNode.getNode(), sinkNode.getNode()). It can miss an argument that is a macro call such as format!(...), because the standard path reaches the sink through the macro expansion. Say in the pull request which variant you used and why.
Also update the two texts that describe the old behavior.
The doc comment of class IronResolvedResponseBody becomes:
/**
* The argument of a call of `Response::with` that resolves, when it is a body on its own and not a tuple
* of modifiers. The data extension row for the function makes it a sink of the standard cross-site
* scripting query. This query reports a flow into it only when the standard query does not (see the
* `where` clause): when the handler is not registered, so that the row has no source for it, or when a
* call that has no target lies between the request and the body.
*/And in the "Known limitations" section of rust/src/security/CWE-079/IronResponseXss.qhelp, this change:
-<code>Chain::new</code> registers it. For a handler that nothing registers in the file, a body that is
-passed on its own to a call of <code>Response::with</code> that resolves is reported by this query, as
-the standard query has no source for it.
+<code>Chain::new</code> registers it. A body that is passed on its own to a call of
+<code>Response::with</code> that resolves is reported by this query only when the standard query does
+not report it: the handler is not registered in the file, or a call that has no target lies between the
+request and the body.This change has been written but has not been run. Treat it as a design to confirm with the test, not as a verified patch.
How to check it
All commands run from the repository root. The pinned CodeQL CLI and the packs are installed by .github/workflows/copilot-setup-steps.yml.
-
Add the fixture and run the test before the query change. It must fail only because the new alert is missing:
codeql test run --additional-packs rust -J=-XX:-UsePerfData --verbosity=progress --ram=2048 rust/test/security/CWE-079/IronResponseXss -
Make the query change and run it again with
--learn. Reviewgit diffof every.expectedfile in that directory. The only changes allowed are added rows: the rows of the new alert inIronResponseXss.expected(edges, nodes and a select row whose sink isloudinResponse::with(loud)), and rows for the new functions inIronModels.expected, which lists every modelled source and sink. Not allowed: a removed row, a changed row, or any change inXSS.expectedorGlobCalls.expected. The alert count goes from 19 to 20. If anything else changes, find out why before you commit: another new alert is the same bug in another fixture, and a lost alert is a regression. -
A
--learnrun of a query that does not compile writes the compile error into.expected. Look forERRORin the files before you commit. -
Run the test once more without
--learn. It must pass. -
Format and compile with warnings as errors:
codeql query format --check-only rust/src/security/CWE-079/IronResponseXss.ql codeql query compile --additional-packs rust --warnings=error rust/src/security/CWE-079/IronResponseXss.ql
Never edit an .expected file by hand. A cold run of this test builds the iron crate and takes two to five minutes, so run only this directory and not the whole rust/test tree. If the crates cannot be downloaded in your environment, say so in the pull request description, push the query and fixture changes, and stop. Do not guess the expected output.
Rules for the change
- Change only
rust/src/security/CWE-079/IronResponseXss.ql,rust/src/security/CWE-079/IronResponseXss.qhelpand the files inrust/test/security/CWE-079/IronResponseXss/. - Do not change any pack version,
qlpack.yml, lock file (Cargo.lockincluded), suite, configuration or release file. Revert a file that a command rewrote. - Do not commit
*.actualfiles,*.testprojdirectories,targetdirectories or scratch queries. - Keep every name neutral. Do not refer to any other repository, application or project in code, comments, commit messages or the pull request.
- Pull request title:
[Query Fix - githubsecuritylab/rust/iron-response-xss] - report flows that pass a call that has no target. In the description say what changed and give the test evidence: the alert count before and after, and that the new alert is the only change. The second instance of the standard analysis adds some evaluation cost, so say that too. CI does not run on a pull request whose base is notmain, so the local test run is the evidence. - Do not reply to or resolve any review thread, and do not merge anything.
Activity