Skip to content

Commit aa69164

Browse files
gregory-mbradfitz
authored andcommitted
xsrftoken: escape colons
The current clean() replaces : with _ (colons are internally used as separators). This produce can produce same output for different inputs, for example the user _foo_ can obtain valid tokens for user :foo:. This CL replace colons with double colons instead of replacing them with underscores. Fixes golang/go#34308 Change-Id: I3e4148a0836e62fda1a5f0ba32b375121368afd3 Reviewed-on: https://go-review.googlesource.com/c/net/+/196457 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 1a5e07d commit aa69164

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

xsrftoken/xsrf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
// It is exported so clients may set cookie timeouts that match generated tokens.
2121
const Timeout = 24 * time.Hour
2222

23-
// clean sanitizes a string for inclusion in a token by replacing all ":"s.
23+
// clean sanitizes a string for inclusion in a token by replacing all ":" with "::".
2424
func clean(s string) string {
25-
return strings.Replace(s, ":", "_", -1)
25+
return strings.Replace(s, `:`, `::`, -1)
2626
}
2727

2828
// Generate returns a URL-safe secure XSRF token that expires in 24 hours.

xsrftoken/xsrf_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,32 @@ func TestValidToken(t *testing.T) {
3636

3737
// TestSeparatorReplacement tests that separators are being correctly substituted
3838
func TestSeparatorReplacement(t *testing.T) {
39-
tok := generateTokenAtTime("foo:bar", "baz", "wah", now)
40-
tok2 := generateTokenAtTime("foo", "bar:baz", "wah", now)
41-
if tok == tok2 {
42-
t.Errorf("Expected generated tokens to be different")
39+
separatorTests := []struct {
40+
name string
41+
token1 string
42+
token2 string
43+
}{
44+
{
45+
"Colon",
46+
generateTokenAtTime("foo:bar", "baz", "wah", now),
47+
generateTokenAtTime("foo", "bar:baz", "wah", now),
48+
},
49+
{
50+
"Colon and Underscore",
51+
generateTokenAtTime("key", ":foo:", "wah", now),
52+
generateTokenAtTime("key", "_foo_", "wah", now),
53+
},
54+
{
55+
"Colon and Double Colon",
56+
generateTokenAtTime("key", ":foo:", "wah", now),
57+
generateTokenAtTime("key", "::foo::", "wah", now),
58+
},
59+
}
60+
61+
for _, st := range separatorTests {
62+
if st.token1 == st.token2 {
63+
t.Errorf("%v: Expected generated tokens to be different", st.name)
64+
}
4365
}
4466
}
4567

0 commit comments

Comments
 (0)