Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/cli/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,23 @@ 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, 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
prints:
Expand Down
6 changes: 5 additions & 1 deletion docs/guides/user/building-custom-agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
180 changes: 180 additions & 0 deletions internal/agentnew/postscript_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package agentnew

import (
"os"
"os/exec"
"path/filepath"
"regexp"
"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)
}
}

// 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)
}
}
20 changes: 19 additions & 1 deletion internal/agentnew/templates/post.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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']/ }"
Comment thread
waynesun09 marked this conversation as resolved.
Resolved
# 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
Expand Down Expand Up @@ -118,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
Expand Down
20 changes: 19 additions & 1 deletion internal/agentnew/testdata/golden/triage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -270,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
Expand Down
20 changes: 19 additions & 1 deletion internal/agentnew/testdata/golden/validation-loop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -278,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
Expand Down
Loading