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.
For
ExpressionTemplate
to be useful in scenarios like HTML email, webhook URL, or URL-encodedPOST
body construction, a safer mechanism is needed for output encoding.For example, imagine we rewrite Serilog.Sinks.Email to use
ExpressionTemplate
, a message body might look like:Since the email is being fed exceptions from a running application, a malicious user might cause an error to be generated with HTML in the message:
Today, to defend against this an
htmlencode
user-defined function might be used:But, we all know how easily opt-in security measures can be overlooked.
This PR proposes to introduce a new type,
TemplateOutputEncoder
, that users (i.e. the Serilog.Sinks.Email assembly) can implement in order to automatically escape all output that's substituted into template holes. For example:The encoder is provided when parsing/compiling the template:
Opting out of encoding
The proposal introduces a new function in templates called
unsafe
, which can be used to opt out of escaping:Caveats
Note that basic HTML escaping as used in the example can't correctly/safely encode values that appear in
style
orscript
contexts. HTML is a familiar use case for the example, but it's not discussed in full here.Related work
The feature is based on the fork we use in Seq's webhook plug-in, which uses it for URI encoding within webhook URLs: https://github.com/datalust/seq-app-httprequest#configuration (see the URL row in the linked table).