Skip to content

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

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

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

Conversation

@gorredinesh21

@gorredinesh21 gorredinesh21 commented Sep 30, 2026 •

Copy link
Copy Markdown

Issue for this PR

Fixes #52314

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Several template fills pass user-controlled paths as the string replacement to String.prototype.replace, so a $-sequence in the path ($&, $', $`, $$) corrupts the result (JS expands them as match references):

  • format/index.ts builds formatter argv with .replace("$FILE", filepath) - a file named /tmp/a$&b.ts becomes /tmp/a$FILEb.ts, so the formatter is spawned on a nonexistent path and the file is silently never formatted
  • the /init and /review command templates (opencode + core command) fill "${path}" with the worktree directory and mangle the prompt the same way

session/reminders.ts already uses the function-replacer form (.replace(x, () => v)), which inserts verbatim - these five sites predate it. The PR adds a small literalReplace() helper using that form and migrates the call sites. First-match .replace semantics preserved.

How did you verify your code works?

  • new packages/core/test/template.test.ts covers $&/$'/$`/$$ insertion, literal placeholder matching, first-occurrence semantics, and unchanged passthrough - and the $-sequence case fails against the old string-replacer form (checked by mutation)
  • bun test test/command.test.ts test/plugin.test.ts pass in packages/core (covering the migrated modules)
  • tsgo --noEmit clean in packages/core and packages/opencode; oxlint on the touched files: no new warnings

Screenshots / recordings

N/A (no 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) 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 worktree path containing $'
  mangles the assembled prompt

Add literalReplace() (function-replacer form, same approach already used
in session/reminders.ts) and migrate the affected call sites. First-match
semantics are preserved.
@github-actions github-actions Bot added needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Sep 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Sep 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@gorredinesh21

Copy link
Copy Markdown
Author

Scope note for triage (related to the duplicate hint on #52314): #45892 and #33699 cover $-expansion in the edit tool's replacement text (the file-write path, where the model's newString gets expanded). This PR covers the template-fill sites instead - the formatter $FILE argv and the "${path}" fills behind /init and /review - which are separate call sites that are still unfixed on current dev (format/index.ts:82). Same JS footgun class, complementary fixes.

@thdxr

thdxr commented Oct 10, 2026

Copy link
Copy Markdown
Member

This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.

— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

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 ($&, $', $`, $$)

2 participants