HIGH jwt misconfigurationadonisjsbasic auth

Jwt Misconfiguration in Adonisjs with Basic Auth

Jwt Misconfiguration in Adonisjs with Basic Auth — how this specific combination creates or exposes the vulnerability

When AdonisJS applications rely solely on Basic Authentication while also supporting or misusing JSON Web Tokens (JWT), misconfigurations can expose authentication bypass or token confusion risks. Basic Auth transmits credentials as a base64-encoded string over the wire; without TLS, these credentials are easily intercepted. Even with TLS, if the application treats a missing or malformed JWT as a fallback to Basic Auth without strict validation, it may inadvertently accept weak or missing token claims as proof of authentication.

One common pattern is using AdonisJS guards that support both JWT and session-based auth. If route-level protections are not consistently applied, an unauthenticated attacker can access endpoints that should require a valid JWT by omitting the Authorization header or by sending only Basic Auth credentials. Because JWT validation may be skipped when Basic Auth succeeds, the application might treat the request as authenticated without verifying scopes, roles, or token expiry. This can lead to privilege escalation or unauthorized data access, especially if the JWT claims are not verified when Basic Auth is present.

Another risk arises from inconsistent token handling: if the application issues JWTs with overly permissive scopes or long lifetimes while also accepting Basic Auth for certain routes, an attacker who compromises a basic credential pair can use it to obtain or forge JWTs with elevated permissions. Additionally, if the application does not enforce strict audience (aud) or issuer (iss) validation on JWTs when Basic Auth is used as a fallback, tokens issued for other services might be accepted in this context, leading to authentication confusion across microservices.

Middleware misconfigurations exacerbate this issue. For example, if the JWT verification middleware is placed after the Basic Auth middleware and does not short-circuit the request when Basic Auth succeeds, the application may proceed without enforcing JWT-specific checks such as token revocation or nonce validation. This can allow requests with only Basic Auth to bypass intended JWT-only protections, violating the principle of defense in depth and increasing the attack surface for token-related attacks.

Basic Auth-Specific Remediation in Adonisjs — concrete code fixes

To secure AdonisJS applications that support Basic Auth, enforce strict separation between authentication mechanisms and avoid implicit fallbacks to JWT. Always require explicit authentication for each route and validate credentials using AdonisJS built-in guards with strong password hashing. Below are concrete code examples demonstrating secure Basic Auth implementation.

First, define a dedicated Basic Auth guard in config/auth.ts that uses clear‑text password comparison only for testing or legacy support; in production, prefer token-based flows.

import { AuthenticationGuardConfig } from '@ioc:Adonis/Addons/Auth'

const basicAuthConfig: AuthenticationGuardConfig = {
  guardName: 'basic',
  providers: {
    userProvider: {
      driver: 'hash',
      async hash (user) {
        return user.password
      },
      async verify (user, password) {
        // Use constant-time comparison in production
        return user.password === password
      },
    },
  },
}

export default basicAuthConfig

Next, apply route-level protection using the auth middleware and explicitly specify the guard. This ensures that requests without valid Basic Auth credentials are rejected before reaching business logic.

import Route from '@ioc:Adonis/Core/Route'

Route.get('/admin/users', async ({ auth }) => {
  await auth.use('basic').authenticate()
  const user = auth.getUserOrFail()
  return { user: user.id, role: user.role }
}).middleware(['auth'])

To prevent token confusion, disable JWT issuance or validation for routes using Basic Auth. If JWT is required elsewhere, ensure that guards are isolated and that JWT validation middleware does not treat Basic Auth as a valid substitute. For example, configure the JWT guard to reject requests that lack an Authorization header formatted as Bearer <token>.

Finally, enforce HTTPS for all endpoints that use Basic Auth. Combine this with short credential lifetimes and rotate secrets regularly. Use AdonisJS hooks to log authentication attempts and monitor for repeated failures, which may indicate credential stuffing or brute-force attacks targeting Basic Auth endpoints.

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 Basic Auth be used safely in AdonisJS if HTTPS is enforced?
Yes, but only for low-risk internal endpoints. Always enforce HTTPS, avoid fallback to JWT, and use dedicated guards with strong password hashing. Prefer token-based authentication for broader protection.
How does JWT misconfiguration with Basic Auth lead to privilege escalation?
If the application accepts Basic Auth as a valid authentication state without verifying JWT claims, an attacker can use stolen basic credentials to access routes that should require specific JWT scopes or roles, bypassing intended authorization checks.