Skip to content

Commit ca9cc8d

Browse files
wuhan005unknwon
andauthored
csrf: register type for gob serialization (#35)
Co-authored-by: Joe Chen <[email protected]>
1 parent 394a84e commit ca9cc8d

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

.github/workflows/go.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,38 @@ jobs:
3636
go-version: [ 1.18.x, 1.19.x ]
3737
platform: [ ubuntu-latest, macos-latest, windows-latest ]
3838
runs-on: ${{ matrix.platform }}
39+
steps:
40+
- name: Install Go
41+
uses: actions/setup-go@v2
42+
with:
43+
go-version: ${{ matrix.go-version }}
44+
- name: Checkout code
45+
uses: actions/checkout@v2
46+
- name: Run tests with coverage
47+
run: go test -v -race -coverprofile=coverage -covermode=atomic ./... -run !TestGobSerialization
48+
- name: Upload coverage report to Codecov
49+
uses: codecov/[email protected]
50+
with:
51+
file: ./coverage
52+
flags: unittests
53+
54+
redis:
55+
name: Redis
56+
strategy:
57+
matrix:
58+
go-version: [ 1.18.x, 1.19.x ]
59+
platform: [ ubuntu-latest ]
60+
runs-on: ${{ matrix.platform }}
61+
services:
62+
redis:
63+
image: redis:4
64+
options: >-
65+
--health-cmd "redis-cli ping"
66+
--health-interval 10s
67+
--health-timeout 5s
68+
--health-retries 5
69+
ports:
70+
- 6379:6379
3971
steps:
4072
- name: Install Go
4173
uses: actions/setup-go@v2
@@ -45,6 +77,9 @@ jobs:
4577
uses: actions/checkout@v2
4678
- name: Run tests with coverage
4779
run: go test -v -race -coverprofile=coverage -covermode=atomic ./...
80+
env:
81+
REDIS_HOST: localhost
82+
REDIS_PORT: 6379
4883
- name: Upload coverage report to Codecov
4984
uses: codecov/[email protected]
5085
with:

csrf.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package csrf
77

88
import (
9+
"encoding/gob"
910
"fmt"
1011
"math/rand"
1112
"net/http"
@@ -17,6 +18,10 @@ import (
1718
"github.com/flamego/session"
1819
)
1920

21+
func init() {
22+
gob.Register(time.Time{})
23+
}
24+
2025
// CSRF represents a CSRF service and is used to get the current token and
2126
// validate a suspect token.
2227
type CSRF interface {

csrf_test.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"net/http"
1010
"net/http/httptest"
1111
"net/url"
12+
"os"
1213
"runtime"
1314
"strings"
1415
"testing"
1516
"time"
1617

18+
"github.com/flamego/session/redis"
1719
"github.com/stretchr/testify/assert"
1820

1921
"github.com/flamego/flamego"
@@ -238,8 +240,8 @@ func TestInvalid(t *testing.T) {
238240
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
239241
f.ServeHTTP(resp, req)
240242

241-
assert.Equal(t, resp.Code, test.wantCode)
242-
assert.Equal(t, resp.Body.String(), test.wantBody)
243+
assert.Equal(t, test.wantCode, resp.Code)
244+
assert.Equal(t, test.wantBody, resp.Body.String())
243245
})
244246

245247
t.Run("invalid HTTP header", func(t *testing.T) {
@@ -251,8 +253,8 @@ func TestInvalid(t *testing.T) {
251253
req.Header.Set(defaultHeader, "invalid")
252254
f.ServeHTTP(resp, req)
253255

254-
assert.Equal(t, resp.Code, test.wantCode)
255-
assert.Equal(t, resp.Body.String(), test.wantBody)
256+
assert.Equal(t, test.wantCode, resp.Code)
257+
assert.Equal(t, test.wantBody, resp.Body.String())
256258
})
257259
})
258260
}
@@ -289,7 +291,7 @@ func TestTokenExpired(t *testing.T) {
289291
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
290292
f.ServeHTTP(resp, req)
291293

292-
assert.Equal(t, resp.Code, http.StatusOK)
294+
assert.Equal(t, http.StatusOK, resp.Code)
293295

294296
// NOTE: It appears that time.Now().UnixNano() sometimes is the same if the test
295297
// runs too faster (within the same second) on Windows, which results generating
@@ -306,7 +308,37 @@ func TestTokenExpired(t *testing.T) {
306308
req.Header.Set("Cookie", cookie)
307309
f.ServeHTTP(resp, req)
308310

309-
assert.Equal(t, resp.Code, http.StatusOK)
311+
assert.Equal(t, http.StatusOK, resp.Code)
310312
assert.NotEmpty(t, resp.Body.String())
311313
assert.NotEqual(t, token, resp.Body.String())
312314
}
315+
316+
func TestGobSerialization(t *testing.T) {
317+
f := flamego.NewWithLogger(&bytes.Buffer{})
318+
319+
const db = 15
320+
f.Use(session.Sessioner(session.Options{
321+
Initer: redis.Initer(),
322+
Config: redis.Config{
323+
Options: &redis.Options{
324+
Addr: os.ExpandEnv("$REDIS_HOST:$REDIS_PORT"),
325+
DB: db,
326+
},
327+
},
328+
}))
329+
f.Use(Csrfer())
330+
331+
var token string
332+
f.Get("/touch", func(x CSRF) string {
333+
token = x.Token()
334+
return token
335+
})
336+
337+
resp := httptest.NewRecorder()
338+
req, err := http.NewRequest(http.MethodGet, "/touch", nil)
339+
assert.NoError(t, err)
340+
341+
f.ServeHTTP(resp, req)
342+
assert.Equal(t, http.StatusOK, resp.Code)
343+
assert.Equal(t, token, resp.Body.String())
344+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ require (
1010

1111
require (
1212
github.com/alecthomas/participle/v2 v2.0.0-beta.5 // indirect
13+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
1314
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
1416
github.com/fatih/color v1.13.0 // indirect
17+
github.com/go-redis/redis/v8 v8.11.5 // indirect
1518
github.com/mattn/go-colorable v0.1.9 // indirect
1619
github.com/mattn/go-isatty v0.0.14 // indirect
1720
github.com/pkg/errors v0.9.1 // indirect

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,31 @@ github.com/alecthomas/assert/v2 v2.0.3 h1:WKqJODfOiQG0nEJKFKzDIG3E29CN2/4zR9XGJz
22
github.com/alecthomas/participle/v2 v2.0.0-beta.5 h1:y6dsSYVb1G5eK6mgmy+BgI3Mw35a3WghArZ/Hbebrjo=
33
github.com/alecthomas/participle/v2 v2.0.0-beta.5/go.mod h1:RC764t6n4L8D8ITAJv0qdokritYSNR3wV5cVwmIEaMM=
44
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
5+
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
6+
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
57
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
68
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
79
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
11+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
812
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
913
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
1014
github.com/flamego/flamego v1.7.0 h1:c1Lu16PBAZKkpsjHw42vwotdoQnMMpUi60ITP41W12w=
1115
github.com/flamego/flamego v1.7.0/go.mod h1:dnVMBJyHKaxjcqRVN93taSK+YB/9p+Op1GdLIuA1hFQ=
1216
github.com/flamego/session v1.2.1 h1:tk4695rdBkkRhT6a4LdxzH/qz+ToO1XYCsmVcypZZXM=
1317
github.com/flamego/session v1.2.1/go.mod h1:wV3JdoW1hG9y8QsKKQw885nHuVzHjxU2jLkNqVhTmb8=
18+
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
19+
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
20+
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
1421
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
1522
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
1623
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
1724
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
1825
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
1926
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
27+
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
28+
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
29+
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
2030
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
2131
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2232
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -28,13 +38,17 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
2838
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2939
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
3040
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
41+
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
3142
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3243
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3344
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3445
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
3546
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
47+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
3648
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3749
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
50+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
51+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
3852
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3953
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
4054
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)