fix: stop leaking internal SafeString type through subexpression results (issue #660) - #664
Merged
Merged
Conversation
…lts (issue #660) ReturnInvoke (added for issue #543, 2026-06-20) wrapped every writer-based helper's captured output in an internal sealed SafeString when used as a subexpression, so the outer expression wouldn't double-encode already-safe content. But only a few internal call sites were taught to unwrap it (EncodedTextWriter.Write<T>, WriteSafeString(object), HandlebarsUtils.IsFalsy, PartialBinder) — any other consumer, including reflection-based/typed helper binders in third-party packages this library has no visibility into, received an opaque internal type it could neither cast to string nor unwrap. Replaced the wrapper type with SafeStrings, a ConditionalWeakTable-backed marker keyed by object reference. ReturnInvoke now returns the captured string itself, invisibly marked as already-encoded, instead of boxing it in a new type. The value stays a genuine System.String all the way through — castable, reflectable, and indistinguishable from any other string to every consumer except the encoding writer, which is the only place the signal actually needs to matter. This also let PartialBinder revert to its original Cast<string>(pex.PartialName) and dropped the now-redundant SafeString cases in HandlebarsUtils.IsFalsy and HandlebarsExtensions.WriteSafeString(object). Added regression coverage: a subexpression's captured result is asserted to be typeof(string) and to survive a direct (string) cast, mirroring the reporter's Append(string, string) helper receiving a subexpression argument. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
rexm
enabled auto-merge
August 7, 2026 00:38
rexm
disabled auto-merge
August 7, 2026 00:38
rexm
enabled auto-merge
August 7, 2026 00:38
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
ReturnInvoke(added for issue Subexpression encoding/escaping issue #543 on 2026-06-20) wraps every writer-based helper's captured output in an internalsealed class SafeStringwhenever it's used as a subexpression, so the outer expression doesn't double-encode already-safe content (e.g. content written viaWriteSafeString).EncodedTextWriter.Write<T>,HandlebarsExtensions.WriteSafeString(object),HandlebarsUtils.IsFalsy, andPartialBinder. Any other consumer — including reflection-based/typed helper binders in third-party packages this library has no visibility into (e.g.Handlebars.Net.Helpers) — receives an opaqueinternaltype it can neither cast tostringnor unwrap.(string) arguments[0]cast on a subexpression's result throwsInvalidCastException: Unable to cast object of type 'HandlebarsDotNet.SafeString' to type 'System.String'.Fix
Replaced the wrapper type with
SafeStrings(source/Handlebars/IO/SafeStrings.cs), aConditionalWeakTable<string, object>-backed marker keyed by object reference.ReturnInvokenow returns the captured string itself, invisibly marked as already-encoded, instead of boxing it in a new type.The value stays a genuine
System.Stringthe entire way through — castable, reflectable, and indistinguishable from any other string to every consumer except the encoding writer, which is the only place the "already encoded" signal actually needs to matter. Reference-keyed lookup means marking never affects a string a caller didn't obtain from this exact capture (no risk from string interning, sinceStringWriter.ToString()always produces a fresh instance).This also let
PartialBinderrevert to its originalCast<string>(pex.PartialName)(dynamic partial names no longer need SafeString-aware unwrapping — they're just strings again) and dropped the now-redundantSafeStringcases inHandlebarsUtils.IsFalsyandHandlebarsExtensions.WriteSafeString(object).Fixes #660.
Test plan
dotnet test— full suite passes (1908/1908)HelperTests.SubexpressionWriteSafeStringNotDoubleEncoded,StandaloneWriteSafeStringNotEncoded) still pass — double-encoding fix is preservedIssue660_SubexpressionResultIsPlainString— asserts a subexpression's captured result istypeof(string), not the wrapper typeIssue660_SubexpressionResultIsCastableToTypedStringParameter— mirrors the reporter'sAppend(string, string)helper, direct-casting a subexpression argument tostringInvalidCastException : Unable to cast object of type 'HandlebarsDotNet.SafeString' to type 'System.String'when the source fix is reverted, confirming they're a faithful regression guard🤖 Generated with Claude Code