HIGH information disclosurechiapi keys

Information Disclosure in Chi with Api Keys

Information Disclosure in Chi with Api Keys — how this specific combination creates or exposes the vulnerability

Information Disclosure in the context of a Chi server (a lightweight HTTP framework for Node.js) often occurs when API keys are mishandled in request processing, logging, error responses, or client-side delivery. Chi routes are typically defined using middleware, and if developers inadvertently expose sensitive values in responses, logs, or URLs, an attacker can harvest API keys that grant access to downstream services.

Chi uses a compact, functional middleware style. When an API key is passed via headers, query parameters, or cookies, improper validation or verbose error handling can leak that key. For example, returning the full request object or configuration in error details may surface environment variables that include secrets. Similarly, logging middleware that captures headers without redaction can store or stream API keys to centralized logging systems, creating a persistent exposure window.

Another common pattern is the use of static file serving combined with route definitions. If an API key is embedded in client-side JavaScript or exposed through an open endpoint that returns configuration, an attacker can retrieve the key by inspecting network traffic or source files. Chi’s reliance on explicit route matching means developers must ensure that routes handling sensitive operations do not also serve public assets or debug endpoints.

In OpenAPI spec analysis, which middleBrick performs across Swagger 2.0, OpenAPI 3.0, and 3.1 with full $ref resolution, paths that accept API keys in headers or query strings must be explicitly marked as security schemes. When specs omit security requirements or define them inconsistently, runtime behavior may diverge from intended protections, increasing the risk of unintentional disclosure during unauthenticated scans.

Real-world attack patterns include error message enumeration, where a malformed request containing a key triggers a stack trace or detailed message that includes the key itself. SSRF-related probes can also redirect internal requests to services that return configuration data, inadvertently exposing keys that were meant to remain server-side. These issues are detectable through active scanning that compares declared security schemes with actual runtime responses.

Api Keys-Specific Remediation in Chi

Remediation focuses on ensuring API keys never appear in responses, logs, or URLs, and are transmitted only over encrypted channels. In Chi, define routes with explicit security requirements and avoid echoing user-supplied values in error replies.

Use environment variables to inject keys at runtime and keep them out of source code. When validating incoming requests, reject malformed inputs with generic messages that do not reference the key or request details that might contain it.

const chi = require('chi')();
const dotenv = require('dotenv');
dotenv.config();

// Secure route example: validate API key from header, do not echo it
chi.get('/v1/data', (req, res, next) => {
  const apiKey = req.headers['x-api-key'];
  if (!apiKey || apiKey !== process.env.API_KEY) {
    // Generic error to avoid disclosure
    res.statusCode = 401;
    return res.end('Unauthorized');
  }
  // Proceed with authenticated logic
  res.end('Access granted');
  return next();
});

// Logging middleware that redacts sensitive headers
const logger = require('pino')();
chi.use((req, res, next) => {
  const safeHeaders = { ...req.headers };
  if (safeHeaders['x-api-key']) safeHeaders['x-api-key'] = 'REDACTED';
  logger.info({
    method: req.method,
    url: req.url,
    headers: safeHeaders
  });
  next();
});

// Serve static files from a separate, restricted path to avoid mixing with API routes
chi.folder('/public').static('public');

For OpenAPI compliance, declare an API key security scheme and apply it only to endpoints that require it, ensuring the spec aligns with runtime behavior. This reduces the gap that middleBrick detects when comparing spec definitions against actual responses.

In production, enforce HTTPS using Chi’s built-in support for secure servers and avoid returning configuration or debug endpoints that could expose keys. Combine these practices with continuous scanning using tools like the middleBrick CLI to validate that changes do not reintroduce disclosure risks.

Frequently Asked Questions

Can an API key be safely passed as a query parameter in Chi routes?
No. Query parameters are often logged in server and proxy logs, making them unsuitable for API keys. Use headers instead and ensure logs redact sensitive values.
How does middleBrick detect Information Disclosure involving API keys in Chi configurations?
middleBrick compares declared security schemes in OpenAPI specs (including $ref resolution) with runtime responses, flagging endpoints that leak keys in error messages, logs, or unauthenticated outputs.