Cors Wildcard in Actix with Cockroachdb
Cors Wildcard in Actix with Cockroachdb — how this specific combination creates or exposes the vulnerability
A permissive CORS policy in an Actix-web service that proxies or directly interfaces with CockroachDB can amplify data exposure and authorization risks. When the server uses a wildcard Access-Control-Allow-Origin: * while handling database operations against CockroachDB, the browser enforces relaxed origin checks but the server-side logic must still enforce strict ownership and permissions. If Actix routes rely only on CORS for access control, an attacker can make authenticated requests from an arbitrary origin, and if route handlers do not validate the requesting user against CockroachDB row-level permissions, the request may succeed with elevated or unintended access.
In this combination, the risk typically materializes as an insecure direct object reference (IDOR) or privilege escalation via CORS: a frontend served from any origin can call endpoints that return or modify records belonging to other users, because CORS does not enforce business-level authorization. CockroachDB’s role-based access controls and database-side policies must therefore complement Actix middleware; a wildcard origin without corresponding database ownership checks means the API effectively trusts the caller once authenticated at the HTTP level. Attackers probe origins and endpoints to exfiltrate or modify data, and middleBrick’s CORS and BOLA/IDOR checks would flag the missing origin validation and missing row ownership enforcement.
Moreover, wildcard CORS can expose error details or schema information when combined with CockroachDB errors leaking table or column names. If Actix returns verbose database errors to any origin, an attacker can iterate queries to infer schema details. Proper remediation involves tightening CORS to specific origins and ensuring Actix handlers validate requests against CockroachDB grants and tenant context, so API responses remain consistent across origins and sensitive metadata is not leaked.
Cockroachdb-Specific Remediation in Actix — concrete code fixes
Remediate by combining strict CORS configuration in Actix with explicit row ownership checks against CockroachDB. Do not rely on wildcard origins for authenticated endpoints. Use middleware to verify the authenticated principal against database-level tenant or user identifiers before constructing SQL.
use actix_cors::Cors; use actix_web::{web, App, HttpResponse, HttpServer, Responder}; use cockroachdb_rs::Client; // hypothetical client
async fn get_user_data(
db: web::Data,
req: actix_web::HttpRequest,
) -> impl Responder {
let origin = req.headers().get("origin");
// Enforce strict origin allowlist instead of wildcard
if let Some(o) = origin {
if o != "https://app.example.com" {
return HttpResponse::Forbidden().body("CORS origin not allowed");
}
}
// Authenticated user ID from your auth extractor
let user_id = match req.extensions().get::() {
Some(id) => *id,
None => return HttpResponse::Unauthorized().finish(),
};
// Cockroachdb row-level ownership check
let row: (String,) = db
.query_one(
"SELECT data FROM user_data WHERE user_id = $1 AND tenant_id = (SELECT tenant_id FROM tenants WHERE owner_id = $1)",
&[&user_id],
)
.await
.map_err(|e| HttpResponse::InternalServerError().body(e.to_string()))?;
HttpResponse::Ok().body(row.0)
}
// Configure Cors with specific origins
let cors = Cors::default()
.allowed_origin("https://app.example.com")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec!["Authorization", "Content-Type"])
.max_age(3600);
HttpServer::new(move || {
App::new()
.wrap(cors.clone())
.app_data(web::Data::new(db_client.clone()))
.route("/api/data", web::get().to(get_user_data))
})
On the CockroachDB side, enforce role-based access and tenant isolation with least-privilege users per service account. Avoid granting broad SELECT on sensitive tables; instead use row-level security via tenant_id or organization_id columns, verified in the application before query construction.
-- CockroachDB setup: ensure roles and grants match Actix service identity
CREATE ROACTIX_SERVICE WITH LOGIN PASSWORD 'service-password';
GRANT SELECT ON TABLE user_data TO ROACTIX_SERVICE;
-- Row-level policy: users can only see their own tenant data
CREATE POLICY tenant_isolation ON user_data
USING (tenant_id = current_setting('app.tenant_id')::UUID);
SET app.tenant_id = 'tenant-uuid-here';
Combine these database policies with Actix middleware that sets the tenant context (e.g., via session or JWT claims) and validates per request. middleBrick’s scans would highlight missing origin validation and insufficient row-level checks, guiding you to tighten CORS and add ownership predicates in SQL.
Related CWEs: dataExposure
| CWE ID | Name | Severity |
|---|---|---|
| CWE-200 | Exposure of Sensitive Information | HIGH |
| CWE-209 | Error Information Disclosure | MEDIUM |
| CWE-213 | Exposure of Sensitive Information Due to Incompatible Policies | HIGH |
| CWE-215 | Insertion of Sensitive Information Into Debugging Code | MEDIUM |
| CWE-312 | Cleartext Storage of Sensitive Information | HIGH |
| CWE-359 | Exposure of Private Personal Information (PII) | HIGH |
| CWE-522 | Insufficiently Protected Credentials | CRITICAL |
| CWE-532 | Insertion of Sensitive Information into Log File | MEDIUM |
| CWE-538 | Insertion of Sensitive Information into Externally-Accessible File | HIGH |
| CWE-540 | Inclusion of Sensitive Information in Source Code | HIGH |