|
| 1 | +/** |
| 2 | + * @name Permissive CORS configuration |
| 3 | + * @description Allowing any origin to read cross-origin responses lets an |
| 4 | + * arbitrary website read data served to a victim's browser. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @security-severity 2.5 |
| 8 | + * @sub-severity low |
| 9 | + * @precision very-low |
| 10 | + * @id githubsecuritylab/audit/rust/permissive-cors |
| 11 | + * @tags security |
| 12 | + * experimental |
| 13 | + * audit |
| 14 | + * external/cwe/cwe-942 |
| 15 | + */ |
| 16 | + |
| 17 | +import rust |
| 18 | +import codeql.rust.dataflow.DataFlow |
| 19 | + |
| 20 | +/** Holds if `e` is a string literal whose value is `value`. */ |
| 21 | +bindingset[value] |
| 22 | +private predicate isStringLiteral(Expr e, string value) { |
| 23 | + e.(StringLiteralExpr).getTextValue() = "\"" + value + "\"" |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Holds if `c` builds a header value from its argument, such as `HeaderValue::from_static("*")`. |
| 28 | + */ |
| 29 | +private predicate isHeaderValueConstructor(CallExpr c) { |
| 30 | + isPathCall(c, "HeaderValue", |
| 31 | + ["from_static", "from_str", "from_bytes", "from_maybe_shared", "try_from"]) |
| 32 | + or |
| 33 | + isPathCall(c, "String", "from") |
| 34 | + or |
| 35 | + isPathCall(c, "AllowOrigin", "exact") |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Holds if the method `name` returns its receiver, or a conversion of it, without changing the value: |
| 40 | + * parsing, unwrapping and conversions. A method that changes the value, such as `replace`, is not |
| 41 | + * listed. |
| 42 | + */ |
| 43 | +private predicate isValuePreservingMethod(string name) { |
| 44 | + name = |
| 45 | + [ |
| 46 | + "parse", "unwrap", "expect", "to_string", "to_owned", "into", "try_into", "clone", "as_str", |
| 47 | + "as_ref", "as_bytes", "to_vec", "ok", "map_err" |
| 48 | + ] |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Gets an expression whose value the read `e` of a local variable can have: the initializer or the |
| 53 | + * right-hand side of an assignment that reaches `e`. A later assignment replaces the earlier one, so |
| 54 | + * only the one that is live at `e` is followed. |
| 55 | + */ |
| 56 | +private Expr getAReachingValue(Expr e) { |
| 57 | + exists(Variable v, DataFlow::ExprNode source, DataFlow::ExprNode target | |
| 58 | + e = v.getAnAccess() and |
| 59 | + target.asExpr() = e and |
| 60 | + source.asExpr() = result and |
| 61 | + source.asExpr() != e and |
| 62 | + DataFlow::localFlow(source, target) |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Gets an expression that the value of `e` is taken from: the receiver of a value-preserving method |
| 68 | + * call, the argument of a constructor of a header value, a referenced or `?`-operand expression, or a |
| 69 | + * reaching assignment to a local variable read by `e`. |
| 70 | + * |
| 71 | + * This handles wrappers such as `"*".parse::<HeaderValue>().unwrap()`, |
| 72 | + * `HeaderValue::from_static("*")` and `let origin = "*"; ... origin`. Other arguments are not |
| 73 | + * followed: in `origin.parse::<HeaderValue>().expect("*")` the `"*"` is only a message. Other methods |
| 74 | + * are not followed either: `"*".replace("*", "https://trusted.example.com")` is not a wildcard. |
| 75 | + */ |
| 76 | +private Expr getAnInnerExpr(Expr e) { |
| 77 | + result = e.(MethodCallExpr).getReceiver() and |
| 78 | + isValuePreservingMethod(e.(MethodCallExpr).getIdentifier().getText()) |
| 79 | + or |
| 80 | + isHeaderValueConstructor(e) and result = e.(CallExpr).getArgList().getAnArg() |
| 81 | + or |
| 82 | + result = e.(RefExpr).getExpr() |
| 83 | + or |
| 84 | + result = e.(TryExpr).getExpr() |
| 85 | + or |
| 86 | + result = getAReachingValue(e) |
| 87 | +} |
| 88 | + |
| 89 | +/** Holds if `p` is a path whose final segment is `name` (for example `Any` or `cors::Any`). */ |
| 90 | +bindingset[name] |
| 91 | +private predicate pathEndsWith(Path p, string name) { p.getText() = name } |
| 92 | + |
| 93 | +/** |
| 94 | + * Holds if `p` is a path whose final segment is `name` and whose qualifier |
| 95 | + * ends in `qualifier` (for example `CorsLayer::permissive`). |
| 96 | + */ |
| 97 | +bindingset[qualifier, name] |
| 98 | +private predicate qualifiedPathEndsWith(Path p, string qualifier, string name) { |
| 99 | + p.getText() = name and |
| 100 | + p.getQualifier().getText() = qualifier |
| 101 | +} |
| 102 | + |
| 103 | +/** Holds if `c` is a call to a function at a path ending in `qualifier::name`. */ |
| 104 | +bindingset[qualifier, name] |
| 105 | +private predicate isPathCall(CallExpr c, string qualifier, string name) { |
| 106 | + qualifiedPathEndsWith(c.getFunction().(PathExpr).getPath(), qualifier, name) |
| 107 | +} |
| 108 | + |
| 109 | +/** Holds if `e` is (or is derived from) a value denoting the wildcard CORS origin. */ |
| 110 | +private predicate isWildcardOriginValue(Expr e) { |
| 111 | + isStringLiteral(e, "*") |
| 112 | + or |
| 113 | + // `tower_http::cors::Any`, `salvo::cors::Any` |
| 114 | + pathEndsWith(e.(PathExpr).getPath(), "Any") |
| 115 | + or |
| 116 | + // `tower_http::cors::AllowOrigin::any()`, `AllowOrigin::mirror_request()` |
| 117 | + isPathCall(e, "AllowOrigin", ["any", "mirror_request"]) |
| 118 | + or |
| 119 | + isWildcardOriginValue(getAnInnerExpr(e)) |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Holds if `e` names the `Access-Control-Allow-Origin` header, either directly, |
| 124 | + * via a header-name constructor such as `HeaderName::from_static(...)`, or through a |
| 125 | + * local variable that holds such a name (`let name = ACCESS_CONTROL_ALLOW_ORIGIN;`). |
| 126 | + */ |
| 127 | +private predicate isAllowOriginHeaderName(Expr e) { |
| 128 | + e.(StringLiteralExpr).getTextValue().toLowerCase() = "\"access-control-allow-origin\"" |
| 129 | + or |
| 130 | + pathEndsWith(e.(PathExpr).getPath(), "ACCESS_CONTROL_ALLOW_ORIGIN") |
| 131 | + or |
| 132 | + isAllowOriginHeaderName(e.(RefExpr).getExpr()) |
| 133 | + or |
| 134 | + isAllowOriginHeaderName(getAReachingValue(e)) |
| 135 | + or |
| 136 | + exists(CallExpr c | c = e | |
| 137 | + isPathCall(c, "HeaderName", ["from_static", "from_lowercase", "from_bytes", "from_str"]) and |
| 138 | + isAllowOriginHeaderName(c.getArgList().getArg(0)) |
| 139 | + ) |
| 140 | + or |
| 141 | + exists(MethodCallExpr c | |
| 142 | + c = e and |
| 143 | + c.getIdentifier().getText() in ["unwrap", "expect"] and |
| 144 | + isAllowOriginHeaderName(c.getReceiver()) |
| 145 | + ) |
| 146 | + or |
| 147 | + isAllowOriginHeaderName(e.(TryExpr).getExpr()) |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Holds if `call` sets a header on a response, a header map or a header layer: `insert`, `append` |
| 152 | + * and `header` style methods, and functions such as `Header::new` and `warp::reply::with::header`. |
| 153 | + * A call that only receives a header name and a value, such as a comparison helper, does not. |
| 154 | + */ |
| 155 | +private predicate isHeaderWriter(Expr call) { |
| 156 | + call.(MethodCallExpr).getIdentifier().getText() = |
| 157 | + ["insert", "append", "header", "insert_header", "append_header", "add_header", "set_header"] |
| 158 | + or |
| 159 | + call.(CallExpr).getFunction().(PathExpr).getPath().getText() = |
| 160 | + ["header", "with_header", "overriding", "if_not_present", "appending"] |
| 161 | + or |
| 162 | + isPathCall(call, "Header", "new") |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * Holds if `name` and `value` are an adjacent header name/value pair, either |
| 167 | + * consecutive arguments of a call that sets a header (for example |
| 168 | + * `headers.insert(name, value)` or `with_header(reply, name, value)`) or a |
| 169 | + * 2-tuple `(name, value)`. |
| 170 | + */ |
| 171 | +private predicate isHeaderPair(Expr pair, Expr name, Expr value) { |
| 172 | + isHeaderWriter(pair) and |
| 173 | + exists(ArgList args, int i | |
| 174 | + args = pair.(MethodCallExpr).getArgList() or args = pair.(CallExpr).getArgList() |
| 175 | + | |
| 176 | + name = args.getArg(i) and |
| 177 | + value = args.getArg(i + 1) |
| 178 | + ) |
| 179 | + or |
| 180 | + pair.(TupleExpr).getNumberOfFields() = 2 and |
| 181 | + name = pair.(TupleExpr).getField(0) and |
| 182 | + value = pair.(TupleExpr).getField(1) |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * Holds if `e` configures a CORS policy (or sets a CORS response header) that |
| 187 | + * allows any origin, described by `description`. |
| 188 | + */ |
| 189 | +predicate isPermissiveCors(Expr e, string description) { |
| 190 | + // warp: `warp::cors().allow_any_origin()`; actix-cors: `Cors::default().allow_any_origin()` |
| 191 | + e.(MethodCallExpr).getIdentifier().getText() = "allow_any_origin" and |
| 192 | + e.(MethodCallExpr).getArgList().getNumberOfArgs() = 0 and |
| 193 | + description = "This CORS policy allows any origin" |
| 194 | + or |
| 195 | + // tower-http / salvo: `.allow_origin(Any)`, `.allow_origin("*".parse().unwrap())` |
| 196 | + e.(MethodCallExpr).getIdentifier().getText() = "allow_origin" and |
| 197 | + isWildcardOriginValue(e.(MethodCallExpr).getArgList().getArg(0)) and |
| 198 | + description = "This CORS policy allows any origin" |
| 199 | + or |
| 200 | + // actix-cors: `Cors::permissive()`; tower-http: `CorsLayer::permissive()` / `very_permissive()` |
| 201 | + ( |
| 202 | + isPathCall(e, "Cors", "permissive") |
| 203 | + or |
| 204 | + isPathCall(e, "CorsLayer", ["permissive", "very_permissive"]) |
| 205 | + or |
| 206 | + // rocket_cors: `AllowedOrigins::all()` |
| 207 | + isPathCall(e, "AllowedOrigins", "all") |
| 208 | + ) and |
| 209 | + description = "This CORS policy allows any origin" |
| 210 | + or |
| 211 | + // iron / hyper 0.10: `AccessControlAllowOrigin::Any` |
| 212 | + qualifiedPathEndsWith(e.(PathExpr).getPath(), "AccessControlAllowOrigin", "Any") and |
| 213 | + description = "This Access-Control-Allow-Origin header allows any origin" |
| 214 | + or |
| 215 | + // raw header: `headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN, "*")`, `("Access-Control-Allow-Origin", "*")` |
| 216 | + exists(Expr name, Expr value | |
| 217 | + isHeaderPair(e, name, value) and |
| 218 | + isAllowOriginHeaderName(name) and |
| 219 | + isWildcardOriginValue(value) |
| 220 | + ) and |
| 221 | + description = "This Access-Control-Allow-Origin header allows any origin" |
| 222 | +} |
| 223 | + |
| 224 | +from Expr e, string description |
| 225 | +where isPermissiveCors(e, description) |
| 226 | +select e, |
| 227 | + description + |
| 228 | + ". Review whether the responses of this service contain sensitive or credentialed data." |
0 commit comments