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..70b033fc --- /dev/null +++ b/rust/src/security/CWE-079/IronResponseXss.qhelp @@ -0,0 +1,55 @@ + + + + +

+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 ?. The type that the literal is +parsed into is not checked. +

+
+ + +
  • 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..49f127de --- /dev/null +++ b/rust/src/security/CWE-079/IronResponseXss.ql @@ -0,0 +1,106 @@ +/** + * @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%"]) +} + +/** + * 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. + */ +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) + ) +} + +/** + * 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 | + not isMarkupContentType(literal) and + isParsedFrom(modifiers.getAField(), literal) + ) + ) + } +} + +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..de103ab2 --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/IronModels.expected @@ -0,0 +1,19 @@ +| 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: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/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..1113fc2c --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/IronResponseXss.expected @@ -0,0 +1,114 @@ +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: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 | +| 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: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: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/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..fb740796 --- /dev/null +++ b/rust/test/security/CWE-079/IronResponseXss/main.rs @@ -0,0 +1,86 @@ +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 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(); + 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(); + Iron::new(bad_parsed_literal_in_body_handler).http("localhost:3008").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"