Repository navigation
fix(core): insert template values verbatim to prevent $-sequence corruption - #52315
gorredinesh21 wants to merge 1 commit into
Conversation
…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.
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
Scope note for triage (related to the duplicate hint on #52314): #45892 and #33699 cover |
|
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 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊 |
Issue for this PR
Fixes #52314
Type of change
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.tsbuilds formatter argv with.replace("$FILE", filepath)- a file named/tmp/a$&b.tsbecomes/tmp/a$FILEb.ts, so the formatter is spawned on a nonexistent path and the file is silently never formatted/initand/reviewcommand templates (opencode + corecommand) fill"${path}"with the worktree directory and mangle the prompt the same waysession/reminders.tsalready uses the function-replacer form (.replace(x, () => v)), which inserts verbatim - these five sites predate it. The PR adds a smallliteralReplace()helper using that form and migrates the call sites. First-match.replacesemantics preserved.How did you verify your code works?
packages/core/test/template.test.tscovers$&/$'/$`/$$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.tspass inpackages/core(covering the migrated modules)tsgo --noEmitclean inpackages/coreandpackages/opencode; oxlint on the touched files: no new warningsScreenshots / recordings
N/A (no UI change)
Checklist