From d7d3f224a47007428a639a1a1d6a2a1d38a15c68 Mon Sep 17 00:00:00 2001 From: Chad Bentz <1760475+felickz@users.noreply.github.com> Date: Sat, 21 Nov 2026 13:22:38 -0500 Subject: [PATCH 1/5] Rust: add Iron models and the response modifier tuple XSS query Add source, sink and summary rows for the Iron web framework, and githubsecuritylab/rust/iron-response-xss (CWE-79), which reports user input in a modifier tuple that is passed to Response::with when no modifier sets a content type other than HTML or XML. A body passed to Response::with on its own is covered by the html-injection sink row. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 --- rust/ext/manual/iron.model.yml | 22 + .../security/CWE-079/IronResponseXss.qhelp | 54 ++ rust/src/security/CWE-079/IronResponseXss.ql | 104 ++++ .../security/CWE-079/IronResponseXssBad.rs | 8 + .../security/CWE-079/IronResponseXssGood.rs | 10 + .../CWE-079/IronResponseXss/Cargo.lock | 487 ++++++++++++++++++ .../IronResponseXss/IronModels.expected | 17 + .../CWE-079/IronResponseXss/IronModels.ql | 16 + .../IronResponseXss/IronResponseXss.expected | 92 ++++ .../IronResponseXss/IronResponseXss.qlref | 1 + .../security/CWE-079/IronResponseXss/main.rs | 77 +++ .../CWE-079/IronResponseXss/options.yml | 2 + 12 files changed, 890 insertions(+) create mode 100644 rust/ext/manual/iron.model.yml create mode 100644 rust/src/security/CWE-079/IronResponseXss.qhelp create mode 100644 rust/src/security/CWE-079/IronResponseXss.ql create mode 100644 rust/src/security/CWE-079/IronResponseXssBad.rs create mode 100644 rust/src/security/CWE-079/IronResponseXssGood.rs create mode 100644 rust/test/security/CWE-079/IronResponseXss/Cargo.lock create mode 100644 rust/test/security/CWE-079/IronResponseXss/IronModels.expected create mode 100644 rust/test/security/CWE-079/IronResponseXss/IronModels.ql create mode 100644 rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected create mode 100644 rust/test/security/CWE-079/IronResponseXss/IronResponseXss.qlref create mode 100644 rust/test/security/CWE-079/IronResponseXss/main.rs create mode 100644 rust/test/security/CWE-079/IronResponseXss/options.yml diff --git a/rust/ext/manual/iron.model.yml b/rust/ext/manual/iron.model.yml new file mode 100644 index 00000000..2175e89d --- /dev/null +++ b/rust/ext/manual/iron.model.yml @@ -0,0 +1,22 @@ +extensions: + - addsTo: + pack: codeql/rust-all + extensible: sourceModel + data: + # The `&mut Request` passed to a handler registered with `Chain::new` or `Iron::new`. + - ["::new", "Argument[0].Parameter[0]", "remote", "manual"] + - ["::new", "Argument[0].Parameter[0]", "remote", "manual"] + - addsTo: + pack: codeql/rust-all + extensible: sinkModel + data: + # `Response::with(body)` sets the response body without a `Content-Type`, so browsers + # sniff the content type and render markup as HTML. Bodies passed inside a modifier tuple, + # such as `Response::with((status::Ok, body))`, cannot be expressed as a MaD sink and are + # handled by the `githubsecuritylab/rust/iron-response-xss` query. + - ["::with", "Argument[0]", "html-injection", "manual"] + - addsTo: + pack: codeql/rust-all + extensible: summaryModel + data: + - ["::path", "Argument[self].Reference", "ReturnValue", "taint", "manual"] diff --git a/rust/src/security/CWE-079/IronResponseXss.qhelp b/rust/src/security/CWE-079/IronResponseXss.qhelp new file mode 100644 index 00000000..fb27ecf4 --- /dev/null +++ b/rust/src/security/CWE-079/IronResponseXss.qhelp @@ -0,0 +1,54 @@ + + + + +

+The Iron web framework builds a response from a tuple of modifiers, for example +Response::with((status::Ok, body)). A body that is added this way is sent without a +Content-Type header unless one of the modifiers sets it, and browsers then guess the +content type from the body. If the body contains user-controlled data, an attacker can make the +browser render it as HTML and run a script in the context of the application. +

+

+This query finds user-controlled data that is part of a modifier tuple passed to +Response::with. A tuple that also contains a content type parsed from a string literal +that does not name HTML or XML, such as "text/plain".parse::<Mime>().unwrap(), is +not reported. +

+
+ + +

+Set an explicit content type that is not HTML when the body is not markup, or encode the +user-controlled data for HTML before it is added to the body. +

+
+ + +

+In the following example, the path of the request is written into the body of the response without +a content type, so a path that contains markup is rendered as HTML: +

+ +

+In the following corrected example, the response declares that its body is plain text: +

+ +
+ +
+

+A content type that is set by another kind of modifier, such as Header(ContentType::json()), +is not recognized, so the body of such a response is still reported. A content type is recognized +only when it is parsed directly from a string literal and flows to the tuple through local +variables, unwrap, expect or ?. +

