Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respond 400 to malformed methods instead of 405 #3338

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kenballus
Copy link

The HTTP standard has a grammar rule that defines valid HTTP methods. This patch checks that methods conform to that rule. Note that this does not preclude a user from defining their own custom methods; only that such custom methods cannot contain (e.g.) null bytes and other crazy junk like that.

@bdarnell
Copy link
Member

This makes sense, but does it ever matter in practice? It doesn't seem worth the (small) performance cost of an extra re.match call. However, I think we could merge the split and version re.match into one big regex to parse the request line and probably improve performance while being more strict about method naming.

@kenballus
Copy link
Author

kenballus commented Jun 12, 2024

The issue is that 405 is cacheable.

When Tornado is deployed behind a cache server that parses methods differently from the way that Tornado does, there's the potential for cache poisoning.

For example, there exist caches that forward GET\x00 / as-is, but treat them as equivalent to GET /. Tornado will see this, and respond 405, which will be stored in the cache. The next time someone tries to GET /, they'll get 405.

Of course, that behavior is a bug in the cache, and needs to be fixed there too.

@bdarnell
Copy link
Member

Ah, I see.

RFC 9110 section 9.1 also distinguishes between methods that are not recognized (501) and those that are recognized but not supported by the target resource (405). The logic behind that is probably similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants