fix(notifications): don't HTML-escape email template vars in place (#12413)
escapeStringMap mutated the variables map it was given. The per-recipient render loop reuses the same fields map for every grantee, and the plain-text template consumes the unescaped values, so escaping in place corrupted the text body of every recipient after the first and added one extra layer of HTML escaping to their HTML body (a space named "R&D" became "R&amp;D"). This affected notifications that fan out to multiple recipients, such as inviting a group to a space. escapeStringMap now returns a new map and leaves its input untouched, so every recipient renders from the original values. Signed-off-by: David Walter <david.walter@kiteworks.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
1ff20df320
commit
924af77963
3 files changed
+115
-3
No files matched your search
@@ -0,0 +1,12 @@
|
||||
Bugfix: Fix double HTML-escaping of notification emails for multiple recipients
|
||||
|
||||
The notifications service reuses the same template variables map for every
|
||||
recipient of an event (for example when a group is invited to a space). The
|
||||
helper that HTML-escapes those variables for the HTML email body escaped the
|
||||
map in place, so every recipient after the first received a plain-text body
|
||||
containing HTML entities and an HTML body with one additional layer of escaping
|
||||
per recipient (a space named `R&D` became `R&amp;D`). The helper now returns
|
||||
a new map and leaves the shared variables untouched, so every recipient renders
|
||||
from the original values.
|
||||
|
||||
https://github.com/owncloud/ocis/pull/12413
|
||||
@@ -203,9 +203,15 @@ func validateMime(incipit []byte) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// escapeStringMap returns a new map holding the HTML-escaped values of vars.
|
||||
// It must not mutate its input: callers (e.g. the per-recipient render loop)
|
||||
// reuse the same vars map across multiple renders, and the unescaped values are
|
||||
// also consumed by the plain-text template. Escaping in place would corrupt the
|
||||
// text body and add one extra layer of HTML escaping to every subsequent render.
|
||||
func escapeStringMap(vars map[string]string) map[string]string {
|
||||
for k := range vars {
|
||||
vars[k] = html.EscapeString(vars[k])
|
||||
escaped := make(map[string]string, len(vars))
|
||||
for k, v := range vars {
|
||||
escaped[k] = html.EscapeString(v)
|
||||
}
|
||||
return vars
|
||||
return escaped
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestEscapeStringMapDoesNotMutateInput guards the root cause: escapeStringMap
|
||||
// must return a new map and leave its argument untouched. The render loop reuses
|
||||
// the same vars map for every recipient and also feeds the unescaped values to
|
||||
// the plain-text template, so in-place mutation corrupts later renders.
|
||||
func TestEscapeStringMapDoesNotMutateInput(t *testing.T) {
|
||||
in := map[string]string{
|
||||
"SpaceName": "R&D",
|
||||
"SpaceSharer": "<Alice>",
|
||||
"ShareLink": "https://example.test/s/x?a=1&b=2",
|
||||
}
|
||||
want := map[string]string{
|
||||
"SpaceName": "R&D",
|
||||
"SpaceSharer": "<Alice>",
|
||||
"ShareLink": "https://example.test/s/x?a=1&b=2",
|
||||
}
|
||||
|
||||
out := escapeStringMap(in)
|
||||
|
||||
// Input must be unchanged.
|
||||
for k, v := range want {
|
||||
if in[k] != v {
|
||||
t.Errorf("escapeStringMap mutated input[%q]: got %q, want %q", k, in[k], v)
|
||||
}
|
||||
}
|
||||
|
||||
// Output must be escaped exactly once.
|
||||
if got, want := out["SpaceName"], "R&D"; got != want {
|
||||
t.Errorf("out[SpaceName] = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := out["SpaceSharer"], "<Alice>"; got != want {
|
||||
t.Errorf("out[SpaceSharer] = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
// Calling again with the same (still-unescaped) input must be stable.
|
||||
out2 := escapeStringMap(in)
|
||||
for k := range out {
|
||||
if out[k] != out2[k] {
|
||||
t.Errorf("escapeStringMap not stable for %q: %q vs %q", k, out[k], out2[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenderEmailTemplateRepeatableWithSharedVars reproduces the user-facing
|
||||
// symptom: the notifications render loop calls RenderEmailTemplate once per
|
||||
// recipient with the SAME vars map. Rendering twice with the same map must yield
|
||||
// identical output. Before the fix the map was escaped in place on the first
|
||||
// render, so the second render's text body and subject carried already-escaped
|
||||
// values and its HTML body was double-escaped.
|
||||
func TestRenderEmailTemplateRepeatableWithSharedVars(t *testing.T) {
|
||||
vars := map[string]string{
|
||||
"SpaceSharer": "A&B",
|
||||
"SpaceGrantee": "Bob",
|
||||
"SpaceName": "R&D",
|
||||
"ShareLink": "https://example.test/s/abc?a=1&b=2",
|
||||
}
|
||||
|
||||
first, err := RenderEmailTemplate(SharedSpace, "en", "en", "", "", vars)
|
||||
if err != nil {
|
||||
t.Fatalf("first render failed: %v", err)
|
||||
}
|
||||
second, err := RenderEmailTemplate(SharedSpace, "en", "en", "", "", vars)
|
||||
if err != nil {
|
||||
t.Fatalf("second render failed: %v", err)
|
||||
}
|
||||
|
||||
if first.Subject != second.Subject {
|
||||
t.Errorf("subject differs across renders with a shared vars map:\n first = %q\n second = %q", first.Subject, second.Subject)
|
||||
}
|
||||
if first.TextBody != second.TextBody {
|
||||
t.Errorf("text body differs across renders with a shared vars map:\n first = %q\n second = %q", first.TextBody, second.TextBody)
|
||||
}
|
||||
if first.HTMLBody != second.HTMLBody {
|
||||
t.Errorf("html body differs across renders with a shared vars map:\n first = %q\n second = %q", first.HTMLBody, second.HTMLBody)
|
||||
}
|
||||
|
||||
// The plain-text body must contain the raw ampersand, not an HTML entity.
|
||||
if !strings.Contains(first.TextBody, "R&D") {
|
||||
t.Errorf("text body should contain the unescaped space name %q, got: %q", "R&D", first.TextBody)
|
||||
}
|
||||
// The HTML body must contain the value escaped exactly once (not double-escaped).
|
||||
if !strings.Contains(first.HTMLBody, "R&D") {
|
||||
t.Errorf("html body should contain the single-escaped space name %q, got: %q", "R&D", first.HTMLBody)
|
||||
}
|
||||
if strings.Contains(first.HTMLBody, "R&amp;D") {
|
||||
t.Errorf("html body is double-escaped, contains %q: %q", "R&amp;D", first.HTMLBody)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user