Skip to content

fix: HTML-escape values in indented standalone partials - #700

Open
stencil-harness[bot] wants to merge 2 commits into
masterfrom
robot/issue-699
Open

stencil-harness[bot] wants to merge 2 commits into
masterfrom
robot/issue-699

Conversation

@stencil-harness

@stencil-harness stencil-harness Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Since 2.2.0 (#635, the fix for #614), a standalone partial tag on an indented line writes its values without HTML escaping. The same partial called inline, or on its own line at column 0, is escaped. Repro from #699:

var handlebars = Handlebars.Create();
handlebars.RegisterTemplate("p", "[{{v}}]");
handlebars.Compile("<div>\n    {{> p}}\n</div>")(new { v = "<b>&" });
// 2.2.0 – 2.5.0: "<div>\n    [<b>&]</div>"          ← unescaped
// this PR:       "<div>\n    [&lt;b&gt;&amp;]</div>"

To indent each line, PartialBinder renders the partial into a temporary writer and then passes the captured text to WriteWithIndent. That temporary writer was created with suppressEncoding: true, and WriteWithIndent writes with encode: false, so nothing on that path encoded. The same argument was used in all four indentation branches: InvokePartialWithFallback, plus the @partial-block, inline-partial and registered-template branches of InvokePartial. Everything the indented partial rendered was unescaped, including partials it called itself.

Fix

The temporary writer now takes the caller's writer.SuppressEncoding instead of true. Indented partials therefore escape exactly as the non-indented path does, which renders through the caller's writer. {{{triple}}} output and Configuration.NoEscape = true still produce unescaped output, as before.

Thanks to @nadrummond for the analysis in #699, which identifies the same root cause and fix.

Tests

New tests in PartialTests:

  • IndentedPartialEscapesValues: covers a registered partial, an inline partial, @partial-block, the missing-partial fallback to the partial block, and an indented partial that calls another partial
  • PartialIndentationKeepsValuesEscaped: the repro from the issue
  • IndentedPartialMatchesUnindentedEscaping: indented and non-indented output agree, and triple-stash stays raw
  • IndentedPartialHonorsNoEscape

7 of the 8 new tests fail against the current master code (only the NoEscape test passes there, as expected), and all pass with the fix. Full suite: 2352 passed, 0 failed.

Compatibility / release

This puts back the escaping that 2.1.x had and that non-indented partials still have. Templates that rely on the 2.2.0–2.5.0 unescaped output inside indented partials will now get escaped output. That output was a bug and a potential XSS vector, not a feature. Such templates should use {{{ }}}, exactly as they would need to for a non-indented partial. Recommend a patch release (2.5.1).

Fixes #699

🤖 Generated with Claude Code

Closes #699

The indented-partial rendering paths (registered, inline, @partial-block and
partial-block fallback) buffered output through an EncodedTextWriter created
with suppressEncoding: true, so {{value}} inside an indented partial was
written raw. The buffer now inherits the outer writer's SuppressEncoding,
matching the non-indented path (and still honoring NoEscape / triple-stash).

Fixes #699

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Exercise the missing-partial fallback branch with an actual indent, add a
nested-partial case and the reproduction from the issue report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

Indented standalone partial writes its values unescaped (regression in 2.2.0)

0 participants