|
| 1 | +/** |
| 2 | + * @name Cross-site scripting through an Iron response modifier tuple |
| 3 | + * @description Writing user input into the body of an Iron response built from a |
| 4 | + * modifier tuple (for example `Response::with((status::Ok, body))`) |
| 5 | + * allows for a cross-site scripting vulnerability, because Iron does |
| 6 | + * not set a `Content-Type` and browsers sniff the body as HTML. |
| 7 | + * @kind path-problem |
| 8 | + * @problem.severity error |
| 9 | + * @security-severity 6.1 |
| 10 | + * @precision medium |
| 11 | + * @id githubsecuritylab/rust/iron-response-xss |
| 12 | + * @tags security |
| 13 | + * experimental |
| 14 | + * external/cwe/cwe-079 |
| 15 | + * external/cwe/cwe-116 |
| 16 | + */ |
| 17 | + |
| 18 | +import rust |
| 19 | +import codeql.rust.dataflow.DataFlow |
| 20 | +import codeql.rust.dataflow.TaintTracking |
| 21 | +import codeql.rust.security.XssExtensions |
| 22 | + |
| 23 | +/** |
| 24 | + * A string literal that is parsed, such as the `"text/plain"` of |
| 25 | + * `"text/plain".parse::<Mime>().unwrap()`. |
| 26 | + */ |
| 27 | +private class ParsedStringLiteral extends StringLiteralExpr { |
| 28 | + ParsedStringLiteral() { |
| 29 | + exists(MethodCallExpr parse | |
| 30 | + parse.getIdentifier().getText() = "parse" and |
| 31 | + parse.getReceiver() = this |
| 32 | + ) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** Holds if `literal` names a content type that browsers render as markup. */ |
| 37 | +private predicate isMarkupContentType(StringLiteralExpr literal) { |
| 38 | + literal.getTextValue().toLowerCase().matches(["%html%", "%xml%"]) |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Holds if `e` is the value that parsing `literal` produces: the `parse` call itself, that call |
| 43 | + * unwrapped with `unwrap`, `expect` or `?`, or a local variable that holds such a value. A value |
| 44 | + * that is merely derived from it, for example by formatting it into a string, is not. |
| 45 | + */ |
| 46 | +private predicate isParsedFrom(Expr e, ParsedStringLiteral literal) { |
| 47 | + e.(MethodCallExpr).getIdentifier().getText() = "parse" and |
| 48 | + e.(MethodCallExpr).getReceiver() = literal |
| 49 | + or |
| 50 | + e.(MethodCallExpr).getIdentifier().getText() = ["unwrap", "expect"] and |
| 51 | + isParsedFrom(e.(MethodCallExpr).getReceiver(), literal) |
| 52 | + or |
| 53 | + isParsedFrom(e.(TryExpr).getExpr(), literal) |
| 54 | + or |
| 55 | + exists(DataFlow::ExprNode source, DataFlow::ExprNode target | |
| 56 | + target.asExpr() = e and |
| 57 | + source.asExpr() != e and |
| 58 | + DataFlow::localFlow(source, target) and |
| 59 | + isParsedFrom(source.asExpr(), literal) |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Holds if `tuple` is a tuple expression whose value the expression `e` holds: `e` is the tuple, or a |
| 65 | + * local variable or expression that the tuple flows to. |
| 66 | + */ |
| 67 | +private predicate holdsTuple(Expr e, TupleExpr tuple) { |
| 68 | + exists(DataFlow::ExprNode tupleNode, DataFlow::ExprNode eNode | |
| 69 | + tupleNode.asExpr() = tuple and |
| 70 | + eNode.asExpr() = e and |
| 71 | + DataFlow::localFlow(tupleNode, eNode) |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Gets a modifier of the response that is built from `e`, the argument of `Response::with`: an element |
| 77 | + * of the tuple that `e` holds, or of a tuple that is nested in it. Iron applies the modifiers of a |
| 78 | + * nested tuple to the same response. |
| 79 | + */ |
| 80 | +private Expr getAModifier(Expr e) { |
| 81 | + exists(TupleExpr tuple, Expr element | holdsTuple(e, tuple) and element = tuple.getAField() | |
| 82 | + result = element and not holdsTuple(element, _) |
| 83 | + or |
| 84 | + result = getAModifier(element) |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Holds if the response that is built from `argument` has an explicit content type that is not markup: |
| 90 | + * a modifier is parsed from a string literal that does not name HTML or XML, and no modifier of the |
| 91 | + * response is parsed from a literal that does. Iron applies the modifiers in order, so a `text/html` |
| 92 | + * modifier after a `text/plain` one replaces it, and a response whose content type can be markup is |
| 93 | + * not safe. This also covers a content type that is parsed from different literals on different |
| 94 | + * branches of an `if`. |
| 95 | + */ |
| 96 | +private predicate hasSafeContentType(Expr argument) { |
| 97 | + exists(ParsedStringLiteral safe | |
| 98 | + isParsedFrom(getAModifier(argument), safe) and |
| 99 | + not isMarkupContentType(safe) |
| 100 | + ) and |
| 101 | + not exists(ParsedStringLiteral markup | |
| 102 | + isParsedFrom(getAModifier(argument), markup) and |
| 103 | + isMarkupContentType(markup) |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * An element of a modifier tuple passed to `iron::Response::with`, such as `body` in |
| 109 | + * `Response::with((status::Ok, body))`, or of a tuple that is nested in it. |
| 110 | + * |
| 111 | + * A response whose modifiers include a content type that is parsed from a string literal that does not |
| 112 | + * name HTML or XML, and none that does, has an explicit, safe `Content-Type` and is not considered a |
| 113 | + * sink. |
| 114 | + */ |
| 115 | +class IronResponseModifierTupleElement extends DataFlow::Node { |
| 116 | + IronResponseModifierTupleElement() { |
| 117 | + exists(Call call, Expr argument | |
| 118 | + call.getStaticTarget().getCanonicalPath() = "<iron::response::Response>::with" and |
| 119 | + argument = call.getPositionalArgument(0) and |
| 120 | + this.asExpr() = getAModifier(argument) and |
| 121 | + not hasSafeContentType(argument) |
| 122 | + ) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +module IronResponseXssConfig implements DataFlow::ConfigSig { |
| 127 | + predicate isSource(DataFlow::Node node) { node instanceof Xss::Source } |
| 128 | + |
| 129 | + predicate isSink(DataFlow::Node node) { node instanceof IronResponseModifierTupleElement } |
| 130 | + |
| 131 | + predicate isBarrier(DataFlow::Node barrier) { barrier instanceof Xss::Barrier } |
| 132 | + |
| 133 | + predicate observeDiffInformedIncrementalMode() { any() } |
| 134 | +} |
| 135 | + |
| 136 | +module IronResponseXssFlow = TaintTracking::Global<IronResponseXssConfig>; |
| 137 | + |
| 138 | +import IronResponseXssFlow::PathGraph |
| 139 | + |
| 140 | +from IronResponseXssFlow::PathNode sourceNode, IronResponseXssFlow::PathNode sinkNode |
| 141 | +where IronResponseXssFlow::flowPath(sourceNode, sinkNode) |
| 142 | +select sinkNode.getNode(), sourceNode, sinkNode, "Cross-site scripting vulnerability due to a $@.", |
| 143 | + sourceNode.getNode(), "user-provided value" |
0 commit comments