HIGH jwt misconfigurationchiapi keys

Jwt Misconfiguration in Chi with Api Keys

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

Chi is a minimal, functional web framework for Clojure. When developers use Chi routes together with API key based authentication, misconfigurations can unintentionally expose protected endpoints or weaken authorization boundaries. A common pattern is to guard a route with an API key check but then forward the request into a Chi handler that also inspects the Authorization header for a JWT. If the API key validation is incomplete or the JWT verification is permissive, an attacker can bypass intended access controls.

For example, consider a route defined as /admin that first checks for a valid API key in a header. If the check only verifies presence and not the intended audience or scope, and the downstream Chi handler still parses and trusts a JWT when present, an attacker may supply a malformed or unsigned JWT that passes loose validation. This can lead to JWT Misconfiguration where the API key does not effectively gate the endpoint, and the JWT path introduces risks such as algorithm confusion (e.g., expecting HS256 but accepting none) or weak claims validation.

Another scenario involves logging or error handling within Chi middleware. If JWTs are parsed and their payloads are logged or reflected in error messages, API keys combined with verbose errors can disclose sensitive information. Additionally, if the API key is passed as a bearer-like token in headers and JWT parsing is overly broad, it may enable information leakage or permit unauthenticated access to endpoints that should require both factors. These patterns illustrate how Jwt Misconfiguration intersects with Api Keys in Chi to expand the unauthenticated attack surface that middleBrick scans for as part of its black-box assessment.

Api Keys-Specific Remediation in Chi — concrete code fixes

To secure Chi routes that rely on API keys and optionally JWTs, enforce strict validation and avoid mixing trust boundaries. Define a small middleware that validates the API key against a known set of values before allowing the request to proceed into the Chi handler chain. Do not rely on route ordering alone; ensure the check is explicit and fails closed.

Example of correct API key validation in Chi:

(ns myapp.core
  (:require [compojure.core :refer [defroutes GET]]
            [ring.util.response :as resp]))

(def valid-api-keys #{"sk_live_abc123" "sk_test_xyz789"})

(defn require-api-key
  [handler]
  (fn [request]
    (if (contains? valid-api-keys (get-in request [:headers "x-api-key"]))
      (handler request)
      (resp/response-forbidden "Invalid API key"))))

(defn admin-handler [request]
  (resp/json-response {:status "admin endpoint protected"}))

(defroutes app-routes
  (GET "/admin" [] (admin-handler))
  (route/not-found "Not found"))

(def app
  (-> app-routes
      (require-api-key)))

If you must also handle JWTs, validate them separately and do not derive access control decisions from the API key alone. Validate JWT signatures, audiences, issuers, and expiration rigorously using a trusted library, and avoid accepting alg: none or overly permissive claims. Do not log raw JWTs or API keys, and ensure error messages do not reveal which component failed.

Example of strict JWT verification alongside API key checks:

(ns myapp.auth
  (:require [buddy.jwt :as jwt]
            [cheshire.core :as json]))

(defn verify-jwt
  [token secret]
  (try
    (jwt/unsign token secret {:alg :hs256})
    (catch Exception _ nil)))

(defn require-jwt-or-api-key
  [handler]
  (fn [request]
    (let [api-key (get-in request [:headers "x-api-key"])
          auth-header (get-in request [:headers "authorization"])
          jwt-token (when auth-header (re-find #"Bearer\s+(.*)" auth-header))]
      (if (contains? #{"special-key"} api-key)
        (handler request)
        (if-claims-valid jwt-token secret]
          (handler request)
          (resp/response-forbidden "Unauthorized"))))))

Use these patterns to reduce Jwt Misconfiguration risks when API keys and JWTs coexist. Combine with continuous scanning so that changes to routing or authentication logic are automatically assessed for security regressions.

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

Can JWT Misconfiguration in Chi with Api Keys lead to privilege escalation?
Yes. If API key checks are weak and JWT validation is permissive, an attacker may bypass intended authorization boundaries and escalate privileges.
How does middleBrick detect Jwt Misconfiguration in Chi setups with Api Keys?
middleBrick performs black-box scans including authentication checks, input validation, and authentication boundary analysis to identify weak API key usage and JWT handling flaws.