From ae5f59d7e9658025521a44de3fab0ec3cf6dbb40 Mon Sep 17 00:00:00 2001 From: Ludovic Henry Date: Fri, 20 Nov 2026 19:28:38 +0100 Subject: [PATCH] upstreaming: preserve exact agent logs safely --- .github/workflows/upstreaming-worker.lock.yml | 20 ++--- .github/workflows/upstreaming-worker.md | 21 +++-- SESSIONLOG.md | 1 + upstreaming/INTEGRATION.md | 12 +-- upstreaming/gh_aw_log_guard.py | 83 ------------------- upstreaming/tests/test_gh_aw_log_guard.py | 44 ---------- upstreaming/tests/test_workflow_contracts.py | 14 ++++ 7 files changed, 46 insertions(+), 149 deletions(-) delete mode 100644 upstreaming/gh_aw_log_guard.py delete mode 100644 upstreaming/tests/test_gh_aw_log_guard.py diff --git a/.github/workflows/upstreaming-worker.lock.yml b/.github/workflows/upstreaming-worker.lock.yml index d9c56bbc7bd..a52acb8f993 100644 --- a/.github/workflows/upstreaming-worker.lock.yml +++ b/.github/workflows/upstreaming-worker.lock.yml @@ -1,4 +1,4 @@ -# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"1ecac67c591c2dc4f11faa96584780eea9395b748d6db9a94ef3f7abff725ea5","body_hash":"c2baea3582364d048cf2d83f3eaae073738dda88c35c483d78b99c7d2d6689b9","compiler_version":"v1.2.2","strict":true,"agent_id":"pi","agent_model":"anthropic/claude-opus-5-5","engine_versions":{"pi":"1.5.0"}} +# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"f679f53df8bef4798d2652565464f0a94991cccf0fc695772d9dec94dc6c615d","body_hash":"c2baea3582364d048cf2d83f3eaae073738dda88c35c483d78b99c7d2d6689b9","compiler_version":"v1.2.2","strict":true,"agent_id":"pi","agent_model":"anthropic/claude-opus-5-5","engine_versions":{"pi":"1.5.0"}} # gh-aw-manifest: {"version":1,"secrets":["ANTHROPIC_API_KEY","COPILOT_GITHUB_TOKEN","GH_AW_DEFAULT_OTLP_ENDPOINT","GH_AW_DEFAULT_OTLP_HEADERS","GH_AW_GITHUB_MCP_SERVER_TOKEN","GH_AW_GITHUB_TOKEN","GITHUB_TOKEN","RISEPROJECT_APP_PRIVATE_KEY","RISEPROJECT_UPSTREAMING_APP_PRIVATE_KEY"],"actions":[{"repo":"actions/checkout","sha":"3d3c42e5aac5ba805825da76410c181273ba90b1","version":"v7.0.1"},{"repo":"actions/create-github-app-token","sha":"64ab729a6294000f5aa4a4bc0b73d1f32bc45510","version":"v3.3.0"},{"repo":"actions/download-artifact","sha":"9000827ccba6bdab643e8b6fd33ac0654aef8333","version":"v8.0.2"},{"repo":"actions/github-script","sha":"3a2844b7e9c422d3c10d287c895573f7108da1b3","version":"v9.0.0"},{"repo":"actions/setup-node","sha":"949feb2413d6458794dcd2491c4babbbce0c15c1","version":"v7.1.0"},{"repo":"actions/upload-artifact","sha":"043fb46d1a93c77aae656e7c1c64a875d1fc6a0a","version":"v7.0.1"},{"repo":"actions/upload-artifact","sha":"cf430e030ddbb5b0abf93d22962f4752f3646cd9","version":"v7.0.2"},{"repo":"astral-sh/setup-uv","sha":"20cfd1bf945f4377ade1205e4dbc17946fc9a30d","version":"v10.0.1"},{"repo":"github/gh-aw-actions/setup","sha":"53d0749e19424a918acd82b4d140264ce48f2f14","version":"v1.2.2"}],"containers":[{"image":"ghcr.io/github/gh-aw-firewall/agent:0.29.11","digest":"sha256:8774a73c351395b41d2ad0af4ddb4689007c67de3cd08bd9f43bb8a0e300690e","pinned_image":"ghcr.io/github/gh-aw-firewall/agent:0.29.11@sha256:8774a73c351395b41d2ad0af4ddb4689007c67de3cd08bd9f43bb8a0e300690e"},{"image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.29.11","digest":"sha256:88d2d0e315a504d93fd4fb8847fd0f6b7f4469f41a6fc6047b3dc9654c56ab2c","pinned_image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.29.11@sha256:88d2d0e315a504d93fd4fb8847fd0f6b7f4469f41a6fc6047b3dc9654c56ab2c"},{"image":"ghcr.io/github/gh-aw-firewall/squid:0.29.11","digest":"sha256:861739ecf924dd2db5e5fb652db2ad82aaafd1615623297ddabc8f41a36b2847","pinned_image":"ghcr.io/github/gh-aw-firewall/squid:0.29.11@sha256:861739ecf924dd2db5e5fb652db2ad82aaafd1615623297ddabc8f41a36b2847"},{"image":"ghcr.io/github/gh-aw-mcpg:v0.5.6","digest":"sha256:342cd624e1f179d91be2de7a146fc4f387092e62e6c8a47506d7932e113d2311","pinned_image":"ghcr.io/github/gh-aw-mcpg:v0.5.6@sha256:342cd624e1f179d91be2de7a146fc4f387092e62e6c8a47506d7932e113d2311"},{"image":"ghcr.io/github/gh-aw-node","digest":"sha256:8735428c2d4e602f90781ba0c571c400e8ff93c7ee1508a75d71f87e34e415cc","pinned_image":"ghcr.io/github/gh-aw-node@sha256:8735428c2d4e602f90781ba0c571c400e8ff93c7ee1508a75d71f87e34e415cc"},{"image":"ghcr.io/github/github-mcp-server:v2.1.0","digest":"sha256:f6056fc10bfffcc8c81a6417a2e9ca12808647e8dd6049642a4feaadee0e0f8a","pinned_image":"ghcr.io/github/github-mcp-server:v2.1.0@sha256:f6056fc10bfffcc8c81a6417a2e9ca12808647e8dd6049642a4feaadee0e0f8a"}],"mcp_servers":[{"name":"github","tools":["get_commit","get_file_contents","get_latest_release","get_me","get_pull_request","get_pull_request_comments","get_pull_request_diff","get_pull_request_files","get_pull_request_review_comments","get_pull_request_reviews","get_pull_request_status","get_release_by_tag","get_tag","issue_read","list_branches","list_commits","list_issue_types","list_issues","list_pull_requests","list_releases","list_starred_repositories","list_tags","pull_request_read","search_code","search_issues","search_pull_requests","search_repositories"]},{"name":"github-authenticated","tools":["actions_list","get_commit","get_file_contents","get_pull_request","get_pull_request_comments","get_pull_request_diff","get_pull_request_files","get_pull_request_review_comments","get_pull_request_reviews","get_pull_request_status","issue_read","list_commits","list_issues","list_pull_requests","pull_request_read","search_code","search_issues","search_pull_requests"]},{"name":"safeoutputs","tools":["noop","report_incomplete","report_missing_data","report_missing_tool","upload_artifact"]},{"name":"work-queue","tools":["work_queue_claim_finish","work_queue_dispatch_next","work_queue_explain","work_queue_read","work_queue_submit"]}],"threat_detection":{"mode":"enabled"},"admission":false,"work_queue":{"worker":true,"logical_contract":"056b332b022951fc3d1ae91c58cb202ea509dcef2a5d2c296a6cc0a9f8abc00a"},"agent_metadata":{}} # This file was automatically generated by gh-aw (v1.2.2). DO NOT EDIT. To debug this workflow, load the skill at https://github.com/github/gh-aw/blob/main/debug.md # @@ -733,15 +733,9 @@ jobs: env: GH_AW_SKILL_DIR: ".pi/skills" run: bash "${RUNNER_TEMP}/gh-aw/actions/restore_inline_skills.sh" - - uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 - with: - enable-cache: false - - env: - GH_AW_ACTIONS_DIR: ${{ runner.temp }}/gh-aw/actions - name: Guard the Actions log from untrusted workflow commands - run: | - set -euo pipefail - uv run --no-project python -m upstreaming.gh_aw_log_guard "$GH_AW_ACTIONS_DIR" + - id: suspend_commands + name: Suspend workflow commands around untrusted agent output + run: "set -euo pipefail\ntoken=\"upstreaming-stop-$(openssl rand -hex 32)\"\necho \"token=$token\" >> \"$GITHUB_OUTPUT\"\nprintf '::stop-commands::%s\\n' \"$token\"\n" - env: GH_AW_ACTIONS_DIR: ${{ runner.temp }}/gh-aw/actions name: Check out the contribution fork at the expected head (read-only) @@ -1398,6 +1392,12 @@ jobs: path: ${{ runner.temp }}/gh-aw/safeoutputs/upload-artifacts/ retention-days: ${{ vars.GH_AW_DEFAULT_ARTIFACT_RETENTION_DAYS || '1' }} if-no-files-found: ignore + - env: + STOP_COMMANDS_TOKEN: ${{ steps.suspend_commands.outputs.token }} + if: always() + name: Resume workflow commands after untrusted agent output + run: "set -euo pipefail\ntest -n \"$STOP_COMMANDS_TOKEN\"\nprintf '::%s::\\n' \"$STOP_COMMANDS_TOKEN\"" + - name: Validate recovery envelopes before upload id: validate_final_recovery_envelopes if: always() && steps.redact_secrets.outcome == 'success' diff --git a/.github/workflows/upstreaming-worker.md b/.github/workflows/upstreaming-worker.md index afd87d4c324..0ac7699a49c 100644 --- a/.github/workflows/upstreaming-worker.md +++ b/.github/workflows/upstreaming-worker.md @@ -68,15 +68,13 @@ safe-outputs: noop: report-as-issue: false pre-agent-steps: - - uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 - with: - enable-cache: false - - name: Guard the Actions log from untrusted workflow commands - env: - GH_AW_ACTIONS_DIR: ${{ runner.temp }}/gh-aw/actions + - name: Suspend workflow commands around untrusted agent output + id: suspend_commands run: | set -euo pipefail - uv run --no-project python -m upstreaming.gh_aw_log_guard "$GH_AW_ACTIONS_DIR" + token="upstreaming-stop-$(openssl rand -hex 32)" + echo "token=$token" >> "$GITHUB_OUTPUT" + printf '::stop-commands::%s\n' "$token" - name: Check out the contribution fork at the expected head (read-only) env: GH_AW_ACTIONS_DIR: ${{ runner.temp }}/gh-aw/actions @@ -120,6 +118,15 @@ pre-agent-steps: git config user.name "riseproject-upstreaming[bot]" git config user.email "359135081+riseproject-upstreaming[bot]@users.noreply.github.com" echo "checked out $(git rev-parse HEAD) into ./fork" +post-steps: + - name: Resume workflow commands after untrusted agent output + if: always() + env: + STOP_COMMANDS_TOKEN: ${{ steps.suspend_commands.outputs.token }} + run: | + set -euo pipefail + test -n "$STOP_COMMANDS_TOKEN" + printf '::%s::\n' "$STOP_COMMANDS_TOKEN" jobs: safe_outputs: pre-steps: diff --git a/SESSIONLOG.md b/SESSIONLOG.md index 30b57366855..9e9b749b7b8 100644 --- a/SESSIONLOG.md +++ b/SESSIONLOG.md @@ -29,3 +29,4 @@ - Added a fast ungrouped controller job that idempotently reacts `+1` to visible `/upstreaming` tracking-issue comments while leaving all parsing, authorization and command effects in serialized reconciliation. - Aligned the controller's cheap comment filter with first-nonblank-line command parsing so blank-line-prefixed commands are both acknowledged and reconciled. - Added a hash-pinned gh-aw v1.2.2 console guard that preserves canonical agent evidence while preventing untrusted model or MCP text from being parsed as GitHub Actions workflow commands. +- Replaced console-byte rewriting with GitHub's native stop-commands protocol: trusted pre/post agent steps bracket exact output with a random step-output token and always restore command processing. diff --git a/upstreaming/INTEGRATION.md b/upstreaming/INTEGRATION.md index 7d7e704099e..38ee83b4f40 100644 --- a/upstreaming/INTEGRATION.md +++ b/upstreaming/INTEGRATION.md @@ -41,11 +41,13 @@ release-path rehearsal still need live evidence. - The dispatcher and patch worker stay sandboxed. The worker's native detector currently uses a provider-specific CLI; Pi-only detection is an unresolved compatibility limitation and is not silently worked around. -- Before worker model execution, trusted code hash-checks the pinned gh-aw - v1.2.2 process runner and patches only its console-forwarding path. Every - colon byte in untrusted child output gains a following space in the Actions - log, so model and MCP content cannot become `::workflow-command::` syntax, - including across output chunks. Canonical artifact evidence stays unchanged. +- Before worker model execution, a trusted pre-agent step generates a + cryptographically random token, stores it as that trusted step's output, and + emits GitHub's `::stop-commands::` directive. An `if: always()` post-step resumes + command processing with that exact token after the engine exits. Agent and + MCP output therefore stays exact and searchable in both the Actions console + and canonical artifacts without being interpreted as workflow commands. The + random token is not exposed to the model. ## Deployed and observed remotely diff --git a/upstreaming/gh_aw_log_guard.py b/upstreaming/gh_aw_log_guard.py deleted file mode 100644 index b205f606918..00000000000 --- a/upstreaming/gh_aw_log_guard.py +++ /dev/null @@ -1,83 +0,0 @@ -# SPDX-FileCopyrightText: 2026 The RISE Project -# SPDX-License-Identifier: MIT -"""Hash-pinned guard against GitHub workflow commands in agent output. - -The canonical agent stream remains unchanged in its artifact. Only the child -process bytes forwarded to the Actions console are made command-safe. -""" - -from __future__ import annotations - -import argparse -import hashlib -from pathlib import Path - -SOURCE_SHA256 = "28c69eb1c56469eac2e1182fea6cde195ee95c698b8412b5ed85c821e331b8f6" -PATCHED_SHA256 = "74bb397cd5e0502d4a83c6c9e9a9d838d52d588b4402807f1f24b97178f21326" - -IMPORT_NEEDLE = '''const { StringDecoder } = require("string_decoder"); -''' -IMPORT_REPLACEMENT = '''const { StringDecoder } = require("string_decoder"); - -// Agent and tool output is untrusted. GitHub Actions interprets any line that -// begins with two colons as a runner command. Insert a space after every colon -// byte only in the console copy. Operating on bytes keeps this safe across -// output chunks and preserves the canonical stream collected for artifacts. -function workflowCommandSafe(data) { - let colonCount = 0; - for (const byte of data) if (byte === 0x3a) colonCount += 1; - if (colonCount === 0) return data; - const safe = Buffer.allocUnsafe(data.length + colonCount); - let target = 0; - for (const byte of data) { - safe[target++] = byte; - if (byte === 0x3a) safe[target++] = 0x20; - } - return safe; -} -''' -WRITE_REPLACEMENTS = { - "process.stdout.write(data)": "process.stdout.write(workflowCommandSafe(data))", - "process.stderr.write(data)": "process.stderr.write(workflowCommandSafe(data))", -} - - -def digest(data: bytes) -> str: - return hashlib.sha256(data).hexdigest() - - -def patch_bytes(source: bytes, *, verify_source: bool = True) -> bytes: - if verify_source and digest(source) != SOURCE_SHA256: - raise RuntimeError("gh-aw process_runner.cjs does not match pinned v1.2.2 input") - text = source.decode() - if text.count(IMPORT_NEEDLE) != 1: - raise RuntimeError("gh-aw process_runner import anchor is missing or ambiguous") - text = text.replace(IMPORT_NEEDLE, IMPORT_REPLACEMENT) - for old, new in WRITE_REPLACEMENTS.items(): - if text.count(old) != 1: - raise RuntimeError(f"gh-aw process_runner write anchor is missing or ambiguous: {old}") - text = text.replace(old, new) - return text.encode() - - -def apply(actions_dir: Path) -> None: - path = actions_dir / "process_runner.cjs" - source = path.read_bytes() - if PATCHED_SHA256 != "TO_BE_FILLED" and digest(source) == PATCHED_SHA256: - return - patched = patch_bytes(source) - if PATCHED_SHA256 == "TO_BE_FILLED" or digest(patched) != PATCHED_SHA256: - raise RuntimeError("gh-aw process_runner.cjs patched output hash mismatch") - path.write_bytes(patched) - - -def main(argv=None) -> int: - parser = argparse.ArgumentParser() - parser.add_argument("actions_dir", type=Path) - args = parser.parse_args(argv) - apply(args.actions_dir) - return 0 - - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/upstreaming/tests/test_gh_aw_log_guard.py b/upstreaming/tests/test_gh_aw_log_guard.py deleted file mode 100644 index 9415d56ec03..00000000000 --- a/upstreaming/tests/test_gh_aw_log_guard.py +++ /dev/null @@ -1,44 +0,0 @@ -# SPDX-FileCopyrightText: 2026 The RISE Project -# SPDX-License-Identifier: MIT - -import subprocess -import unittest - -from upstreaming import gh_aw_log_guard - - -SYNTHETIC = b'''const { StringDecoder } = require("string_decoder"); -function forwardOut(data) { return process.stdout.write(data); } -function forwardErr(data) { return process.stderr.write(data); } -''' - - -class TestGhAwLogGuard(unittest.TestCase): - def test_patch_rejects_unpinned_runtime(self): - with self.assertRaisesRegex(RuntimeError, "does not match pinned"): - gh_aw_log_guard.patch_bytes(SYNTHETIC) - - def test_console_copy_cannot_form_workflow_command_across_chunks(self): - patched = gh_aw_log_guard.patch_bytes(SYNTHETIC, verify_source=False).decode() - script = patched + ''' -process.stdout.write(workflowCommandSafe(Buffer.from(":"))); -process.stdout.write(workflowCommandSafe(Buffer.from(":add-matcher::bad"))); -process.stdout.write("\\n"); -process.stdout.write(workflowCommandSafe(Buffer.from("::error::bad"))); -''' - result = subprocess.run(["node", "-e", script], check=True, capture_output=True) - output = result.stdout.decode() - self.assertNotIn("::", output) - self.assertIn(": : add-matcher: : bad", output) - self.assertIn(": : error: : bad", output) - - def test_patch_changes_only_forwarded_console_bytes(self): - patched = gh_aw_log_guard.patch_bytes(SYNTHETIC, verify_source=False).decode() - self.assertIn("process.stdout.write(workflowCommandSafe(data))", patched) - self.assertIn("process.stderr.write(workflowCommandSafe(data))", patched) - self.assertNotIn("stdout = collect(stdout, workflowCommandSafe", patched) - self.assertNotIn("stderr = collect(stderr, workflowCommandSafe", patched) - - -if __name__ == "__main__": - unittest.main() diff --git a/upstreaming/tests/test_workflow_contracts.py b/upstreaming/tests/test_workflow_contracts.py index 3b6f315cecb..328a6635bf5 100644 --- a/upstreaming/tests/test_workflow_contracts.py +++ b/upstreaming/tests/test_workflow_contracts.py @@ -195,6 +195,20 @@ def test_command_acknowledgement_is_fast_idempotent_and_not_serialized(self): self.assertEqual(reconcile["concurrency"]["group"], "python-wheels-upstreaming-controller") self.assertFalse(reconcile["concurrency"]["cancel-in-progress"]) + def test_worker_brackets_untrusted_output_with_stop_commands(self): + worker = source("upstreaming-worker") + before = worker["pre-agent-steps"][0] + self.assertIn("openssl rand -hex 32", before["run"]) + self.assertEqual(before["id"], "suspend_commands") + self.assertIn('echo "token=$token" >> "$GITHUB_OUTPUT"', before["run"]) + self.assertIn("::stop-commands::%s", before["run"]) + after = worker["post-steps"][0] + self.assertEqual(after["if"], "always()") + self.assertEqual(after["env"]["STOP_COMMANDS_TOKEN"], + "${{ steps.suspend_commands.outputs.token }}") + self.assertIn("printf '::%s::", after["run"]) + self.assertNotIn("gh_aw_log_guard", (WF / "upstreaming-worker.md").read_text()) + def test_worker_forbids_unrelated_fork_ci_debug_changes(self): worker = source("upstreaming-worker") self.assertNotIn("github", worker["tools"])