CSRF protection
middleware.CSRF protects apps that authenticate with cookies. It uses the double submit pattern:
- A safe request (
GET,HEAD,OPTIONSorTRACE) without a token gets a randomcsrf_tokencookie that your frontend can read. - An unsafe request (
POST,PUT,PATCHorDELETE) must send the same value in theX-CSRF-Tokenheader. Otherwise it gets a 403 with the codeCSRF.
func csrf() dygo.Option { return dygo.WithMiddleware(middleware.CSRF(middleware.CSRFOptions{Secure: true}))}Set Secure: true in production. CookieName and HeaderName change the names. APIs that authenticate with bearer tokens don’t need CSRF protection.