origin_check
An extremely simple Tower middleware layer, that lets you mitigate CSRF attacks
by examining the Origin or Referer header, and comparing it to the Host
and uri.
IMPORTANT NOTES:
This crate makes several assumptions that must all be true for it to be a good choice for you:
- Your site is accessed exclusively in "secure context"s, like over
httpsor onlocalhost. - State changes are never performed in response to
GETorHEADrequests. Such requests are always allowed by this service, regardless of CSRF indicators. - All other requests should fail if the hostname and port of the
OriginorRefererdoes not exactly match theHost. This means that you cannot, e.g., send POST requests from one subdomain to another, or from one port to another. - Your users' browsers will set the
OriginorRefererheader on non-GET/-HEADrequests, when those requests are initiated by your site. In order to ensure this, be careful that theReferrer-Policyfor your site is set to something other thanno-referrer.
You probably want to set SameSite=Strict or SameSite=Lax on any
authentication cookies, as additional protection against CSRF.
You likely also want to set X-Frame-Options: DENY for your site by default,
to prevent clickjacking, which is a distinct but related problem to CSRF.