diff options
| author | Roland Shoemaker <bracewell@google.com> | 2026-01-09 11:12:01 -0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-05 16:12:49 -0800 |
| commit | 994692847a2cd3efd319f0cb61a07c0012c8a4ff (patch) | |
| tree | 6420c79163711856095efff03655727438064651 /src/html/template/escape_test.go | |
| parent | 65c7d7a9fb3a9d1fbf1e702a211b8cc3a7bedb53 (diff) | |
| download | go-994692847a2cd3efd319f0cb61a07c0012c8a4ff.tar.xz | |
[release-branch.go1.26] html/template: properly escape URLs in meta content attributes
The meta tag can include a content attribute that contains URLs, which
we currently don't escape if they are inserted via a template action.
This can plausibly lead to XSS vulnerabilities if untrusted data is
inserted there, the http-equiv attribute is set to "refresh", and the
content attribute contains an action like `url={{.}}`.
Track whether we are inside of a meta element, if we are inside of a
content attribute, _and_ if the content attribute contains "url=". If
all of those are true, then we will apply the same URL escaping that we
use elsewhere.
Also add a new GODEBUG, htmlmetacontenturlescape, to allow disabling this
escaping for cases where this behavior is considered safe. The behavior
can be disabled by setting htmlmetacontenturlescape=0.
Updates #77954
Fixes #77972
Fixes CVE-2026-27142
Change-Id: I9bbca263be9894688e6ef1e9a8f8d2f4304f5873
Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/3360
Reviewed-by: Neal Patel <nealpatel@google.com>
Reviewed-by: Nicholas Husin <husin@google.com>
Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/3643
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/752081
Auto-Submit: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Bypass: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Diffstat (limited to 'src/html/template/escape_test.go')
| -rw-r--r-- | src/html/template/escape_test.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/html/template/escape_test.go b/src/html/template/escape_test.go index 003060e90f..49710c38b7 100644 --- a/src/html/template/escape_test.go +++ b/src/html/template/escape_test.go @@ -734,6 +734,16 @@ func TestEscape(t *testing.T) { "<script>var a = `${ var a = \"{{\"a \\\" d\"}}\" }`</script>", "<script>var a = `${ var a = \"a \\u0022 d\" }`</script>", }, + { + "meta content attribute url", + `<meta http-equiv="refresh" content="asd; url={{"javascript:alert(1)"}}; asd; url={{"vbscript:alert(1)"}}; asd">`, + `<meta http-equiv="refresh" content="asd; url=#ZgotmplZ; asd; url=#ZgotmplZ; asd">`, + }, + { + "meta content string", + `<meta http-equiv="refresh" content="{{"asd: 123"}}">`, + `<meta http-equiv="refresh" content="asd: 123">`, + }, } for _, test := range tests { @@ -1016,6 +1026,14 @@ func TestErrors(t *testing.T) { "<script>var tmpl = `asd ${return \"{\"}`;</script>", ``, }, + { + `{{if eq "" ""}}<meta>{{end}}`, + ``, + }, + { + `{{if eq "" ""}}<meta content="url={{"asd"}}">{{end}}`, + ``, + }, // Error cases. { @@ -2198,3 +2216,19 @@ func TestAliasedParseTreeDoesNotOverescape(t *testing.T) { t.Fatalf(`Template "foo" and "bar" rendered %q and %q respectively, expected equal values`, got1, got2) } } + +func TestMetaContentEscapeGODEBUG(t *testing.T) { + savedGODEBUG := os.Getenv("GODEBUG") + os.Setenv("GODEBUG", savedGODEBUG+",htmlmetacontenturlescape=0") + defer func() { os.Setenv("GODEBUG", savedGODEBUG) }() + + tmpl := Must(New("").Parse(`<meta http-equiv="refresh" content="asd; url={{"javascript:alert(1)"}}; asd; url={{"vbscript:alert(1)"}}; asd">`)) + var b strings.Builder + if err := tmpl.Execute(&b, nil); err != nil { + t.Fatalf("unexpected error: %s", err) + } + want := `<meta http-equiv="refresh" content="asd; url=javascript:alert(1); asd; url=vbscript:alert(1); asd">` + if got := b.String(); got != want { + t.Fatalf("got %q, want %q", got, want) + } +} |
