tornadoweb / tornadoweb/tornado
web: New XSRF/CSRF protection: `Sec-Fetch-Site`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 22.2k
- Forks
- 5.6k
- Avg merge
- 3h 42m
- Merged PRs (30d)
- 16
Description
Since 2023, major browsers have offered a new protection from XSRF: the Sec-Fetch-Site header. This is much simpler to use than the invasive xsrf_cookies feature currently offered by tornado, while simultaneously offering stronger protection. See golang/go#73626 for an in-depth discussion of this feature. Also see #3226, in which we previously considered (and rejected) XSRF protection based on the SameSite cookie feature instead.
It is possible for applications to use this header today, such as by checking it in the prepare method:
def prepare(self):
if (self.request.method not in ("GET", "HEAD", "OPTIONS") and
"Sec-Fetch-Site" in self.request.headers and
self.request.headers["Sec-Fetch-Site"] not in ("same-origin", "none")):
raise HTTPError(403)
We should make this available in Tornado itself so it is more easily usable and can potentially be turned on by default.
Open questions:
- Should it be the default? Probably not immediately but perhaps in Tornado 7.0
- How is it enabled or disabled? A new flag in Application like
xsrf_cookiesor something else? (Consider that it may need to be overridden on a per handler basis) - Where does the check live? In web.py like the current one, or somewhere deeper in the HTTP stack? (perhaps a middleware at the HTTPMessageDelegate level?)
- Do we want a fallback like the Origin==Host check proposed in golang/go#73626?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the existing xsrf_cookies handling in web.py, then compare the Application, per-handler prepare method, and HTTPMessageDelegate entry points mentioned in the issue. The work is complete when the enablement model, default behavior, request-check location, and any Origin fallback are decided and implemented with coverage for the chosen behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, security
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100