🛡️ Sentinel: [CRITICAL] Fix command injection in multiple API routes#81
🛡️ Sentinel: [CRITICAL] Fix command injection in multiple API routes#81bobdivx wants to merge 1 commit into
Conversation
🚨 Severity: CRITICAL 💡 Vulnerability: Command injection in `docker-logs.ts`, `docker-health.ts`, and `github-clone.ts` due to unsafe usage of `execSync` and `exec` with string interpolation. 🎯 Impact: Attackers could inject arbitrary commands into the host shell, leading to Remote Code Execution (RCE) and system compromise. 🔧 Fix: Refactored `execSync` and `exec` to `execFileAsync` with proper argument arrays. Used `--` for git commands to prevent argument injection, and added strict input validation regexes for identifiers. Prevented token leaks in error messages. ✅ Verification: Ensure `pnpm run check` and `pnpm test` pass. Manually test API endpoints to ensure normal behavior works and malicious inputs (e.g. `id=; echo oops`) are safely handled. Co-authored-by: bobdivx <6737167+bobdivx@users.noreply.github.com>
|
👋 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. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Code Review
This pull request mitigates command injection vulnerabilities across several API routes (docker-health.ts, docker-logs.ts, and github-clone.ts) by replacing shell execution with execFile and utilizing explicit argument arrays. It also introduces input validation and sanitizes error messages to prevent credential leaks. The review feedback highlights opportunities to improve these changes: restoring support for the tail=all option in Docker logs, utilizing the robust URL API for parsing and credential injection in the GitHub clone route, and reusing the existing isSafeRepoDirName helper function for repository name validation.
| const tailNum = parseInt(tail, 10); | ||
| const validTail = isNaN(tailNum) ? '100' : String(tailNum); |
There was a problem hiding this comment.
The current validation of tail using parseInt breaks the tail=all functionality, which is a valid and common option in Docker logs. Additionally, it does not handle negative numbers gracefully. We can simplify and secure this by validating that tail is either exactly 'all' or a non-negative integer using a regular expression.
| const tailNum = parseInt(tail, 10); | |
| const validTail = isNaN(tailNum) ? '100' : String(tailNum); | |
| const validTail = (tail === 'all' || /^\d+$/.test(tail)) ? tail : '100'; |
| if (!repoUrl.startsWith('https://github.com/')) { | ||
| return new Response(JSON.stringify({ error: 'URL Github invalide' }), { status: 400 }); | ||
| } | ||
| const authUrl = repoUrl.replace('https://', `https://oauth2:${githubToken}@`); |
There was a problem hiding this comment.
Using simple string replacement (replace('https://', ...)) and startsWith for URL validation and credential injection can be fragile and prone to parsing discrepancies. Utilizing the built-in URL API is much more robust and secure, ensuring that the protocol is strictly https: and the hostname is strictly github.com, while safely injecting the credentials without manual string manipulation.
let parsedUrl: URL;
try {
parsedUrl = new URL(repoUrl);
if (parsedUrl.protocol !== 'https:' || parsedUrl.hostname !== 'github.com') {
return new Response(JSON.stringify({ error: 'URL Github invalide' }), { status: 400 });
}
} catch {
return new Response(JSON.stringify({ error: 'URL Github invalide' }), { status: 400 });
}
parsedUrl.username = 'oauth2';
parsedUrl.password = githubToken;
const authUrl = parsedUrl.toString();| if (!/^[a-zA-Z0-9_.-]+$/.test(repoName) || repoName.includes('..')) { | ||
| return new Response(JSON.stringify({ error: 'Nom de dépôt invalide' }), { status: 400 }); | ||
| } |
There was a problem hiding this comment.
Instead of writing a custom regex to validate the repository name, you can reuse the existing isSafeRepoDirName helper function from src/lib/forge-repos.ts to ensure consistency and maintainability across the codebase. Note that you will also need to add isSafeRepoDirName to the imports from ../../lib/forge-repos.
| if (!/^[a-zA-Z0-9_.-]+$/.test(repoName) || repoName.includes('..')) { | |
| return new Response(JSON.stringify({ error: 'Nom de dépôt invalide' }), { status: 400 }); | |
| } | |
| if (!isSafeRepoDirName(repoName) || repoName === '.' || repoName === '..') { | |
| return new Response(JSON.stringify({ error: 'Nom de dépôt invalide' }), { status: 400 }); | |
| } |
🚨 Severity: CRITICAL
💡 Vulnerability: Command injection in
docker-logs.ts,docker-health.ts, andgithub-clone.tsdue to unsafe usage ofexecSyncandexecwith string interpolation.🎯 Impact: Attackers could inject arbitrary commands into the host shell, leading to Remote Code Execution (RCE) and system compromise.
🔧 Fix: Refactored
execSyncandexectoexecFileAsyncwith proper argument arrays. Used--for git commands to prevent argument injection, and added strict input validation regexes for identifiers. Prevented token leaks in error messages.✅ Verification: Ensure
pnpm run checkandpnpm testpass. Manually test API endpoints to ensure normal behavior works and malicious inputs (e.g.id=; echo oops) are safely handled.PR created automatically by Jules for task 9804105707731073344 started by @bobdivx