

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
CORS permission belongs only on routes intentionally available to other browser origins. Applying it globally can expose authenticated APIs.
Imagine middleware that reflects every origin and allows credentials:
func permissiveCORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
if origin := request.Header.Get("Origin"); origin != "" {
responseWriter.Header().Set("Access-Control-Allow-Origin", origin)
responseWriter.Header().Set("Access-Control-Allow-Credentials", "true")
}
next.ServeHTTP(responseWriter, request)
})
}
That gives every requesting website permission to read authenticated responses. Scary!
A storefront can have different policies for different routes:
For a public response, Access-Control-Allow-Origin: * is appropriate because the data needs no credentials. Its preflight response can advertise only GET, without allowing credentials or unnecessary request headers. Authenticated routes should omit CORS permission headers entirely.
Bearly Secure grants every requesting origin access to all dynamic APIs. Replace the global policy with CORS limited to the public product API.
go run ./cmd/attackerlab
With Bearly Secure and Bearly Evil still running, run and submit the CLI tests from the project root.
You can stop Bearly Evil after submitting the lesson.