superpowers icon indicating copy to clipboard operation
superpowers copied to clipboard

brainstorming visual companion: a screen containing $' (e.g. 'NT$' + amount) has its JS rendered as page text — wrapInFrame uses screen HTML as a replace() replacement string

Open andrew-yian opened this issue 2 months ago • 7 comments

  • [x] I searched existing issues and this is not a duplicate

Environment (required)

Field Value
Superpowers version 6.3.0
Harness (Claude Code, Cursor, etc.) Claude Code
Harness version 2.1.278
Your model + version Claude Opus 5 (1M context) — claude-opus-5[1m]
All plugins installed superpowers, claude-md-management, code-review, code-simplifier, commit-commands, context7, feature-dev, frontend-design, github, playwright, pr-review-toolkit, ralph-loop, security-guidance, supabase, typescript-lsp, mattpocock
OS + shell macOS 26.5.1 (Darwin 25.5.0), zsh; Node v22.12.0

Is this a Superpowers issue or a platform issue?

  • [x] I confirmed this issue does not occur without Superpowers installed

The defect is a single line of Superpowers' own code (skills/brainstorming/scripts/server.cjs:264). It cannot occur without the plugin.

What happened?

The brainstorming visual companion renders a screen's JavaScript source as page text, while HTML fragments inside its string literals get parsed into real DOM elements — a stray <select>, stray <input>s, a stray styled warning box. The mockup is unusable and its console shows SyntaxError: Invalid or unexpected token.

The trigger is any screen whose content contains the two-character sequence $' — most commonly a currency literal such as 'NT$' + amount or 'US$' + total. Money is extremely common in mockups, so this is not an exotic input.

Root cause

wrapInFrame() passes arbitrary screen HTML as the replacement string of String.prototype.replace:

// skills/brainstorming/scripts/server.cjs:264
function wrapInFrame(content) {
  return renderBranding(frameTemplate).replace('<!-- CONTENT -->', content);
}

In a replacement string, $$, $&, $`, $' and $n are special. $' means "the portion of the string after the match" — so the frame template's own tail (</div></div></body></html>) gets spliced into the middle of the screen's <script>, and the 'NT$ string literal is left unterminated.

That alone would only be a SyntaxError. The visible corruption comes from the next step:

// skills/brainstorming/scripts/server.cjs:412-413
if (html.includes('</body>')) {
  html = html.replace('</body>', helperInjection + '\n</body>');
}

replace with a string pattern hits the first </body> — which is now the injected one inside the <script>. helperInjection is '<script>\n' + helperScript + '\n</script>', and its </script> terminates the screen's script element early. Everything after it lands in <body> and is parsed as markup.

Note renderBranding() at server.cjs:255 already uses the safe idiom — html.split('<!-- BRANDING -->').join(brandMarkup()) — so the hazard was avoided in one place but not the other, nine lines apart.

Steps to reproduce

  1. Start the companion: skills/brainstorming/scripts/start-server.sh --project-dir <repo>
  2. Write a screen file into the session's content/ directory whose first line is not <!doctype/<html> (so it goes through wrapInFrame), containing a $' sequence inside a <script>:
<h2>Repro</h2>
<script>
(function () {
  var fmt = function (n) { return 'US$' + n.toFixed(2); };   // <-- the $' sequence
  document.body.insertAdjacentHTML('beforeend', '<p>' + fmt(1234.5) + '</p>');
})();
</script>
  1. Open the companion URL.

Minimal proof without the server:

const frame  = 'HEAD\n<!-- CONTENT -->\nTAIL\n</body>\n</html>\n';
const screen = "<script>var f = n => 'US$' + n;</script>";
console.log(frame.replace('<!-- CONTENT -->', screen));
// the frame's own tail is spliced in where $' appeared, and "</body>" now appears twice

Expected behavior

The screen renders exactly as authored; its <script> executes.

Actual behavior

  • </body> appears twice in the served HTML (the injected one sits inside the screen's <script>).
  • The helper injection's </script> closes the screen's script early.
  • The remainder of the screen's JS renders as visible page text; HTML inside its string literals becomes real elements.
  • Console: SyntaxError: Invalid or unexpected token. Nothing in the mockup is interactive.

Suggested fix

One line — a replacer function disables all $-substitution:

 function wrapInFrame(content) {
-  return renderBranding(frameTemplate).replace('<!-- CONTENT -->', content);
+  return renderBranding(frameTemplate).replace('<!-- CONTENT -->', () => content);
 }

(.split('<!-- CONTENT -->').join(content) would work equally well and would match renderBranding's existing style.)

Line 413's replacement is built from helperScript, which ships with the plugin and today contains none of the special sequences — so it is not currently triggerable, but it is the same class of hazard and would become live if helper.js ever gained a $'/$`/$&/$$.

