Skip to content

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

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

stencil-harness[bot] wants to merge 4 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.

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. {{{triple}}} output and Configuration.NoEscape = true still produce unescaped output, as before. WriteWithIndent is unchanged, so indentation and line endings are unchanged too.

Parity with handlebars.js

In handlebars.js 4.7.10, invokePartialWrapper (lib/handlebars/runtime.js) renders the partial normally, with escaping, and then prefixes each line of the finished string with the indent. Escaping happens before indentation and can't be affected by it. This PR gives Handlebars.Net the same order: encode while rendering, then indent.

.NET outputs below come from actual runs with v = "<b>&". The JS column follows the handlebars.js 4.7.10 runtime source. It also matches the handlebars.js run reported in #699.

Case handlebars.js master (2.5.0) this PR
<div>\n {{> p}}\n</div>, p=[{{v}}] <div>\n [&lt;b&gt;&amp;]</div> <div>\n [<b>&]</div> <div>\n [&lt;b&gt;&amp;]</div>
indented {{> @partial-block}} escaped raw escaped
indented inline partial escaped raw escaped
two-line partial, indented …[&lt;b&gt;&amp;]\n (&lt;b&gt;&amp;)… raw …[&lt;b&gt;&amp;]\n (&lt;b&gt;&amp;)…
{{{v}}} in indented partial raw raw raw
NoEscape / noEscape: true raw raw raw
not indented escaped escaped escaped
indented {{> missing}} inside {{#> p}}…{{/p}} throws "The partial missing could not be found" renders block, raw renders block, escaped

The last row has a difference that comes before this PR and is unchanged by it. When a partial inside a partial block can't be found, Handlebars.Net renders the block's content instead of throwing. It does this with or without indentation. This PR only makes that fallback content escaped, like the non-indented fallback already is.

Relation to #701

#701 by @nadrummond makes the identical change to PartialBinder.cs (same four lines). The two PRs differ only in tests. #701 covers the registered, @partial-block, missing-partial-fallback, inline and NoEscape cases. This PR covers all of those, plus a nested partial, triple-stash, indented vs. non-indented agreement, and a multi-line partial. That last test was added after #701's parity report. Either PR fixes #699, and only one should be merged.

Tests

New tests in PartialTests: IndentedPartialEscapesValues (registered, inline, @partial-block, missing-partial fallback, nested), PartialIndentationKeepsValuesEscaped, IndentedPartialMatchesUnindentedEscaping, MultiLineIndentedPartialEscapesEveryLine, IndentedPartialHonorsNoEscape. The escaping cases fail on the pre-fix code and pass with the fix.

dotnet test source/Handlebars.Test/Handlebars.Test.csproj: 2358 passed, 0 failed, after merging current master.

Compatibility / release

This puts back the escaping that 2.1.x had, that non-indented partials still have, and that handlebars.js has. 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. Such templates should use {{{ }}}, as they would 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>
stencil-harness Bot and others added 3 commits November 25, 2026 01:49
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>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stencil-harness

Copy link
Copy Markdown
Contributor Author

Automated adversarial review of 1570180: pass

Commit 1570180.

Read the diff and the PartialBinder/EncodedTextWriter/HandlebarsEnvironment code around it. The fix makes the temporary writer in all four indentation branches inherit writer.SuppressEncoding, which matches the non-indented path. Reverting the fix makes 7 of the PR's new tests fail (the NoEscape test passes either way), so the tests can catch the bug. Extra edge-case tests pass with the fix: nested indented partials (escaped once, no double encoding), SafeString helpers, triple-stash, string-returning helpers, multi-line #each, dynamic partial names with a tab indent and CRLF, and NoEscape with nested indents. The escaping cases fail without the fix. Full suite: 2357 passed, 0 failed. No issues found.

Report only: this review does not block merging.

@sonarqubecloud

Copy link
Copy Markdown

@stencil-harness

Copy link
Copy Markdown
Contributor Author

Automated adversarial review of 5c83a6d: pass

Commit 5c83a6d.

Reviewed the PartialBinder change: the temporary writer for indented partials now takes the caller's SuppressEncoding instead of always suppressing encoding. Full suite passes (2365, including 7 extra reviewer cases). With the fix reverted, the 8 new escaping tests fail, so they test the change. Extra cases compared indented and non-indented output for CRLF line endings, hash arguments, a lookup-chosen partial name, a tab indent, a nested indented partial, a SafeString helper, a delegate-registered partial and an indented @partial-block; output matched apart from the indent in every case. Tracing the wrapper writer showed no double-encoding. One user-visible change, consistent with non-indented partials: raw text written by a delegate partial is now escaped when indented. No defects found.

Report only: this review does not block merging.

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