HIGH information disclosurefiberapi keys

Information Disclosure in Fiber with Api Keys

Information Disclosure in Fiber with Api Keys

Information Disclosure in a Fiber application involving API keys occurs when sensitive credentials are exposed through improper handling, logging, or serialization. Fiber, a fast web framework for Node.js, does not inherently leak keys, but common patterns in route handlers, middleware, and error responses can inadvertently expose them to unauthorized parties.

One typical scenario involves logging incoming request headers that include an Authorization header carrying an API key. If the logger is configured to record full headers without filtering, the API key appears in log files or console output, creating a data exposure finding under the Data Checks category. These findings map to the OWASP API Top 10 (API1:2023 – Broken Object Level Authorization) and can also align with SOC 2 controls related to access control and audit logging.

A second vector arises from error messages that include stack traces or request metadata. In development environments, unhandled errors may serialize the request object, including headers, and return them in the HTTP response body. An attacker who triggers such an error path could obtain an API key embedded in the headers, leading to unauthorized access to downstream services. This behavior would be flagged by middleBrick’s Data Exposure and Input Validation checks, which test how the application behaves under malformed or unexpected inputs.

Serialization and caching mechanisms can also contribute to unintentional disclosure. If API keys are included in objects that are cached in memory or serialized to JSON for client responses, they may be stored or transmitted in clear text. middleBrick’s Data Exposure checks examine responses for sensitive content such as keys, secrets, and PII, and would identify these findings with severity guidance and remediation steps. The scanner runs in 5–15 seconds, testing the unauthenticated attack surface without agents or credentials, providing a quick view of how an API behaves when exposed to the internet.

When integrating LLM-related endpoints, the risk extends to prompt injection and output leakage. Although API keys are not prompts, a compromised endpoint might expose configuration that includes key references. middleBrick’s LLM/AI Security checks perform active prompt injection probes and scan outputs for API keys and other secrets, ensuring that AI-facing endpoints do not become an additional channel for disclosure. These capabilities are unique to middleBrick, which focuses on detection and reporting rather than remediation.

In summary, the combination of verbose logging, improper error handling, and insecure serialization in a Fiber application can turn API keys into exposed data. Using middleBrick’s scans, developers can identify these patterns through categorized findings and receive prioritized guidance. The tool integrates with CI/CD via the GitHub Action to fail builds if risk thresholds are exceeded, and the Web Dashboard allows teams to track scores over time, supporting the Pro plan’s continuous monitoring for API security.

Api Keys-Specific Remediation in Fiber

Remediation focuses on preventing API keys from appearing in logs, error responses, or serialized data. In Fiber, this involves careful middleware configuration, structured error handling, and secure handling of headers.

First, avoid logging full request headers. Instead, log only necessary metadata and explicitly exclude sensitive headers. The following example shows a Fiber handler that reads an API key from the Authorization header for validation but ensures the key is not included in any log output:

const { app } = require('express-fiber'); // Fiber-compatible wrapper
const app = require('fastify')();

app.addHook('preHandler', (request, reply, done) => {
  const authHeader = request.headers['authorization'];
  if (authHeader && authHeader.startsWith('Bearer ')) {
    const apiKey = authHeader.slice(7);
    // Validate the key against a secure store
    const isValid = validateApiKey(apiKey);
    if (!isValid) {
      reply.code(401).send({ error: 'Unauthorized' });
      return;
    }
    // Attach a normalized claim instead of the raw key
    request.context.apiKey = '**REDACTED**';
  }
  done();
});

function validateApiKey(key) {
  // Replace with secure lookup, e.g., constant-time comparison
  return key === process.env.VALID_API_KEY;
}

app.get('/resource', (request, reply) => {
  reply.send({ data: 'secure-response' });
});

app.listen(3000, (err) => {
  if (err) {
    console.error('Server error');
    // Do not log request or headers here
    return;
  }
  console.log('Server running on port 3000');
});

Second, ensure error responses do not include stack traces or request headers in production. Use a centralized error handler that sanitizes output:

app.setErrorHandler((error, request, reply) => {
  // Log internally without request headers
  console.error('Request failed:', error.message);
  // Return a generic error to the client
  reply.code(error.statusCode || 500).send({
    error: 'Internal Server Error',
  });
});

Third, if you use middleware that processes headers for routing or authentication, ensure that API keys are stripped or redacted before the request continues to downstream handlers. The preHandler hook above demonstrates this by validating and replacing the key reference rather than passing the raw value.

Finally, integrate middleBrick’s GitHub Action to enforce security thresholds in CI/CD. This ensures that any new route or configuration change is scanned before deployment. The Web Dashboard supports tracking historical scans, and the Pro plan enables continuous monitoring with scheduled scans and alerts. While middleBrick detects and reports findings, developers must apply these code-level practices to reduce the risk of information disclosure involving API keys in Fiber applications.

Frequently Asked Questions

Can middleBrick remove API keys from my Fiber logs?
middleBrick detects and reports information disclosure findings, including exposed API keys in logs. It does not modify or remove data; remediation requires code and configuration changes in your application and logging setup.
Does scanning my Fiber app with API keys risk exposing them?
Scans are unauthenticated and black-box, meaning they do not require credentials. They test public endpoints and inspect responses for sensitive content, including API keys, without altering or storing your production data.