Skip to content

Commit 00a73d8

Browse files
committed
Add csrf protection to forms
1 parent 3bece62 commit 00a73d8

10 files changed

+25
-1
lines changed

cmd/web/helpers.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"net/http"
77
"runtime/debug"
88
"time"
9+
10+
"github.com/justinas/nosurf"
911
)
1012

1113
func (app *application) serveError(w http.ResponseWriter, err error) {
@@ -49,6 +51,7 @@ func (app *application) addDefaultData(td *templateData, r *http.Request) *templ
4951
td.CurrentYear = time.Now().Year()
5052
td.Flash = app.session.PopString(r, "flash")
5153
td.IsAuthenticated = app.isAuthenticated(r)
54+
td.CSRFToken = nosurf.Token(r)
5255
return td
5356
}
5457

cmd/web/middleware.go

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
7+
"github.com/justinas/nosurf"
68
)
79

810
func secureHeaders(next http.Handler) http.Handler {
@@ -47,3 +49,14 @@ func (app *application) requireAuthentication(next http.Handler) http.Handler {
4749
next.ServeHTTP(w, r)
4850
})
4951
}
52+
53+
func noSurf(next http.Handler) http.Handler {
54+
csrfHandler := nosurf.New(next)
55+
csrfHandler.SetBaseCookie(http.Cookie{
56+
HttpOnly: true,
57+
Path: "/",
58+
Secure: true,
59+
})
60+
61+
return csrfHandler
62+
}

cmd/web/routes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func (app *application) routes() http.Handler {
1111
standardMiddleware := alice.New(app.recoverPanic, app.logRequest, secureHeaders)
12-
dynamicMiddleware := alice.New(app.session.Enable)
12+
dynamicMiddleware := alice.New(app.session.Enable, noSurf)
1313

1414
mux := pat.New()
1515
mux.Get("/", dynamicMiddleware.ThenFunc(app.home))

cmd/web/templates.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
type templateData struct {
13+
CSRFToken string
1314
CurrentYear int
1415
Flash string
1516
Form *forms.Form

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/go-sql-driver/mysql v1.6.0 // indirect
88
github.com/golangcollege/sessions v1.2.0 // indirect
99
github.com/justinas/alice v1.2.0 // indirect
10+
github.com/justinas/nosurf v1.1.1 // indirect
1011
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6 // indirect
1112
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
1213
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/golangcollege/sessions v1.2.0 h1:2aD9jac/N8NC/y+NEoirYMGlYymzS0ZQN6AS
66
github.com/golangcollege/sessions v1.2.0/go.mod h1:7iTf/FrZku0hWyjV95lES7abH89WBlyBjPyA1htnuks=
77
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
88
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
9+
github.com/justinas/nosurf v1.1.1 h1:92Aw44hjSK4MxJeMSyDa7jwuI9GR2J/JCQiaKvXXSlk=
10+
github.com/justinas/nosurf v1.1.1/go.mod h1:ALpWdSbuNGy2lZWtyXdjkYv4edL23oSEgfBT1gPJ5BQ=
911
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1012
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6 h1:TjszyFsQsyZNHwdVdZ5m7bjmreu0znc2kRYsEml9/Ww=
1113
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

ui/html/base.layout.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<div>
2323
{{if .IsAuthenticated}}
2424
<form action='/user/logout' method='POST'>
25+
<input type='hidden' name='csrf_token' value='{{.CSRFToken}}'>
2526
<button>Logout</button>
2627
</form>
2728
{{else}}

ui/html/create.page.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
{{define "main"}}
66
<form action='/snippet/create' method='POST'>
7+
<input type='hidden' name='csrf_token' value='{{.CSRFToken}}'>
78
{{with .Form}}
89
<div>
910
<label>Title:</label>

ui/html/login.page.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
{{define "main"}}
66
<form action='/user/login' method='POST' novalidate>
7+
<input type='hidden' name='csrf_token' value='{{.CSRFToken}}'>
78
{{with .Form}}
89
{{with .Errors.Get "generic"}}
910
<div class='error'>{{.}}</div>

ui/html/signup.page.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
{{define "main"}}
66
<form action='/user/signup' method='POST' novalidate>
7+
<input type='hidden' name='csrf_token' value='{{.CSRFToken}}'>
78
{{with .Form}}
89
<div>
910
<label>Name:</label>

0 commit comments

Comments
 (0)