🛡️ Sentinel: [CRITICAL] Fix command injection and token leak in git clone#66
🛡️ Sentinel: [CRITICAL] Fix command injection and token leak in git clone#66bobdivx wants to merge 1 commit into
Conversation
…lone 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 replaces exec with execFile in the GitHub clone API route to prevent command injection, and adds error sanitization to prevent leaking the GitHub token. However, two critical issues were identified: first, the repoName parameter is vulnerable to path traversal because it is not validated before being passed to git clone; second, retrieving the GitHub token inside the catch block can cause a double-fault if the database is down, and calling replaceAll with an empty token will throw a TypeError.
| // Clone the repository | ||
| const { stdout, stderr } = await execPromise(`git clone ${authUrl} ${repoName}`, { cwd: reposRoot }); | ||
| // Use execFile with array arguments to prevent command injection | ||
| const { stdout, stderr } = await execFileAsync('git', ['clone', '--', authUrl, repoName], { cwd: reposRoot }); |
There was a problem hiding this comment.
The repoName parameter is taken directly from the user request and is not validated before being used in path.join and passed to git clone. This can lead to a Path Traversal vulnerability, allowing an attacker to clone repositories into arbitrary directories outside of reposRoot (e.g., by using ../ in repoName).
To prevent this, validate repoName using a strict alphanumeric/safe character regex (matching the isSafeRepoDirName helper pattern) before proceeding with the clone operation.
if (!/^[a-zA-Z0-9._-]{1,128}$/.test(repoName)) {
return new Response(JSON.stringify({ error: 'Nom de dépôt invalide' }), { status: 400 });
}
const { stdout, stderr } = await execFileAsync('git', ['clone', '--', authUrl, repoName], { cwd: reposRoot });| const githubToken = await getConfig('githubToken', true); | ||
| // Secure error handling: prevent leaking the github token | ||
| const safeError = error.message | ||
| ? (githubToken ? error.message.replaceAll(githubToken, '***') : error.message) | ||
| : 'Erreur lors du clonage'; |
There was a problem hiding this comment.
Calling await getConfig('githubToken', true) inside the catch block is risky. If the initial error in the try block was caused by a database failure, calling getConfig again will throw another error, causing a double-fault/unhandled promise rejection and crashing the request without returning a proper JSON response.
Additionally, if githubToken is empty or undefined, calling replaceAll with an empty string will throw a TypeError in JavaScript.
Wrap the token retrieval and replacement in a try/catch block and ensure githubToken is a non-empty string before calling replaceAll.
let safeError = error.message || 'Erreur lors du clonage';
try {
const githubToken = await getConfig('githubToken', true);
if (githubToken && githubToken.trim() !== '' && error.message) {
safeError = error.message.replaceAll(githubToken, '***');
}
} catch {
// Ignore DB errors to prevent double-faulting
}
🚨 Severity: CRITICAL
💡 Vulnerability: The
src/pages/api/github-clone.tsendpoint usedexecto rungit clone, concatenating unvalidated user inputs (repoUrl,repoName) and an auth URL containing the GitHub token. This allowed arbitrary shell command execution and risked leaking the token to the client if the command failed.🎯 Impact: An attacker could execute arbitrary code on the server by crafting malicious input, or extract the
githubTokenfrom error responses.🔧 Fix: Refactored the git execution to use
execFileAsyncwith an arguments array, utilizing--to safely delimit arguments and prevent flag injection. Added robust error sanitization to explicitly mask the GitHub token (if present) before returning error messages to the client.✅ Verification: Ran the full test suite (
pnpm test), type checks (pnpm run check), and verified that the token replacement safely falls back if the token is not configured.PR created automatically by Jules for task 6982014510491893398 started by @bobdivx