From e64be8f20cbee4b01651d3c5619bcc9d0b3e9dab Mon Sep 17 00:00:00 2001 From: Wayne Sun Date: Thu, 1 Oct 2026 10:18:36 -0400 Subject: [PATCH 1/3] fix(#6966): flatten and cap a rejected status before the generated post-script logs it The generated post-script echoed a rejected status raw. The value is model output and jq -r turns JSON \n/\r escapes into real line breaks, so a crafted status could split the runner's log line and start a new one with a :: workflow-command prefix. Flatten CR/LF to spaces, cap the value at 40 characters and write it with printf '%s\n', which never interprets backslash escapes (echo does under xpg_echo). summary already had this check; status did not. Tests feed LF, CR-only and CRLF statuses followed by ::error::injected and assert a single log line, and run the script under bash -O xpg_echo with a literal backslash-n. Goldens regenerated. Assisted-by: Claude (fix), Claude (review) Signed-off-by: Wayne Sun --- internal/agentnew/postscript_status_test.go | 105 ++++++++++++++++++ internal/agentnew/templates/post.sh.tmpl | 11 +- internal/agentnew/testdata/golden/triage.txt | 11 +- .../testdata/golden/validation-loop.txt | 11 +- 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 internal/agentnew/postscript_status_test.go diff --git a/internal/agentnew/postscript_status_test.go b/internal/agentnew/postscript_status_test.go new file mode 100644 index 0000000000..ecb16a5558 --- /dev/null +++ b/internal/agentnew/postscript_status_test.go @@ -0,0 +1,105 @@ +package agentnew + +import ( + "os" + "os/exec" + "strings" + "testing" +) + +// TestGeneratedPostScriptFlattensAnInvalidStatusBeforeLogging feeds a status +// that carries a line break (LF, CR alone, or CRLF) and a workflow-command +// prefix. The rejection message must stay on one line and must not reproduce +// the injected line, because the script's stderr lands in the runner log +// where `::` at line start is interpreted as a command. +func TestGeneratedPostScriptFlattensAnInvalidStatusBeforeLogging(t *testing.T) { + if _, err := exec.LookPath("jq"); err != nil { + t.Skip("jq not installed; the generated post-script needs it") + } + script := renderPostScriptTo(t, t.TempDir()) + for name, brk := range map[string]string{"LF": "\n", "CR": "\r", "CRLF": "\r\n"} { + t.Run(name, func(t *testing.T) { + runDir := writeRunDir(t, map[string]any{ + "iteration-1": map[string]any{ + "status": "bogus" + brk + "::error::injected", + "summary": "s", + "comment": "c", + }, + }) + _, stderr, err := runPostScript(t, script, runDir) + if err == nil { + t.Fatal("expected the script to reject an invalid status") + } + if !strings.Contains(stderr, "status must be ok, findings or error") { + t.Fatalf("expected the status rejection, got stderr:\n%s", stderr) + } + for _, line := range strings.FieldsFunc(stderr, func(r rune) bool { return r == '\n' || r == '\r' }) { + if strings.HasPrefix(line, "::") { + t.Fatalf("model-supplied status reached the log as its own line: %q", line) + } + } + if strings.ContainsRune(stderr, '\r') || strings.Count(strings.TrimSpace(stderr), "\n") != 0 { + t.Fatalf("rejection must be a single log line, got: %q", stderr) + } + }) + } +} + +// TestGeneratedPostScriptDoesNotExpandEscapesUnderXpgEcho runs the script +// with bash's xpg_echo option on, where `echo` interprets backslash escapes. +// A status carrying a literal backslash-n must still be logged on one line. +func TestGeneratedPostScriptDoesNotExpandEscapesUnderXpgEcho(t *testing.T) { + if _, err := exec.LookPath("jq"); err != nil { + t.Skip("jq not installed; the generated post-script needs it") + } + script := renderPostScriptTo(t, t.TempDir()) + runDir := writeRunDir(t, map[string]any{ + "iteration-1": map[string]any{ + "status": `bogus\n::error::injected`, + "summary": "s", + "comment": "c", + }, + }) + cmd := exec.Command("bash", "-O", "xpg_echo", script) + cmd.Dir = runDir + cmd.Env = append(os.Environ(), + "ISSUE_URL=https://github.com/fullsend-ai/demo/pull/99", + "GH_TOKEN=test-token", + DryRunEnvVar("lint-docs")+"=1", + ) + var stderr strings.Builder + cmd.Stderr = &stderr + if err := cmd.Run(); err == nil { + t.Fatal("expected the script to reject an invalid status") + } + out := strings.TrimSpace(stderr.String()) + if strings.Count(out, "\n") != 0 { + t.Fatalf("backslash escapes were expanded into a new log line:\n%s", out) + } + if !strings.Contains(out, `bogus\n::error::injected`) { + t.Fatalf("expected the literal value on the single line, got: %q", out) + } +} + +// TestGeneratedPostScriptCapsALongInvalidStatus pins the 40-character cap on +// the logged value, so a long model-supplied status cannot flood the log. +func TestGeneratedPostScriptCapsALongInvalidStatus(t *testing.T) { + if _, err := exec.LookPath("jq"); err != nil { + t.Skip("jq not installed; the generated post-script needs it") + } + script := renderPostScriptTo(t, t.TempDir()) + runDir := writeRunDir(t, map[string]any{ + "iteration-1": map[string]any{ + "status": strings.Repeat("x", 200), + "summary": "s", + "comment": "c", + }, + }) + _, stderr, err := runPostScript(t, script, runDir) + if err == nil { + t.Fatal("expected the script to reject an invalid status") + } + if !strings.Contains(stderr, "(got '"+strings.Repeat("x", 40)+"')") { + t.Fatalf("expected the status capped at 40 characters, got: %q", stderr) + } +} diff --git a/internal/agentnew/templates/post.sh.tmpl b/internal/agentnew/templates/post.sh.tmpl index 7a049dd895..f2ff7bee8b 100644 --- a/internal/agentnew/templates/post.sh.tmpl +++ b/internal/agentnew/templates/post.sh.tmpl @@ -81,7 +81,16 @@ comment=$(jq -r 'if (.comment | type) == "string" then .comment else empty end' case "${status}" in ok|findings|error) ;; - *) echo "post-__AGENT_NAME__: status must be ok, findings or error (got '${status}')" >&2; exit 1 ;; + *) + # status is model output: never echo it raw. A value carrying CR/LF would + # split the runner's log line (a workflow-command injection vector), so + # flatten it and cap it before it reaches stderr. + shown="${status//[$'\r\n']/ }" + # printf '%s' never interprets backslash escapes; echo does under xpg_echo, + # so a literal "\n" in the value could still forge a log line through echo. + printf '%s\n' "post-__AGENT_NAME__: status must be ok, findings or error (got '${shown:0:40}')" >&2 + exit 1 + ;; esac [[ -n "${summary}" ]] || { echo "post-__AGENT_NAME__: summary is required" >&2; exit 1; } if [[ "${summary}" == *$'\n'* || "${summary}" == *$'\r'* ]]; then diff --git a/internal/agentnew/testdata/golden/triage.txt b/internal/agentnew/testdata/golden/triage.txt index 8c518de616..caeeb2e4f3 100644 --- a/internal/agentnew/testdata/golden/triage.txt +++ b/internal/agentnew/testdata/golden/triage.txt @@ -233,7 +233,16 @@ comment=$(jq -r 'if (.comment | type) == "string" then .comment else empty end' case "${status}" in ok|findings|error) ;; - *) echo "post-lint-docs: status must be ok, findings or error (got '${status}')" >&2; exit 1 ;; + *) + # status is model output: never echo it raw. A value carrying CR/LF would + # split the runner's log line (a workflow-command injection vector), so + # flatten it and cap it before it reaches stderr. + shown="${status//[$'\r\n']/ }" + # printf '%s' never interprets backslash escapes; echo does under xpg_echo, + # so a literal "\n" in the value could still forge a log line through echo. + printf '%s\n' "post-lint-docs: status must be ok, findings or error (got '${shown:0:40}')" >&2 + exit 1 + ;; esac [[ -n "${summary}" ]] || { echo "post-lint-docs: summary is required" >&2; exit 1; } if [[ "${summary}" == *$'\n'* || "${summary}" == *$'\r'* ]]; then diff --git a/internal/agentnew/testdata/golden/validation-loop.txt b/internal/agentnew/testdata/golden/validation-loop.txt index 3253350e5b..8b47497613 100644 --- a/internal/agentnew/testdata/golden/validation-loop.txt +++ b/internal/agentnew/testdata/golden/validation-loop.txt @@ -241,7 +241,16 @@ comment=$(jq -r 'if (.comment | type) == "string" then .comment else empty end' case "${status}" in ok|findings|error) ;; - *) echo "post-lint-docs: status must be ok, findings or error (got '${status}')" >&2; exit 1 ;; + *) + # status is model output: never echo it raw. A value carrying CR/LF would + # split the runner's log line (a workflow-command injection vector), so + # flatten it and cap it before it reaches stderr. + shown="${status//[$'\r\n']/ }" + # printf '%s' never interprets backslash escapes; echo does under xpg_echo, + # so a literal "\n" in the value could still forge a log line through echo. + printf '%s\n' "post-lint-docs: status must be ok, findings or error (got '${shown:0:40}')" >&2 + exit 1 + ;; esac [[ -n "${summary}" ]] || { echo "post-lint-docs: summary is required" >&2; exit 1; } if [[ "${summary}" == *$'\n'* || "${summary}" == *$'\r'* ]]; then From c057eb8f40228b0869a9e7c9275f6b20f4fe00c8 Mon Sep 17 00:00:00 2001 From: Wayne Sun Date: Thu, 1 Oct 2026 12:41:11 -0400 Subject: [PATCH 2/3] fix(#6966): keep the dry-run preview from issuing workflow commands The dry-run branch printed the model's comment raw. Under GitHub Actions a line starting with "::" (the runner trims leading spaces first) is a workflow command, so a crafted comment could still reach the runner the way the rejected status did. Wrap the preview in a ::stop-commands:: block whose 128-bit token comes from /dev/urandom and is closed after the preview: nothing inside is interpreted, whatever its line breaks. This is the documented Actions mechanism and does not depend on how the runner splits lines, unlike prefixing each line. The building-custom-agents guide's example post-script echoed a raw STATUS the same way; it now flattens, caps and prints it with printf. docs/cli/agent.md shows the new dry-run output, captured from the generated script. Assisted-by: Claude (fix), Claude (review) Signed-off-by: Wayne Sun --- docs/cli/agent.md | 6 ++ docs/guides/user/building-custom-agents.md | 6 +- internal/agentnew/postscript_status_test.go | 75 +++++++++++++++++++ internal/agentnew/templates/post.sh.tmpl | 9 +++ internal/agentnew/testdata/golden/triage.txt | 9 +++ .../testdata/golden/validation-loop.txt | 9 +++ 6 files changed, 113 insertions(+), 1 deletion(-) diff --git a/docs/cli/agent.md b/docs/cli/agent.md index e413df7220..759e2cdfde 100644 --- a/docs/cli/agent.md +++ b/docs/cli/agent.md @@ -317,16 +317,22 @@ The tail of a successful run: • Cleaning up sandbox ✓ Sandbox deleted (45.7s) • Running post-script: .fullsend/scripts/post-lint-docs.sh +::stop-commands::0e3a20110bf83ae618cd260cbd0e67e4 **2 broken links added in docs/** ### Broken links - `docs/a.md:14` -> `../missing.md` - `docs/b.md:3` -> `/docs/gone.md` +::0e3a20110bf83ae618cd260cbd0e67e4:: post-lint-docs: dry run, not posting ✓ Post-script completed (0.1s) ``` +The `::stop-commands::` lines around the preview stop GitHub Actions from +reading anything the model wrote as a workflow command. The token is +random on every run, and a GitHub Actions log masks it as `***`. + With `--runtime claude`, the same agent against a real pull request — the model does the work, the schema gate runs, and the post-script still only prints: diff --git a/docs/guides/user/building-custom-agents.md b/docs/guides/user/building-custom-agents.md index 9e668c0f15..a2fb278711 100644 --- a/docs/guides/user/building-custom-agents.md +++ b/docs/guides/user/building-custom-agents.md @@ -446,7 +446,11 @@ case "${STATUS}" in echo "Agent needs more information" ;; *) - echo "ERROR: Unknown or missing status '${STATUS}'" + # STATUS is model output: flatten CR/LF and cap it before logging, and + # use printf, which never expands backslash escapes, so a crafted value + # cannot start a new log line (a "::" workflow command). + shown="${STATUS//[$'\r\n']/ }" + printf '%s\n' "ERROR: Unknown or missing status '${shown:0:40}'" exit 1 ;; esac diff --git a/internal/agentnew/postscript_status_test.go b/internal/agentnew/postscript_status_test.go index ecb16a5558..36e810055b 100644 --- a/internal/agentnew/postscript_status_test.go +++ b/internal/agentnew/postscript_status_test.go @@ -3,6 +3,8 @@ package agentnew import ( "os" "os/exec" + "path/filepath" + "regexp" "strings" "testing" ) @@ -103,3 +105,76 @@ func TestGeneratedPostScriptCapsALongInvalidStatus(t *testing.T) { t.Fatalf("expected the status capped at 40 characters, got: %q", stderr) } } + +// TestGeneratedPostScriptDryRunPreviewCannotIssueWorkflowCommands feeds a +// findings comment whose lines start with "::" (after LF, after CR, and +// after leading spaces, which the Actions runner trims) and checks that the +// dry-run preview keeps every one of them inside a stop-commands block with +// a random 32-hex token, opened before the preview and closed after it. +func TestGeneratedPostScriptDryRunPreviewCannotIssueWorkflowCommands(t *testing.T) { + if _, err := exec.LookPath("jq"); err != nil { + t.Skip("jq not installed; the generated post-script needs it") + } + script := renderPostScriptTo(t, t.TempDir()) + runDir := writeRunDir(t, map[string]any{ + "iteration-1": map[string]any{ + "status": "findings", + "summary": "s", + "comment": "intro\n::error::injected\r::add-mask::x\n ::warning::indented", + }, + }) + token := regexp.MustCompile(`^::stop-commands::([0-9a-f]{32})$`) + var tokens []string + for i := 0; i < 2; i++ { + stdout, stderr, err := runPostScript(t, script, runDir) + if err != nil { + t.Fatalf("dry run must exit 0, got %v; stderr:\n%s", err, stderr) + } + lines := strings.FieldsFunc(stdout, func(r rune) bool { return r == '\n' || r == '\r' }) + m := token.FindStringSubmatch(lines[0]) + if m == nil { + t.Fatalf("preview must open with ::stop-commands::<32 hex>, got first line %q", lines[0]) + } + if last := lines[len(lines)-1]; last != "::"+m[1]+"::" { + t.Fatalf("preview must close with ::%s::, got last line %q", m[1], last) + } + inside := strings.Join(lines[1:len(lines)-1], "\n") + for _, injected := range []string{"::error::injected", "::add-mask::x", "::warning::indented"} { + if !strings.Contains(inside, injected) { + t.Fatalf("%q must appear inside the stop-commands block, got:\n%s", injected, stdout) + } + } + tokens = append(tokens, m[1]) + } + if tokens[0] == tokens[1] { + t.Fatalf("the stop-commands token must be random per run, got %s twice", tokens[0]) + } +} + +// TestGeneratedPostScriptDryRunFailsClosedWithoutAToken pins the token +// guard: if od yields no token, an empty ::stop-commands:: would be +// rejected by the runner and the preview read as commands, so the script +// must exit 1 before printing anything. +func TestGeneratedPostScriptDryRunFailsClosedWithoutAToken(t *testing.T) { + if _, err := exec.LookPath("jq"); err != nil { + t.Skip("jq not installed; the generated post-script needs it") + } + binDir := t.TempDir() + if err := os.WriteFile(filepath.Join(binDir, "od"), []byte("#!/usr/bin/env bash\nexit 0\n"), 0o755); err != nil { + t.Fatal(err) + } + script := renderPostScriptTo(t, t.TempDir()) + runDir := writeRunDir(t, map[string]any{ + "iteration-1": map[string]any{"status": "findings", "summary": "s", "comment": "::error::injected"}, + }) + stdout, stderr, err := runPostScript(t, script, runDir, "PATH="+binDir+string(os.PathListSeparator)+os.Getenv("PATH")) + if err == nil { + t.Fatalf("a dry run without a token must fail; stdout:\n%s", stdout) + } + if strings.TrimSpace(stdout) != "" { + t.Fatalf("nothing may be printed without a token, got:\n%s", stdout) + } + if !strings.Contains(stderr, "could not generate a stop-commands token") { + t.Fatalf("expected the token guard's message, got:\n%s", stderr) + } +} diff --git a/internal/agentnew/templates/post.sh.tmpl b/internal/agentnew/templates/post.sh.tmpl index f2ff7bee8b..e1252584a5 100644 --- a/internal/agentnew/templates/post.sh.tmpl +++ b/internal/agentnew/templates/post.sh.tmpl @@ -127,7 +127,16 @@ REPO="${BASH_REMATCH[1]}" NUMBER="${BASH_REMATCH[3]}" if [[ "${__DRY_RUN_VAR__:-}" == "1" ]]; then + # The preview prints model output. Under GitHub Actions a line starting + # with "::" is a workflow command (the runner even trims leading spaces + # first), so wrap the preview in a stop-commands block whose token the + # model cannot guess: nothing inside it is interpreted, whatever its line + # breaks. + stop_token=$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n') + [[ ${#stop_token} -eq 32 ]] || { echo "post-__AGENT_NAME__: could not generate a stop-commands token" >&2; exit 1; } + printf '::stop-commands::%s\n' "${stop_token}" printf '**%s**\n\n%s\n' "${summary}" "${comment}" + printf '::%s::\n' "${stop_token}" echo "post-__AGENT_NAME__: dry run, not posting" >&2 exit 0 fi diff --git a/internal/agentnew/testdata/golden/triage.txt b/internal/agentnew/testdata/golden/triage.txt index caeeb2e4f3..46e5c50a05 100644 --- a/internal/agentnew/testdata/golden/triage.txt +++ b/internal/agentnew/testdata/golden/triage.txt @@ -279,7 +279,16 @@ REPO="${BASH_REMATCH[1]}" NUMBER="${BASH_REMATCH[3]}" if [[ "${POST_LINT_DOCS_DRY_RUN:-}" == "1" ]]; then + # The preview prints model output. Under GitHub Actions a line starting + # with "::" is a workflow command (the runner even trims leading spaces + # first), so wrap the preview in a stop-commands block whose token the + # model cannot guess: nothing inside it is interpreted, whatever its line + # breaks. + stop_token=$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n') + [[ ${#stop_token} -eq 32 ]] || { echo "post-lint-docs: could not generate a stop-commands token" >&2; exit 1; } + printf '::stop-commands::%s\n' "${stop_token}" printf '**%s**\n\n%s\n' "${summary}" "${comment}" + printf '::%s::\n' "${stop_token}" echo "post-lint-docs: dry run, not posting" >&2 exit 0 fi diff --git a/internal/agentnew/testdata/golden/validation-loop.txt b/internal/agentnew/testdata/golden/validation-loop.txt index 8b47497613..52032e6623 100644 --- a/internal/agentnew/testdata/golden/validation-loop.txt +++ b/internal/agentnew/testdata/golden/validation-loop.txt @@ -287,7 +287,16 @@ REPO="${BASH_REMATCH[1]}" NUMBER="${BASH_REMATCH[3]}" if [[ "${POST_LINT_DOCS_DRY_RUN:-}" == "1" ]]; then + # The preview prints model output. Under GitHub Actions a line starting + # with "::" is a workflow command (the runner even trims leading spaces + # first), so wrap the preview in a stop-commands block whose token the + # model cannot guess: nothing inside it is interpreted, whatever its line + # breaks. + stop_token=$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n') + [[ ${#stop_token} -eq 32 ]] || { echo "post-lint-docs: could not generate a stop-commands token" >&2; exit 1; } + printf '::stop-commands::%s\n' "${stop_token}" printf '**%s**\n\n%s\n' "${summary}" "${comment}" + printf '::%s::\n' "${stop_token}" echo "post-lint-docs: dry run, not posting" >&2 exit 0 fi From 3664e90f6e60a2ea7657d995540e63569b914eb3 Mon Sep 17 00:00:00 2001 From: Wayne Sun Date: Thu, 1 Oct 2026 15:28:06 -0400 Subject: [PATCH 3/3] docs(#6966): drop the token-masking sentence from the dry-run note The note now states only what the guard relies on: the token is random on every run, so the model cannot predict the closing line. Assisted-by: Claude (fix), Claude (review) Signed-off-by: Wayne Sun --- docs/cli/agent.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/cli/agent.md b/docs/cli/agent.md index 759e2cdfde..54df004c66 100644 --- a/docs/cli/agent.md +++ b/docs/cli/agent.md @@ -331,7 +331,8 @@ post-lint-docs: dry run, not posting The `::stop-commands::` lines around the preview stop GitHub Actions from reading anything the model wrote as a workflow command. The token is -random on every run, and a GitHub Actions log masks it as `***`. +random on every run, so the model cannot predict the line that closes the +block. With `--runtime claude`, the same agent against a real pull request — the model does the work, the schema gate runs, and the post-script still only