Verification

Applied locally against a real 247-line screen that contained 'NT$' + n.toLocaleString(...):

JS leaked as page text </body> count console
before yes (4119 chars of source) 2 Invalid or unexpected token
after (() => content) no (679 chars — the screen's own copy) 1 clean

After the fix the mockup behaves as authored (editing a number updates the derived fields and adds a row). Verified by rendering the server's actual output in headless Chrome, and by extracting the patched wrapInFrame from server.cjs with vm and running it against the real frame template — not against a hand-written copy.

andrew-yian avatar Sep 22 '26 01:09 andrew-yian

Thanks for the detailed reproduction and the local fix. I would like to package the replacement-callback fix with regression coverage for literal dollar sequences, crediting your report. Are you already preparing a PR? I will prepare and test locally meanwhile, and avoid opening a competing PR if you are submitting yours.

luochen211 avatar Sep 22 '26 06:09 luochen211

No PR from me — please go ahead. I see you've already opened #2364; the replacement-callback fix plus the literal-token regression test covers exactly what I hit. Thanks for picking it up.

andrew-yian avatar Sep 22 '26 07:09 andrew-yian

coordinator dispatching implementing-a-ticket (full tier).

Acceptance criteria:

  1. Screen HTML containing $' (e.g. 'US$' + n) round-trips through wrapInFrame with no $-substitution corruption.
  2. Fix uses a safe insert (replacer function or split/join), matching renderBranding.
  3. Gate regression in tests/brainstorm-server covers the case.

Verification tier: full — guard: screen HTML must never be mutated by $-substitution in String.replace replacement strings. Base pin: origin/harness-trial @ c5cc551e244458581e68bb08f67f912f2ec195dd Worktree: ./tkt-2362-wrapinframe-dollar-quote/ PR target: kweston-66d/superpowers base harness-trial (reference obra/superpowers#2362 / Fixes obra/superpowers#2362). Note: cannot flip status:* labels (issues on upstream; fork has issues disabled).

kweston-66d avatar Sep 25 '26 20:09 kweston-66d

Implementer opened PR on fork: will post URL in follow-up if needed. Branch tkt-2362-wrapinframe-dollar-quote targets harness-trial on kweston-66d/superpowers.

kweston-66d avatar Sep 25 '26 21:09 kweston-66d

PR on fork: https://github.com/kweston-66d/superpowers/pull/3 (base harness-trial).

kweston-66d avatar Sep 25 '26 21:09 kweston-66d

Fixed by #2364, now merged into dev: the brainstorming server inserts screen content literally, so $', $& and friends no longer splice the frame into the page. It ships in the next release. Thanks for the clear report!

— Claude Opus 5.5, Claude Code 2.1.283, session 49301b31-b716-41b8-a316-80b8461e0de9, on behalf of @obra

obra avatar Sep 26 '26 19:09 obra

Fresh Cursor orchestration dispatch: implementing-a-ticket (full tier) against kweston-66d/superpowers:harness-trial @ 81b23ad2ea2137a5636d73e3d32078151791772c. Acceptance: screen HTML containing $' round-trips through wrapInFrame; insertion avoids replacement-string substitution; tests/brainstorm-server carries a regression. Worktree: ./tkt-2362-wrapinframe-dollar-quote/. Fork issues are disabled, so no status:* transition is available.

kweston-66d avatar Sep 26 '26 23:09 kweston-66d