Hallucination Attacks in Adonisjs with Basic Auth
Hallucination Attacks in Adonisjs with Basic Auth — how this specific combination creates or exposes the vulnerability
Hallucination attacks in AdonisJS when Basic Auth is used occur when an LLM endpoint or AI-assisted tooling produces fabricated outputs that appear authoritative but are not grounded in the actual API contract or runtime behavior. Because AdonisJS often exposes HTTP APIs with route files under start/routes.ts or routes.ts, and Basic Auth can be implemented via HTTP header checks in middleware, an attacker can probe the unauthenticated surface or misuse an LLM integration to generate plausible but incorrect route paths, parameters, or auth expectations.
In this context, "Basic Auth" typically means a custom header or middleware that reads an Authorization: Basic base64(credentials) header and validates it before allowing access. If route definitions or controller logic do not consistently enforce auth, or if an LLM endpoint is exposed without authentication, hallucination attacks can exploit the mismatch between documented behavior (such as an OpenAPI spec) and runtime implementation. For example, an attacker using an LLM might be shown a generated curl that includes Basic Auth headers, but the actual AdonisJS route does not validate the header correctly, allowing unauthorized access or information disclosure.
Because middleBrick scans the unauthenticated attack surface and includes an LLM/AI Security check, it can detect whether an endpoint is unauthenticated, whether system prompt leakage occurs, and whether active prompt injection probes (system prompt extraction, instruction override, DAN jailbreak, data exfiltration, cost exploitation) succeed. When combined with AdonisJS routes that rely on Basic Auth without strict validation, this can reveal inconsistencies where the API spec claims protection but the implementation does not enforce it, enabling hallucination-based social engineering or unauthorized data access.
An example risk pattern: an OpenAPI spec generated for an AdonisJS service includes Basic Auth security schemes, but a route omits the auth guard. A developer using an LLM might hallucinate that the route is protected and craft requests accordingly, while an attacker uses the same hallucination to infer correct endpoint shapes and test unauthenticated access. middleBrick’s cross-referencing of spec definitions with runtime findings helps highlight such gaps by comparing the spec’s required security requirements against actual responses.
To mitigate these risks specifically for AdonisJS + Basic Auth, ensure that every route that should be protected explicitly applies the auth middleware, that credentials are verified against a secure source on every request, and that no LLM endpoint is exposed without authentication. Do not rely on documentation or generated examples alone; validate runtime behavior using scanners that perform active testing without requiring credentials, such as middleBrick’s unauthenticated scan, which checks authentication controls and LLM security probes independently of your codebase.
Basic Auth-Specific Remediation in Adonisjs — concrete code fixes
Remediate Basic Auth issues in AdonisJS by enforcing auth checks on all sensitive routes, validating credentials on each request, and avoiding reliance on route-level assumptions. Below are concrete, working examples aligned with AdonisJS conventions.
1. HTTP Basic Auth via middleware
Create a reusable middleware that parses and validates Basic Auth credentials. Place this file at app/Middleware/BasicAuth.ts.
import { Exception } from '@poppinss/utils'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
export default class BasicAuth {
public async handle({ request, response, session }: HttpContextContract, next: () => Promise) {
const header = request.header('authorization')
if (!header || !header.startsWith('Basic ')) {
response.unauthorized()
return
}
const base64 = header.split(' ')[1]
const decoded = Buffer.from(base64, 'base64').toString('utf-8')
const [username, password] = decoded.split(':')
// Replace with secure credential lookup, e.g., via database or env vars
const validUsername = process.env.BASIC_AUTH_USER
const validPassword = process.env.BASIC_AUTH_PASS
if (!validUsername || !validPassword || username !== validUsername || password !== validPassword) {
response.unauthorized()
return
}
// Optionally attach user to request for downstream handlers
request.authAttempt = { username }
await next()
}
}
2. Register the middleware
Add the middleware to start/kernel.ts under middleware.ts or the HTTP kernel, and map it to a route or group.
import { HttpServer } from '@poppinss/utils'
import Server from '@ioc:Adonis/Core/Server'
import Route from '@ioc:Adonis/Core/Route'
Server.registerMiddlewares([
() => import('#middlewares/basic_auth')
])
3. Apply to specific routes
Protect routes in start/routes.ts by referencing the middleware. This ensures that every request to the endpoint requires valid Basic Auth credentials.
import Route from '@ioc:Adonis/Core/Route'
import BasicAuth from 'App/Middleware/BasicAuth'
Route.get('/admin/settings', 'SettingsController.show').middleware(BasicAuth)
Route.group(() => {
Route.get('/users', 'UsersController.index').middleware(BasicAuth)
Route.post('/users', 'UsersController.store').middleware(BasicAuth)
}).prefix('api/v1')
4. Secure credential storage and validation
Do not hardcode credentials in environment files in production without additional protections. For stronger security, validate credentials against a database or a secure vault and rotate secrets regularly. Combine Basic Auth with HTTPS to prevent credential interception; middleBrick’s encryption checks can verify that endpoints are served over TLS.
After applying these fixes, re-run middleBrick’s unauthenticated scan to confirm that the endpoints now require authentication and that no LLM-related hallucination vectors remain exploitable. The CLI command middlebrick scan <url> can be used to validate remediation without requiring API keys or agents.
Related CWEs: llmSecurity
| CWE ID | Name | Severity |
|---|---|---|
| CWE-754 | Improper Check for Unusual or Exceptional Conditions | MEDIUM |