HIGH symlink attackadonisjsapi keys

Symlink Attack in Adonisjs with Api Keys

Symlink Attack in Adonisjs with Api Keys — how this specific combination creates or exposes the vulnerability

A symlink attack in AdonisJS becomes significant when API keys are involved because key validation and file resolution may share the same filesystem traversal logic. If an endpoint that serves files or configuration uses user-controlled input to build paths and also checks an API key for authorization, an attacker can supply a crafted path containing .. or a symbolic link to escape the intended directory. Because the API key is typically verified before or alongside path resolution, the attacker may leverage a valid key to access files outside the allowed scope.

For example, an AdonisJS route that authenticates via an API key and then reads a file based on a query parameter can be tricked into reading arbitrary files if the path is not properly sanitized. Consider a route that intends to serve assets from a controlled directory:

// routes.ts
Route.get('/files', async (ctx) => {
  const apiKey = ctx.request.header('x-api-key')
  // naive key check
  if (!isValidKey(apiKey)) {
    return ctx.response.unauthorized('Invalid key')
  }
  const requested = ctx.request.qs.path
  // dangerous: user-controlled path
  const fullPath = `resources/uploads/${requested}`
  const content = await fs.readFile(fullPath, 'utf-8')
  ctx.response.type('text/plain').send(content)
})

An attacker with a valid API key can set path to something like ../../../etc/passwd or to a symlink placed earlier that points outside resources/uploads. The API key check passes, but the resolved path escapes the intended sandbox. This becomes a file disclosure vector, and if the application also writes files based on similar input, it may enable path manipulation or overwrite sensitive files. The attack leverages trust in the API key to bypass directory boundaries that should be enforced independently of authentication.

In the context of the 12 security checks run by middleBrick, this pattern maps to BOLA/IDOR and Input Validation. A scanner can detect whether API-key-protected endpoints exhibit path traversal or symlink resolution risks by probing with encoded traversal sequences and observing whether directory boundaries are respected. Even when keys are rotated, the underlying path handling flaw remains until the resolution logic is hardened.

Api Keys-Specific Remediation in Adonisjs — concrete code fixes

To mitigate symlink risks when using API keys in AdonisJS, ensure path construction never trusts user input and that resolution is confined to a strict base directory. Use Node.js built-in path utilities to canonicalize and validate paths, and avoid string concatenation for filesystem operations. API key validation should remain separate from path resolution, and you should enforce strict allowlists for accessible resources.

Secure route example with explicit base path and sanitization:

// routes.ts
import { join, normalize, resolve } from 'path'
import { existsSync } from 'fs'

Route.get('/files', async (ctx) => {
  const apiKey = ctx.request.header('x-api-key')
  if (!isValidKey(apiKey)) {
    return ctx.response.unauthorized('Invalid key')
  }
  const requested = ctx.request.qs.path
  const base = resolve('resources/uploads')
  // normalize and resolve to eliminate '..' and symlinks
  const target = resolve(normalize(join(base, requested)))
  // ensure the resolved path is within base
  if (!target.startsWith(base)) {
    return ctx.response.badRequest('Invalid path')
  }
  if (!existsSync(target)) {
    return ctx.response.notFound()
  }
  const content = await fs.readFile(target, 'utf-8')
  ctx.response.type('text/plain').send(content)
})

Additional hardening measures include:

  • Disable symbolic link following when possible (e.g., prefer fs.readFile with controlled paths rather than low-level file system APIs that follow symlinks by default).
  • Use allowlists for file names or extensions if the use case is predictable.
  • Rotate API keys regularly and scope keys to least privilege, so a compromised key does not grant broad filesystem access.
  • Log and monitor requests that trigger path validation failures for signs of probing.

These practices align with input validation checks in middleBrick scans, and applying them reduces the likelihood of successful traversal or symlink-based exposure. The scanner can then verify whether the remediation effectively constrains path resolution under authenticated and unauthenticated conditions.

Frequently Asked Questions

How does middleBrick detect symlink risks in AdonisJS APIs that use API keys?
middleBrick runs unauthenticated checks that probe endpoints with path traversal and symlink sequences while an API key is present, then verifies whether resolution remains confined to the intended directory and does not follow unauthorized links.
Can the GitHub Action prevent symlink-related regressions in AdonisJS projects?
Yes, by setting a security score threshold in the GitHub Action, builds will fail if new changes introduce path handling issues that lower the score, encouraging fixes before deployment.