HIGH missing authenticationadonisjsapi keys

Missing Authentication in Adonisjs with Api Keys

Missing Authentication in Adonisjs with Api Keys — how this specific combination creates or exposes the vulnerability

In AdonisJS, APIs often rely on API keys as a lightweight authentication mechanism. A missing or improperly enforced API key check creates a Missing Authentication vulnerability, allowing unauthenticated attackers to invoke endpoints that should be restricted. When API keys are accepted but not validated on every request, or when the validation logic is bypassed through route grouping or middleware ordering, the attack surface expands significantly.

Consider an AdonisJS route that reads an API key from a custom header and uses it to scope data access. If the route handler does not consistently verify the key against a trusted source (for example, a database or environment-based allowlist), an attacker can call the endpoint without providing the header or by supplying any arbitrary string. This is a common misconfiguration in services that expose CRUD operations over REST or GraphQL, and it can lead to BOLA/IDOR or excessive data exposure when combined with weak authorization checks.

Real-world patterns that increase risk include:

  • Deferring authentication to a later middleware stage while earlier routes remain publicly accessible.
  • Using route-level middleware that applies only to specific HTTP methods, leaving GET endpoints unprotected while POST endpoints are secured.
  • Accepting API keys in query parameters or headers but failing to reject requests when the key is missing, malformed, or expired.

During a black-box scan, such as one performed by middleBrick, these issues are detected by sending unauthenticated requests to sensitive endpoints and observing whether access is granted. For example, a request to GET /api/v1/admin/users without an API key that returns a 200 status and a full dataset indicates a Missing Authentication flaw. middleBrick also cross-references these runtime findings with OpenAPI/Swagger specs (2.0, 3.0, 3.1), resolving $ref pointers to confirm whether security schemes are defined but not enforced.

Because AdonisJS applications can integrate multiple authentication schemes (session-based web auth, JWT, and API keys), developers must ensure that API key validation is explicit, centralized, and applied consistently across all routes. Without this, the API behaves as if authentication is optional, which maps directly to the OWASP API Top 10 category of Broken Object Level Authorization and Authentication.

Api Keys-Specific Remediation in Adonisjs — concrete code fixes

To remediate Missing Authentication when using API keys in AdonisJS, implement a dedicated middleware that validates the key on every request and rejects invalid or missing keys with a 401 response. Centralize the validation logic so it cannot be accidentally bypassed by route ordering or group inheritance.

Below is a concrete example of an API key middleware in AdonisJS using the HttpContext contract. This middleware reads the key from a custom header, compares it against a list of valid keys (loaded from environment variables for safety), and short-circuits the request pipeline on failure.

// start/kernel.ts
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

export default class ApiKeyMiddleware {
  public async handle({ request, response, proceed }: HttpContextContract) {
    const providedKey = request.header('x-api-key')
    const validKeys = process.env.API_KEYS?.split(',') || []

    if (!providedKey || !validKeys.includes(providedKey)) {
      return response.unauthorized({ message: 'Invalid or missing API key' })
    }

    return proceed()
  }
}

Register this middleware in start/kernel.ts and apply it globally or to specific route groups to ensure consistent enforcement:

// start/kernel.ts
import Route from '@ioc:Adonis/Core/Route'
import ApiKeyMiddleware from 'App/Middleware/ApiKey'

Route.group(() => {
  Route.get('/admin/users', 'UsersController.index').middleware('apiKey')
  Route.post('/admin/users', 'UsersController.store').middleware('apiKey')
  Route.get('/api/reports', 'ReportsController.index').middleware('apiKey')
}).prefix('api/v1')

For higher assurance, rotate keys programmatically and avoid embedding them in client-side code. Combine API key validation with rate limiting and request validation to reduce the impact of compromised keys. middleBrick’s scans can verify that your API key middleware is applied to all intended endpoints and that unauthenticated requests are correctly rejected.

When using the CLI, you can run middlebrick scan https://api.example.com to obtain a security risk score and findings that highlight routes missing authentication controls. The Pro plan supports continuous monitoring so that regressions in authentication enforcement are flagged promptly, and the GitHub Action can fail builds if new endpoints introduce Missing Authentication issues.

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

What should I do if my AdonisJS API returns 200 for requests without an API key during a scan?
Treat this as a Missing Authentication finding. Add centralized API key validation middleware, ensure it runs before route handlers, and re-scan to confirm that unauthenticated requests are rejected with 401.
Can middleware-based API key validation in AdonisJS prevent BOLA/IDOR in addition to authentication gaps?
Authentication middleware alone does not prevent BOLA/IDOR. You must pair key validation with per-request authorization that scopes resources to the caller, ensuring that one key cannot access another user’s data.