HIGH llm data leakageadonisjs

Llm Data Leakage in Adonisjs

How Llm Data Leakage Manifests in Adonisjs

Llm Data Leakage in Adonisjs applications typically occurs when AI/ML endpoints inadvertently expose sensitive system prompts, training data, or proprietary information through API responses. This vulnerability is particularly concerning in Adonisjs because the framework's flexibility and rapid development patterns can lead to insufficient output validation.

The most common manifestation involves Adonisjs controllers that serve as proxies to LLM services. Developers often create endpoints that forward user queries to OpenAI, Anthropic, or other LLM providers, then return the raw responses without proper sanitization. Consider this vulnerable Adonisjs controller:

class LlmController {
  async query({ request, response }) {
    const { prompt } = request.post()
    
    // Direct LLM call without sanitization
    const result = await openai.chat.completions.create({
      model: 'gpt-4',
      messages: [{ role: 'user', content: prompt }]
    })
    
    // Returns raw system prompt + user data
    return response.json(result.choices[0].message)
  }
}

This pattern is dangerous because LLM responses often contain concatenated system prompts, training examples, and context that wasn't intended for end users. The system prompt might include proprietary instructions, API keys, or sensitive business logic that becomes exposed through the API response.

Another Adonisjs-specific scenario involves middleware that processes AI responses. Developers might create middleware to log or transform LLM outputs, but if the middleware doesn't properly filter system prompts, the raw data flows through the application stack. The framework's middleware pipeline can inadvertently pass sensitive AI metadata to the client.

Adonisjs applications also face risks when using third-party AI SDKs or services that don't properly handle response boundaries. The framework's TypeScript support and flexible routing can lead to situations where AI service responses are treated as generic JSON, bypassing any content inspection.

Adonisjs-Specific Detection

Detecting Llm Data Leakage in Adonisjs requires a multi-layered approach that leverages the framework's built-in capabilities and external scanning tools. The first line of defense is implementing comprehensive logging and monitoring in your Adonisjs application to detect suspicious patterns in AI responses.

Adonisjs's Logger can be configured to flag responses containing common system prompt patterns. Here's a detection middleware specific to Adonisjs:

const SYSTEM_PROMPT_PATTERNS = [
  /You are a helpful AI assistant/, // Common system prompt prefix
  /Do not include/, // Typical instruction patterns
  /System:.*User:/s // ChatML format detection
]

export class LlmLeakageDetector {
  async handle({ response }, next) {
    await next()
    
    const body = response.lazyBody()
    if (body && typeof body === 'string') {
      const hasSystemPrompt = SYSTEM_PROMPT_PATTERNS.some(pattern => 
        pattern.test(body)
      )
      
      if (hasSystemPrompt) {
        Logger.warn('Potential LLM data leakage detected', {
          endpoint: response.request.url(),
          contentLength: body.length
        })
      }
    }
  }
}

For comprehensive scanning, middleBrick's LLM/AI Security checks provide specialized detection for Adonisjs applications. The scanner tests for system prompt leakage using 27 regex patterns that cover various LLM formats including ChatML, Llama 2, Mistral, and Alpaca formats. It performs active prompt injection testing with five sequential probes to extract system prompts and detect data exfiltration capabilities.

middleBrick's black-box scanning approach is particularly effective for Adonisjs applications because it tests the actual running API without requiring source code access. The scanner identifies unauthenticated LLM endpoints and tests whether they expose system prompts or sensitive training data through responses.

Additionally, Adonisjs developers should implement response validation using the framework's validator. Create schemas that specifically check for system prompt patterns in AI responses:

import { schema } from '@adonisjs/core/validator'

const llmResponseSchema = schema.create({
  content: schema.string.optional({
    escape: true,
    trim: true
  }),
  systemPrompt: schema.string.optional({
    escape: true,
    trim: true
  }),
  // Additional fields to capture AI response structure
})

