fix(security): sanitize user-controlled fields before email rendering - #210
Open
Emmanuelchukwunonso wants to merge 2 commits into
Open
fix(security): sanitize user-controlled fields before email rendering#210Emmanuelchukwunonso wants to merge 2 commits into
Emmanuelchukwunonso wants to merge 2 commits into
Conversation
Investigation: all 4 email templates (payment completed, escrow
expiration warning, payment failed, password reset) already use
html/template (not text/template), and none use template.HTML(...) to
disable escaping — confirmed empirically that html/template already
contextually escapes malicious markup (e.g. "<script>...</script>"
renders as "<script>...</script>") for every {{.Field}}
substitution. Subject lines carry no user-controlled data, so header
injection isn't reachable there either.
Added sanitizeEmailField() as defense-in-depth per the issue's
acceptance criteria: strips HTML/script tags and control characters
(including CR/LF) from user, recipient, and reason fields before they
enter template data, so the property doesn't silently depend on every
future template staying on html/template.
Also fixed (blocking, unrelated to this issue, pre-existing on main):
- handlers/auth.go: stray `})` after ResetPassword's closing brace,
a syntax error blocking the whole module from building.
- go.mod: testcontainers-go v0.17.1 doesn't exist on the module
proxy; pinned to v0.17.0.
Test plan:
- go build ./services/...
- go test ./services/... -v (existing suite + new
email_sanitization_test.go covering tag stripping, control-character
stripping, whitespace trimming, and html/template's own escaping)
Closes parkerwinner#194
Owner
|
fix conflict |
|
@Emmanuelchukwunonso Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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
Investigated #194's premise before implementing: all 4 email templates already use
html/template(nottext/template), and none disable escaping viatemplate.HTML(...). Verified empirically thathtml/templatealready contextually escapes malicious markup for every{{.Field}}substitution (e.g.<script>alert(1)</script>renders as<script>alert(1)</script>). Subject lines carry no user-controlled data, so header injection isn't reachable there either.Added
sanitizeEmailField()as defense-in-depth per the issue's literal acceptance criteria ("sanitize all user input before email rendering"): strips HTML/script tags and control characters (including CR/LF) fromUserName,RecipientAccount, andReasonfields before they enter template data, so this property doesn't silently depend on every future template staying onhtml/template.Also fixed (blocking, unrelated to this issue, pre-existing on
main):handlers/auth.go: stray})afterResetPassword's closing brace — a syntax error that blocks the whole module from building.go.mod:testcontainers-go v0.17.1doesn't exist on the module proxy; pinned tov0.17.0.Test plan
go build ./services/...go test ./services/... -v— existing suite passes unchangedemail_sanitization_test.go: tag stripping, control-character stripping, whitespace trimming, and a pinning test forhtml/template's own auto-escapingCloses #194