Skip to content

Commit 5b55567

Browse files
Christian FranciaChristian Francia
Christian Francia
authored and
Christian Francia
committed
add csrf token to forms
1 parent 9c692d2 commit 5b55567

9 files changed

+23
-3
lines changed

cmd/web/helpers.go

+4-1
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
// The serverError helper writes an error message and stack trace to the errorLog
@@ -47,10 +49,11 @@ func (app *application) addDefaultData(td *templateData, r *http.Request) *templ
4749
if td == nil {
4850
td = &templateData{}
4951
}
50-
td.CurrentYear = time.Now().Year()
5152

5253
// Here we are adding the default value to our sessions
5354
td.Flash = app.session.PopString(r, "flash")
55+
td.CSRFToken = nosurf.Token(r)
56+
td.CurrentYear = time.Now().Year()
5457
td.IsAuthenticated = app.isAuthenticated(r)
5558
return td
5659
}

cmd/web/middleware.go

+12
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 {
@@ -48,3 +50,13 @@ func (app *application) requireAuthentication(next http.Handler) http.Handler {
4850
next.ServeHTTP(w, r)
4951
})
5052
}
53+
54+
func noSurf(next http.Handler) http.Handler {
55+
csrfHandler := nosurf.New(next)
56+
csrfHandler.SetBaseCookie(http.Cookie{
57+
HttpOnly: true,
58+
Path: "/",
59+
Secure: true,
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
mux := pat.New()
1414
mux.Get("/", dynamicMiddleware.ThenFunc(app.home))
1515
mux.Get("/snippet/create", dynamicMiddleware.Append(app.requireAuthentication).ThenFunc(app.createSnippetForm))

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
Form *forms.Form
1516
Flash string

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ require (
77
github.com/go-sql-driver/mysql v1.5.0
88
github.com/golangcollege/sessions v1.2.0
99
github.com/justinas/alice v1.2.0
10-
github.com/justinas/nosurf v1.1.0 // indirect
10+
github.com/justinas/nosurf v1.1.0
1111
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6
1212
)

ui/html/base.layout.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<div>
2525
{{if .IsAuthenticated}}
2626
<form action='/user/logout' method='POST'>
27+
<input type='hidden' name='csrf_token' value='{{.CSRFToken}}'>
2728
<button>Logout</button>
2829
</form>
2930
{{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
<label class='error'>{{.}}

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)