From 27c559d1b9bda02c4d5e0a355fcd03333fcd53ca Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Fri, 17 Oct 2025 11:14:27 -0700 Subject: [PATCH] improv: tests --- core/util/sanitization.vitest.ts | 197 +++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) diff --git a/core/util/sanitization.vitest.ts b/core/util/sanitization.vitest.ts index 3a29e9f7a..09ce67fa0 100644 --- a/core/util/sanitization.vitest.ts +++ b/core/util/sanitization.vitest.ts @@ -1,3 +1,4 @@ +import { execSync } from "child_process"; import { describe, expect, it } from "vitest"; import { sanitizeShellArgument, validateGitHubRepoUrl } from "./sanitization"; @@ -87,3 +88,199 @@ describe("validateGitHubRepoUrl", () => { expect(validateGitHubRepoUrl(undefined as any)).toBe(false); }); }); + +/** + * Integration tests for sanitizeShellArgument + * + * These tests actually execute shell commands with sanitized dangerous inputs + * to verify end-to-end that the sanitization prevents injection attacks. + * + * IMPORTANT: These are CRITICAL security tests. The unit tests above verify + * that sanitization transforms inputs, but only these integration tests prove + * that the transformed output is safe when executed in a real shell. + */ +describe("sanitizeShellArgument - integration tests", () => { + // Helper function to safely execute a command with a timeout + const safeExec = (command: string): string => { + try { + return execSync(command, { + encoding: "utf-8", + timeout: 5000, // 5 second timeout to prevent hanging + shell: "/bin/sh", // Use standard POSIX shell + }).trim(); + } catch (error: any) { + // If command fails (non-zero exit), return the error output + return error.stdout?.trim() || ""; + } + }; + + it("should prevent command injection with semicolon separator", () => { + const malicious = "; echo INJECTED"; + const sanitized = sanitizeShellArgument(malicious); + + // Execute echo command with the sanitized input + const result = safeExec(`echo ${sanitized}`); + + // The output should be the literal string, not execute "echo INJECTED" + expect(result).toBe(malicious); + expect(result).not.toBe("INJECTED"); + }); + + it("should prevent command injection with && operator", () => { + const malicious = "safe && echo INJECTED"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + // Should output the literal string, not execute the injected command + // The string "INJECTED" will appear, but as part of "echo INJECTED" literal text + expect(result).toBe(malicious); + expect(result).toContain("echo INJECTED"); // Verify it's the literal command text + }); + + it("should prevent command injection with || operator", () => { + const malicious = "safe || echo INJECTED"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + // Should output the literal string, not execute the injected command + expect(result).toBe(malicious); + expect(result).toContain("echo INJECTED"); // Verify it's the literal command text + }); + + it("should prevent command injection with pipe operator", () => { + const malicious = "safe | echo INJECTED"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + expect(result).toBe(malicious); + }); + + it("should prevent command substitution with $()", () => { + const malicious = "$(echo INJECTED)"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + // Should output the literal string "$(echo INJECTED)", not "INJECTED" + expect(result).toBe(malicious); + expect(result).not.toBe("INJECTED"); + }); + + it("should prevent command substitution with backticks", () => { + const malicious = "`echo INJECTED`"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + // Should output the literal backtick string, not execute it + expect(result).toBe(malicious); + expect(result).not.toBe("INJECTED"); + }); + + it("should prevent variable expansion", () => { + // Set an environment variable for this test + process.env.TEST_VAR = "EXPANDED"; + + const malicious = "$TEST_VAR"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + // Should output literal "$TEST_VAR", not "EXPANDED" + expect(result).toBe(malicious); + expect(result).not.toBe("EXPANDED"); + + // Cleanup + delete process.env.TEST_VAR; + }); + + it("should handle git stash message use case safely", () => { + // This mirrors the actual usage in VsCodeMessenger.ts:269 + const agentId = "agent-123; rm -rf /"; + const stashMessage = `Continue: Stashed before opening agent ${agentId}`; + const sanitized = sanitizeShellArgument(stashMessage); + + // Simulate the git stash command (using echo as a safe substitute) + // In real code: `git stash push -m ${sanitized}` + const result = safeExec(`echo ${sanitized}`); + + // The message should contain the full literal string including dangerous chars + expect(result).toContain("agent-123; rm -rf /"); + expect(result).toContain("Continue: Stashed before opening agent"); + // Verify it's one line (not executed as multiple commands) + expect(result.split("\n").length).toBe(1); + }); + + it("should handle multi-line injection attempts", () => { + const malicious = "line1\necho INJECTED\nline3"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + // Should preserve the structure but not execute embedded commands + expect(result).toContain("line1"); + expect(result).toContain("line3"); + // The literal "echo INJECTED" text should appear, but not executed + expect(result).toContain("echo INJECTED"); + }); + + it("should handle shell redirection attempts", () => { + const malicious = "message > /tmp/test.txt"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + // Should output the literal string, not create a file + expect(result).toBe(malicious); + // The command should not have created the file + // (we're not checking file system to keep test isolated) + }); + + it("should handle complex injection with multiple attack vectors", () => { + const malicious = "; $(whoami) && `date` || $HOME | cat > /dev/null"; + const sanitized = sanitizeShellArgument(malicious); + + const result = safeExec(`echo ${sanitized}`); + + // Should output the entire literal string + expect(result).toBe(malicious); + // Verify none of the commands were executed by checking output is literal + expect(result).toContain("$(whoami)"); + expect(result).toContain("`date`"); + expect(result).toContain("$HOME"); + }); + + it("should handle special characters safely", () => { + const special = 'test with spaces, quotes\', and "more"'; + const sanitized = sanitizeShellArgument(special); + + const result = safeExec(`echo ${sanitized}`); + + expect(result).toBe(special); + }); + + it("should handle empty string without errors", () => { + const sanitized = sanitizeShellArgument(""); + + // Should not throw when used in a command + expect(() => safeExec(`echo ${sanitized}`)).not.toThrow(); + }); + + it("should verify shell-quote properly escapes for git log format strings", () => { + // Another common use case: git log with custom format strings + const userInput = "Author: $(whoami) Date: `date`"; + const sanitized = sanitizeShellArgument(userInput); + + // Simulate: git log --format="%s: ${sanitized}" + // Using printf as a safer test substitute + const result = safeExec(`printf '%s' ${sanitized}`); + + // Should output the literal string, not execute substitutions + expect(result).toBe(userInput); + expect(result).toContain("$(whoami)"); + expect(result).toContain("`date`"); + }); +});