Skip to content

fix(core): insert template values verbatim to prevent $-sequence corruption - #55317

Closed
gorredinesh21 wants to merge 1 commit into
anomalyco:devfrom
gorredinesh21:fix/template-fill-dollar-sequence-v2
Closed

gorredinesh21 wants to merge 1 commit into
anomalyco:devfrom
gorredinesh21:fix/template-fill-dollar-sequence-v2

Conversation

@gorredinesh21

Copy link
Copy Markdown

Issue for this PR

Closes #52314

Type of change

  • Bug fix

What does this PR do?

String.prototype.replace expands $-sequences ($&, $', $`, $$) in the replacement string, so template fills with user-controlled values corrupt their input: a file path like /tmp/a$&b.ts filled into the formatter argv becomes /tmp/a$FILEb.ts, and a project path containing $' mangles the /init and /review prompt templates.

This ports the fix from #52315 to V2, as requested when that PR was closed: add a literalReplace() helper using a function replacer (the same approach the $ARGUMENTS fill in plugin/command.ts already uses) and use it at the three affected sites — the $FILE fill in formatter.ts and the ${path} fills in plugin/command.ts. First-match semantics are unchanged.

How did you verify your code works?

New test/template.test.ts covers plain fills and each $-sequence case. Mutation check: reverting the helper to a plain string replacer makes the suite fail; with the fix it passes. Existing formatter.test.ts and command.test.ts pass unchanged, and bun run typecheck + oxlint are clean in packages/core.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…uption

String.prototype.replace interprets $-sequences ($&, $', $`, $$) in
string replacements. Filling templates with user-controlled values (file
paths, project directories) via a string replacer silently corrupts the
result:

- formatter argv: a path like /tmp/a$&b.ts becomes /tmp/a$FILEb.ts, so
  the formatter is invoked on a nonexistent file
- /init and /review command templates: a project path containing $'
  mangles the assembled prompt

Add literalReplace() (function-replacer form, matching the existing
$ARGUMENTS fill in plugin/command.ts) and migrate the affected call
sites. First-match semantics are preserved.
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@opencode-agent opencode-agent Bot closed this Oct 14, 2026
@opencode-agent

Copy link
Copy Markdown
Contributor

Thanks for porting your fix from #52315 to v2, and for the clear write-up and the test that fails without the fix!

I think this duplicates #54918. That PR fixes #52314 at the same three places ($FILE in formatter.ts, and ${path} for /init and /review in plugin/command.ts), also using function replacers, and it adds tests with a $& filename and project path. It was opened a little before this one.

If you think something is missing there, such as a shared helper or more cases for $', $` and $$, reviewing or commenting on #54918 would be the best way to get it in.

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.

Formatter argv and prompt templates corrupt paths containing $-sequences ($&, $', $`, $$)

1 participant