Skip to content

🛡️ Sentinel: [CRITICAL] Fix command injection and token leak in git clone#66

Open
bobdivx wants to merge 1 commit into
devfrom
sentinel/fix-git-clone-injection-6982014510491893398
Open

🛡️ Sentinel: [CRITICAL] Fix command injection and token leak in git clone#66
bobdivx wants to merge 1 commit into
devfrom
sentinel/fix-git-clone-injection-6982014510491893398

Conversation

@bobdivx
Copy link
Copy Markdown
Owner

@bobdivx bobdivx commented May 26, 2026

🚨 Severity: CRITICAL
💡 Vulnerability: The src/pages/api/github-clone.ts endpoint used exec to run git 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 githubToken from error responses.
🔧 Fix: Refactored the git execution to use execFileAsync with 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

…lone

Co-authored-by: bobdivx <6737167+bobdivx@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
forge Ready Ready Preview, Comment May 26, 2026 5:32pm

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 });
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

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 });

Comment on lines +57 to +61
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';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant