From d7f989c2d4ab91c2637be2345b08c26b427c342c Mon Sep 17 00:00:00 2001
From: Chad Bentz <1760475+felickz@users.noreply.github.com>
Date: Sat, 21 Nov 2026 13:22:40 -0500
Subject: [PATCH 1/4] Rust: add the permissive CORS audit query
Add githubsecuritylab/audit/rust/permissive-cors (CWE-942). It reports a CORS
policy or an Access-Control-Allow-Origin header that allows any origin, for
warp, actix-cors, tower-http, rocket_cors, Iron and raw headers. It is an audit
query: a wildcard origin is often intended for public data, so the message asks
for a review, and the query is not selected by the default Rust suite.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
---
rust/src/audit/CWE-942/PermissiveCors.qhelp | 55 +++
rust/src/audit/CWE-942/PermissiveCors.ql | 173 ++++++++++
rust/src/audit/CWE-942/PermissiveCorsBad.rs | 9 +
rust/src/audit/CWE-942/PermissiveCorsGood.rs | 9 +
.../PermissiveCors/PermissiveCors.expected | 22 ++
.../PermissiveCors/PermissiveCors.qlref | 1 +
.../test/audit/CWE-942/PermissiveCors/main.rs | 314 ++++++++++++++++++
7 files changed, 583 insertions(+)
create mode 100644 rust/src/audit/CWE-942/PermissiveCors.qhelp
create mode 100644 rust/src/audit/CWE-942/PermissiveCors.ql
create mode 100644 rust/src/audit/CWE-942/PermissiveCorsBad.rs
create mode 100644 rust/src/audit/CWE-942/PermissiveCorsGood.rs
create mode 100644 rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
create mode 100644 rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.qlref
create mode 100644 rust/test/audit/CWE-942/PermissiveCors/main.rs
diff --git a/rust/src/audit/CWE-942/PermissiveCors.qhelp b/rust/src/audit/CWE-942/PermissiveCors.qhelp
new file mode 100644
index 00000000..768700b3
--- /dev/null
+++ b/rust/src/audit/CWE-942/PermissiveCors.qhelp
@@ -0,0 +1,55 @@
+
+
+
+
+
+Cross-Origin Resource Sharing (CORS) lets a server relax the browser's same-origin policy by
+naming the origins that are allowed to read its responses in the
+Access-Control-Allow-Origin response header. A policy that allows any origin, for
+example by sending Access-Control-Allow-Origin: *, reflecting the request origin, or
+using a framework's "allow any origin" or "permissive" CORS configuration, lets every website
+read the responses of this service from a victim's browser.
+
+
+This is harmless for truly public, unauthenticated data, but it exposes any sensitive data the
+service returns (now or in the future), including data served to clients on a local or private
+network that the attacker cannot reach directly.
+
+
+This query recognizes, among others: allow_any_origin() (warp, actix-cors),
+Cors::permissive() (actix-cors), CorsLayer::permissive(),
+CorsLayer::very_permissive() and allow_origin with Any,
+"*", AllowOrigin::any() or AllowOrigin::mirror_request()
+(tower-http), AllowedOrigins::all() (rocket_cors),
+AccessControlAllowOrigin::Any (iron / hyper 0.10), and setting the
+Access-Control-Allow-Origin header to "*" directly.
+
+
+
+
+
+Allow only an explicit list of trusted origins. If the endpoint really serves only public data
+that every website may read, the alert can be dismissed.
+
+
+
+
+
+In the following example, an axum application uses a tower-http CorsLayer that
+allows the wildcard origin, so any website can read the JSON responses:
+
+
+
+The fix is to allow only the trusted front-end origin:
+
+
+
+
+
+MDN: Cross-Origin Resource Sharing (CORS).
+OWASP: Testing Cross Origin Resource Sharing.
+Common Weakness Enumeration: CWE-942.
+
+
diff --git a/rust/src/audit/CWE-942/PermissiveCors.ql b/rust/src/audit/CWE-942/PermissiveCors.ql
new file mode 100644
index 00000000..58d9614d
--- /dev/null
+++ b/rust/src/audit/CWE-942/PermissiveCors.ql
@@ -0,0 +1,173 @@
+/**
+ * @name Permissive CORS configuration
+ * @description Allowing any origin to read cross-origin responses lets an
+ * arbitrary website read data served to a victim's browser.
+ * @kind problem
+ * @problem.severity warning
+ * @security-severity 2.5
+ * @sub-severity low
+ * @precision very-low
+ * @id githubsecuritylab/audit/rust/permissive-cors
+ * @tags security
+ * experimental
+ * audit
+ * external/cwe/cwe-942
+ */
+
+import rust
+import codeql.rust.dataflow.DataFlow
+
+/** Holds if `e` is a string literal whose value is `value`. */
+bindingset[value]
+private predicate isStringLiteral(Expr e, string value) {
+ e.(StringLiteralExpr).getTextValue() = "\"" + value + "\""
+}
+
+/**
+ * Gets an expression that can contribute to the value of `e`: a receiver or
+ * argument of a call, a referenced or `?`-operand expression, or a reaching
+ * assignment to a local variable read by `e`.
+ *
+ * This handles wrappers such as `"*".parse::().unwrap()`,
+ * `HeaderValue::from_static("*")` and `let origin = "*"; ... origin`.
+ */
+private Expr getAnInnerExpr(Expr e) {
+ result = e.(MethodCallExpr).getReceiver()
+ or
+ result = e.(MethodCallExpr).getArgList().getAnArg()
+ or
+ result = e.(CallExpr).getArgList().getAnArg()
+ or
+ result = e.(RefExpr).getExpr()
+ or
+ result = e.(TryExpr).getExpr()
+ or
+ exists(Variable v, DataFlow::ExprNode source, DataFlow::ExprNode target |
+ e = v.getAnAccess() and
+ target.asExpr() = e and
+ source.asExpr() = result and
+ source.asExpr() != e and
+ DataFlow::localFlow(source, target)
+ )
+}
+
+/** Holds if `p` is a path whose final segment is `name` (for example `Any` or `cors::Any`). */
+bindingset[name]
+private predicate pathEndsWith(Path p, string name) { p.getText() = name }
+
+/**
+ * Holds if `p` is a path whose final segment is `name` and whose qualifier
+ * ends in `qualifier` (for example `CorsLayer::permissive`).
+ */
+bindingset[qualifier, name]
+private predicate qualifiedPathEndsWith(Path p, string qualifier, string name) {
+ p.getText() = name and
+ p.getQualifier().getText() = qualifier
+}
+
+/** Holds if `c` is a call to a function at a path ending in `qualifier::name`. */
+bindingset[qualifier, name]
+private predicate isPathCall(CallExpr c, string qualifier, string name) {
+ qualifiedPathEndsWith(c.getFunction().(PathExpr).getPath(), qualifier, name)
+}
+
+/** Holds if `e` is (or is derived from) a value denoting the wildcard CORS origin. */
+private predicate isWildcardOriginValue(Expr e) {
+ isStringLiteral(e, "*")
+ or
+ // `tower_http::cors::Any`, `salvo::cors::Any`
+ pathEndsWith(e.(PathExpr).getPath(), "Any")
+ or
+ // `tower_http::cors::AllowOrigin::any()`, `AllowOrigin::mirror_request()`
+ isPathCall(e, "AllowOrigin", ["any", "mirror_request"])
+ or
+ isWildcardOriginValue(getAnInnerExpr(e))
+}
+
+/**
+ * Holds if `e` names the `Access-Control-Allow-Origin` header, either directly
+ * or via a header-name constructor such as `HeaderName::from_static(...)`.
+ */
+private predicate isAllowOriginHeaderName(Expr e) {
+ e.(StringLiteralExpr).getTextValue().toLowerCase() = "\"access-control-allow-origin\""
+ or
+ pathEndsWith(e.(PathExpr).getPath(), "ACCESS_CONTROL_ALLOW_ORIGIN")
+ or
+ isAllowOriginHeaderName(e.(RefExpr).getExpr())
+ or
+ exists(CallExpr c | c = e |
+ isPathCall(c, "HeaderName", ["from_static", "from_lowercase", "from_bytes", "from_str"]) and
+ isAllowOriginHeaderName(c.getArgList().getArg(0))
+ )
+ or
+ exists(MethodCallExpr c |
+ c = e and
+ c.getIdentifier().getText() in ["unwrap", "expect"] and
+ isAllowOriginHeaderName(c.getReceiver())
+ )
+ or
+ isAllowOriginHeaderName(e.(TryExpr).getExpr())
+}
+
+/**
+ * Holds if `name` and `value` are an adjacent header name/value pair, either
+ * consecutive arguments of a call (for example `headers.insert(name, value)`
+ * or `with_header(reply, name, value)`) or a 2-tuple `(name, value)`.
+ */
+private predicate isHeaderPair(Expr pair, Expr name, Expr value) {
+ exists(ArgList args, int i |
+ args = pair.(MethodCallExpr).getArgList() or args = pair.(CallExpr).getArgList()
+ |
+ name = args.getArg(i) and
+ value = args.getArg(i + 1)
+ )
+ or
+ pair.(TupleExpr).getNumberOfFields() = 2 and
+ name = pair.(TupleExpr).getField(0) and
+ value = pair.(TupleExpr).getField(1)
+}
+
+/**
+ * Holds if `e` configures a CORS policy (or sets a CORS response header) that
+ * allows any origin, described by `description`.
+ */
+predicate isPermissiveCors(Expr e, string description) {
+ // warp: `warp::cors().allow_any_origin()`; actix-cors: `Cors::default().allow_any_origin()`
+ e.(MethodCallExpr).getIdentifier().getText() = "allow_any_origin" and
+ e.(MethodCallExpr).getArgList().getNumberOfArgs() = 0 and
+ description = "This CORS policy allows any origin"
+ or
+ // tower-http / salvo: `.allow_origin(Any)`, `.allow_origin("*".parse().unwrap())`
+ e.(MethodCallExpr).getIdentifier().getText() = "allow_origin" and
+ isWildcardOriginValue(e.(MethodCallExpr).getArgList().getArg(0)) and
+ description = "This CORS policy allows any origin"
+ or
+ // actix-cors: `Cors::permissive()`; tower-http: `CorsLayer::permissive()` / `very_permissive()`
+ (
+ isPathCall(e, "Cors", "permissive")
+ or
+ isPathCall(e, "CorsLayer", ["permissive", "very_permissive"])
+ or
+ // rocket_cors: `AllowedOrigins::all()`
+ isPathCall(e, "AllowedOrigins", "all")
+ ) and
+ description = "This CORS policy allows any origin"
+ or
+ // iron / hyper 0.10: `AccessControlAllowOrigin::Any`
+ qualifiedPathEndsWith(e.(PathExpr).getPath(), "AccessControlAllowOrigin", "Any") and
+ description = "This Access-Control-Allow-Origin header allows any origin"
+ or
+ // raw header: `headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN, "*")`, `("Access-Control-Allow-Origin", "*")`
+ exists(Expr name, Expr value |
+ isHeaderPair(e, name, value) and
+ isAllowOriginHeaderName(name) and
+ isWildcardOriginValue(value)
+ ) and
+ description = "This Access-Control-Allow-Origin header allows any origin"
+}
+
+from Expr e, string description
+where isPermissiveCors(e, description)
+select e,
+ description +
+ ". Review whether the responses of this service contain sensitive or credentialed data."
diff --git a/rust/src/audit/CWE-942/PermissiveCorsBad.rs b/rust/src/audit/CWE-942/PermissiveCorsBad.rs
new file mode 100644
index 00000000..8e53b37f
--- /dev/null
+++ b/rust/src/audit/CWE-942/PermissiveCorsBad.rs
@@ -0,0 +1,9 @@
+use axum::{http::HeaderValue, routing::get, Router};
+use tower_http::cors::CorsLayer;
+
+fn app() -> Router {
+ Router::new().route("/json", get(|| async { "{}" })).layer(
+ // BAD: any website can read responses from this service
+ CorsLayer::new().allow_origin("*".parse::().unwrap()),
+ )
+}
diff --git a/rust/src/audit/CWE-942/PermissiveCorsGood.rs b/rust/src/audit/CWE-942/PermissiveCorsGood.rs
new file mode 100644
index 00000000..7559b341
--- /dev/null
+++ b/rust/src/audit/CWE-942/PermissiveCorsGood.rs
@@ -0,0 +1,9 @@
+use axum::{http::HeaderValue, routing::get, Router};
+use tower_http::cors::CorsLayer;
+
+fn app() -> Router {
+ Router::new().route("/json", get(|| async { "{}" })).layer(
+ // GOOD: only the trusted front-end origin can read responses
+ CorsLayer::new().allow_origin("https://app.example.com".parse::().unwrap()),
+ )
+}
diff --git a/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
new file mode 100644
index 00000000..688d1739
--- /dev/null
+++ b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
@@ -0,0 +1,22 @@
+| main.rs:11:20:12:31 | ... .allow_any_origin() | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:27:19:27:79 | ...::header(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:62:27:62:55 | ...::Any | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:99:13:100:66 | ... .allow_origin(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:108:20:108:79 | ... .allow_origin(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:114:20:114:53 | ... .allow_origin(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:120:20:120:59 | ... .allow_origin(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:126:20:126:68 | ... .allow_origin(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:132:20:132:79 | ... .allow_origin(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:139:20:139:88 | ... .allow_origin(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:147:20:147:88 | ... .allow_origin(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:161:20:161:42 | ...::permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:167:20:167:47 | ...::very_permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:172:9:172:90 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:177:11:177:52 | TupleExpr | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:216:9:219:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:225:9:228:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:235:9:238:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:263:28:263:63 | TupleExpr | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:276:23:276:56 | ... .allow_any_origin() | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:277:34:277:51 | ...::permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:301:30:301:50 | ...::all(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
diff --git a/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.qlref b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.qlref
new file mode 100644
index 00000000..e9f39ddc
--- /dev/null
+++ b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.qlref
@@ -0,0 +1 @@
+query: audit/CWE-942/PermissiveCors.ql
diff --git a/rust/test/audit/CWE-942/PermissiveCors/main.rs b/rust/test/audit/CWE-942/PermissiveCors/main.rs
new file mode 100644
index 00000000..ef4ad42b
--- /dev/null
+++ b/rust/test/audit/CWE-942/PermissiveCors/main.rs
@@ -0,0 +1,314 @@
+// Test cases for `rust/permissive-cors` (CWE-942).
+//
+// The third-party crates referenced here (warp, iron, tower-http, axum,
+// actix-web, actix-cors, rocket, rocket_cors) are not fetched for this test,
+// so the query must recognize the permissive CORS patterns syntactically.
+
+mod warp_cors {
+ use warp::Filter;
+
+ pub async fn bad() {
+ let cors = warp::cors()
+ .allow_any_origin() // $ Alert
+ .allow_methods(vec!["GET", "POST"])
+ .allow_headers(vec!["Content-Type", "Authorization"]);
+
+ let route = warp::path("example")
+ .and(warp::get())
+ .map(|| "Hello, CORS!")
+ .with(cors);
+
+ warp::serve(route).run(([127, 0, 0, 1], 3030)).await;
+ }
+
+ pub async fn bad_raw_header() {
+ let route = warp::path("example")
+ .map(|| "Hello, CORS!")
+ .with(warp::reply::with::header("Access-Control-Allow-Origin", "*")); // $ Alert
+ warp::serve(route).run(([127, 0, 0, 1], 3031)).await;
+ }
+
+ pub async fn good() {
+ let cors = warp::cors()
+ .allow_origin("https://trusted.example.com")
+ .allow_methods(vec!["GET", "POST"]);
+
+ let route = warp::path("example")
+ .map(|| "Hello, CORS!")
+ .with(cors);
+
+ warp::serve(route).run(([127, 0, 0, 1], 3032)).await;
+ }
+
+ pub async fn good_raw_header() {
+ let route = warp::path("example")
+ .map(|| "Hello, CORS!")
+ .with(warp::reply::with::header(
+ "Access-Control-Allow-Origin",
+ "https://trusted.example.com",
+ ));
+ warp::serve(route).run(([127, 0, 0, 1], 3033)).await;
+ }
+}
+
+mod iron_cors {
+ use iron::headers::AccessControlAllowOrigin;
+ use iron::prelude::*;
+ use iron::status;
+
+ pub fn bad() {
+ Iron::new(|_: &mut Request| {
+ let mut r = Response::with((status::Ok, "Hello World!"));
+ r.headers.set(AccessControlAllowOrigin::Any); // $ Alert
+ Ok(r)
+ })
+ .http("localhost:8080")
+ .unwrap();
+ }
+
+ pub fn good() {
+ Iron::new(|_: &mut Request| {
+ let mut r = Response::with((status::Ok, "Hello World!"));
+ r.headers.set(AccessControlAllowOrigin::Value(
+ "https://trusted.example.com".to_owned(),
+ ));
+ Ok(r)
+ })
+ .http("localhost:8081")
+ .unwrap();
+ }
+}
+
+mod tower_http_cors {
+ use std::str::FromStr;
+
+ use axum::{
+ http::{header, HeaderMap, HeaderValue, Method},
+ response::IntoResponse,
+ routing::get,
+ Json, Router,
+ };
+ use tower_http::cors::{self, AllowOrigin, Any, CorsLayer};
+
+ async fn json() -> impl IntoResponse {
+ Json(vec!["one", "two", "three"])
+ }
+
+ pub fn bad_parsed_wildcard() -> Router {
+ Router::new().route("/json", get(json)).layer(
+ CorsLayer::new()
+ .allow_origin("*".parse::().unwrap()) // $ Alert
+ .allow_methods([Method::GET]),
+ )
+ }
+
+ pub fn bad_from_static_wildcard() -> Router {
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_origin(HeaderValue::from_static("*"))) // $ Alert
+ }
+
+ pub fn bad_any() -> Router {
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_origin(Any)) // $ Alert
+ }
+
+ pub fn bad_qualified_any() -> Router {
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_origin(cors::Any)) // $ Alert
+ }
+
+ pub fn bad_allow_origin_any() -> Router {
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_origin(AllowOrigin::any())) // $ Alert
+ }
+
+ pub fn bad_mirror_request() -> Router {
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_origin(AllowOrigin::mirror_request())) // $ Alert
+ }
+
+ pub fn bad_wildcard_variable() -> Router {
+ let origin = "*";
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_origin(origin.parse::().unwrap())) // $ Alert
+ }
+
+ pub fn bad_reassigned_wildcard_variable() -> Router {
+ let mut origin = "https://trusted.example.com";
+ origin = "*";
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_origin(origin.parse::().unwrap())) // $ Alert
+ }
+
+ pub fn good_reassigned_trusted_variable() -> Router {
+ let mut origin = "*";
+ origin = "https://trusted.example.com";
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_origin(origin.parse::().unwrap()))
+ }
+
+ pub fn bad_permissive() -> Router {
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::permissive()) // $ Alert
+ }
+
+ pub fn bad_very_permissive() -> Router {
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::very_permissive()) // $ Alert
+ }
+
+ pub async fn bad_raw_header_map() -> impl IntoResponse {
+ let mut headers = HeaderMap::new();
+ headers.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static("*")); // $ Alert
+ (headers, "Hello")
+ }
+
+ pub async fn bad_raw_header_tuple() -> impl IntoResponse {
+ ([(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*")], "Hello") // $ Alert
+ }
+
+ pub fn good_exact_origin() -> Router {
+ Router::new().route("/json", get(json)).layer(
+ CorsLayer::new()
+ .allow_origin("https://trusted.example.com".parse::().unwrap())
+ .allow_methods([Method::GET])
+ .allow_headers(Any),
+ )
+ }
+
+ pub fn good_origin_list() -> Router {
+ Router::new().route("/json", get(json)).layer(
+ CorsLayer::new().allow_origin(AllowOrigin::list([
+ HeaderValue::from_static("https://a.example.com"),
+ HeaderValue::from_static("https://b.example.com"),
+ ])),
+ )
+ }
+
+ pub fn good_any_methods_only() -> Router {
+ Router::new()
+ .route("/json", get(json))
+ .layer(CorsLayer::new().allow_methods(Any).allow_headers(Any))
+ }
+
+ pub async fn good_raw_header_map() -> impl IntoResponse {
+ let mut headers = HeaderMap::new();
+ headers.insert(
+ header::ACCESS_CONTROL_ALLOW_ORIGIN,
+ HeaderValue::from_static("https://trusted.example.com"),
+ );
+ headers.insert(header::ACCESS_CONTROL_ALLOW_METHODS, HeaderValue::from_static("*"));
+ (headers, "Hello")
+ }
+
+ pub async fn bad_raw_header_name_constructor() -> impl IntoResponse {
+ let mut headers = HeaderMap::new();
+ headers.insert(
+ header::HeaderName::from_static("access-control-allow-origin"),
+ HeaderValue::from_static("*"),
+ ); // $ Alert
+ (headers, "Hello")
+ }
+
+ pub async fn bad_raw_header_name_constructor_unwrap() -> impl IntoResponse {
+ let mut headers = HeaderMap::new();
+ headers.insert(
+ header::HeaderName::from_str("access-control-allow-origin").unwrap(),
+ HeaderValue::from_static("*"),
+ ); // $ Alert
+ (headers, "Hello")
+ }
+
+ pub async fn bad_raw_header_name_constructor_try(
+ ) -> Result> {
+ let mut headers = HeaderMap::new();
+ headers.insert(
+ header::HeaderName::from_str("access-control-allow-origin")?,
+ HeaderValue::from_static("*"),
+ ); // $ Alert
+ Ok((headers, "Hello"))
+ }
+
+ fn check(value: Option<&HeaderValue>, expected: &str) -> bool {
+ value.map_or(false, |v| v == expected)
+ }
+
+ // Reading (rather than setting) the header must not be reported.
+ pub fn good_read_header(headers: &HeaderMap) {
+ assert_eq!(headers.get(header::ACCESS_CONTROL_ALLOW_ORIGIN).unwrap(), "*");
+ assert!(check(headers.get(header::ACCESS_CONTROL_ALLOW_ORIGIN), "*"));
+ }
+}
+
+mod actix_cors_example {
+ use actix_cors::Cors;
+ use actix_web::{web, App, HttpResponse, HttpServer};
+
+ async fn index() -> HttpResponse {
+ HttpResponse::Ok().body("Hello")
+ }
+
+ async fn bad_insert_header() -> HttpResponse {
+ HttpResponse::Ok()
+ .insert_header(("Access-Control-Allow-Origin", "*")) // $ Alert
+ .body("Hello")
+ }
+
+ async fn good_insert_header() -> HttpResponse {
+ HttpResponse::Ok()
+ .insert_header(("access-control-allow-origin", "https://trusted.example.com"))
+ .insert_header(("X-Custom", "*"))
+ .body("Hello")
+ }
+
+ pub async fn run() -> std::io::Result<()> {
+ HttpServer::new(|| {
+ let bad = Cors::default().allow_any_origin(); // $ Alert
+ let bad_permissive = Cors::permissive(); // $ Alert
+ let good = Cors::default()
+ .allowed_origin("https://trusted.example.com")
+ .allow_any_method()
+ .allow_any_header();
+ App::new()
+ .wrap(bad)
+ .wrap(bad_permissive)
+ .wrap(good)
+ .route("/", web::get().to(index))
+ .route("/bad", web::get().to(bad_insert_header))
+ .route("/good", web::get().to(good_insert_header))
+ })
+ .bind(("127.0.0.1", 8080))?
+ .run()
+ .await
+ }
+}
+
+mod rocket_cors_example {
+ use rocket_cors::{AllowedOrigins, CorsOptions};
+
+ pub fn bad() -> CorsOptions {
+ CorsOptions {
+ allowed_origins: AllowedOrigins::all(), // $ Alert
+ ..Default::default()
+ }
+ }
+
+ pub fn good() -> CorsOptions {
+ CorsOptions {
+ allowed_origins: AllowedOrigins::some_exact(&["https://trusted.example.com"]),
+ ..Default::default()
+ }
+ }
+}
+
+fn main() {}
From 47dad7ef7d49c019ea559d7a1805c2ecce5ffbff 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/4] Rust CORS audit: restrict wildcard and header matching
Follow call arguments only for constructors of header values, so a wildcard
that is only the message of expect no longer makes an exact origin look like
a wildcard. Accept a header name and value pair only for calls that set a
header, so a read-only helper that receives them is not reported.
Add a regression for each.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
---
rust/src/audit/CWE-942/PermissiveCors.ql | 44 +++++++++++++++----
.../PermissiveCors/PermissiveCors.expected | 8 ++--
.../test/audit/CWE-942/PermissiveCors/main.rs | 17 +++++++
3 files changed, 57 insertions(+), 12 deletions(-)
diff --git a/rust/src/audit/CWE-942/PermissiveCors.ql b/rust/src/audit/CWE-942/PermissiveCors.ql
index 58d9614d..156b7eb6 100644
--- a/rust/src/audit/CWE-942/PermissiveCors.ql
+++ b/rust/src/audit/CWE-942/PermissiveCors.ql
@@ -24,19 +24,30 @@ private predicate isStringLiteral(Expr e, string value) {
}
/**
- * Gets an expression that can contribute to the value of `e`: a receiver or
- * argument of a call, a referenced or `?`-operand expression, or a reaching
+ * Holds if `c` builds a header value from its argument, such as `HeaderValue::from_static("*")`.
+ */
+private predicate isHeaderValueConstructor(CallExpr c) {
+ isPathCall(c, "HeaderValue",
+ ["from_static", "from_str", "from_bytes", "from_maybe_shared", "try_from"])
+ or
+ isPathCall(c, "String", "from")
+ or
+ isPathCall(c, "AllowOrigin", "exact")
+}
+
+/**
+ * Gets an expression that the value of `e` is taken from: the receiver of a method call, the
+ * argument of a constructor of a header value, a referenced or `?`-operand expression, or a reaching
* assignment to a local variable read by `e`.
*
* This handles wrappers such as `"*".parse::().unwrap()`,
- * `HeaderValue::from_static("*")` and `let origin = "*"; ... origin`.
+ * `HeaderValue::from_static("*")` and `let origin = "*"; ... origin`. Other arguments are not
+ * followed: in `origin.parse::().expect("*")` the `"*"` is only a message.
*/
private Expr getAnInnerExpr(Expr e) {
result = e.(MethodCallExpr).getReceiver()
or
- result = e.(MethodCallExpr).getArgList().getAnArg()
- or
- result = e.(CallExpr).getArgList().getAnArg()
+ isHeaderValueConstructor(e) and result = e.(CallExpr).getArgList().getAnArg()
or
result = e.(RefExpr).getExpr()
or
@@ -109,12 +120,29 @@ private predicate isAllowOriginHeaderName(Expr e) {
isAllowOriginHeaderName(e.(TryExpr).getExpr())
}
+/**
+ * Holds if `call` sets a header on a response, a header map or a header layer: `insert`, `append`
+ * and `header` style methods, and functions such as `Header::new` and `warp::reply::with::header`.
+ * A call that only receives a header name and a value, such as a comparison helper, does not.
+ */
+private predicate isHeaderWriter(Expr call) {
+ call.(MethodCallExpr).getIdentifier().getText() =
+ ["insert", "append", "header", "insert_header", "append_header", "add_header", "set_header"]
+ or
+ call.(CallExpr).getFunction().(PathExpr).getPath().getText() =
+ ["header", "with_header", "overriding", "if_not_present", "appending"]
+ or
+ isPathCall(call, "Header", "new")
+}
+
/**
* Holds if `name` and `value` are an adjacent header name/value pair, either
- * consecutive arguments of a call (for example `headers.insert(name, value)`
- * or `with_header(reply, name, value)`) or a 2-tuple `(name, value)`.
+ * consecutive arguments of a call that sets a header (for example
+ * `headers.insert(name, value)` or `with_header(reply, name, value)`) or a
+ * 2-tuple `(name, value)`.
*/
private predicate isHeaderPair(Expr pair, Expr name, Expr value) {
+ isHeaderWriter(pair) and
exists(ArgList args, int i |
args = pair.(MethodCallExpr).getArgList() or args = pair.(CallExpr).getArgList()
|
diff --git a/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
index 688d1739..f01cd5c9 100644
--- a/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
+++ b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
@@ -16,7 +16,7 @@
| main.rs:216:9:219:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
| main.rs:225:9:228:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
| main.rs:235:9:238:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:263:28:263:63 | TupleExpr | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:276:23:276:56 | ... .allow_any_origin() | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:277:34:277:51 | ...::permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:301:30:301:50 | ...::all(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:280:28:280:63 | TupleExpr | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:293:23:293:56 | ... .allow_any_origin() | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:294:34:294:51 | ...::permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:318:30:318:50 | ...::all(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
diff --git a/rust/test/audit/CWE-942/PermissiveCors/main.rs b/rust/test/audit/CWE-942/PermissiveCors/main.rs
index ef4ad42b..5f297ff7 100644
--- a/rust/test/audit/CWE-942/PermissiveCors/main.rs
+++ b/rust/test/audit/CWE-942/PermissiveCors/main.rs
@@ -243,11 +243,28 @@ mod tower_http_cors {
value.map_or(false, |v| v == expected)
}
+ fn has_header(headers: &HeaderMap, name: &str, expected: &str) -> bool {
+ headers.get(name).map_or(false, |v| v == expected)
+ }
+
// Reading (rather than setting) the header must not be reported.
pub fn good_read_header(headers: &HeaderMap) {
assert_eq!(headers.get(header::ACCESS_CONTROL_ALLOW_ORIGIN).unwrap(), "*");
assert!(check(headers.get(header::ACCESS_CONTROL_ALLOW_ORIGIN), "*"));
}
+
+ // A helper that is given a header name and a value, and only compares them, does not set a header.
+ pub fn good_header_comparison_helper(headers: &HeaderMap) -> bool {
+ has_header(headers, "Access-Control-Allow-Origin", "*")
+ }
+
+ // The wildcard is only the message of `expect`: the origin itself is an exact one.
+ pub fn good_wildcard_only_in_expect_message() -> Router {
+ Router::new().route("/json", get(json)).layer(
+ CorsLayer::new()
+ .allow_origin("https://trusted.example.com".parse::().expect("*")),
+ )
+ }
}
mod actix_cors_example {
From 6fcfffa2ebc83050fbcbe9cc6426afd130bc4fc6 Mon Sep 17 00:00:00 2001
From: Chad Bentz <1760475+felickz@users.noreply.github.com>
Date: Sat, 21 Nov 2026 23:31:12 -0500
Subject: [PATCH 3/4] Rust CORS audit: follow only value-preserving methods,
correct the help text
Follow the receiver of a method call only for methods that keep the value
(parse, unwrap, expect, to_string, to_owned, into, try_into, clone, as_str,
as_ref, as_bytes, to_vec, ok, map_err), so a wildcard that is changed with
replace before it is used is not reported. Add regressions for an origin and a
raw header value.
Correct the query help: a browser does not honor a wildcard origin for requests
that carry credentials. A wildcard exposes data that is protected only by the
location of the client, and session-protected data is exposed by a reflected
origin together with Access-Control-Allow-Credentials: true.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
---
rust/src/audit/CWE-942/PermissiveCors.qhelp | 16 +++++++-----
rust/src/audit/CWE-942/PermissiveCors.ql | 25 +++++++++++++++----
.../PermissiveCors/PermissiveCors.expected | 8 +++---
.../test/audit/CWE-942/PermissiveCors/main.rs | 21 ++++++++++++++++
4 files changed, 55 insertions(+), 15 deletions(-)
diff --git a/rust/src/audit/CWE-942/PermissiveCors.qhelp b/rust/src/audit/CWE-942/PermissiveCors.qhelp
index 768700b3..68fba2e6 100644
--- a/rust/src/audit/CWE-942/PermissiveCors.qhelp
+++ b/rust/src/audit/CWE-942/PermissiveCors.qhelp
@@ -8,14 +8,18 @@
Cross-Origin Resource Sharing (CORS) lets a server relax the browser's same-origin policy by
naming the origins that are allowed to read its responses in the
Access-Control-Allow-Origin response header. A policy that allows any origin, for
-example by sending Access-Control-Allow-Origin: *, reflecting the request origin, or
-using a framework's "allow any origin" or "permissive" CORS configuration, lets every website
-read the responses of this service from a victim's browser.
+example by sending Access-Control-Allow-Origin: * or by using a framework's "allow any
+origin" or "permissive" CORS configuration, lets every website read the responses that the browser
+requests without credentials.
-This is harmless for truly public, unauthenticated data, but it exposes any sensitive data the
-service returns (now or in the future), including data served to clients on a local or private
-network that the attacker cannot reach directly.
+This is harmless for truly public data. It exposes data that is protected only by where the client
+is, such as a service on localhost or on a private network that the attacker cannot reach directly
+but the victim's browser can. A browser does not honor a wildcard for requests that carry
+credentials (cookies or HTTP authentication), so a wildcard alone does not expose data that a login
+session protects. That data is exposed when a policy reflects the request origin (for example
+AllowOrigin::mirror_request() or CorsLayer::very_permissive()) and also
+sets Access-Control-Allow-Credentials: true.
This query recognizes, among others: allow_any_origin() (warp, actix-cors),
diff --git a/rust/src/audit/CWE-942/PermissiveCors.ql b/rust/src/audit/CWE-942/PermissiveCors.ql
index 156b7eb6..305eda11 100644
--- a/rust/src/audit/CWE-942/PermissiveCors.ql
+++ b/rust/src/audit/CWE-942/PermissiveCors.ql
@@ -36,16 +36,31 @@ private predicate isHeaderValueConstructor(CallExpr c) {
}
/**
- * Gets an expression that the value of `e` is taken from: the receiver of a method call, the
- * argument of a constructor of a header value, a referenced or `?`-operand expression, or a reaching
- * assignment to a local variable read by `e`.
+ * Holds if the method `name` returns its receiver, or a conversion of it, without changing the value:
+ * parsing, unwrapping and conversions. A method that changes the value, such as `replace`, is not
+ * listed.
+ */
+private predicate isValuePreservingMethod(string name) {
+ name =
+ [
+ "parse", "unwrap", "expect", "to_string", "to_owned", "into", "try_into", "clone", "as_str",
+ "as_ref", "as_bytes", "to_vec", "ok", "map_err"
+ ]
+}
+
+/**
+ * Gets an expression that the value of `e` is taken from: the receiver of a value-preserving method
+ * call, the argument of a constructor of a header value, a referenced or `?`-operand expression, or a
+ * reaching assignment to a local variable read by `e`.
*
* This handles wrappers such as `"*".parse::().unwrap()`,
* `HeaderValue::from_static("*")` and `let origin = "*"; ... origin`. Other arguments are not
- * followed: in `origin.parse::().expect("*")` the `"*"` is only a message.
+ * followed: in `origin.parse::().expect("*")` the `"*"` is only a message. Other methods
+ * are not followed either: `"*".replace("*", "https://trusted.example.com")` is not a wildcard.
*/
private Expr getAnInnerExpr(Expr e) {
- result = e.(MethodCallExpr).getReceiver()
+ result = e.(MethodCallExpr).getReceiver() and
+ isValuePreservingMethod(e.(MethodCallExpr).getIdentifier().getText())
or
isHeaderValueConstructor(e) and result = e.(CallExpr).getArgList().getAnArg()
or
diff --git a/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
index f01cd5c9..d6c90c55 100644
--- a/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
+++ b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
@@ -16,7 +16,7 @@
| main.rs:216:9:219:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
| main.rs:225:9:228:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
| main.rs:235:9:238:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:280:28:280:63 | TupleExpr | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:293:23:293:56 | ... .allow_any_origin() | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:294:34:294:51 | ...::permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:318:30:318:50 | ...::all(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:301:28:301:63 | TupleExpr | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:314:23:314:56 | ... .allow_any_origin() | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:315:34:315:51 | ...::permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:339:30:339:50 | ...::all(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
diff --git a/rust/test/audit/CWE-942/PermissiveCors/main.rs b/rust/test/audit/CWE-942/PermissiveCors/main.rs
index 5f297ff7..c538c8f6 100644
--- a/rust/test/audit/CWE-942/PermissiveCors/main.rs
+++ b/rust/test/audit/CWE-942/PermissiveCors/main.rs
@@ -265,6 +265,27 @@ mod tower_http_cors {
.allow_origin("https://trusted.example.com".parse::().expect("*")),
)
}
+
+ // The wildcard is replaced by an exact origin before it is used.
+ pub fn good_wildcard_replaced_by_exact_origin() -> Router {
+ Router::new().route("/json", get(json)).layer(
+ CorsLayer::new().allow_origin(
+ "*".replace("*", "https://trusted.example.com")
+ .parse::()
+ .unwrap(),
+ ),
+ )
+ }
+
+ // A raw header whose value is a wildcard that is replaced by an exact origin is not a wildcard.
+ pub async fn good_raw_header_wildcard_replaced() -> impl IntoResponse {
+ let mut headers = HeaderMap::new();
+ headers.insert(
+ header::ACCESS_CONTROL_ALLOW_ORIGIN,
+ HeaderValue::from_str(&"*".replace("*", "https://trusted.example.com")).unwrap(),
+ );
+ (headers, "Hello")
+ }
}
mod actix_cors_example {
From 51793cfe60d47e868745ccca09931e5701aeb986 Mon Sep 17 00:00:00 2001
From: Chad Bentz <1760475+felickz@users.noreply.github.com>
Date: Mon, 23 Nov 2026 10:56:28 -0500
Subject: [PATCH 4/4] Rust CORS audit: follow local aliases of the header name
The header name of a raw Access-Control-Allow-Origin header was recognized
only when it was written in the call. A name that is held in a local
variable, as in `let name = ACCESS_CONTROL_ALLOW_ORIGIN;`, was missed.
Follow the variable to the value that reaches it, as is already done for the
header value, so that a later assignment replaces an earlier one.
Add regressions for a name from a constant and from a constructor, and for
a name that is replaced before it is used.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
---
rust/src/audit/CWE-942/PermissiveCors.ql | 30 +++++++++++++------
.../PermissiveCors/PermissiveCors.expected | 10 ++++---
.../test/audit/CWE-942/PermissiveCors/main.rs | 25 ++++++++++++++++
3 files changed, 52 insertions(+), 13 deletions(-)
diff --git a/rust/src/audit/CWE-942/PermissiveCors.ql b/rust/src/audit/CWE-942/PermissiveCors.ql
index 305eda11..d3ae4d6c 100644
--- a/rust/src/audit/CWE-942/PermissiveCors.ql
+++ b/rust/src/audit/CWE-942/PermissiveCors.ql
@@ -48,6 +48,21 @@ private predicate isValuePreservingMethod(string name) {
]
}
+/**
+ * Gets an expression whose value the read `e` of a local variable can have: the initializer or the
+ * right-hand side of an assignment that reaches `e`. A later assignment replaces the earlier one, so
+ * only the one that is live at `e` is followed.
+ */
+private Expr getAReachingValue(Expr e) {
+ exists(Variable v, DataFlow::ExprNode source, DataFlow::ExprNode target |
+ e = v.getAnAccess() and
+ target.asExpr() = e and
+ source.asExpr() = result and
+ source.asExpr() != e and
+ DataFlow::localFlow(source, target)
+ )
+}
+
/**
* Gets an expression that the value of `e` is taken from: the receiver of a value-preserving method
* call, the argument of a constructor of a header value, a referenced or `?`-operand expression, or a
@@ -68,13 +83,7 @@ private Expr getAnInnerExpr(Expr e) {
or
result = e.(TryExpr).getExpr()
or
- exists(Variable v, DataFlow::ExprNode source, DataFlow::ExprNode target |
- e = v.getAnAccess() and
- target.asExpr() = e and
- source.asExpr() = result and
- source.asExpr() != e and
- DataFlow::localFlow(source, target)
- )
+ result = getAReachingValue(e)
}
/** Holds if `p` is a path whose final segment is `name` (for example `Any` or `cors::Any`). */
@@ -111,8 +120,9 @@ private predicate isWildcardOriginValue(Expr e) {
}
/**
- * Holds if `e` names the `Access-Control-Allow-Origin` header, either directly
- * or via a header-name constructor such as `HeaderName::from_static(...)`.
+ * Holds if `e` names the `Access-Control-Allow-Origin` header, either directly,
+ * via a header-name constructor such as `HeaderName::from_static(...)`, or through a
+ * local variable that holds such a name (`let name = ACCESS_CONTROL_ALLOW_ORIGIN;`).
*/
private predicate isAllowOriginHeaderName(Expr e) {
e.(StringLiteralExpr).getTextValue().toLowerCase() = "\"access-control-allow-origin\""
@@ -121,6 +131,8 @@ private predicate isAllowOriginHeaderName(Expr e) {
or
isAllowOriginHeaderName(e.(RefExpr).getExpr())
or
+ isAllowOriginHeaderName(getAReachingValue(e))
+ or
exists(CallExpr c | c = e |
isPathCall(c, "HeaderName", ["from_static", "from_lowercase", "from_bytes", "from_str"]) and
isAllowOriginHeaderName(c.getArgList().getArg(0))
diff --git a/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
index d6c90c55..56ad4737 100644
--- a/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
+++ b/rust/test/audit/CWE-942/PermissiveCors/PermissiveCors.expected
@@ -16,7 +16,9 @@
| main.rs:216:9:219:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
| main.rs:225:9:228:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
| main.rs:235:9:238:9 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:301:28:301:63 | TupleExpr | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:314:23:314:56 | ... .allow_any_origin() | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:315:34:315:51 | ...::permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
-| main.rs:339:30:339:50 | ...::all(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:294:9:294:59 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:302:9:302:59 | headers.insert(...) | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:326:28:326:63 | TupleExpr | This Access-Control-Allow-Origin header allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:339:23:339:56 | ... .allow_any_origin() | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:340:34:340:51 | ...::permissive(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
+| main.rs:364:30:364:50 | ...::all(...) | This CORS policy allows any origin. Review whether the responses of this service contain sensitive or credentialed data. |
diff --git a/rust/test/audit/CWE-942/PermissiveCors/main.rs b/rust/test/audit/CWE-942/PermissiveCors/main.rs
index c538c8f6..6c2b270a 100644
--- a/rust/test/audit/CWE-942/PermissiveCors/main.rs
+++ b/rust/test/audit/CWE-942/PermissiveCors/main.rs
@@ -286,6 +286,31 @@ mod tower_http_cors {
);
(headers, "Hello")
}
+
+ // A header name that is held in a local binding is still the CORS header.
+ pub async fn bad_raw_header_name_alias() -> impl IntoResponse {
+ let name = header::ACCESS_CONTROL_ALLOW_ORIGIN;
+ let mut headers = HeaderMap::new();
+ headers.insert(name, HeaderValue::from_static("*")); // $ Alert
+ (headers, "Hello")
+ }
+
+ // The same, for a name that is built from a string literal.
+ pub async fn bad_raw_header_name_constructor_alias() -> impl IntoResponse {
+ let name = header::HeaderName::from_static("access-control-allow-origin");
+ let mut headers = HeaderMap::new();
+ headers.insert(name, HeaderValue::from_static("*")); // $ Alert
+ (headers, "Hello")
+ }
+
+ // The name is replaced by another header before it is used.
+ pub async fn good_raw_header_name_alias_reassigned() -> impl IntoResponse {
+ let mut name = header::ACCESS_CONTROL_ALLOW_ORIGIN;
+ name = header::ACCESS_CONTROL_ALLOW_METHODS;
+ let mut headers = HeaderMap::new();
+ headers.insert(name, HeaderValue::from_static("*"));
+ (headers, "Hello")
+ }
}
mod actix_cors_example {