Skip to content

fix: prevent context injection into prompt template directives - #73586

Merged
pelikhan merged 12 commits into
mainfrom
pelikhan/prompt-template-injection
Nov 1, 2026
Merged

pelikhan merged 12 commits into
mainfrom
pelikhan/prompt-template-injection

Conversation

@pelikhan

Copy link
Copy Markdown
Collaborator

Fixes code-scanning alert 1042 (CWE-1336). Raw context substitutions could introduce conditional template markers that removed or changed author instructions. Sanitized comments could also retain active markers inside inline code because the sanitizer and renderer protected different code regions.

Context and experiment values now remain opaque through runtime imports and template branch parsing. Conditions resolve their values only after author-owned directives have been parsed; selected content restores literal values once, without further template processing. Single-pass interpolation also prevents recursive replacement of placeholders supplied by untrusted content.

The renderer shares the sanitizer's inline/fenced code-region handling. Runtime-import provenance and extracted agent/skill artifacts restore literal values without leaking internal tokens. Legitimate if/elseif conditions, experiment comparisons, and split prompts remain supported. Templating documentation describes the processing boundary.

Validation

  • Reproduced the original failure: both raw and inline-code {{#if false}} title payloads removed author instructions.
  • Added 77 adversarial regression cases covering titles, sanitized comments, inline/fenced contexts, branch markers, conditions, experiments, nested imports, split prompts, artifact boundaries, and recursive placeholders.
  • All 1,254 tests across 11 targeted suites passed.
  • make fmt-cjs, make lint-cjs, JavaScript typecheck, and make agent-report-progress passed. Lint reported warnings but no errors; impacted Go tests were skipped because there are no Go changes.

No alert dismissal or workflow dispatch was performed. Alert closure requires integration and rescanning.

Keep context and experiment values opaque until author-owned directives are parsed and selected. Align renderer code-region protection with sanitization and cover adversarial substitutions and runtime imports.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@pelikhan
pelikhan marked this pull request as ready for review November 1, 2026 00:46
Copilot AI dynamic review requested due to automatic review settings November 1, 2026 00:46
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

✅ Design Decision Gate 🏗️ completed the design decision gate check. See the comment below for the result and any generated ADR draft.

Design Decision Gate 🏗️

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from Design Decision Gate 🏗️ · routed: opus55 medium · ~11 AIC · ⌖ ~1 AIC · ◇ ~1 AIC · ⊞ 7.7K · ◷

🏗️ ADR gate enforced by Design Decision Gate 🏗️

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR Code Quality Reviewer completed the code quality review.

🔎 Code quality review by PR Code Quality Reviewer

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

✅ Test Quality Sentinel completed test quality analysis.

Test Quality Sentinel

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from Test Quality Sentinel · sonnet50 · ~12 AIC · ⌖ ~16 AIC · ◇ ~5 AIC · ⊞ 12.7K · ◷

🧪 Test quality analysis by Test Quality Sentinel

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

✅ Ponytail Reviewer completed successfully!

Ponytail Reviewer

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from Ponytail Reviewer · gpt56 · ~13 AIC · ⌖ ~6 AIC · ⊞ 15.1K · ◷

Generated by Ponytail Reviewer for #73586

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: no blocking findings

I found no actionable correctness, security, or performance issues in the changed lines. The specialist pass was unavailable because its file access was denied; this review is based on the pre-fetched diff and targeted source inspection only.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~2 AIC · ⌖ ~1 AIC · ⊞ 1.7K
Comment /review to run again

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skills-Based Review 🧠

Applied /diagnosing-bugs and /codebase-design (triage agent unavailable — used the documented fallback heuristic: classified as a security bugfix, prioritized interpolate_prompt.cjs, sanitize_content_core.cjs, runtime_import.cjs, render_template.cjs as high-impact files).

This is a solid, well-scoped fix for CWE-1336. The opaque-token (protectValue/restoreValues) design cleanly separates "untrusted context insertion" from "author-directive parsing," and reusing the sanitizer's code-region logic in the renderer closes a real gap (inline-code spans weren't previously protected the same way as fenced blocks). The new interpolate_prompt_injection.test.cjs suite is a strong, genuinely adversarial regression suite (prose/inline/fenced contexts × 14 injection payloads, plus nested imports, split prompts, and extracted-artifact boundaries).

Left two inline suggestions, neither blocking:

📋 Key Themes & Highlights

Key Themes

  • /codebase-design: sanitize_content_core.cjs's applyToNonCodeRegions now decides whether to re-throw a fence-parsing error based on arguments.length >= 3 rather than on whether a meaningful codeFn was supplied. This is a subtle trap for future refactors (e.g. a caller explicitly passing undefined as the third arg would silently flip behavior).
  • /tdd: The new regression suite doesn't cover a forged/replayed opaque marker (\x00VALUE_<uuid>_<n>\x00) appearing in untrusted content. Given a fresh UUID per run this should fail closed (via the value === undefined throw in restoreValues), but that fail-closed behavior isn't asserted anywhere, so a future change could silently make it fail open instead.

Verified, non-issues

  • Confirmed render_template.cjs's standalone main() (which doesn't thread resolveCondition) is not a separately invoked pipeline step — it's only listed in setup.sh's file-bundling manifest and used as a library by interpolate_prompt.cjs (protected) and firewall_blocked_domains.cjs (internal-only template, no untrusted context). Not a gap.
  • Verified evaluateExpression/processExpressions in runtime_import.cjs correctly leaves unresolved ${{ }} expressions unprotected (so they remain literal GHA syntax for later evaluation) while wrapping all resolved values — the protect/no-protect split is deliberate and correct.
  • Verified ordering in extract_inline_sub_agents.cjs: restoreValues runs before frontmatter preservation and experiment-reference resolution, so no opaque token can leak into the experiment-reference regex.