Adonisjs-Specific Remediation

Remediating Llm Data Leakage in Adonisjs applications requires a defense-in-depth approach that combines framework-specific features with security best practices. The most effective strategy involves implementing response sanitization middleware that specifically targets AI service outputs.

Adonisjs's middleware system is ideal for creating a centralized LLM response filter. Here's a comprehensive remediation middleware:

import { once } = '@adonisjs/core/services'

export class LlmResponseSanitizer {
  async handle({ response }, next) {
    const originalSend = response.send.bind(response)
    
    response.send = async function (body) {
      if (body && typeof body === 'object') {
        const jsonString = JSON.stringify(body)
        
        // Remove system prompts and sensitive patterns
        const sanitized = jsonString
          .replace(/System:.*?(?=
User:)/gs, '') // Remove system prompt blocks
          .replace(/You are a helpful AI assistant.*?\n/gs, '') // Remove common prefixes
          .replace(/API keys?|secret|password/gi, '[REDACTED]') // Sensitive terms
        
        return originalSend(JSON.parse(sanitized))
      }
      
      return originalSend(body)
    }
    
    await next()
  }
}

Register this middleware globally in your Adonisjs application to ensure all responses pass through the sanitization layer. The middleware intercepts the response before it's sent to the client, removing system prompts and sensitive information while preserving the user-facing content.

For Adonisjs applications using OpenAI or similar services, implement response validation at the service layer. Create a dedicated LLM service class that handles API calls and response processing:

import OpenAI from 'openai'

export class LlmService {
  constructor() {
    this.client = new OpenAI({
      apiKey: process.env.OPENAI_API_KEY
    })
  }

  async safeQuery(prompt) {
    const response = await this.client.chat.completions.create({
      model: 'gpt-4',
      messages: [{ role: 'user', content: prompt }]
    })

    // Extract only user-relevant content
    const sanitizedContent = this.sanitizeResponse(
      response.choices[0].message.content
    )

    return {
      content: sanitizedContent,
      metadata: {
        model: response.model,
        usage: response.usage
      }
    }
  }

  sanitizeResponse(content) {
    if (!content) return ''
    
    // Remove system prompt blocks
    return content
      .replace(/System:.*?(?=
User:)/gs, '')
      .replace(/You are a helpful AI assistant.*?\n/gs, '')
      .replace(/\n\n/gs, '\n') // Clean up spacing
  }
}

Adonisjs's dependency injection container makes it easy to register and use this service across your application. The service encapsulates the security logic, ensuring consistent handling of LLM responses throughout your codebase.

Additionally, implement rate limiting and input validation specific to AI endpoints. Adonisjs's rate limiting middleware can prevent abuse that might lead to data exfiltration through repeated queries:

import { RateLimiterMemory } from 'rate-limiter-flexible'

const llmRateLimiter = new RateLimiterMemory({
  keyGenerator: request => request.ip(),
  points: 10, // 10 requests
  duration: 60 // per minute
})

This combination of response sanitization, service-layer validation, and rate limiting provides comprehensive protection against Llm Data Leakage in Adonisjs applications while maintaining the framework's development efficiency.

Related CWEs: llmSecurity

CWE IDNameSeverity
CWE-754Improper Check for Unusual or Exceptional Conditions MEDIUM

Frequently Asked Questions

How can I test my Adonisjs API for LLM data leakage?
Use middleBrick's LLM/AI Security scanner which tests for system prompt leakage using 27 regex patterns, performs active prompt injection testing, and scans for unauthenticated LLM endpoints. The scanner works without credentials and provides a security risk score with specific findings for your Adonisjs application.
What's the difference between LLM data leakage and regular API data exposure?
LLM data leakage specifically involves AI service responses that contain system prompts, training data, or proprietary information that wasn't intended for end users. Regular API data exposure involves database records or application data. LLM leakage is unique because responses often contain concatenated system instructions and context that developers don't see in the raw API response.