Skip to content

Commit 5c40d46

Browse files
committed
Merge branch 'master' of https://github.com/cbonello/revel-csrf
Conflicts: csrf.go
2 parents 0611ca9 + 5aca7a9 commit 5c40d46

File tree

5 files changed

+30
-31
lines changed

5 files changed

+30
-31
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ An integer value that defines the number of characters that should be found with
2323

2424
## Operating instructions
2525

26-
Simply call the CSRFFilter() filter in `app/init.go` right after `revel.SessionFilter`. The CSRF token is saved in the session cookie.
26+
Simply call the CSRFFilter() filter in `app/init.go`.
2727

2828
package app
2929

@@ -40,8 +40,8 @@ Simply call the CSRFFilter() filter in `app/init.go` right after `revel.SessionF
4040
revel.FilterConfiguringFilter, // A hook for adding or removing per-Action filters.
4141
revel.ParamsFilter, // Parse parameters into Controller.Params.
4242
revel.SessionFilter, // Restore and write the session cookie.
43-
csrf.CSRFFilter, // CSRF prevention.
4443
revel.FlashFilter, // Restore and write the flash cookie.
44+
csrf.CSRFFilter, // CSRF prevention.
4545
revel.ValidationFilter, // Restore kept validation errors and save new ones from cookie.
4646
revel.I18nFilter, // Resolve the requested language
4747
revel.InterceptorFilter, // Run interceptors around the action.
@@ -101,4 +101,4 @@ A demo application is provided in the samples directory. To launch it:
101101

102102
## CONTRIBUTORS
103103
* Otto Bretz
104-
* Allen Dang
104+
* Allen Dang

csrf.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
errNoReferer = "REVEL_CSRF: A secure request contained no Referer or its value was malformed."
2222
errBadReferer = "REVEL_CSRF: Same-origin policy failure."
2323
errBadToken = "REVEL_CSRF: tokens mismatch."
24-
safeMethods = regexp.MustCompile("^(GET|HEAD|OPTIONS|TRACE)$")
24+
safeMethods = regexp.MustCompile("^(GET|HEAD|OPTIONS|TRACE)$")
2525
)
2626

2727
var CSRFFilter = func(c *revel.Controller, fc []revel.Filter) {
@@ -70,15 +70,15 @@ var CSRFFilter = func(c *revel.Controller, fc []revel.Filter) {
7070

7171
sentToken := ""
7272
if ajaxSupport := revel.Config.BoolDefault("csrf.ajax", false); ajaxSupport {
73-
// Accept CSRF token in the custom HTTP header X-CSRF-Token, for ease
74-
// of use with popular JavaScript toolkits which allow insertion of
75-
// custom headers into all AJAX requests.
76-
// See http://erlend.oftedal.no/blog/?blogid=118
77-
sentToken = r.Header.Get(headerName)
73+
// Accept CSRF token in the custom HTTP header X-CSRF-Token, for ease
74+
// of use with popular JavaScript toolkits which allow insertion of
75+
// custom headers into all AJAX requests.
76+
// See http://erlend.oftedal.no/blog/?blogid=118
77+
sentToken = r.Header.Get(headerName)
7878
}
7979
if sentToken == "" {
8080
// Get CSRF token from form.
81-
sentToken = c.Params.Get(fieldName)
81+
sentToken = c.Params.Get(fieldName)
8282
}
8383
glog.V(2).Infof("REVEL-CSRF: Token received from client: '%s'", sentToken)
8484

@@ -101,4 +101,3 @@ var CSRFFilter = func(c *revel.Controller, fc []revel.Filter) {
101101
func sameOrigin(u1, u2 *url.URL) bool {
102102
return (u1.Scheme == u2.Scheme && u1.Host == u2.Host)
103103
}
104-

exemptions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
package csrf
33

44
import (
5-
"github.com/golang/glog"
65
"fmt"
6+
"github.com/golang/glog"
77
pathPackage "path"
88
"sync"
99
)
@@ -19,9 +19,9 @@ var (
1919
exemptionsFullPath = struct {
2020
sync.RWMutex
2121
list map[string]struct{}
22-
} {
23-
list: make(map[string]struct{}),
24-
}
22+
}{
23+
list: make(map[string]struct{}),
24+
}
2525

2626
exemptionsGlobs globPath
2727
)

samples/demo/app/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ func init() {
1313
revel.FilterConfiguringFilter, // A hook for adding or removing per-Action filters.
1414
revel.ParamsFilter, // Parse parameters into Controller.Params.
1515
revel.SessionFilter, // Restore and write the session cookie.
16-
csrf.CSRFFilter, // CSRF prevention.
1716
revel.FlashFilter, // Restore and write the flash cookie.
17+
csrf.CSRFFilter, // CSRF prevention.
1818
revel.ValidationFilter, // Restore kept validation errors and save new ones from cookie.
1919
revel.I18nFilter, // Resolve the requested language
2020
revel.InterceptorFilter, // Run interceptors around the action.

tokengen.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
package csrf
33

44
import (
5-
"github.com/golang/glog"
6-
"github.com/robfig/revel"
75
"crypto/rand"
86
"encoding/base64"
97
"fmt"
8+
"github.com/golang/glog"
9+
"github.com/robfig/revel"
1010
"io"
1111
)
1212

@@ -15,7 +15,7 @@ var rawTokenLength, lengthCSRFToken int
1515
func getRandomBytes(length int) (bytes []byte, err error) {
1616
bytes = make([]byte, length)
1717
_, err = io.ReadFull(rand.Reader, bytes)
18-
return
18+
return
1919
}
2020

2121
// A CSRF token is generated by encoding bytes read from crypto/rand as base64.
@@ -30,17 +30,17 @@ func generateNewToken(c *revel.Controller) (token string) {
3030
}
3131

3232
func init() {
33-
revel.OnAppStart(func() {
34-
rawTokenLength = revel.Config.IntDefault("csrf.token.length", 32)
35-
if rawTokenLength < 32 || rawTokenLength > 512 {
36-
panic(fmt.Sprintf("REVEL_CSRF: csrf.token.length=%d: expected a length in [32..512]", rawTokenLength))
37-
}
38-
lengthCSRFToken = base64.StdEncoding.EncodedLen(rawTokenLength)
33+
revel.OnAppStart(func() {
34+
rawTokenLength = revel.Config.IntDefault("csrf.token.length", 32)
35+
if rawTokenLength < 32 || rawTokenLength > 512 {
36+
panic(fmt.Sprintf("REVEL_CSRF: csrf.token.length=%d: expected a length in [32..512]", rawTokenLength))
37+
}
38+
lengthCSRFToken = base64.StdEncoding.EncodedLen(rawTokenLength)
3939

40-
// Check that cryptographically secure PRNG is available.
41-
_, err := getRandomBytes(1)
42-
if err != nil {
43-
panic(fmt.Sprintf("REVEL_CSRF: crypto/rand is unavailable: Read() failed with %#v", err))
44-
}
45-
})
40+
// Check that cryptographically secure PRNG is available.
41+
_, err := getRandomBytes(1)
42+
if err != nil {
43+
panic(fmt.Sprintf("REVEL_CSRF: crypto/rand is unavailable: Read() failed with %#v", err))
44+
}
45+
})
4646
}

0 commit comments

Comments
 (0)