Caveat

  • Could not execute interpolate_prompt_injection.test.cjs in this review sandbox (network/cert restrictions blocked npm/vitest). The review relies on static reading of the new test file and diff; the PR description states all 1,254 tests across 11 suites passed, which I was unable to independently verify here.

Positive Highlights

  • ✅ Per-run randomUUID() markers (both in interpolate_prompt.cjs and render_template.cjs) prevent an attacker from pre-seeding a literal marker string that collides with the sanitizer's placeholder — a real improvement over the prior static \x00FENCE\x00 sentinel.
  • ✅ Single combined-regex interpolation pass in interpolateVariables correctly prevents recursive placeholder expansion (tested explicitly).
  • ✅ Documentation (templating.md) updated to describe the new processing order.

@copilot please address the review comments above.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · sonnet50 · ~191 AIC · ⌖ ~16 AIC · ⊞ 10.7K
Comment /matt to run again

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical defects cause runtime data to trigger unintended substitutions, discard authored skill instructions, and leave conditional branches and directive markers in rendered prompts.

5 open issues
What changed in this PR

This PR hardens gh-aw’s runtime prompt pipeline against context-supplied template directives by separating substituted data from author-owned template structure.

Changes:

  • Keeps context and experiment values opaque during directive parsing and uses single-pass interpolation.
  • Shares inline/fenced code handling and restores literal values in import provenance and extracted artifacts.
  • Adds adversarial regression coverage and documents the processing boundary.
File Description
docs/​src/​content/​docs/​reference/​templating.md Documents directive boundaries and runtime processing order.
actions/​setup/​js/​template_branch.cjs Resolves condition values after branch parsing.
actions/​setup/​js/​sanitize_content_core.cjs Adds optional transformations for protected code regions.
actions/​setup/​js/​runtime_import.cjs Carries value protection through nested file and URL imports.
actions/​setup/​js/​render_template.cjs Protects inline and fenced code during conditional rendering.
actions/​setup/​js/​is_truthy.cjs Supports multiline comparison operands.
actions/​setup/​js/​interpolate_prompt.cjs Coordinates opaque substitutions, condition evaluation, and literal restoration.
actions/​setup/​js/​interpolate_prompt_injection.test.cjs Covers injection payloads, imports, artifacts, and split prompts.
actions/​setup/​js/​extract_inline_sub_agents.cjs Adds literal restoration when writing sub-agent artifacts.
actions/​setup/​js/​extract_inline_skills.cjs Adds literal restoration when writing skill artifacts.