+
+ + +
  • OWASP: Cross Site Scripting (XSS).
  • +
  • Common Weakness Enumeration: CWE-79.
  • +
    +
    diff --git a/rust/src/security/CWE-079/IronResponseXss.ql b/rust/src/security/CWE-079/IronResponseXss.ql new file mode 100644 index 00000000..5b4c1261 --- /dev/null +++ b/rust/src/security/CWE-079/IronResponseXss.ql @@ -0,0 +1,104 @@ +/** + * @name Cross-site scripting through an Iron response modifier tuple + * @description Writing user input into the body of an Iron response built from a + * modifier tuple (for example `Response::with((status::Ok, body))`) + * allows for a cross-site scripting vulnerability, because Iron does + * not set a `Content-Type` and browsers sniff the body as HTML. + * @kind path-problem + * @problem.severity error + * @security-severity 6.1 + * @precision medium + * @id githubsecuritylab/rust/iron-response-xss + * @tags security + * experimental + * external/cwe/cwe-079 + * external/cwe/cwe-116 + */ + +import rust +import codeql.rust.dataflow.DataFlow +import codeql.rust.dataflow.TaintTracking +import codeql.rust.security.XssExtensions + +/** + * A string literal that is parsed, such as the `"text/plain"` of + * `"text/plain".parse::().unwrap()`. + */ +private class ParsedStringLiteral extends StringLiteralExpr { + ParsedStringLiteral() { + exists(MethodCallExpr parse | + parse.getIdentifier().getText() = "parse" and + parse.getReceiver() = this + ) + } +} + +/** Holds if `literal` names a content type that browsers render as markup. */ +private predicate isMarkupContentType(StringLiteralExpr literal) { + literal.getTextValue().toLowerCase().matches(["%html%", "%xml%"]) +} + +/** + * Tracks a parsed string literal to the tuple elements it becomes, through `unwrap`, `expect`, `?` + * and local variables. + */ +module ParsedLiteralConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node node) { node.asExpr() instanceof ParsedStringLiteral } + + predicate isSink(DataFlow::Node node) { + exists(TupleExpr modifiers | node.asExpr() = modifiers.getAField()) + } + + predicate isBarrier(DataFlow::Node barrier) { none() } +} + +module ParsedLiteralFlow = TaintTracking::Global; + +/** + * An element of a modifier tuple passed to `iron::Response::with`, such as `body` in + * `Response::with((status::Ok, body))`. + * + * Tuples that contain a content type modifier parsed from a string literal that does not name + * HTML or XML set an explicit, safe `Content-Type` and are not considered sinks. + */ +class IronResponseModifierTupleElement extends DataFlow::Node { + IronResponseModifierTupleElement() { + exists(Call call, TupleExpr modifiers, Expr argument | + call.getStaticTarget().getCanonicalPath() = "::with" and + argument = call.getPositionalArgument(0) and + exists(DataFlow::ExprNode tupleNode, DataFlow::ExprNode argumentNode | + tupleNode.asExpr() = modifiers and + argumentNode.asExpr() = argument and + DataFlow::localFlow(tupleNode, argumentNode) + ) and + this.asExpr() = modifiers.getAField() and + not exists( + ParsedStringLiteral literal, DataFlow::ExprNode literalNode, DataFlow::ExprNode modifierNode + | + not isMarkupContentType(literal) and + literalNode.asExpr() = literal and + modifierNode.asExpr() = modifiers.getAField() and + ParsedLiteralFlow::flow(literalNode, modifierNode) + ) + ) + } +} + +module IronResponseXssConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node node) { node instanceof Xss::Source } + + predicate isSink(DataFlow::Node node) { node instanceof IronResponseModifierTupleElement } + + predicate isBarrier(DataFlow::Node barrier) { barrier instanceof Xss::Barrier } + + predicate observeDiffInformedIncrementalMode() { any() } +} + +module IronResponseXssFlow = TaintTracking::Global; + +import IronResponseXssFlow::PathGraph + +from IronResponseXssFlow::PathNode sourceNode, IronResponseXssFlow::PathNode sinkNode +where IronResponseXssFlow::flowPath(sourceNode, sinkNode) +select sinkNode.getNode(), sourceNode, sinkNode, "Cross-site scripting vulnerability due to a $@.", + sourceNode.getNode(), "user-provided value" diff --git a/rust/src/security/CWE-079/IronResponseXssBad.rs b/rust/src/security/CWE-079/IronResponseXssBad.rs new file mode 100644 index 00000000..1ade1e4c --- /dev/null +++ b/rust/src/security/CWE-079/IronResponseXssBad.rs @@ -0,0 +1,8 @@ +use iron::status; +use iron::{IronResult, Request, Response}; + +fn handler(req: &mut Request) -> IronResult { + let name = req.url.path(); + let body = format!("

    Hello, {:?}!

    ", name); + Ok(Response::with((status::Ok, body))) // BAD: no content type, the body may be sniffed as HTML +} diff --git a/rust/src/security/CWE-079/IronResponseXssGood.rs b/rust/src/security/CWE-079/IronResponseXssGood.rs new file mode 100644 index 00000000..a750c098 --- /dev/null +++ b/rust/src/security/CWE-079/IronResponseXssGood.rs @@ -0,0 +1,10 @@ +use iron::mime::Mime; +use iron::status; +use iron::{IronResult, Request, Response}; + +fn handler(req: &mut Request) -> IronResult { + let name = req.url.path(); + let body = format!("Hello, {:?}!", name); + let plain: Mime = "text/plain".parse().unwrap(); + Ok(Response::with((status::Ok, plain, body))) // GOOD: the body is declared as plain text +} diff --git a/rust/test/security/CWE-079/IronResponseXss/Cargo.lock b/rust/test/security/CWE-079/IronResponseXss/Cargo.lock new file mode 100644 index 00000000..990ead15 --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/Cargo.lock @@ -0,0 +1,487 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "autocfg" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dde43e75fd43e8a1bf86103336bc699aa8d17ad1be60c76c0bdfd4828e19b78" +dependencies = [ + "autocfg 1.5.1", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "base64" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" +dependencies = [ + "byteorder", + "safemem", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +dependencies = [ + "bitflags", +] + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + +[[package]] +name = "hermit-abi" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hyper" +version = "0.10.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a0652d9a2609a968c14be1a9ea00bf4b1d64e2e1f53a1b51b6fff3a6e829273" +dependencies = [ + "base64", + "httparse", + "language-tags", + "log 0.3.9", + "mime", + "num_cpus", + "time", + "traitobject", + "typeable", + "unicase", + "url", +] + +[[package]] +name = "idna" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "iron" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6d308ca2d884650a8bf9ed2ff4cb13fbb2207b71f64cda11dc9b892067295e8" +dependencies = [ + "hyper", + "log 0.3.9", + "mime_guess", + "modifier", + "num_cpus", + "plugin", + "typemap", + "url", +] + +[[package]] +name = "language-tags" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "log" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" +dependencies = [ + "log 0.4.35", +] + +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + +[[package]] +name = "matches" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5" + +[[package]] +name = "mime" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" +dependencies = [ + "log 0.3.9", +] + +[[package]] +name = "mime_guess" +version = "1.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "216929a5ee4dd316b1702eedf5e74548c123d370f47841ceaac38ca154690ca3" +dependencies = [ + "mime", + "phf", + "phf_codegen", + "unicase", +] + +[[package]] +name = "modifier" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f5c9112cb662acd3b204077e0de5bc66305fa8df65c8019d5adb10e9ab6e58" + +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "percent-encoding" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" + +[[package]] +name = "phf" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_codegen" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e" +dependencies = [ + "phf_generator", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_shared" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" +dependencies = [ + "siphasher", + "unicase", +] + +[[package]] +name = "plugin" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a6a0dc3910bc8db877ffed8e457763b317cf880df4ae19109b9f77d277cf6e0" +dependencies = [ + "typemap", +] + +[[package]] +name = "rand" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" +dependencies = [ + "autocfg 0.1.8", + "libc", + "rand_chacha", + "rand_core 0.4.3", + "rand_hc", + "rand_isaac", + "rand_jitter", + "rand_os", + "rand_pcg", + "rand_xorshift", + "winapi", +] + +[[package]] +name = "rand_chacha" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" +dependencies = [ + "autocfg 0.1.8", + "rand_core 0.3.2", +] + +[[package]] +name = "rand_core" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96f815e01bbd9678b50d927f79aa1cf3ffdfdb1b9787317c1284dadb894ad0e8" +dependencies = [ + "rand_core 0.4.3", +] + +[[package]] +name = "rand_core" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e5937858e6fd18cd595d558f90bb5de3b72ae23f9e3763af0e805949b04ef60" + +[[package]] +name = "rand_hc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" +dependencies = [ + "rand_core 0.3.2", +] + +[[package]] +name = "rand_isaac" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" +dependencies = [ + "rand_core 0.3.2", +] + +[[package]] +name = "rand_jitter" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" +dependencies = [ + "libc", + "rand_core 0.4.3", + "winapi", +] + +[[package]] +name = "rand_os" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +dependencies = [ + "cloudabi", + "fuchsia-cprng", + "libc", + "rand_core 0.4.3", + "rdrand", + "winapi", +] + +[[package]] +name = "rand_pcg" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" +dependencies = [ + "autocfg 0.1.8", + "rand_core 0.4.3", +] + +[[package]] +name = "rand_xorshift" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" +dependencies = [ + "rand_core 0.3.2", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.2", +] + +[[package]] +name = "safemem" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" + +[[package]] +name = "siphasher" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" + +[[package]] +name = "test" +version = "0.0.1" +dependencies = [ + "iron", +] + +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "tinyvec" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" + +[[package]] +name = "traitobject" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04a79e25382e2e852e8da874249358d382ebaf259d0d34e75d8db16a7efabbc7" + +[[package]] +name = "typeable" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887" + +[[package]] +name = "typemap" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "653be63c80a3296da5551e1bfd2cca35227e13cdd08c6668903ae2f4f77aa1f6" +dependencies = [ + "unsafe-any", +] + +[[package]] +name = "unicase" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-bidi" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" + +[[package]] +name = "unicode-normalization" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cd3fb03d5288b3ba9a1ee103ac7e9cbb59f0b03d64f8ddfcc04dd42758a85d" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unsafe-any" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30360d7979f5e9c6e6cea48af192ea8fab4afb3cf72597154b8f08935bc9c7f" +dependencies = [ + "traitobject", +] + +[[package]] +name = "url" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" +dependencies = [ + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "version_check" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected new file mode 100644 index 00000000..68a0fe02 --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected @@ -0,0 +1,17 @@ +| main.rs:9:23:9:44 | TupleExpr | html-injection sink | +| main.rs:16:23:16:49 | MacroExpr | html-injection sink | +| main.rs:24:23:24:51 | TupleExpr | html-injection sink | +| main.rs:32:23:32:50 | TupleExpr | html-injection sink | +| main.rs:37:23:37:52 | TupleExpr | html-injection sink | +| main.rs:48:27:48:48 | TupleExpr | html-injection sink | +| main.rs:57:23:57:31 | modifiers | html-injection sink | +| main.rs:65:23:65:50 | TupleExpr | html-injection sink | +| main.rs:69:15:69:25 | bad_handler | remote source | +| main.rs:70:15:70:47 | ...::new(...) | remote source | +| main.rs:70:26:70:46 | bad_body_only_handler | remote source | +| main.rs:71:15:71:37 | good_plain_text_handler | remote source | +| main.rs:72:15:72:31 | good_json_handler | remote source | +| main.rs:73:15:73:35 | good_constant_handler | remote source | +| main.rs:74:15:74:36 | ...::handler | remote source | +| main.rs:75:15:75:37 | bad_named_tuple_handler | remote source | +| main.rs:76:15:76:35 | bad_html_mime_handler | remote source | diff --git a/rust/test/security/CWE-079/IronResponseXss/IronModels.ql b/rust/test/security/CWE-079/IronResponseXss/IronModels.ql new file mode 100644 index 00000000..1908ea0f --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/IronModels.ql @@ -0,0 +1,16 @@ +/** + * Tests the Iron data extension rows: the request passed to a handler is a remote source, and the + * argument of `Response::with` is an HTML injection sink. + */ + +import rust +import codeql.rust.dataflow.DataFlow +import codeql.rust.dataflow.FlowSink +import codeql.rust.dataflow.FlowSource + +from DataFlow::Node node, string kind +where + sourceNode(node, "remote") and kind = "remote source" + or + sinkNode(node, "html-injection") and kind = "html-injection sink" +select node, kind diff --git a/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected new file mode 100644 index 00000000..a39d611a --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected @@ -0,0 +1,92 @@ +edges +| main.rs:5:16:5:32 | ...: ... | main.rs:6:15:6:21 | req.url | provenance | | +| main.rs:6:9:6:11 | res | main.rs:8:28:8:68 | MacroExpr | provenance | | +| main.rs:6:15:6:21 | req.url | main.rs:6:15:6:28 | ... .path() | provenance | MaD:17587 | +| main.rs:6:15:6:28 | ... .path() | main.rs:6:9:6:11 | res | provenance | | +| main.rs:8:9:8:16 | response | main.rs:9:36:9:43 | response | provenance | | +| main.rs:8:28:8:68 | ...::format(...) | main.rs:8:28:8:68 | { ... } | provenance | | +| main.rs:8:28:8:68 | ...::must_use(...) | main.rs:8:9:8:16 | response | provenance | | +| main.rs:8:28:8:68 | MacroExpr | main.rs:8:28:8:68 | ...::format(...) | provenance | MaD:371 | +| main.rs:8:28:8:68 | { ... } | main.rs:8:28:8:68 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:44:20:44:36 | ...: ... | main.rs:45:19:45:25 | req.url | provenance | | +| main.rs:45:13:45:15 | res | main.rs:46:32:46:72 | MacroExpr | provenance | | +| main.rs:45:19:45:25 | req.url | main.rs:45:19:45:32 | ... .path() | provenance | MaD:17587 | +| main.rs:45:19:45:32 | ... .path() | main.rs:45:13:45:15 | res | provenance | | +| main.rs:46:13:46:20 | response | main.rs:48:40:48:47 | response | provenance | | +| main.rs:46:32:46:72 | ...::format(...) | main.rs:46:32:46:72 | { ... } | provenance | | +| main.rs:46:32:46:72 | ...::must_use(...) | main.rs:46:13:46:20 | response | provenance | | +| main.rs:46:32:46:72 | MacroExpr | main.rs:46:32:46:72 | ...::format(...) | provenance | MaD:371 | +| main.rs:46:32:46:72 | { ... } | main.rs:46:32:46:72 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:52:28:52:44 | ...: ... | main.rs:53:15:53:21 | req.url | provenance | | +| main.rs:53:9:53:11 | res | main.rs:54:28:54:68 | MacroExpr | provenance | | +| main.rs:53:15:53:21 | req.url | main.rs:53:15:53:28 | ... .path() | provenance | MaD:17587 | +| main.rs:53:15:53:28 | ... .path() | main.rs:53:9:53:11 | res | provenance | | +| main.rs:54:9:54:16 | response | main.rs:56:34:56:41 | response | provenance | | +| main.rs:54:28:54:68 | ...::format(...) | main.rs:54:28:54:68 | { ... } | provenance | | +| main.rs:54:28:54:68 | ...::must_use(...) | main.rs:54:9:54:16 | response | provenance | | +| main.rs:54:28:54:68 | MacroExpr | main.rs:54:28:54:68 | ...::format(...) | provenance | MaD:371 | +| main.rs:54:28:54:68 | { ... } | main.rs:54:28:54:68 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:60:26:60:42 | ...: ... | main.rs:61:15:61:21 | req.url | provenance | | +| main.rs:61:9:61:11 | res | main.rs:62:28:62:68 | MacroExpr | provenance | | +| main.rs:61:15:61:21 | req.url | main.rs:61:15:61:28 | ... .path() | provenance | MaD:17587 | +| main.rs:61:15:61:28 | ... .path() | main.rs:61:9:61:11 | res | provenance | | +| main.rs:62:9:62:16 | response | main.rs:65:42:65:49 | response | provenance | | +| main.rs:62:28:62:68 | ...::format(...) | main.rs:62:28:62:68 | { ... } | provenance | | +| main.rs:62:28:62:68 | ...::must_use(...) | main.rs:62:9:62:16 | response | provenance | | +| main.rs:62:28:62:68 | MacroExpr | main.rs:62:28:62:68 | ...::format(...) | provenance | MaD:371 | +| main.rs:62:28:62:68 | { ... } | main.rs:62:28:62:68 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:69:15:69:25 | bad_handler | main.rs:5:16:5:32 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:74:15:74:36 | ...::handler | main.rs:44:20:44:36 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:75:15:75:37 | bad_named_tuple_handler | main.rs:52:28:52:44 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:76:15:76:35 | bad_html_mime_handler | main.rs:60:26:60:42 | ...: ... | provenance | Src:MaD:17584 | +nodes +| main.rs:5:16:5:32 | ...: ... | semmle.label | ...: ... | +| main.rs:6:9:6:11 | res | semmle.label | res | +| main.rs:6:15:6:21 | req.url | semmle.label | req.url | +| main.rs:6:15:6:28 | ... .path() | semmle.label | ... .path() | +| main.rs:8:9:8:16 | response | semmle.label | response | +| main.rs:8:28:8:68 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:8:28:8:68 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:8:28:8:68 | MacroExpr | semmle.label | MacroExpr | +| main.rs:8:28:8:68 | { ... } | semmle.label | { ... } | +| main.rs:9:36:9:43 | response | semmle.label | response | +| main.rs:44:20:44:36 | ...: ... | semmle.label | ...: ... | +| main.rs:45:13:45:15 | res | semmle.label | res | +| main.rs:45:19:45:25 | req.url | semmle.label | req.url | +| main.rs:45:19:45:32 | ... .path() | semmle.label | ... .path() | +| main.rs:46:13:46:20 | response | semmle.label | response | +| main.rs:46:32:46:72 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:46:32:46:72 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:46:32:46:72 | MacroExpr | semmle.label | MacroExpr | +| main.rs:46:32:46:72 | { ... } | semmle.label | { ... } | +| main.rs:48:40:48:47 | response | semmle.label | response | +| main.rs:52:28:52:44 | ...: ... | semmle.label | ...: ... | +| main.rs:53:9:53:11 | res | semmle.label | res | +| main.rs:53:15:53:21 | req.url | semmle.label | req.url | +| main.rs:53:15:53:28 | ... .path() | semmle.label | ... .path() | +| main.rs:54:9:54:16 | response | semmle.label | response | +| main.rs:54:28:54:68 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:54:28:54:68 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:54:28:54:68 | MacroExpr | semmle.label | MacroExpr | +| main.rs:54:28:54:68 | { ... } | semmle.label | { ... } | +| main.rs:56:34:56:41 | response | semmle.label | response | +| main.rs:60:26:60:42 | ...: ... | semmle.label | ...: ... | +| main.rs:61:9:61:11 | res | semmle.label | res | +| main.rs:61:15:61:21 | req.url | semmle.label | req.url | +| main.rs:61:15:61:28 | ... .path() | semmle.label | ... .path() | +| main.rs:62:9:62:16 | response | semmle.label | response | +| main.rs:62:28:62:68 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:62:28:62:68 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:62:28:62:68 | MacroExpr | semmle.label | MacroExpr | +| main.rs:62:28:62:68 | { ... } | semmle.label | { ... } | +| main.rs:65:42:65:49 | response | semmle.label | response | +| main.rs:69:15:69:25 | bad_handler | semmle.label | bad_handler | +| main.rs:74:15:74:36 | ...::handler | semmle.label | ...::handler | +| main.rs:75:15:75:37 | bad_named_tuple_handler | semmle.label | bad_named_tuple_handler | +| main.rs:76:15:76:35 | bad_html_mime_handler | semmle.label | bad_html_mime_handler | +subpaths +#select +| main.rs:9:36:9:43 | response | main.rs:69:15:69:25 | bad_handler | main.rs:9:36:9:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:69:15:69:25 | bad_handler | user-provided value | +| main.rs:48:40:48:47 | response | main.rs:74:15:74:36 | ...::handler | main.rs:48:40:48:47 | response | Cross-site scripting vulnerability due to a $@. | main.rs:74:15:74:36 | ...::handler | user-provided value | +| main.rs:56:34:56:41 | response | main.rs:75:15:75:37 | bad_named_tuple_handler | main.rs:56:34:56:41 | response | Cross-site scripting vulnerability due to a $@. | main.rs:75:15:75:37 | bad_named_tuple_handler | user-provided value | +| main.rs:65:42:65:49 | response | main.rs:76:15:76:35 | bad_html_mime_handler | main.rs:65:42:65:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:76:15:76:35 | bad_html_mime_handler | user-provided value | diff --git a/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.qlref b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.qlref new file mode 100644 index 00000000..90a131eb --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.qlref @@ -0,0 +1 @@ +query: security/CWE-079/IronResponseXss.ql diff --git a/rust/test/security/CWE-079/IronResponseXss/main.rs b/rust/test/security/CWE-079/IronResponseXss/main.rs new file mode 100644 index 00000000..0101a061 --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/main.rs @@ -0,0 +1,77 @@ +use iron::mime::Mime; +use iron::status; +use iron::{Chain, Iron, IronResult, Request, Response}; + +fn bad_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + // BAD: the request path is formatted into markup and returned without encoding. + let response = format!("

    Assessing resource, {:?}!

    ", res); + Ok(Response::with((status::Ok, response))) +} + +fn bad_body_only_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + // BAD: a body modifier on its own is also served without a `Content-Type`. It is not a tuple, + // so it is covered by the `Response::with` sink model and not by the tuple query. + Ok(Response::with(format!("

    {:?}

    ", res))) +} + +fn good_plain_text_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // GOOD: an explicit `text/plain` content type is set. + let plain: Mime = "text/plain".parse().unwrap(); + Ok(Response::with((status::Ok, plain, response))) +} + +fn good_json_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("{{\"path\": {:?}}}", res); + // GOOD: an explicit `application/json` content type is set. + let json: Mime = "application/json".parse().unwrap(); + Ok(Response::with((status::Ok, json, response))) +} + +fn good_constant_handler(_req: &mut Request) -> IronResult { + // GOOD: no user input in the body. + Ok(Response::with((status::Ok, "

    Hello

    "))) +} + +mod prelude_style { + use iron::prelude::*; + use iron::status; + + pub fn handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // BAD: same as `bad_handler`, using the `iron::prelude` imports. + Ok(Response::with((status::Ok, response))) + } +} + +fn bad_named_tuple_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // BAD: the tuple is built first and passed to `Response::with` later. + let modifiers = (status::Ok, response); + Ok(Response::with(modifiers)) +} + +fn bad_html_mime_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // BAD: an explicit `text/html` content type does not make the body safe. + let html: Mime = "text/html".parse().unwrap(); + Ok(Response::with((status::Ok, html, response))) +} + +fn main() { + Iron::new(bad_handler).http("localhost:3000").unwrap(); + Iron::new(Chain::new(bad_body_only_handler)).http("localhost:3001").unwrap(); + Iron::new(good_plain_text_handler).http("localhost:3002").unwrap(); + Iron::new(good_json_handler).http("localhost:3003").unwrap(); + Iron::new(good_constant_handler).http("localhost:3004").unwrap(); + Iron::new(prelude_style::handler).http("localhost:3005").unwrap(); + Iron::new(bad_named_tuple_handler).http("localhost:3006").unwrap(); + Iron::new(bad_html_mime_handler).http("localhost:3007").unwrap(); +} diff --git a/rust/test/security/CWE-079/IronResponseXss/options.yml b/rust/test/security/CWE-079/IronResponseXss/options.yml new file mode 100644 index 00000000..15d7e4ee --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/options.yml @@ -0,0 +1,2 @@ +qltest_dependencies: + - iron = "0.6.1" From dc3f07af1e748c586f8a60b5d489fe4a471426a1 Mon Sep 17 00:00:00 2001 From: Chad Bentz <1760475+felickz@users.noreply.github.com> Date: Sat, 21 Nov 2026 22:48:06 -0500 Subject: [PATCH 2/5] Rust Iron XSS: only the parsed content type itself suppresses the alert A string literal that was parsed and then merely formatted into the body suppressed the whole response, because the suppression used taint tracking from the literal to any tuple element. Require the tuple element to be the parsed value itself (the parse call, also through unwrap, expect, ? and local variables) and add a regression for a body built from a parsed literal. State in the query help that the parsed type is not checked. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 --- .../security/CWE-079/IronResponseXss.qhelp | 3 +- rust/src/security/CWE-079/IronResponseXss.ql | 38 +++++++-------- .../IronResponseXss/IronModels.expected | 20 ++++---- .../IronResponseXss/IronResponseXss.expected | 46 ++++++++++++++----- .../security/CWE-079/IronResponseXss/main.rs | 9 ++++ 5 files changed, 76 insertions(+), 40 deletions(-) diff --git a/rust/src/security/CWE-079/IronResponseXss.qhelp b/rust/src/security/CWE-079/IronResponseXss.qhelp index fb27ecf4..70b033fc 100644 --- a/rust/src/security/CWE-079/IronResponseXss.qhelp +++ b/rust/src/security/CWE-079/IronResponseXss.qhelp @@ -43,7 +43,8 @@ In the following corrected example, the response declares that its body is plain A content type that is set by another kind of modifier, such as Header(ContentType::json()), is not recognized, so the body of such a response is still reported. A content type is recognized only when it is parsed directly from a string literal and flows to the tuple through local -variables, unwrap, expect or ?. +variables, unwrap, expect or ?. The type that the literal is +parsed into is not checked.

    diff --git a/rust/src/security/CWE-079/IronResponseXss.ql b/rust/src/security/CWE-079/IronResponseXss.ql index 5b4c1261..49f127de 100644 --- a/rust/src/security/CWE-079/IronResponseXss.ql +++ b/rust/src/security/CWE-079/IronResponseXss.ql @@ -39,21 +39,27 @@ private predicate isMarkupContentType(StringLiteralExpr literal) { } /** - * Tracks a parsed string literal to the tuple elements it becomes, through `unwrap`, `expect`, `?` - * and local variables. + * Holds if `e` is the value that parsing `literal` produces: the `parse` call itself, that call + * unwrapped with `unwrap`, `expect` or `?`, or a local variable that holds such a value. A value + * that is merely derived from it, for example by formatting it into a string, is not. */ -module ParsedLiteralConfig implements DataFlow::ConfigSig { - predicate isSource(DataFlow::Node node) { node.asExpr() instanceof ParsedStringLiteral } - - predicate isSink(DataFlow::Node node) { - exists(TupleExpr modifiers | node.asExpr() = modifiers.getAField()) - } - - predicate isBarrier(DataFlow::Node barrier) { none() } +private predicate isParsedFrom(Expr e, ParsedStringLiteral literal) { + e.(MethodCallExpr).getIdentifier().getText() = "parse" and + e.(MethodCallExpr).getReceiver() = literal + or + e.(MethodCallExpr).getIdentifier().getText() = ["unwrap", "expect"] and + isParsedFrom(e.(MethodCallExpr).getReceiver(), literal) + or + isParsedFrom(e.(TryExpr).getExpr(), literal) + or + exists(DataFlow::ExprNode source, DataFlow::ExprNode target | + target.asExpr() = e and + source.asExpr() != e and + DataFlow::localFlow(source, target) and + isParsedFrom(source.asExpr(), literal) + ) } -module ParsedLiteralFlow = TaintTracking::Global; - /** * An element of a modifier tuple passed to `iron::Response::with`, such as `body` in * `Response::with((status::Ok, body))`. @@ -72,13 +78,9 @@ class IronResponseModifierTupleElement extends DataFlow::Node { DataFlow::localFlow(tupleNode, argumentNode) ) and this.asExpr() = modifiers.getAField() and - not exists( - ParsedStringLiteral literal, DataFlow::ExprNode literalNode, DataFlow::ExprNode modifierNode - | + not exists(ParsedStringLiteral literal | not isMarkupContentType(literal) and - literalNode.asExpr() = literal and - modifierNode.asExpr() = modifiers.getAField() and - ParsedLiteralFlow::flow(literalNode, modifierNode) + isParsedFrom(modifiers.getAField(), literal) ) ) } diff --git a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected index 68a0fe02..de103ab2 100644 --- a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected +++ b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected @@ -6,12 +6,14 @@ | main.rs:48:27:48:48 | TupleExpr | html-injection sink | | main.rs:57:23:57:31 | modifiers | html-injection sink | | main.rs:65:23:65:50 | TupleExpr | html-injection sink | -| main.rs:69:15:69:25 | bad_handler | remote source | -| main.rs:70:15:70:47 | ...::new(...) | remote source | -| main.rs:70:26:70:46 | bad_body_only_handler | remote source | -| main.rs:71:15:71:37 | good_plain_text_handler | remote source | -| main.rs:72:15:72:31 | good_json_handler | remote source | -| main.rs:73:15:73:35 | good_constant_handler | remote source | -| main.rs:74:15:74:36 | ...::handler | remote source | -| main.rs:75:15:75:37 | bad_named_tuple_handler | remote source | -| main.rs:76:15:76:35 | bad_html_mime_handler | remote source | +| main.rs:73:23:73:44 | TupleExpr | html-injection sink | +| main.rs:77:15:77:25 | bad_handler | remote source | +| main.rs:78:15:78:47 | ...::new(...) | remote source | +| main.rs:78:26:78:46 | bad_body_only_handler | remote source | +| main.rs:79:15:79:37 | good_plain_text_handler | remote source | +| main.rs:80:15:80:31 | good_json_handler | remote source | +| main.rs:81:15:81:35 | good_constant_handler | remote source | +| main.rs:82:15:82:36 | ...::handler | remote source | +| main.rs:83:15:83:37 | bad_named_tuple_handler | remote source | +| main.rs:84:15:84:35 | bad_html_mime_handler | remote source | +| main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | remote source | diff --git a/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected index a39d611a..1113fc2c 100644 --- a/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected +++ b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected @@ -35,10 +35,20 @@ edges | main.rs:62:28:62:68 | ...::must_use(...) | main.rs:62:9:62:16 | response | provenance | | | main.rs:62:28:62:68 | MacroExpr | main.rs:62:28:62:68 | ...::format(...) | provenance | MaD:371 | | main.rs:62:28:62:68 | { ... } | main.rs:62:28:62:68 | ...::must_use(...) | provenance | MaD:13380 | -| main.rs:69:15:69:25 | bad_handler | main.rs:5:16:5:32 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:74:15:74:36 | ...::handler | main.rs:44:20:44:36 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:75:15:75:37 | bad_named_tuple_handler | main.rs:52:28:52:44 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:76:15:76:35 | bad_html_mime_handler | main.rs:60:26:60:42 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:68:39:68:55 | ...: ... | main.rs:69:15:69:21 | req.url | provenance | | +| main.rs:69:9:69:11 | res | main.rs:72:28:72:58 | MacroExpr | provenance | | +| main.rs:69:15:69:21 | req.url | main.rs:69:15:69:28 | ... .path() | provenance | MaD:17587 | +| main.rs:69:15:69:28 | ... .path() | main.rs:69:9:69:11 | res | provenance | | +| main.rs:72:9:72:16 | response | main.rs:73:36:73:43 | response | provenance | | +| main.rs:72:28:72:58 | ...::format(...) | main.rs:72:28:72:58 | { ... } | provenance | | +| main.rs:72:28:72:58 | ...::must_use(...) | main.rs:72:9:72:16 | response | provenance | | +| main.rs:72:28:72:58 | MacroExpr | main.rs:72:28:72:58 | ...::format(...) | provenance | MaD:371 | +| main.rs:72:28:72:58 | { ... } | main.rs:72:28:72:58 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:77:15:77:25 | bad_handler | main.rs:5:16:5:32 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:82:15:82:36 | ...::handler | main.rs:44:20:44:36 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:83:15:83:37 | bad_named_tuple_handler | main.rs:52:28:52:44 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:84:15:84:35 | bad_html_mime_handler | main.rs:60:26:60:42 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | main.rs:68:39:68:55 | ...: ... | provenance | Src:MaD:17584 | nodes | main.rs:5:16:5:32 | ...: ... | semmle.label | ...: ... | | main.rs:6:9:6:11 | res | semmle.label | res | @@ -80,13 +90,25 @@ nodes | main.rs:62:28:62:68 | MacroExpr | semmle.label | MacroExpr | | main.rs:62:28:62:68 | { ... } | semmle.label | { ... } | | main.rs:65:42:65:49 | response | semmle.label | response | -| main.rs:69:15:69:25 | bad_handler | semmle.label | bad_handler | -| main.rs:74:15:74:36 | ...::handler | semmle.label | ...::handler | -| main.rs:75:15:75:37 | bad_named_tuple_handler | semmle.label | bad_named_tuple_handler | -| main.rs:76:15:76:35 | bad_html_mime_handler | semmle.label | bad_html_mime_handler | +| main.rs:68:39:68:55 | ...: ... | semmle.label | ...: ... | +| main.rs:69:9:69:11 | res | semmle.label | res | +| main.rs:69:15:69:21 | req.url | semmle.label | req.url | +| main.rs:69:15:69:28 | ... .path() | semmle.label | ... .path() | +| main.rs:72:9:72:16 | response | semmle.label | response | +| main.rs:72:28:72:58 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:72:28:72:58 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:72:28:72:58 | MacroExpr | semmle.label | MacroExpr | +| main.rs:72:28:72:58 | { ... } | semmle.label | { ... } | +| main.rs:73:36:73:43 | response | semmle.label | response | +| main.rs:77:15:77:25 | bad_handler | semmle.label | bad_handler | +| main.rs:82:15:82:36 | ...::handler | semmle.label | ...::handler | +| main.rs:83:15:83:37 | bad_named_tuple_handler | semmle.label | bad_named_tuple_handler | +| main.rs:84:15:84:35 | bad_html_mime_handler | semmle.label | bad_html_mime_handler | +| main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | semmle.label | bad_parsed_literal_in_body_handler | subpaths #select -| main.rs:9:36:9:43 | response | main.rs:69:15:69:25 | bad_handler | main.rs:9:36:9:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:69:15:69:25 | bad_handler | user-provided value | -| main.rs:48:40:48:47 | response | main.rs:74:15:74:36 | ...::handler | main.rs:48:40:48:47 | response | Cross-site scripting vulnerability due to a $@. | main.rs:74:15:74:36 | ...::handler | user-provided value | -| main.rs:56:34:56:41 | response | main.rs:75:15:75:37 | bad_named_tuple_handler | main.rs:56:34:56:41 | response | Cross-site scripting vulnerability due to a $@. | main.rs:75:15:75:37 | bad_named_tuple_handler | user-provided value | -| main.rs:65:42:65:49 | response | main.rs:76:15:76:35 | bad_html_mime_handler | main.rs:65:42:65:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:76:15:76:35 | bad_html_mime_handler | user-provided value | +| main.rs:9:36:9:43 | response | main.rs:77:15:77:25 | bad_handler | main.rs:9:36:9:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:77:15:77:25 | bad_handler | user-provided value | +| main.rs:48:40:48:47 | response | main.rs:82:15:82:36 | ...::handler | main.rs:48:40:48:47 | response | Cross-site scripting vulnerability due to a $@. | main.rs:82:15:82:36 | ...::handler | user-provided value | +| main.rs:56:34:56:41 | response | main.rs:83:15:83:37 | bad_named_tuple_handler | main.rs:56:34:56:41 | response | Cross-site scripting vulnerability due to a $@. | main.rs:83:15:83:37 | bad_named_tuple_handler | user-provided value | +| main.rs:65:42:65:49 | response | main.rs:84:15:84:35 | bad_html_mime_handler | main.rs:65:42:65:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:84:15:84:35 | bad_html_mime_handler | user-provided value | +| main.rs:73:36:73:43 | response | main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | main.rs:73:36:73:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | user-provided value | diff --git a/rust/test/security/CWE-079/IronResponseXss/main.rs b/rust/test/security/CWE-079/IronResponseXss/main.rs index 0101a061..fb740796 100644 --- a/rust/test/security/CWE-079/IronResponseXss/main.rs +++ b/rust/test/security/CWE-079/IronResponseXss/main.rs @@ -65,6 +65,14 @@ fn bad_html_mime_handler(req: &mut Request) -> IronResult { Ok(Response::with((status::Ok, html, response))) } +fn bad_parsed_literal_in_body_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + // BAD: a parsed literal that is only formatted into the body is not a content type modifier. + let label = "text/plain".parse::().unwrap(); + let response = format!("

    {} {:?}!

    ", label, res); + Ok(Response::with((status::Ok, response))) +} + fn main() { Iron::new(bad_handler).http("localhost:3000").unwrap(); Iron::new(Chain::new(bad_body_only_handler)).http("localhost:3001").unwrap(); @@ -74,4 +82,5 @@ fn main() { Iron::new(prelude_style::handler).http("localhost:3005").unwrap(); Iron::new(bad_named_tuple_handler).http("localhost:3006").unwrap(); Iron::new(bad_html_mime_handler).http("localhost:3007").unwrap(); + Iron::new(bad_parsed_literal_in_body_handler).http("localhost:3008").unwrap(); } From d09c1a7b99bae8a38e5eb7d3508fcce2962f97b2 Mon Sep 17 00:00:00 2001 From: Chad Bentz <1760475+felickz@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:32:42 -0500 Subject: [PATCH 3/5] Rust Iron XSS: reject mixed content types, test the standard query with the rows A content type that several parsed literals can reach, for example one on each branch of an if, was treated as safe when any of them was not markup. It is now safe only when a parsed literal reaches it and none of them names HTML or XML. Add a regression for a plain text and HTML mix. Add the standard rust/xss query as a test of the data extension rows, as the test packs of the other languages do: the Rust test pack depends on codeql/rust-queries 0.1.47 (the version that the pinned CLI bundles) and the lock file lists it and codeql/suite-helpers. The new test has one alert, the body that is passed on its own to Response::with. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 --- .../security/CWE-079/IronResponseXss.qhelp | 3 +- rust/src/security/CWE-079/IronResponseXss.ql | 22 ++++++-- rust/test/codeql-pack.lock.yml | 4 ++ rust/test/qlpack.yml | 1 + .../IronResponseXss/IronModels.expected | 22 ++++---- .../IronResponseXss/IronResponseXss.expected | 52 +++++++++++++------ .../CWE-079/IronResponseXss/XSS.expected | 24 +++++++++ .../CWE-079/IronResponseXss/XSS.qlref | 1 + .../security/CWE-079/IronResponseXss/main.rs | 14 +++++ 9 files changed, 114 insertions(+), 29 deletions(-) create mode 100644 rust/test/security/CWE-079/IronResponseXss/XSS.expected create mode 100644 rust/test/security/CWE-079/IronResponseXss/XSS.qlref diff --git a/rust/src/security/CWE-079/IronResponseXss.qhelp b/rust/src/security/CWE-079/IronResponseXss.qhelp index 70b033fc..58c6c1b0 100644 --- a/rust/src/security/CWE-079/IronResponseXss.qhelp +++ b/rust/src/security/CWE-079/IronResponseXss.qhelp @@ -44,7 +44,8 @@ A content type that is set by another kind of modifier, such as Header(Con is not recognized, so the body of such a response is still reported. A content type is recognized only when it is parsed directly from a string literal and flows to the tuple through local variables, unwrap, expect or ?. The type that the literal is -parsed into is not checked. +parsed into is not checked. If several parsed literals can reach the content type, for example one on +each branch of an if, it is recognized only when none of them names HTML or XML.

    diff --git a/rust/src/security/CWE-079/IronResponseXss.ql b/rust/src/security/CWE-079/IronResponseXss.ql index 49f127de..d8c23c48 100644 --- a/rust/src/security/CWE-079/IronResponseXss.ql +++ b/rust/src/security/CWE-079/IronResponseXss.ql @@ -60,6 +60,22 @@ private predicate isParsedFrom(Expr e, ParsedStringLiteral literal) { ) } +/** + * Holds if `modifier` is a content type that is parsed from string literals, and every parsed literal + * that reaches it names a content type that is not markup. A content type that can also be HTML or XML, + * for example on one branch of an `if`, is not safe. + */ +private predicate isSafeContentType(Expr modifier) { + exists(ParsedStringLiteral safe | + isParsedFrom(modifier, safe) and + not isMarkupContentType(safe) + ) and + not exists(ParsedStringLiteral markup | + isParsedFrom(modifier, markup) and + isMarkupContentType(markup) + ) +} + /** * An element of a modifier tuple passed to `iron::Response::with`, such as `body` in * `Response::with((status::Ok, body))`. @@ -78,9 +94,9 @@ class IronResponseModifierTupleElement extends DataFlow::Node { DataFlow::localFlow(tupleNode, argumentNode) ) and this.asExpr() = modifiers.getAField() and - not exists(ParsedStringLiteral literal | - not isMarkupContentType(literal) and - isParsedFrom(modifiers.getAField(), literal) + not exists(Expr modifier | + modifier = modifiers.getAField() and + isSafeContentType(modifier) ) ) } diff --git a/rust/test/codeql-pack.lock.yml b/rust/test/codeql-pack.lock.yml index 18fce78d..5d36ef0f 100644 --- a/rust/test/codeql-pack.lock.yml +++ b/rust/test/codeql-pack.lock.yml @@ -15,8 +15,12 @@ dependencies: version: 1.0.62 codeql/rust-all: version: 0.2.26 + codeql/rust-queries: + version: 0.1.47 codeql/ssa: version: 2.0.38 + codeql/suite-helpers: + version: 1.0.62 codeql/threat-models: version: 1.1.2 codeql/tutorial: diff --git a/rust/test/qlpack.yml b/rust/test/qlpack.yml index fd42b6bb..38640d21 100644 --- a/rust/test/qlpack.yml +++ b/rust/test/qlpack.yml @@ -2,6 +2,7 @@ name: githubsecuritylab/codeql-rust-tests groups: [rust, test] dependencies: codeql/rust-all: '0.2.26' + codeql/rust-queries: '0.1.47' githubsecuritylab/codeql-rust-queries: '*' githubsecuritylab/codeql-rust-extensions: '*' diff --git a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected index de103ab2..44775b03 100644 --- a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected +++ b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected @@ -7,13 +7,15 @@ | main.rs:57:23:57:31 | modifiers | html-injection sink | | main.rs:65:23:65:50 | TupleExpr | html-injection sink | | main.rs:73:23:73:44 | TupleExpr | html-injection sink | -| main.rs:77:15:77:25 | bad_handler | remote source | -| main.rs:78:15:78:47 | ...::new(...) | remote source | -| main.rs:78:26:78:46 | bad_body_only_handler | remote source | -| main.rs:79:15:79:37 | good_plain_text_handler | remote source | -| main.rs:80:15:80:31 | good_json_handler | remote source | -| main.rs:81:15:81:35 | good_constant_handler | remote source | -| main.rs:82:15:82:36 | ...::handler | remote source | -| main.rs:83:15:83:37 | bad_named_tuple_handler | remote source | -| main.rs:84:15:84:35 | bad_html_mime_handler | remote source | -| main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | remote source | +| main.rs:86:23:86:50 | TupleExpr | html-injection sink | +| main.rs:90:15:90:25 | bad_handler | remote source | +| main.rs:91:15:91:47 | ...::new(...) | remote source | +| main.rs:91:26:91:46 | bad_body_only_handler | remote source | +| main.rs:92:15:92:37 | good_plain_text_handler | remote source | +| main.rs:93:15:93:31 | good_json_handler | remote source | +| main.rs:94:15:94:35 | good_constant_handler | remote source | +| main.rs:95:15:95:36 | ...::handler | remote source | +| main.rs:96:15:96:37 | bad_named_tuple_handler | remote source | +| main.rs:97:15:97:35 | bad_html_mime_handler | remote source | +| main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | remote source | +| main.rs:99:15:99:36 | bad_mixed_mime_handler | remote source | diff --git a/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected index 1113fc2c..ff53f038 100644 --- a/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected +++ b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected @@ -44,11 +44,21 @@ edges | main.rs:72:28:72:58 | ...::must_use(...) | main.rs:72:9:72:16 | response | provenance | | | main.rs:72:28:72:58 | MacroExpr | main.rs:72:28:72:58 | ...::format(...) | provenance | MaD:371 | | main.rs:72:28:72:58 | { ... } | main.rs:72:28:72:58 | ...::must_use(...) | provenance | MaD:13380 | -| main.rs:77:15:77:25 | bad_handler | main.rs:5:16:5:32 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:82:15:82:36 | ...::handler | main.rs:44:20:44:36 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:83:15:83:37 | bad_named_tuple_handler | main.rs:52:28:52:44 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:84:15:84:35 | bad_html_mime_handler | main.rs:60:26:60:42 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | main.rs:68:39:68:55 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:76:27:76:43 | ...: ... | main.rs:77:15:77:21 | req.url | provenance | | +| main.rs:77:9:77:11 | res | main.rs:78:28:78:68 | MacroExpr | provenance | | +| main.rs:77:15:77:21 | req.url | main.rs:77:15:77:28 | ... .path() | provenance | MaD:17587 | +| main.rs:77:15:77:28 | ... .path() | main.rs:77:9:77:11 | res | provenance | | +| main.rs:78:9:78:16 | response | main.rs:86:42:86:49 | response | provenance | | +| main.rs:78:28:78:68 | ...::format(...) | main.rs:78:28:78:68 | { ... } | provenance | | +| main.rs:78:28:78:68 | ...::must_use(...) | main.rs:78:9:78:16 | response | provenance | | +| main.rs:78:28:78:68 | MacroExpr | main.rs:78:28:78:68 | ...::format(...) | provenance | MaD:371 | +| main.rs:78:28:78:68 | { ... } | main.rs:78:28:78:68 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:90:15:90:25 | bad_handler | main.rs:5:16:5:32 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:95:15:95:36 | ...::handler | main.rs:44:20:44:36 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:96:15:96:37 | bad_named_tuple_handler | main.rs:52:28:52:44 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:97:15:97:35 | bad_html_mime_handler | main.rs:60:26:60:42 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | main.rs:68:39:68:55 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:99:15:99:36 | bad_mixed_mime_handler | main.rs:76:27:76:43 | ...: ... | provenance | Src:MaD:17584 | nodes | main.rs:5:16:5:32 | ...: ... | semmle.label | ...: ... | | main.rs:6:9:6:11 | res | semmle.label | res | @@ -100,15 +110,27 @@ nodes | main.rs:72:28:72:58 | MacroExpr | semmle.label | MacroExpr | | main.rs:72:28:72:58 | { ... } | semmle.label | { ... } | | main.rs:73:36:73:43 | response | semmle.label | response | -| main.rs:77:15:77:25 | bad_handler | semmle.label | bad_handler | -| main.rs:82:15:82:36 | ...::handler | semmle.label | ...::handler | -| main.rs:83:15:83:37 | bad_named_tuple_handler | semmle.label | bad_named_tuple_handler | -| main.rs:84:15:84:35 | bad_html_mime_handler | semmle.label | bad_html_mime_handler | -| main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | semmle.label | bad_parsed_literal_in_body_handler | +| main.rs:76:27:76:43 | ...: ... | semmle.label | ...: ... | +| main.rs:77:9:77:11 | res | semmle.label | res | +| main.rs:77:15:77:21 | req.url | semmle.label | req.url | +| main.rs:77:15:77:28 | ... .path() | semmle.label | ... .path() | +| main.rs:78:9:78:16 | response | semmle.label | response | +| main.rs:78:28:78:68 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:78:28:78:68 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:78:28:78:68 | MacroExpr | semmle.label | MacroExpr | +| main.rs:78:28:78:68 | { ... } | semmle.label | { ... } | +| main.rs:86:42:86:49 | response | semmle.label | response | +| main.rs:90:15:90:25 | bad_handler | semmle.label | bad_handler | +| main.rs:95:15:95:36 | ...::handler | semmle.label | ...::handler | +| main.rs:96:15:96:37 | bad_named_tuple_handler | semmle.label | bad_named_tuple_handler | +| main.rs:97:15:97:35 | bad_html_mime_handler | semmle.label | bad_html_mime_handler | +| main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | semmle.label | bad_parsed_literal_in_body_handler | +| main.rs:99:15:99:36 | bad_mixed_mime_handler | semmle.label | bad_mixed_mime_handler | subpaths #select -| main.rs:9:36:9:43 | response | main.rs:77:15:77:25 | bad_handler | main.rs:9:36:9:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:77:15:77:25 | bad_handler | user-provided value | -| main.rs:48:40:48:47 | response | main.rs:82:15:82:36 | ...::handler | main.rs:48:40:48:47 | response | Cross-site scripting vulnerability due to a $@. | main.rs:82:15:82:36 | ...::handler | user-provided value | -| main.rs:56:34:56:41 | response | main.rs:83:15:83:37 | bad_named_tuple_handler | main.rs:56:34:56:41 | response | Cross-site scripting vulnerability due to a $@. | main.rs:83:15:83:37 | bad_named_tuple_handler | user-provided value | -| main.rs:65:42:65:49 | response | main.rs:84:15:84:35 | bad_html_mime_handler | main.rs:65:42:65:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:84:15:84:35 | bad_html_mime_handler | user-provided value | -| main.rs:73:36:73:43 | response | main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | main.rs:73:36:73:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:85:15:85:48 | bad_parsed_literal_in_body_handler | user-provided value | +| main.rs:9:36:9:43 | response | main.rs:90:15:90:25 | bad_handler | main.rs:9:36:9:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:90:15:90:25 | bad_handler | user-provided value | +| main.rs:48:40:48:47 | response | main.rs:95:15:95:36 | ...::handler | main.rs:48:40:48:47 | response | Cross-site scripting vulnerability due to a $@. | main.rs:95:15:95:36 | ...::handler | user-provided value | +| main.rs:56:34:56:41 | response | main.rs:96:15:96:37 | bad_named_tuple_handler | main.rs:56:34:56:41 | response | Cross-site scripting vulnerability due to a $@. | main.rs:96:15:96:37 | bad_named_tuple_handler | user-provided value | +| main.rs:65:42:65:49 | response | main.rs:97:15:97:35 | bad_html_mime_handler | main.rs:65:42:65:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:97:15:97:35 | bad_html_mime_handler | user-provided value | +| main.rs:73:36:73:43 | response | main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | main.rs:73:36:73:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | user-provided value | +| main.rs:86:42:86:49 | response | main.rs:99:15:99:36 | bad_mixed_mime_handler | main.rs:86:42:86:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:99:15:99:36 | bad_mixed_mime_handler | user-provided value | diff --git a/rust/test/security/CWE-079/IronResponseXss/XSS.expected b/rust/test/security/CWE-079/IronResponseXss/XSS.expected new file mode 100644 index 00000000..91314305 --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/XSS.expected @@ -0,0 +1,24 @@ +edges +| main.rs:12:26:12:42 | ...: ... | main.rs:13:15:13:21 | req.url | provenance | | +| main.rs:13:9:13:11 | res | main.rs:16:31:16:48 | MacroExpr | provenance | | +| main.rs:13:15:13:21 | req.url | main.rs:13:15:13:28 | ... .path() | provenance | MaD:17587 | +| main.rs:13:15:13:28 | ... .path() | main.rs:13:9:13:11 | res | provenance | | +| main.rs:16:31:16:48 | ...::format(...) | main.rs:16:31:16:48 | { ... } | provenance | | +| main.rs:16:31:16:48 | ...::must_use(...) | main.rs:16:23:16:49 | MacroExpr | provenance | Sink:MaD:17586 | +| main.rs:16:31:16:48 | MacroExpr | main.rs:16:31:16:48 | ...::format(...) | provenance | MaD:371 | +| main.rs:16:31:16:48 | { ... } | main.rs:16:31:16:48 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:91:26:91:46 | bad_body_only_handler | main.rs:12:26:12:42 | ...: ... | provenance | Src:MaD:17585 | +nodes +| main.rs:12:26:12:42 | ...: ... | semmle.label | ...: ... | +| main.rs:13:9:13:11 | res | semmle.label | res | +| main.rs:13:15:13:21 | req.url | semmle.label | req.url | +| main.rs:13:15:13:28 | ... .path() | semmle.label | ... .path() | +| main.rs:16:23:16:49 | MacroExpr | semmle.label | MacroExpr | +| main.rs:16:31:16:48 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:16:31:16:48 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:16:31:16:48 | MacroExpr | semmle.label | MacroExpr | +| main.rs:16:31:16:48 | { ... } | semmle.label | { ... } | +| main.rs:91:26:91:46 | bad_body_only_handler | semmle.label | bad_body_only_handler | +subpaths +#select +| main.rs:16:23:16:49 | MacroExpr | main.rs:91:26:91:46 | bad_body_only_handler | main.rs:16:23:16:49 | MacroExpr | Cross-site scripting vulnerability due to a $@. | main.rs:91:26:91:46 | bad_body_only_handler | user-provided value | diff --git a/rust/test/security/CWE-079/IronResponseXss/XSS.qlref b/rust/test/security/CWE-079/IronResponseXss/XSS.qlref new file mode 100644 index 00000000..12aff7f2 --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/XSS.qlref @@ -0,0 +1 @@ +query: queries/security/CWE-079/XSS.ql diff --git a/rust/test/security/CWE-079/IronResponseXss/main.rs b/rust/test/security/CWE-079/IronResponseXss/main.rs index fb740796..037e07d4 100644 --- a/rust/test/security/CWE-079/IronResponseXss/main.rs +++ b/rust/test/security/CWE-079/IronResponseXss/main.rs @@ -73,6 +73,19 @@ fn bad_parsed_literal_in_body_handler(req: &mut Request) -> IronResult Ok(Response::with((status::Ok, response))) } +fn bad_mixed_mime_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // BAD: the content type is plain text on one branch and HTML on the other, so the body can still be + // rendered as HTML. + let mime: Mime = if res.len() > 3 { + "text/plain".parse().unwrap() + } else { + "text/html".parse().unwrap() + }; + Ok(Response::with((status::Ok, mime, response))) +} + fn main() { Iron::new(bad_handler).http("localhost:3000").unwrap(); Iron::new(Chain::new(bad_body_only_handler)).http("localhost:3001").unwrap(); @@ -83,4 +96,5 @@ fn main() { Iron::new(bad_named_tuple_handler).http("localhost:3006").unwrap(); Iron::new(bad_html_mime_handler).http("localhost:3007").unwrap(); Iron::new(bad_parsed_literal_in_body_handler).http("localhost:3008").unwrap(); + Iron::new(bad_mixed_mime_handler).http("localhost:3009").unwrap(); } From 9d1e0234cf220bc4ac400e0894dbceceabca6a5e Mon Sep 17 00:00:00 2001 From: Chad Bentz <1760475+felickz@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:56:30 -0500 Subject: [PATCH 4/5] Rust Iron XSS: look through nested modifier tuples, judge every content type Only the immediate elements of the modifier tuple were sinks, so a body in a nested tuple was missed. A content type that is parsed from a safe literal removed the sinks of the whole tuple, even when another modifier of the same response is parsed from an HTML literal and is applied after it. Use the leaf elements of the tuple and of the tuples that are nested in it as the sinks, and judge the content type of the whole response: it is safe only when a modifier is parsed from a literal that is not markup and no modifier is parsed from one that is. Add regressions for a body in a nested tuple (written in place, or built first), a text/html modifier after a text/plain one, and a text/plain modifier in a nested tuple. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 --- .../security/CWE-079/IronResponseXss.qhelp | 14 ++- rust/src/security/CWE-079/IronResponseXss.ql | 61 +++++++---- .../IronResponseXss/IronModels.expected | 30 ++++-- .../IronResponseXss/IronResponseXss.expected | 102 ++++++++++++++---- .../CWE-079/IronResponseXss/XSS.expected | 6 +- .../security/CWE-079/IronResponseXss/main.rs | 36 +++++++ 6 files changed, 192 insertions(+), 57 deletions(-) diff --git a/rust/src/security/CWE-079/IronResponseXss.qhelp b/rust/src/security/CWE-079/IronResponseXss.qhelp index 58c6c1b0..b277fd50 100644 --- a/rust/src/security/CWE-079/IronResponseXss.qhelp +++ b/rust/src/security/CWE-079/IronResponseXss.qhelp @@ -13,9 +13,10 @@ browser render it as HTML and run a script in the context of the application.

    This query finds user-controlled data that is part of a modifier tuple passed to -Response::with. A tuple that also contains a content type parsed from a string literal -that does not name HTML or XML, such as "text/plain".parse::<Mime>().unwrap(), is -not reported. +Response::with, including the elements of tuples that are nested in it. A response +whose modifiers include a content type parsed from a string literal that does not name HTML or +XML, such as "text/plain".parse::<Mime>().unwrap(), and none that does, is not +reported.

    @@ -44,8 +45,11 @@ A content type that is set by another kind of modifier, such as Header(Con is not recognized, so the body of such a response is still reported. A content type is recognized only when it is parsed directly from a string literal and flows to the tuple through local variables, unwrap, expect or ?. The type that the literal is -parsed into is not checked. If several parsed literals can reach the content type, for example one on -each branch of an if, it is recognized only when none of them names HTML or XML. +parsed into is not checked. If several parsed literals can reach the content types of a response, for +example one on each branch of an if, or one in each of two modifiers, the response is +recognized only when none of them names HTML or XML. Iron applies the modifiers in order, so a +text/html modifier after a text/plain one replaces it, and the query does +not try to tell which of two content types wins.

    diff --git a/rust/src/security/CWE-079/IronResponseXss.ql b/rust/src/security/CWE-079/IronResponseXss.ql index d8c23c48..78a8ca6d 100644 --- a/rust/src/security/CWE-079/IronResponseXss.ql +++ b/rust/src/security/CWE-079/IronResponseXss.ql @@ -61,43 +61,64 @@ private predicate isParsedFrom(Expr e, ParsedStringLiteral literal) { } /** - * Holds if `modifier` is a content type that is parsed from string literals, and every parsed literal - * that reaches it names a content type that is not markup. A content type that can also be HTML or XML, - * for example on one branch of an `if`, is not safe. + * Holds if `tuple` is a tuple expression whose value the expression `e` holds: `e` is the tuple, or a + * local variable or expression that the tuple flows to. */ -private predicate isSafeContentType(Expr modifier) { +private predicate holdsTuple(Expr e, TupleExpr tuple) { + exists(DataFlow::ExprNode tupleNode, DataFlow::ExprNode eNode | + tupleNode.asExpr() = tuple and + eNode.asExpr() = e and + DataFlow::localFlow(tupleNode, eNode) + ) +} + +/** + * Gets a modifier of the response that is built from `e`, the argument of `Response::with`: an element + * of the tuple that `e` holds, or of a tuple that is nested in it. Iron applies the modifiers of a + * nested tuple to the same response. + */ +private Expr getAModifier(Expr e) { + exists(TupleExpr tuple, Expr element | holdsTuple(e, tuple) and element = tuple.getAField() | + result = element and not holdsTuple(element, _) + or + result = getAModifier(element) + ) +} + +/** + * Holds if the response that is built from `argument` has an explicit content type that is not markup: + * a modifier is parsed from a string literal that does not name HTML or XML, and no modifier of the + * response is parsed from a literal that does. Iron applies the modifiers in order, so a `text/html` + * modifier after a `text/plain` one replaces it, and a response whose content type can be markup is + * not safe. This also covers a content type that is parsed from different literals on different + * branches of an `if`. + */ +private predicate hasSafeContentType(Expr argument) { exists(ParsedStringLiteral safe | - isParsedFrom(modifier, safe) and + isParsedFrom(getAModifier(argument), safe) and not isMarkupContentType(safe) ) and not exists(ParsedStringLiteral markup | - isParsedFrom(modifier, markup) and + isParsedFrom(getAModifier(argument), markup) and isMarkupContentType(markup) ) } /** * An element of a modifier tuple passed to `iron::Response::with`, such as `body` in - * `Response::with((status::Ok, body))`. + * `Response::with((status::Ok, body))`, or of a tuple that is nested in it. * - * Tuples that contain a content type modifier parsed from a string literal that does not name - * HTML or XML set an explicit, safe `Content-Type` and are not considered sinks. + * A response whose modifiers include a content type that is parsed from a string literal that does not + * name HTML or XML, and none that does, has an explicit, safe `Content-Type` and is not considered a + * sink. */ class IronResponseModifierTupleElement extends DataFlow::Node { IronResponseModifierTupleElement() { - exists(Call call, TupleExpr modifiers, Expr argument | + exists(Call call, Expr argument | call.getStaticTarget().getCanonicalPath() = "::with" and argument = call.getPositionalArgument(0) and - exists(DataFlow::ExprNode tupleNode, DataFlow::ExprNode argumentNode | - tupleNode.asExpr() = modifiers and - argumentNode.asExpr() = argument and - DataFlow::localFlow(tupleNode, argumentNode) - ) and - this.asExpr() = modifiers.getAField() and - not exists(Expr modifier | - modifier = modifiers.getAField() and - isSafeContentType(modifier) - ) + this.asExpr() = getAModifier(argument) and + not hasSafeContentType(argument) ) } } diff --git a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected index 44775b03..2e5e279b 100644 --- a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected +++ b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected @@ -8,14 +8,22 @@ | main.rs:65:23:65:50 | TupleExpr | html-injection sink | | main.rs:73:23:73:44 | TupleExpr | html-injection sink | | main.rs:86:23:86:50 | TupleExpr | html-injection sink | -| main.rs:90:15:90:25 | bad_handler | remote source | -| main.rs:91:15:91:47 | ...::new(...) | remote source | -| main.rs:91:26:91:46 | bad_body_only_handler | remote source | -| main.rs:92:15:92:37 | good_plain_text_handler | remote source | -| main.rs:93:15:93:31 | good_json_handler | remote source | -| main.rs:94:15:94:35 | good_constant_handler | remote source | -| main.rs:95:15:95:36 | ...::handler | remote source | -| main.rs:96:15:96:37 | bad_named_tuple_handler | remote source | -| main.rs:97:15:97:35 | bad_html_mime_handler | remote source | -| main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | remote source | -| main.rs:99:15:99:36 | bad_mixed_mime_handler | remote source | +| main.rs:93:23:93:58 | TupleExpr | html-injection sink | +| main.rs:101:23:101:41 | TupleExpr | html-injection sink | +| main.rs:110:23:110:57 | TupleExpr | html-injection sink | +| main.rs:118:23:118:53 | TupleExpr | html-injection sink | +| main.rs:122:15:122:25 | bad_handler | remote source | +| main.rs:123:15:123:47 | ...::new(...) | remote source | +| main.rs:123:26:123:46 | bad_body_only_handler | remote source | +| main.rs:124:15:124:37 | good_plain_text_handler | remote source | +| main.rs:125:15:125:31 | good_json_handler | remote source | +| main.rs:126:15:126:35 | good_constant_handler | remote source | +| main.rs:127:15:127:36 | ...::handler | remote source | +| main.rs:128:15:128:37 | bad_named_tuple_handler | remote source | +| main.rs:129:15:129:35 | bad_html_mime_handler | remote source | +| main.rs:130:15:130:48 | bad_parsed_literal_in_body_handler | remote source | +| main.rs:131:15:131:36 | bad_mixed_mime_handler | remote source | +| main.rs:132:15:132:38 | bad_nested_tuple_handler | remote source | +| main.rs:133:15:133:41 | bad_overriding_mime_handler | remote source | +| main.rs:134:15:134:44 | good_nested_plain_text_handler | remote source | +| main.rs:135:15:135:44 | bad_nested_named_tuple_handler | remote source | diff --git a/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected index ff53f038..9c610c33 100644 --- a/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected +++ b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected @@ -53,12 +53,42 @@ edges | main.rs:78:28:78:68 | ...::must_use(...) | main.rs:78:9:78:16 | response | provenance | | | main.rs:78:28:78:68 | MacroExpr | main.rs:78:28:78:68 | ...::format(...) | provenance | MaD:371 | | main.rs:78:28:78:68 | { ... } | main.rs:78:28:78:68 | ...::must_use(...) | provenance | MaD:13380 | -| main.rs:90:15:90:25 | bad_handler | main.rs:5:16:5:32 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:95:15:95:36 | ...::handler | main.rs:44:20:44:36 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:96:15:96:37 | bad_named_tuple_handler | main.rs:52:28:52:44 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:97:15:97:35 | bad_html_mime_handler | main.rs:60:26:60:42 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | main.rs:68:39:68:55 | ...: ... | provenance | Src:MaD:17584 | -| main.rs:99:15:99:36 | bad_mixed_mime_handler | main.rs:76:27:76:43 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:89:29:89:45 | ...: ... | main.rs:90:15:90:21 | req.url | provenance | | +| main.rs:90:9:90:11 | res | main.rs:91:28:91:68 | MacroExpr | provenance | | +| main.rs:90:15:90:21 | req.url | main.rs:90:15:90:28 | ... .path() | provenance | MaD:17587 | +| main.rs:90:15:90:28 | ... .path() | main.rs:90:9:90:11 | res | provenance | | +| main.rs:91:9:91:16 | response | main.rs:93:49:93:56 | response | provenance | | +| main.rs:91:28:91:68 | ...::format(...) | main.rs:91:28:91:68 | { ... } | provenance | | +| main.rs:91:28:91:68 | ...::must_use(...) | main.rs:91:9:91:16 | response | provenance | | +| main.rs:91:28:91:68 | MacroExpr | main.rs:91:28:91:68 | ...::format(...) | provenance | MaD:371 | +| main.rs:91:28:91:68 | { ... } | main.rs:91:28:91:68 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:96:35:96:51 | ...: ... | main.rs:97:15:97:21 | req.url | provenance | | +| main.rs:97:9:97:11 | res | main.rs:98:28:98:68 | MacroExpr | provenance | | +| main.rs:97:15:97:21 | req.url | main.rs:97:15:97:28 | ... .path() | provenance | MaD:17587 | +| main.rs:97:15:97:28 | ... .path() | main.rs:97:9:97:11 | res | provenance | | +| main.rs:98:9:98:16 | response | main.rs:100:30:100:37 | response | provenance | | +| main.rs:98:28:98:68 | ...::format(...) | main.rs:98:28:98:68 | { ... } | provenance | | +| main.rs:98:28:98:68 | ...::must_use(...) | main.rs:98:9:98:16 | response | provenance | | +| main.rs:98:28:98:68 | MacroExpr | main.rs:98:28:98:68 | ...::format(...) | provenance | MaD:371 | +| main.rs:98:28:98:68 | { ... } | main.rs:98:28:98:68 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:104:32:104:48 | ...: ... | main.rs:105:15:105:21 | req.url | provenance | | +| main.rs:105:9:105:11 | res | main.rs:106:28:106:68 | MacroExpr | provenance | | +| main.rs:105:15:105:21 | req.url | main.rs:105:15:105:28 | ... .path() | provenance | MaD:17587 | +| main.rs:105:15:105:28 | ... .path() | main.rs:105:9:105:11 | res | provenance | | +| main.rs:106:9:106:16 | response | main.rs:110:49:110:56 | response | provenance | | +| main.rs:106:28:106:68 | ...::format(...) | main.rs:106:28:106:68 | { ... } | provenance | | +| main.rs:106:28:106:68 | ...::must_use(...) | main.rs:106:9:106:16 | response | provenance | | +| main.rs:106:28:106:68 | MacroExpr | main.rs:106:28:106:68 | ...::format(...) | provenance | MaD:371 | +| main.rs:106:28:106:68 | { ... } | main.rs:106:28:106:68 | ...::must_use(...) | provenance | MaD:13380 | +| main.rs:122:15:122:25 | bad_handler | main.rs:5:16:5:32 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:127:15:127:36 | ...::handler | main.rs:44:20:44:36 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:128:15:128:37 | bad_named_tuple_handler | main.rs:52:28:52:44 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:129:15:129:35 | bad_html_mime_handler | main.rs:60:26:60:42 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:130:15:130:48 | bad_parsed_literal_in_body_handler | main.rs:68:39:68:55 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:131:15:131:36 | bad_mixed_mime_handler | main.rs:76:27:76:43 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:132:15:132:38 | bad_nested_tuple_handler | main.rs:89:29:89:45 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:133:15:133:41 | bad_overriding_mime_handler | main.rs:104:32:104:48 | ...: ... | provenance | Src:MaD:17584 | +| main.rs:135:15:135:44 | bad_nested_named_tuple_handler | main.rs:96:35:96:51 | ...: ... | provenance | Src:MaD:17584 | nodes | main.rs:5:16:5:32 | ...: ... | semmle.label | ...: ... | | main.rs:6:9:6:11 | res | semmle.label | res | @@ -120,17 +150,53 @@ nodes | main.rs:78:28:78:68 | MacroExpr | semmle.label | MacroExpr | | main.rs:78:28:78:68 | { ... } | semmle.label | { ... } | | main.rs:86:42:86:49 | response | semmle.label | response | -| main.rs:90:15:90:25 | bad_handler | semmle.label | bad_handler | -| main.rs:95:15:95:36 | ...::handler | semmle.label | ...::handler | -| main.rs:96:15:96:37 | bad_named_tuple_handler | semmle.label | bad_named_tuple_handler | -| main.rs:97:15:97:35 | bad_html_mime_handler | semmle.label | bad_html_mime_handler | -| main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | semmle.label | bad_parsed_literal_in_body_handler | -| main.rs:99:15:99:36 | bad_mixed_mime_handler | semmle.label | bad_mixed_mime_handler | +| main.rs:89:29:89:45 | ...: ... | semmle.label | ...: ... | +| main.rs:90:9:90:11 | res | semmle.label | res | +| main.rs:90:15:90:21 | req.url | semmle.label | req.url | +| main.rs:90:15:90:28 | ... .path() | semmle.label | ... .path() | +| main.rs:91:9:91:16 | response | semmle.label | response | +| main.rs:91:28:91:68 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:91:28:91:68 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:91:28:91:68 | MacroExpr | semmle.label | MacroExpr | +| main.rs:91:28:91:68 | { ... } | semmle.label | { ... } | +| main.rs:93:49:93:56 | response | semmle.label | response | +| main.rs:96:35:96:51 | ...: ... | semmle.label | ...: ... | +| main.rs:97:9:97:11 | res | semmle.label | res | +| main.rs:97:15:97:21 | req.url | semmle.label | req.url | +| main.rs:97:15:97:28 | ... .path() | semmle.label | ... .path() | +| main.rs:98:9:98:16 | response | semmle.label | response | +| main.rs:98:28:98:68 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:98:28:98:68 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:98:28:98:68 | MacroExpr | semmle.label | MacroExpr | +| main.rs:98:28:98:68 | { ... } | semmle.label | { ... } | +| main.rs:100:30:100:37 | response | semmle.label | response | +| main.rs:104:32:104:48 | ...: ... | semmle.label | ...: ... | +| main.rs:105:9:105:11 | res | semmle.label | res | +| main.rs:105:15:105:21 | req.url | semmle.label | req.url | +| main.rs:105:15:105:28 | ... .path() | semmle.label | ... .path() | +| main.rs:106:9:106:16 | response | semmle.label | response | +| main.rs:106:28:106:68 | ...::format(...) | semmle.label | ...::format(...) | +| main.rs:106:28:106:68 | ...::must_use(...) | semmle.label | ...::must_use(...) | +| main.rs:106:28:106:68 | MacroExpr | semmle.label | MacroExpr | +| main.rs:106:28:106:68 | { ... } | semmle.label | { ... } | +| main.rs:110:49:110:56 | response | semmle.label | response | +| main.rs:122:15:122:25 | bad_handler | semmle.label | bad_handler | +| main.rs:127:15:127:36 | ...::handler | semmle.label | ...::handler | +| main.rs:128:15:128:37 | bad_named_tuple_handler | semmle.label | bad_named_tuple_handler | +| main.rs:129:15:129:35 | bad_html_mime_handler | semmle.label | bad_html_mime_handler | +| main.rs:130:15:130:48 | bad_parsed_literal_in_body_handler | semmle.label | bad_parsed_literal_in_body_handler | +| main.rs:131:15:131:36 | bad_mixed_mime_handler | semmle.label | bad_mixed_mime_handler | +| main.rs:132:15:132:38 | bad_nested_tuple_handler | semmle.label | bad_nested_tuple_handler | +| main.rs:133:15:133:41 | bad_overriding_mime_handler | semmle.label | bad_overriding_mime_handler | +| main.rs:135:15:135:44 | bad_nested_named_tuple_handler | semmle.label | bad_nested_named_tuple_handler | subpaths #select -| main.rs:9:36:9:43 | response | main.rs:90:15:90:25 | bad_handler | main.rs:9:36:9:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:90:15:90:25 | bad_handler | user-provided value | -| main.rs:48:40:48:47 | response | main.rs:95:15:95:36 | ...::handler | main.rs:48:40:48:47 | response | Cross-site scripting vulnerability due to a $@. | main.rs:95:15:95:36 | ...::handler | user-provided value | -| main.rs:56:34:56:41 | response | main.rs:96:15:96:37 | bad_named_tuple_handler | main.rs:56:34:56:41 | response | Cross-site scripting vulnerability due to a $@. | main.rs:96:15:96:37 | bad_named_tuple_handler | user-provided value | -| main.rs:65:42:65:49 | response | main.rs:97:15:97:35 | bad_html_mime_handler | main.rs:65:42:65:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:97:15:97:35 | bad_html_mime_handler | user-provided value | -| main.rs:73:36:73:43 | response | main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | main.rs:73:36:73:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:98:15:98:48 | bad_parsed_literal_in_body_handler | user-provided value | -| main.rs:86:42:86:49 | response | main.rs:99:15:99:36 | bad_mixed_mime_handler | main.rs:86:42:86:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:99:15:99:36 | bad_mixed_mime_handler | user-provided value | +| main.rs:9:36:9:43 | response | main.rs:122:15:122:25 | bad_handler | main.rs:9:36:9:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:122:15:122:25 | bad_handler | user-provided value | +| main.rs:48:40:48:47 | response | main.rs:127:15:127:36 | ...::handler | main.rs:48:40:48:47 | response | Cross-site scripting vulnerability due to a $@. | main.rs:127:15:127:36 | ...::handler | user-provided value | +| main.rs:56:34:56:41 | response | main.rs:128:15:128:37 | bad_named_tuple_handler | main.rs:56:34:56:41 | response | Cross-site scripting vulnerability due to a $@. | main.rs:128:15:128:37 | bad_named_tuple_handler | user-provided value | +| main.rs:65:42:65:49 | response | main.rs:129:15:129:35 | bad_html_mime_handler | main.rs:65:42:65:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:129:15:129:35 | bad_html_mime_handler | user-provided value | +| main.rs:73:36:73:43 | response | main.rs:130:15:130:48 | bad_parsed_literal_in_body_handler | main.rs:73:36:73:43 | response | Cross-site scripting vulnerability due to a $@. | main.rs:130:15:130:48 | bad_parsed_literal_in_body_handler | user-provided value | +| main.rs:86:42:86:49 | response | main.rs:131:15:131:36 | bad_mixed_mime_handler | main.rs:86:42:86:49 | response | Cross-site scripting vulnerability due to a $@. | main.rs:131:15:131:36 | bad_mixed_mime_handler | user-provided value | +| main.rs:93:49:93:56 | response | main.rs:132:15:132:38 | bad_nested_tuple_handler | main.rs:93:49:93:56 | response | Cross-site scripting vulnerability due to a $@. | main.rs:132:15:132:38 | bad_nested_tuple_handler | user-provided value | +| main.rs:100:30:100:37 | response | main.rs:135:15:135:44 | bad_nested_named_tuple_handler | main.rs:100:30:100:37 | response | Cross-site scripting vulnerability due to a $@. | main.rs:135:15:135:44 | bad_nested_named_tuple_handler | user-provided value | +| main.rs:110:49:110:56 | response | main.rs:133:15:133:41 | bad_overriding_mime_handler | main.rs:110:49:110:56 | response | Cross-site scripting vulnerability due to a $@. | main.rs:133:15:133:41 | bad_overriding_mime_handler | user-provided value | diff --git a/rust/test/security/CWE-079/IronResponseXss/XSS.expected b/rust/test/security/CWE-079/IronResponseXss/XSS.expected index 91314305..9f35a8d3 100644 --- a/rust/test/security/CWE-079/IronResponseXss/XSS.expected +++ b/rust/test/security/CWE-079/IronResponseXss/XSS.expected @@ -7,7 +7,7 @@ edges | main.rs:16:31:16:48 | ...::must_use(...) | main.rs:16:23:16:49 | MacroExpr | provenance | Sink:MaD:17586 | | main.rs:16:31:16:48 | MacroExpr | main.rs:16:31:16:48 | ...::format(...) | provenance | MaD:371 | | main.rs:16:31:16:48 | { ... } | main.rs:16:31:16:48 | ...::must_use(...) | provenance | MaD:13380 | -| main.rs:91:26:91:46 | bad_body_only_handler | main.rs:12:26:12:42 | ...: ... | provenance | Src:MaD:17585 | +| main.rs:123:26:123:46 | bad_body_only_handler | main.rs:12:26:12:42 | ...: ... | provenance | Src:MaD:17585 | nodes | main.rs:12:26:12:42 | ...: ... | semmle.label | ...: ... | | main.rs:13:9:13:11 | res | semmle.label | res | @@ -18,7 +18,7 @@ nodes | main.rs:16:31:16:48 | ...::must_use(...) | semmle.label | ...::must_use(...) | | main.rs:16:31:16:48 | MacroExpr | semmle.label | MacroExpr | | main.rs:16:31:16:48 | { ... } | semmle.label | { ... } | -| main.rs:91:26:91:46 | bad_body_only_handler | semmle.label | bad_body_only_handler | +| main.rs:123:26:123:46 | bad_body_only_handler | semmle.label | bad_body_only_handler | subpaths #select -| main.rs:16:23:16:49 | MacroExpr | main.rs:91:26:91:46 | bad_body_only_handler | main.rs:16:23:16:49 | MacroExpr | Cross-site scripting vulnerability due to a $@. | main.rs:91:26:91:46 | bad_body_only_handler | user-provided value | +| main.rs:16:23:16:49 | MacroExpr | main.rs:123:26:123:46 | bad_body_only_handler | main.rs:16:23:16:49 | MacroExpr | Cross-site scripting vulnerability due to a $@. | main.rs:123:26:123:46 | bad_body_only_handler | user-provided value | diff --git a/rust/test/security/CWE-079/IronResponseXss/main.rs b/rust/test/security/CWE-079/IronResponseXss/main.rs index 037e07d4..c6918a07 100644 --- a/rust/test/security/CWE-079/IronResponseXss/main.rs +++ b/rust/test/security/CWE-079/IronResponseXss/main.rs @@ -86,6 +86,38 @@ fn bad_mixed_mime_handler(req: &mut Request) -> IronResult { Ok(Response::with((status::Ok, mime, response))) } +fn bad_nested_tuple_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // BAD: modifier tuples nest, and the body is in the inner tuple. + Ok(Response::with((status::Ok, (status::Ok, response)))) +} + +fn bad_nested_named_tuple_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // BAD: the inner tuple is built first and nested later. + let inner = (status::Ok, response); + Ok(Response::with((status::Ok, inner))) +} + +fn bad_overriding_mime_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // BAD: the `text/html` modifier is applied after the `text/plain` one, so the body is served as HTML. + let plain: Mime = "text/plain".parse().unwrap(); + let html: Mime = "text/html".parse().unwrap(); + Ok(Response::with((status::Ok, plain, html, response))) +} + +fn good_nested_plain_text_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // GOOD: the `text/plain` content type is in the inner tuple and applies to the whole response. + let plain: Mime = "text/plain".parse().unwrap(); + Ok(Response::with((status::Ok, (plain, response)))) +} + fn main() { Iron::new(bad_handler).http("localhost:3000").unwrap(); Iron::new(Chain::new(bad_body_only_handler)).http("localhost:3001").unwrap(); @@ -97,4 +129,8 @@ fn main() { Iron::new(bad_html_mime_handler).http("localhost:3007").unwrap(); Iron::new(bad_parsed_literal_in_body_handler).http("localhost:3008").unwrap(); Iron::new(bad_mixed_mime_handler).http("localhost:3009").unwrap(); + Iron::new(bad_nested_tuple_handler).http("localhost:3010").unwrap(); + Iron::new(bad_overriding_mime_handler).http("localhost:3011").unwrap(); + Iron::new(good_nested_plain_text_handler).http("localhost:3012").unwrap(); + Iron::new(bad_nested_named_tuple_handler).http("localhost:3013").unwrap(); } From d6291b6ef12cecd86279fc15674681713b20545d Mon Sep 17 00:00:00 2001 From: Chad Bentz <1760475+felickz@users.noreply.github.com> Date: Mon, 23 Nov 2026 12:48:51 -0500 Subject: [PATCH 5/5] Rust Iron XSS: document that a tuple field assigned later is not tracked The sinks of the query are the expressions that a modifier tuple is built from, so a body that is assigned to a tuple field after the tuple is built (modifiers.1 = body) is not a sink. Tracking the field contents at the call would need a different sink model that also changes where alerts are reported, for a pattern that Iron code rarely uses, so say so in the query help and pin it with a fixture. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752 --- rust/src/security/CWE-079/IronResponseXss.qhelp | 5 +++++ .../CWE-079/IronResponseXss/IronModels.expected | 2 ++ rust/test/security/CWE-079/IronResponseXss/main.rs | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/rust/src/security/CWE-079/IronResponseXss.qhelp b/rust/src/security/CWE-079/IronResponseXss.qhelp index b277fd50..211cf4f8 100644 --- a/rust/src/security/CWE-079/IronResponseXss.qhelp +++ b/rust/src/security/CWE-079/IronResponseXss.qhelp @@ -51,6 +51,11 @@ recognized only when none of them names HTML or XML. Iron applies the modifiers text/html modifier after a text/plain one replaces it, and the query does not try to tell which of two content types wins.

    +

    +The sinks are the expressions that a modifier tuple is built from, so a tuple that is changed after it +is built, for example by assigning a body to one of its fields (modifiers.1 = body;), is +not tracked. +

    diff --git a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected index 2e5e279b..94a28d4b 100644 --- a/rust/test/security/CWE-079/IronResponseXss/IronModels.expected +++ b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected @@ -27,3 +27,5 @@ | main.rs:133:15:133:41 | bad_overriding_mime_handler | remote source | | main.rs:134:15:134:44 | good_nested_plain_text_handler | remote source | | main.rs:135:15:135:44 | bad_nested_named_tuple_handler | remote source | +| main.rs:136:15:136:49 | missed_field_assigned_later_handler | remote source | +| main.rs:146:23:146:31 | modifiers | html-injection sink | diff --git a/rust/test/security/CWE-079/IronResponseXss/main.rs b/rust/test/security/CWE-079/IronResponseXss/main.rs index c6918a07..3f57120b 100644 --- a/rust/test/security/CWE-079/IronResponseXss/main.rs +++ b/rust/test/security/CWE-079/IronResponseXss/main.rs @@ -133,4 +133,15 @@ fn main() { Iron::new(bad_overriding_mime_handler).http("localhost:3011").unwrap(); Iron::new(good_nested_plain_text_handler).http("localhost:3012").unwrap(); Iron::new(bad_nested_named_tuple_handler).http("localhost:3013").unwrap(); + Iron::new(missed_field_assigned_later_handler).http("localhost:3014").unwrap(); +} + +fn missed_field_assigned_later_handler(req: &mut Request) -> IronResult { + let res = req.url.path(); + let response = format!("

    Assessing resource, {:?}!

    ", res); + // MISSED (known limitation): the body is assigned to a tuple field after the tuple is built, so the + // sink is the original `String::new()` expression and not the assigned value. + let mut modifiers = (status::Ok, String::new()); + modifiers.1 = response; + Ok(Response::with(modifiers)) }