Skip to content

Commit fc3cfc6

Browse files
committed
Merge pull request #24 from gorilla/compare-token-fix
[bugfix] Compare token fix - subtle.ConstantTimeCompare did not check for matching slice lengths prior to Go 1.3 (fixed in https://codereview.appspot.com/118750043). - gorilla/csrf was released a year after this came into place. - Our TravisCI tests did not test against older versions of Go, and this wasn't caught as a result. - Have added Go 1.2 and Go 1.3 to the TravisCI config to address any future regressions.
2 parents 5af6691 + a5a43fb commit fc3cfc6

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

.travis.yml

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
language: go
22
sudo: false
3-
go:
4-
- 1.4
5-
- 1.5
6-
- tip
3+
4+
matrix:
5+
include:
6+
- go: 1.2
7+
- go: 1.3
8+
- go: 1.4
9+
- go: 1.5
10+
- go: tip
11+
712
install:
813
- go get golang.org/x/tools/cmd/vet
14+
915
script:
1016
- go get -t -v ./...
11-
- diff -u <(echo -n) <(gofmt -d -s .)
17+
- diff -u <(echo -n) <(gofmt -d .)
1218
- go tool vet .
1319
- go test -v -race ./...

helpers.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ func sameOrigin(a, b *url.URL) bool {
150150
// compare securely (constant-time) compares the unmasked token from the request
151151
// against the real token from the session.
152152
func compareTokens(a, b []byte) bool {
153-
if subtle.ConstantTimeCompare(a, b) == 1 {
154-
return true
153+
// This is required as subtle.ConstantTimeCompare does not check for equal
154+
// lengths in Go versions prior to 1.3.
155+
if len(a) != len(b) {
156+
return false
155157
}
156158

157-
return false
159+
return subtle.ConstantTimeCompare(a, b) == 1
158160
}
159161

160162
// xorToken XORs tokens ([]byte) to provide unique-per-request CSRF tokens. It

helpers_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,14 @@ func TestTemplateField(t *testing.T) {
254254
templateField, expectedField)
255255
}
256256
}
257+
258+
func TestCompareTokens(t *testing.T) {
259+
// Go's subtle.ConstantTimeCompare prior to 1.3 did not check for matching
260+
// lengths.
261+
a := []byte("")
262+
b := []byte("an-actual-token")
263+
264+
if v := compareTokens(a, b); v == true {
265+
t.Fatalf("compareTokens failed on different tokens: got %v want %v", v, !v)
266+
}
267+
}

0 commit comments

Comments
 (0)