Preventing cross-site request forgery in Go web applications
It's a common practice to secure web applications from a malicious website, email, blog, instant message, or a program attacking a trusted site for which the user is currently authenticated to prevent unwanted action. We often call this cross-site request forgery.
Implementing cross-site request forgery in Go is fairly easy using the Gorilla CSRF package, which we will be covering in this recipe.
How to do it…
- Install the
github.com/gorilla/csrfandgithub.com/gorilla/muxpackages using thego getcommand, as follows:
$ go get github.com/gorilla/csrf $ go get github.com/gorilla/mux
- Create
sign-up.htmlwith name and email input text fields and an action that gets called whenever an HTML form is submitted, as follows:
<html>
<head>
<title>Sign Up!</title>
</head>
<body>
<form method="POST" action="/post" accept-charset="UTF-8">
<input type="text" name="name">
<...