Skip to content

Commit 94b4359

Browse files
committed
Merge pull request #8 from goji/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). - goji/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 issues. - Ref: https://docs.travis-ci.com/user/speeding-up-the-build/
2 parents 92a804c + 8fe8706 commit 94b4359

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

.travis.yml

Lines changed: 11 additions & 5 deletions
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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,13 @@ func sameOrigin(a, b *url.URL) bool {
142142
// compare securely (constant-time) compares the unmasked token from the request
143143
// against the real token from the session.
144144
func compareTokens(a, b []byte) bool {
145-
if subtle.ConstantTimeCompare(a, b) == 1 {
146-
return true
145+
// This is required as subtle.ConstantTimeCompare does not check for equal
146+
// lengths in Go versions prior to 1.3.
147+
if len(a) != len(b) {
148+
return false
147149
}
148150

149-
return false
151+
return subtle.ConstantTimeCompare(a, b) == 1
150152
}
151153

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

helpers_test.go

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

0 commit comments

Comments
 (0)