Hallucination Attacks in Adonisjs with Mutual Tls
Hallucination Attacks in Adonisjs with Mutual Tls
A hallucination attack in AdonisJS occurs when an LLM or AI-assisted component produces fabricated, misleading, or overly confident outputs—such as fake API keys, invented user data, or nonexistent file paths—that are then consumed by application logic. When mutual TLS (mTLS) is in use, the risk profile shifts in subtle ways. mTLS ensures both client and server present valid certificates, which strengthens transport-layer identity and access control. However, it does not sanitize or validate the semantic correctness of data exchanged over the encrypted channel. If an AdonisJS route relies on LLM-generated content—such as dynamic documentation, code suggestions, or request enrichment—and that content is not independently verified, an attacker can indirectly exploit the LLM’s hallucinations through the authenticated mTLS channel.
Consider an AdonisJS service that uses mTLS for strict client authentication and then forwards the authenticated client’s request to an LLM for natural-language query processing. The mTLS layer confirms the client is who it claims to be, but the LLM may hallucinate instructions or data elements (e.g., inventing a userId or claiming a non-existent endpoint is available). If the server trusts the LLM output without validation, it may execute unsafe actions—such as constructing file paths, forming database queries, or invoking internal APIs—based on those hallucinated elements. This becomes a security boundary issue: mTLS protects identity, but it does not prevent logic flaws induced by untrusted AI outputs. Attackers can chain authentication with prompt manipulation to cause the LLM to generate specific hallucinated content, leading to unauthorized file access, injection-like behavior, or privacy leaks within the authenticated session.
In an API security context, hallucination attacks intersect with LLM/AI Security checks supported by tools like middleBrick. For example, active prompt injection probes (system prompt extraction, instruction override, DAN jailbreak, data exfiltration, cost exploitation) can reveal whether an AdonisJS endpoint that uses mTLS improperly trusts LLM responses. If the LLM can be nudged to output sensitive context or if its generated content bypasses authorization checks, the scan will flag issues such as Unsafe Consumption and Property Authorization. Even with mTLS ensuring only authenticated clients speak to the server, unchecked hallucinated content can still result in Insecure Direct Object References (IDOR) or BOLA if object identifiers are fabricated yet trusted. MiddleBrick’s LLM/AI Security capabilities highlight these risks by testing how models behave under adversarial prompts and by scanning outputs for PII, keys, or executable code that should never appear in LLM responses.
Mutual Tls-Specific Remediation in Adonisjs
Remediation focuses on two layers: securing mTLS configuration in AdonisJS and hardening the application’s consumption of LLM outputs. For mTLS, ensure your HTTPS server is configured to require and validate client certificates, and that certificate validation is strict (no fallback to unauthenticated behavior). For LLM integration, treat all model-generated content as untrusted input and apply strict validation, schema enforcement, and context isolation.
Below are concrete, syntactically correct AdonisJS examples that demonstrate mTLS setup and safe patterns for using LLM outputs.
Mutual TLS Server Configuration in AdonisJS
Configure the AdonisJS server to require client certificates and validate them against a trusted Certificate Authority (CA). This ensures only authorized clients can establish TLS sessions.
// start/server.ts
import { defineConfig } from '@adonisjs/core/app'
export default defineConfig({
https: {
enabled: true,
cert: '/path/to/server-cert.pem',
key: '/path/to/server-key.pem',
ca: '/path/to/ca-chain.pem',
requestCert: true,
rejectUnauthorized: true,
},
})
In this configuration:
requestCert: truetells the TLS layer to request a client certificate.rejectUnauthorized: trueensures the server rejects connections if the client certificate is invalid, expired, or not signed by the trusted CA.capoints to the bundle of trusted root/intermediate CAs used to verify client certs.
Validating LLM Output Before Use
Never use raw LLM output in security-sensitive operations. Wrap LLM calls with parsers and validators that enforce strict schemas. Below is an example using zod to validate a generated user identifier before any lookup.
import { z } from 'zod'
const userIdSchema = z.object({
userId: z.string().uuid(), // enforce UUID format
})
async function safeGetUser(llmResponse: string) {
const parsed = userIdSchema.safeParse(JSON.parse(llmResponse))
if (!parsed.success) {
throw new Error('Invalid or hallucinated userId from LLM')
}
// Proceed with trusted, validated userId
const user = await User.findBy('id', parsed.data.userId)
if (!user) {
throw new Error('User not found')
}
return user
}
This approach ensures that even if the LLM hallucinates a user ID, the application will reject malformed or unexpected values before any data access occurs.
Principle of Least Privilege for LLM-Facing Endpoints
Isolate endpoints that consume LLM output behind additional authorization checks and avoid using LLM output to make authorization decisions directly. For example, if an LLM suggests a resource ID, verify ownership or permissions against the authenticated identity rather than trusting the ID alone.
// Example route in a controller
public async show({ params, auth }: HttpContextContract) {
const { id } = params
const userId = auth.user?.id
// Validate id format independently
const validatedId = validateResourceId(id)
if (!validatedId) {
return Response.badRequest('Invalid resource identifier')
}
// Enforce ownership or RBAC checks
const resource = await Resource.findByOrFail('id', validatedId)
if (resource.userId !== userId) {
throw new Error('Unauthorized') // or handle accordingly
}
return resource
}
By combining mTLS for strong client authentication with rigorous validation of LLM-generated content and explicit authorization checks, you reduce the attack surface introduced by hallucination attacks in AdonisJS applications.
Related CWEs: llmSecurity
| CWE ID | Name | Severity |
|---|---|---|
| CWE-754 | Improper Check for Unusual or Exceptional Conditions | MEDIUM |