Repository navigation
fix: HTML-escape values in indented standalone partials - #700
stencil-harness[bot] wants to merge 4 commits into
Conversation
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>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Automated adversarial review of 1570180: passCommit 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. |
|
Automated adversarial review of 5c83a6d: passCommit 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. |



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:
To indent each line,
PartialBinderrenders the partial into a temporary writer and then passes the captured text toWriteWithIndent. That temporary writer was created withsuppressEncoding: true, andWriteWithIndentwrites withencode: 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 ofInvokePartial.Fix
The temporary writer now takes the caller's
writer.SuppressEncodinginstead oftrue. Indented partials therefore escape exactly as the non-indented path does.{{{triple}}}output andConfiguration.NoEscape = truestill produce unescaped output, as before.WriteWithIndentis 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.<div>\n {{> p}}\n</div>, p=[{{v}}]<div>\n [<b>&]</div><div>\n [<b>&]</div><div>\n [<b>&]</div>{{> @partial-block}}…[<b>&]\n (<b>&)……[<b>&]\n (<b>&)…{{{v}}}in indented partialNoEscape/noEscape: true{{> missing}}inside{{#> p}}…{{/p}}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 andNoEscapecases. 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 currentmaster.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