🛡️ Sentinel: [CRITICAL] Fix Command Injection in docker logs#24
Conversation
Migrated from execSync to execFileSync in src/pages/api/docker-logs.ts, passing arguments as an array instead of concatenating strings. Added regex input validation to ensure the containerId parameter only contains valid characters and does not start with a hyphen to prevent flag injection. Safely parsed the tail parameter as an integer. Co-authored-by: bobdivx <6737167+bobdivx@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Code Review
This pull request fixes a command injection vulnerability in the Docker logs API by replacing string-interpolated execSync calls with execFileSync and adding strict input validation for the containerId and tail parameters. Review feedback suggests further optimizing the implementation by using asynchronous execFile to prevent blocking the event loop and improving the log parsing logic to handle different line endings and empty outputs more robustly.
| @@ -1,5 +1,5 @@ | |||
| import type { APIRoute } from 'astro'; | |||
| import { execSync } from 'child_process'; | |||
| import { execFileSync } from 'child_process'; | |||
There was a problem hiding this comment.
Using synchronous child process methods like execFileSync in an asynchronous API route blocks the Node.js event loop, which can severely impact the performance and scalability of the server. It is recommended to use the asynchronous execFile version, ideally promisified to maintain clean async/await syntax, as seen in other parts of the codebase (e.g., src/pages/api/docker/containers.ts).
import { execFile } from 'child_process';
import { promisify } from 'util';
const execFileAsync = promisify(execFile);| const output = execFileSync('docker', ['logs', '--tail', tailParam, containerId], { | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| encoding: 'utf-8' | ||
| }); |
There was a problem hiding this comment.
Switching to the asynchronous execFileAsync prevents blocking the event loop while waiting for the Docker command to complete. This is especially important for log retrieval which might take some time depending on the container's output.
const { stdout: output } = await execFileAsync('docker', ['logs', '--tail', tailParam, containerId], {
encoding: 'utf-8'
});| stdio: ['pipe', 'pipe', 'pipe'], | ||
| encoding: 'utf-8' | ||
| }); | ||
| logs = output.trim().split('\n'); |
There was a problem hiding this comment.
The current split logic can result in an array containing an empty string if the output is empty or contains only whitespace. Using a regex for line breaks and filtering out empty strings ensures a cleaner result and better compatibility with different operating systems (handling \r\n).
| logs = output.trim().split('\n'); | |
| logs = output.trim().split(/\r?\n/).filter(Boolean); |
🚨 Severity: CRITICAL
💡 Vulnerability: User input (
idandtail) was passed directly into a shell command string evaluated byexecSyncin thedocker-logsAPI endpoint.🎯 Impact: An attacker could perform arbitrary command execution by passing a malicious payload like
id=fake-container; echo 'vulnerable'.🔧 Fix: Refactored the endpoint to use
execFileSyncpassing arguments as an array, bypassing shell evaluation. Added regex validation to block invalid characters and leading hyphens (preventing flag injection). Sanitized thetailparameter by parsing it strictly as an integer.✅ Verification:
pnpm run checkandpnpm testpass. The fix ensures that inputs containing command separators or flags are rejected safely.PR created automatically by Jules for task 8291986119183605751 started by @bobdivx