A modern approach to preventing CSRF in Go

AI-generated summary of reporting by Simon Willison’s Weblog. Read the original for the full story.

Why it matters

Go 1.25's built-in http.CrossOriginProtection could replace fragile hidden-form-field CSRF tokens in web apps today.

Read this as

A web security writer, Alex Edwards, examined a new tool added to the Go programming language in version 1.25, released in August. The tool blocks cross-site request forgery (CSRF), an attack where a malicious website makes a victim's browser submit forms to your site. Instead of the old workaround of hidden tokens in forms, it reads a browser-supplied header, Sec-Fetch-Site, that reveals where a submission came from. It works in about 94% of browsers, with a fallback for the rest.

Applications built with Go 1.25 (August) can now stop CSRF attacks without deploying token-based checks, cutting implementation and maintenance cost for web teams. The mechanism relies on the Sec-Fetch-Site browser header, available in 94.18% of browsers globally, so a fallback comparing Origin and Host headers covers the remainder, mainly iOS before version 17 and legacy browsers. Teams still need HSTS enabled, since the fallback cannot verify the connection scheme.

Go 1.25 ships http.CrossOriginProtection (src/net/http/csrf.go). Requests with Sec-Fetch-Site: same-origin or none pass; cross-site and same-site submissions are rejected, subdomains like tools.simonwillison.net and til.simonwillison.net count as same-site, not same-origin, and are blocked. If the header is absent (~5.8% of browsers), it falls back to comparing Origin with Host, which cannot check scheme, hence the HSTS recommendation. Filippo Valsorda's research article covers the underlying design; Firefox's Frederik Braun confirmed *.localhost is a secure context and does send Sec-Fetch-Site.

Read the original

This summary exists to save you time, not to replace the source.

Key terms

Go http.CrossOriginProtection Sec-Fetch-Site HSTS iOS 17 Bluesky

Was this summary useful?

One click, anonymous. It tells us which summaries to re-run.

Related reading