

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Cross-Site Scripting (XSS)
incomplete
2: Fix Cross-Site Scripting
incomplete
3: Cross-Site Request Forgery (CSRF)
incomplete
4: CSRF Tokens
incomplete
5: Content Security Policy
incomplete
6: Legitimate Inline Scripts
incomplete
7: Sandboxing 'iframe' Elements
incomplete
8: Clickjacking
incomplete
9: Same-Origin and Referrer Policies
incomplete
10: Cross-Origin Resource Sharing
incomplete
11: Route-Scoped CORS
incomplete
12: Security Header Middleware
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
A CSRF token proves that a state-changing request includes a secret issued by the application, not just a cookie the browser attached automatically.
The synchronizer token pattern stores a random token in the user's server-side session. Generate it with crypto/rand when the session is created:
tokenBytes := make([]byte, 32)
if _, err := rand.Read(tokenBytes); err != nil {
return err
}
csrfToken := base64.RawURLEncoding.EncodeToString(tokenBytes)
Render that token in each protected form:
<input type="hidden" name="csrfToken" value="{{.CSRFToken}}" />
When the form is submitted, compare its token with the authenticated session's token. subtle.ConstantTimeCompare avoids a comparison whose timing varies with the matching prefix. Check the lengths first because different-length values can never match.
An attacker's page can trigger a request carrying the victim's cookie, but the same-origin policy prevents it from reading a form to obtain the token.
XSS can bypass CSRF tokens by reading one from the DOM and submitting a valid request. You need to prevent both vulnerabilities.
Bearly Secure's forms already carry per-session CSRF tokens, and most mutation handlers already verify them. There are two problems: sessions.CSRFTokensMatch is a stub that accepts every token, and the checkout handler never verifies its submitted token.
Implement the token comparison, then protect checkout.
With Bearly Secure still running, run and submit the CLI tests from the project root.