HIGH jwt misconfigurationactixapi keys

Jwt Misconfiguration in Actix with Api Keys

Jwt Misconfiguration in Actix with Api Keys — how this specific combination creates or exposes the vulnerability

JWT misconfiguration in Actix applications that also rely on API keys can unintentionally weaken authentication and authorization boundaries. When JWT validation is incomplete or overly permissive while API keys are used as an additional gate, mismatched trust boundaries may allow access to protected endpoints without proper authorization.

In Actix, a common pattern is to use middleware that extracts and validates JWTs, but if the validator is configured to accept tokens without strict signature verification, relaxed algorithms (e.g., none), or missing audience/issuer checks, an attacker can supply a crafted JWT that bypasses intended checks. Meanwhile, API keys are often passed in headers (e.g., x-api-key). If the application checks for the presence of an API key before or independently of JWT validation, and if the JWT path is misconfigured to trust unverified claims, an attacker can leverage a valid API key while supplying a malicious JWT to escalate privileges or access another user’s data.

For example, consider an Actix router that applies both JWT and API key checks but incorrectly allows requests to proceed if either credential is valid. This can lead to horizontal or vertical privilege escalation (BOLA/IDOR) where one factor’s weakness undermines the other. An attacker with a valid API key might exploit a JWT misconfiguration—such as missing alg validation—to impersonate higher-privileged users, access admin routes, or manipulate ID-based resources (BOLA/IDOR). This is compounded when token claims are not strictly enforced against an authorization model, enabling unsafe consumption of permissions encoded in the JWT.

Another risk specific to the combination involves unsafe handling of token metadata. If Actix routes do not enforce strict issuer, audience, or expiration validation on JWTs while also using API keys for routing or rate limiting, an attacker can exploit timing differences or missing checks to infer valid identities or escalate via malformed tokens. Such misconfiguration maps to OWASP API Top 10 controls around authentication and authorization failures, and may intersect with compliance requirements in PCI-DSS and SOC2 that demand strict access controls. Because middleBrick tests authentication, BOLA/IDOR, and Property Authorization in parallel, it can surface these compound risks by correlating unauthenticated or weakly authenticated JWT paths with API key usage patterns to produce prioritized findings and remediation guidance.

Api Keys-Specific Remediation in Actix — concrete code fixes

To securely combine JWT validation and API keys in Actix, enforce strict validation for both and ensure they serve distinct, non-overlapping purposes. Prefer using API keys for service-to-service identification and JWTs for user identity and scopes, with clear authorization checks on each. Below are concrete code examples that demonstrate secure handling in Actix.

First, validate JWTs with explicit algorithms and claims before proceeding to API key checks. Use a dedicated extractor that fails the request if the token is invalid, and avoid accepting none algorithms.

use actix_web::{web, App, HttpServer, HttpResponse, Error};
use actix_web_httpauth::extractors::bearer::BearerAuth;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation, TokenData};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Claims {
    sub: String,
    scope: String,
    exp: usize,
    iss: String,
    aud: String,
}

async fn validate_jwt(auth: BearerAuth) -> Result, Error> {
    let token = auth.token();
    let validation = Validation::new(Algorithm::HS256);
    let token_data = decode::(
        token,
        &DecodingKey::from_secret("your-256-bit-secret".as_ref()),
        &validation,
    )?;
    // additional checks: token_data.claims.iss, .aud, exp handled by validation
    Ok(token_data)
}

// A separate extractor or middleware for API key validation
async fn validate_api_key(req: actix_web::HttpRequest) -> Result {
    match req.headers().get("x-api-key") {
        Some(key_header) => {
            let key = key_header.to_str().unwrap_or("");
            if key == std::env::var("EXPECTED_API_KEY").unwrap_or_default() {
                Ok(key.to_string())
            } else {
                Err(actix_web::error::ErrorUnauthorized("Invalid API key"))
            }
        }
        None => Err(actix_web::error::ErrorUnauthorized("Missing API key")),
    }
}

// Example secured route requiring both valid JWT and correct API key
async fn secure_handler(jwt: web::ReqData>, api_key: web::ReqData) -> HttpResponse {
    // business logic here, using jwt.claims.sub for user identity and api_key for service routing
    HttpResponse::Ok().body("Authorized")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .app_data(web::Data::new(validate_jwt))
            .app_data(web::Data::new(validate_api_key))
            .route("/admin", web::get().to(secure_handler))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

This approach ensures JWTs are verified with a specific algorithm and claims checks, while API keys are validated independently and not used to implicitly trust JWTs. In the Pro plan, continuous monitoring can alert you if runtime responses indicate that either factor is being accepted in an unsafe manner, and the GitHub Action can fail builds if risk scores degrade due to permissiveness in authentication logic. For rapid verification before deployment, use the CLI tool by running middlebrick scan <url> to detect authentication and authorization weaknesses without setting up agents or credentials.

Related CWEs: authentication

CWE IDNameSeverity
CWE-287Improper Authentication CRITICAL
CWE-306Missing Authentication for Critical Function CRITICAL
CWE-307Brute Force HIGH
CWE-308Single-Factor Authentication MEDIUM
CWE-309Use of Password System for Primary Authentication MEDIUM
CWE-347Improper Verification of Cryptographic Signature HIGH
CWE-384Session Fixation HIGH
CWE-521Weak Password Requirements MEDIUM
CWE-613Insufficient Session Expiration MEDIUM
CWE-640Weak Password Recovery HIGH

Frequently Asked Questions

How does middleBrick detect JWT misconfigurations combined with API key usage?
middleBrick runs parallel checks including Authentication, BOLA/IDOR, and Property Authorization. It correlates unauthenticated or weakly authenticated JWT validation paths with API key presence patterns to identify cases where one factor’s weakness could allow unauthorized access. Findings include severity, remediation guidance, and mapping to frameworks like OWASP API Top 10.
Can middleBrick scan APIs that use both JWTs and API keys without credentials?
Yes. middleBrick performs black-box scanning of the unauthenticated attack surface, so you can submit a URL and receive a security risk score and findings in 5–15 seconds without providing credentials, agents, or configuration.