Review effort: Max (auto)

Comment thread actions/setup/js/interpolate_prompt.cjs Resolved
Comment thread actions/setup/js/render_template.cjs Resolved
Comment thread actions/setup/js/interpolate_prompt.cjs Resolved
Comment thread actions/setup/js/render_template.cjs Resolved
Address PR review feedback on inline artifact metadata, quoted conditions, code-region callbacks, replayed markers, and nested Dependabot example fences. Persist the author-owned opaque view across activation and deferred substitution and regenerate workflow locks for the provenance artifact.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@pelikhan

pelikhan commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Regarding the Design Decision Gate notice: no code change is needed because this is completion telemetry, not an actionable finding or ADR request. The no-op status is not being used as evidence of correctness.

@pelikhan

pelikhan commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Regarding the Code Quality Reviewer notice: no additional code change is needed because this comment only reports review completion. The associated review reported no blocking findings.

@pelikhan

pelikhan commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Regarding the Test Quality Sentinel notice: no code change is needed because it contains no actionable finding. Correctness is supported by the regression suites, not the no-op telemetry.

@pelikhan

pelikhan commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Regarding the Skills Reviewer notice: both associated inline suggestions were addressed in commit 651a6f2265, with replies posted and threads resolved. This completion notice requests no additional change.

@pelikhan

pelikhan commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Regarding the Ponytail Reviewer notice: no code change is needed because the comment reports completion without an actionable finding. The no-op telemetry is not being treated as independent validation.

@pelikhan

pelikhan commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Rechecked the repeated notices and prior responses below. No additional code changes are warranted, and completion/no-action telemetry is not being treated as correctness evidence.

Notice Prior response Decision
Design Decision Gate 6552098663 No actionable ADR finding; the prior response remains applicable.
Code Quality Reviewer 6552098796 Completion notice only; no additional change requested.
Test Quality Sentinel 6552098950 No actionable test finding; the prior response remains applicable.
Skills Reviewer 6552099133 Associated suggestions were already addressed in 651a6f2265; no further change needed.
Ponytail Reviewer 6552099353 No actionable finding; the prior response remains applicable.

pelikhan and others added 2 commits October 31, 2026 19:22
Remove all harness-owned prompt artifacts on success and failure, with cleanup regression coverage. Refresh golden expectations for the prompt literal sidecar artifact.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@pelikhan

pelikhan commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

@copilot merge main, fix ci failures

Copilot AI and others added 3 commits November 1, 2026 15:24
…ate-injection

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>

Copilot AI commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

@copilot merge main, fix ci failures

Merged latest main in 0d5b8df59d. Fixed the failing import-error assertion in 8dd0f95930 and 85a19a4abe, preserving upstream error provenance while checking that displayed diagnostics exclude filesystem internals. The full workflow unit suite, 248 impacted JavaScript tests, and make agent-report-progress passed; all 353 workflow locks are in sync. Secret scans passed. CodeQL skipped Go analysis because its database is too large. Changes are pushed; remote CI results are pending.

pelikhan and others added 5 commits November 1, 2026 07:52
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep missing-import location, hint, and raw-path privacy assertions while enforcing main's isolated error-chain contract. Pass subtest handles to operations so required assertions report on the correct test.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@pelikhan
pelikhan merged commit 8214416 into main Nov 1, 2026
49 checks passed
@pelikhan
pelikhan deleted the pelikhan/prompt-template-injection branch November 1, 2026 19:06
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v1.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants