Open Redirect in Axum with Jwt Tokens
Open Redirect in Axum with Jwt Tokens — how this specific combination creates or exposes the vulnerability
An open redirect in an Axum application that uses JWT tokens can occur when a redirect target is derived from user-controlled data, such as a query parameter, while JWT tokens are used for session or identity handling. If the application validates authentication via JWT but then redirects to a URL constructed from unchecked input, an attacker can supply a malicious destination. For example, an endpoint that accepts a next parameter after verifying a JWT access token may redirect to that value without strict allowlisting. Because the JWT confirms the user is authenticated, the application may trust the redirect implicitly, violating the same-origin principle.
In Axum, handlers often chain extractors like Query<NextUrl> and State containing JWT validation logic. If the JWT verification passes but the handler uses an unchecked redirect target, the token’s presence can create a false sense of security. Attackers can craft links such as /auth/login?next=https://evil.com, authenticate via a valid JWT issued by the application, and then be redirected to the external site. This can facilitate phishing campaigns where users are redirected from a trusted domain. The vulnerability is not in JWT validation itself but in the unsafe composition of authentication and uncontrolled redirection.
Because middleBrick scans the unauthenticated attack surface, it can detect endpoints that accept redirect parameters and return a high risk score when open redirect patterns are observed alongside authentication mechanisms. The scanner checks for missing allowlists, missing referer or origin validation, and improper use of HTTP response codes like 302 that can lead to unintended navigation. Even with JWT-based protection, if the redirect target is not strictly validated, the application remains susceptible to client-side attacks that can erode user trust.
Jwt Tokens-Specific Remediation in Axum — concrete code fixes
To remediate open redirect risks in Axum when using JWT tokens, ensure that redirect targets are never directly taken from user input. Instead, use allowlisted paths or a mapping of safe identifiers. Validate the redirect destination before issuing any response, and avoid using HTTP 302/303 with untrusted URLs. Below are concrete Axum examples that demonstrate secure handling.
- Secure allowlist-based redirect after JWT verification:
use axum::{routing::get, Router, extract::Query, http::StatusCode};
use serde::Deserialize;
#[derive(Deserialize)]
struct RedirectQuery {
next: Option,
}
async fn login_handler(
Query(params): Query,
) -> Result {
// Assume JWT validation happened earlier in a layer or middleware
let allowed_hosts = ["app.example.com", "dashboard.example.com"];
let default = "/dashboard";
let target = params.next.as_deref().filter(|url| {
url::Url::parse(url)
.ok()
.and_then(|u| u.host_str())
.map(|host| allowed_hosts.contains(&host.as_ref()))
.unwrap_or(false)
}).unwrap_or(default);
// Perform login and JWT issuance here
Ok((StatusCode::SEE_OTHER, [("location", target)]))
}
- Using a path-mapped redirect without external URLs:
async fn safe_redirect() -> impl IntoResponse {
// No user input used; fixed destination after JWT-based session creation
(StatusCode::FOUND, [("location", "/app/home")])
}
- Rejecting redirect parameters entirely when JWT is present:
async fn strict_login() -> Result {
// JWT validated, but any 'next' parameter is ignored
Ok((StatusCode::SEE_OTHER, [("location", "/app/home")]))
}
These examples ensure that even when JWT tokens indicate a verified identity, the application does not inadvertently facilitate open redirects. By decoupling authentication from destination selection and enforcing strict allowlists, you maintain security boundaries that JWT validation alone does not provide.
FAQ
- Can a valid JWT token make an open redirect less severe?
No. A valid JWT indicates authenticated identity but does not mitigate open redirect risks. Attackers can still leverage the redirect to phish users who trust the originating domain. Authentication and redirection must be evaluated independently.
- How does middleBrick detect open redirect risks in Axum APIs?
middleBrick scans endpoints that issue redirects and checks whether the target is derived from unvalidated user input. When JWT-based authentication is present but redirect destinations are not allowlisted, the scanner reports a high-risk finding with guidance to use allowlists or remove external redirect parameters.