|
| 1 | +import { parseCommand, validateCommand } from "../../utils/command-validation" |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the command-validation module |
| 5 | + * |
| 6 | + * These tests include a reproduction of a bug where the shell-quote library |
| 7 | + * used in parseCommand throws an error when parsing commands that contain |
| 8 | + * the Bash $RANDOM variable in array indexing expressions. |
| 9 | + * |
| 10 | + * Error: "Bad substitution: levels[$RANDOM" |
| 11 | + * |
| 12 | + * The issue occurs specifically with complex expressions like: |
| 13 | + * ${levels[$RANDOM % ${#levels[@]}]} |
| 14 | + */ |
| 15 | +describe("command-validation", () => { |
| 16 | + describe("parseCommand", () => { |
| 17 | + it("should correctly parse simple commands", () => { |
| 18 | + const result = parseCommand("echo hello") |
| 19 | + expect(result).toEqual(["echo hello"]) |
| 20 | + }) |
| 21 | + |
| 22 | + it("should correctly parse commands with chaining operators", () => { |
| 23 | + const result = parseCommand("echo hello && echo world") |
| 24 | + expect(result).toEqual(["echo hello", "echo world"]) |
| 25 | + }) |
| 26 | + |
| 27 | + it("should correctly parse commands with quotes", () => { |
| 28 | + const result = parseCommand('echo "hello world"') |
| 29 | + expect(result).toEqual(['echo "hello world"']) |
| 30 | + }) |
| 31 | + |
| 32 | + it("should correctly parse commands with redirections", () => { |
| 33 | + const result = parseCommand("echo hello 2>&1") |
| 34 | + expect(result).toEqual(["echo hello 2>&1"]) |
| 35 | + }) |
| 36 | + |
| 37 | + it("should correctly parse commands with subshells", () => { |
| 38 | + const result = parseCommand("echo $(date)") |
| 39 | + expect(result).toEqual(["echo", "date"]) |
| 40 | + }) |
| 41 | + |
| 42 | + it("should not throw an error when parsing commands with simple array indexing", () => { |
| 43 | + // Simple array indexing works fine |
| 44 | + const commandWithArrayIndex = "value=${array[$index]}" |
| 45 | + |
| 46 | + expect(() => { |
| 47 | + parseCommand(commandWithArrayIndex) |
| 48 | + }).not.toThrow() |
| 49 | + }) |
| 50 | + |
| 51 | + it("should not throw an error when parsing commands with $RANDOM in array index", () => { |
| 52 | + // This test reproduces the specific bug reported in the error |
| 53 | + const commandWithRandom = "level=${levels[$RANDOM % ${#levels[@]}]}" |
| 54 | + |
| 55 | + expect(() => { |
| 56 | + parseCommand(commandWithRandom) |
| 57 | + }).not.toThrow() |
| 58 | + }) |
| 59 | + |
| 60 | + it("should not throw an error with simple $RANDOM variable", () => { |
| 61 | + // Simple $RANDOM usage works fine |
| 62 | + const commandWithRandom = "echo $RANDOM" |
| 63 | + |
| 64 | + expect(() => { |
| 65 | + parseCommand(commandWithRandom) |
| 66 | + }).not.toThrow() |
| 67 | + }) |
| 68 | + |
| 69 | + it("should not throw an error with simple array indexing using $RANDOM", () => { |
| 70 | + // Simple array indexing with $RANDOM works fine |
| 71 | + const commandWithRandomIndex = "echo ${array[$RANDOM]}" |
| 72 | + |
| 73 | + expect(() => { |
| 74 | + parseCommand(commandWithRandomIndex) |
| 75 | + }).not.toThrow() |
| 76 | + }) |
| 77 | + |
| 78 | + it("should not throw an error with complex array indexing using $RANDOM and arithmetic", () => { |
| 79 | + // This test reproduces the exact expression from the original error |
| 80 | + const commandWithComplexRandom = "echo ${levels[$RANDOM % ${#levels[@]}]}" |
| 81 | + |
| 82 | + expect(() => { |
| 83 | + parseCommand(commandWithComplexRandom) |
| 84 | + }).not.toThrow("Bad substitution") |
| 85 | + }) |
| 86 | + |
| 87 | + it("should not throw an error when parsing the full log generator command", () => { |
| 88 | + // This is the exact command from the original error message |
| 89 | + const logGeneratorCommand = `while true; do \\ |
| 90 | + levels=(INFO WARN ERROR DEBUG); \\ |
| 91 | + msgs=("User logged in" "Connection timeout" "Processing request" "Cache miss" "Database query"); \\ |
| 92 | + level=\${levels[$RANDOM % \${#levels[@]}]}; \\ |
| 93 | + msg=\${msgs[$RANDOM % \${#msgs[@]}]}; \\ |
| 94 | + echo "\$(date '+%Y-%m-%d %H:%M:%S') [$level] $msg"; \\ |
| 95 | + sleep 1; \\ |
| 96 | +done` |
| 97 | + |
| 98 | + // This reproduces the original error |
| 99 | + expect(() => { |
| 100 | + parseCommand(logGeneratorCommand) |
| 101 | + }).not.toThrow("Bad substitution: levels[$RANDOM") |
| 102 | + }) |
| 103 | + |
| 104 | + it("should not throw an error when parsing just the problematic part", () => { |
| 105 | + // This isolates just the part mentioned in the error message |
| 106 | + const problematicPart = "level=${levels[$RANDOM" |
| 107 | + |
| 108 | + expect(() => { |
| 109 | + parseCommand(problematicPart) |
| 110 | + }).not.toThrow("Bad substitution") |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + describe("validateCommand", () => { |
| 115 | + it("should validate allowed commands", () => { |
| 116 | + const result = validateCommand("echo hello", ["echo"]) |
| 117 | + expect(result).toBe(true) |
| 118 | + }) |
| 119 | + |
| 120 | + it("should reject disallowed commands", () => { |
| 121 | + const result = validateCommand("rm -rf /", ["echo", "ls"]) |
| 122 | + expect(result).toBe(false) |
| 123 | + }) |
| 124 | + |
| 125 | + it("should not fail validation for commands with simple $RANDOM variable", () => { |
| 126 | + const commandWithRandom = "echo $RANDOM" |
| 127 | + |
| 128 | + expect(() => { |
| 129 | + validateCommand(commandWithRandom, ["echo"]) |
| 130 | + }).not.toThrow() |
| 131 | + }) |
| 132 | + |
| 133 | + it("should not fail validation for commands with simple array indexing using $RANDOM", () => { |
| 134 | + const commandWithRandomIndex = "echo ${array[$RANDOM]}" |
| 135 | + |
| 136 | + expect(() => { |
| 137 | + validateCommand(commandWithRandomIndex, ["echo"]) |
| 138 | + }).not.toThrow() |
| 139 | + }) |
| 140 | + |
| 141 | + it("should return false for the full log generator command due to subshell detection", () => { |
| 142 | + // This is the exact command from the original error message |
| 143 | + const logGeneratorCommand = `while true; do \\ |
| 144 | + levels=(INFO WARN ERROR DEBUG); \\ |
| 145 | + msgs=("User logged in" "Connection timeout" "Processing request" "Cache miss" "Database query"); \\ |
| 146 | + level=\${levels[$RANDOM % \${#levels[@]}]}; \\ |
| 147 | + msg=\${msgs[$RANDOM % \${#msgs[@]}]}; \\ |
| 148 | + echo "\$(date '+%Y-%m-%d %H:%M:%S') [$level] $msg"; \\ |
| 149 | + sleep 1; \\ |
| 150 | +done` |
| 151 | + |
| 152 | + // validateCommand should return false due to subshell detection |
| 153 | + // without throwing an error |
| 154 | + const result = validateCommand(logGeneratorCommand, ["while"]) |
| 155 | + expect(result).toBe(false) |
| 156 | + }) |
| 157 | + }) |
| 158 | +}) |
0 commit comments