Repository navigation
Fixed #37232 -- Added support for the HTTP QUERY method. - #21855
Conversation
BenA-SA
left a comment
There was a problem hiding this comment.
Hi, I'd been looking at this ticket too, and had a patch of my own on the go built on Jonathan's reference implementation. Yours came out very close to mine.
Comparing the two, one thing differed and I think it might be an issue. I've noted it inline with a suggestion, but I'm not overly familiar with this part of the code so I might be wrong. Let me know if so.
Claude Code assisted with the investigation; I ran the tests and reviewed the diff myself.
jacobtylerwalls
left a comment
There was a problem hiding this comment.
Hey, thanks for working on this. Not a full review yet, but an early query inquiry for you:
- doc updates for
DATA_UPLOAD_MAX_NUMBER_FIELDSdescribing how/whether QUERY is checked?
3877e0e to
3202e7f
Compare
Well, we do not parse the QUERY body (just as we do not parse PUT body, see https://code.djangoproject.com/ticket/12635) so I'm unsure what we should add here? |
3202e7f to
2497f74
Compare
Thanks, that answers my question! |
jonbiemond
left a comment
There was a problem hiding this comment.
This looks awesome! And is spec compliant (minus caching) as far as I can tell. Just want to note, as was already mentioned on the ticket, that this ticket is currently owned by 송준호 (Song-Junho97) and it would be inappropriate to merge this PR till they have had enough time to respond. In the meantime I have a couple thoughts.
Without caching support the spec is only partially implemented. Is there precedence for releasing a partial implementation? Can we add caching support to this PR? I get that it's a lot for one PR. It would keep things a bit more manageable if this change was broken up over multiple commits.
I looked into the caching part when I was comparing my branch to this one, and I'd be happy to pick it up if Michiel would like me to — either into this PR or as a separate follow-up ticket once this lands. |
The headline is Starlette. The ASGI layer under FastAPI merged QUERY at +75 -- the maintainer landed his own branch the day he opened it and closed both community PRs (+9 test client, +20 routing) in its favour. FastAPI pins starlette>=0.46.0, so the ASGI half of that story is done, and the discussion (+78) now puts the remaining gate on ReDoc 2 specifically rather than OpenAPI 3.2 generally: Swagger UI supports 3.2, ReDoc is gated on 3.1 and would not classify query as an operation. Django got a real PR. The contributor's fork became django/django#21855 at +74 and is drawing fellow review: QUERY content exposed through HttpRequest.body and deliberately not parsed, the same treatment as PUT, with the diff reaching admin, auth admin, the common/CSRF/conditional-GET middleware, utils.cache, the HTTP view decorators and the test client. The live question is scope -- whether cache/Vary support must land in the same PR or a follow-up ticket. Vapor is unblocked. swift-http-types 1.7.0 shipped at +79 with "Make QUERY method public" (#130), which was the exact condition the approved Vapor PR was held for. Nothing blocks it now; it simply has not been touched since +73. SwiftNIO's own PR is the last unmerged piece of the Swift stack. Status changes: fasthttp merged (+73), reitit merged (+76) after 69 days when a maintainer put it in mergeable shape himself, Hanami merged (+80). Next.js is out of draft but still unreviewed. Helidon changed shape -- after months where Oracle's contributor agreement was the blocker (the OCA download 404s outside the US), a maintainer said at +79 he will take the work over as a PR of his own with the contributor as co-author. Jakarta REST woke up after 62 days: a new contributor volunteered at +79 for the whole scope, annotation through TCK, though the underlying committer vote has not changed. nghttp2 closed not planned at +77 -- the stale bot, not a decision; nobody ever replied. oasdiff released in v1.30.0 (+75). GoFr took a second full review round at +78 and is the most actively reviewed open QUERY PR in the tracker. Non-events worth recording: Envoy's merge still has no release (1.40 not cut, latest v1.39.1 at +72), Spring has cut no milestone since M1 missed the merge, Slim 4.15.3 shipped at +77 without the 4.x backport even though the maintainer pushed the fix himself at +67, django-ninja 1.7.0 shipped at +75 without its PR, and Kong's discussion has now sat 46 days with zero comments. New rows (14). Servers: Grape (merged same-day at +78, the maintainer "excited to beat Rack and Rails on QUERY support", and correct, since Rails' merge is still unreleased), Wolverine, Tempest, APIFlask, Comunica (merged +77 -- the SPARQL engine many Solid apps sit on, moving before the protocol spec did), GenHTTP, Marko Run, Lilya. Clients: Kiota (the maintainer scoped it to adding QUERY to each language's HttpMethod enum, since arbitrary verbs would be source-breaking), Gleam http. Tooling: httpYac, hono-openapi, sphinxcontrib-httpdomain, W3C SPARQL Protocol (closed completed +77 -- not a first-class 1.2 binding, since 1.2 is scoped to RDF 1.2 and errata, but an informative Note mirroring the existing HTTP PATCH note). Also: Apache Traffic Server's row now names the fork carrying the work (JakeChampion/trafficserver#5, merged +69), since nothing has been proposed upstream; chi's row records that v5.3.1 was tagged the day after the merge, the fastest request-to-release turnaround here; HTTP::Message shipped in v7.04 the same day it merged; and Apache HttpClient's alpha1 date is corrected from +52 (the RC3 tag) to +58 (rel/v5.7-alpha1). All 120 changed star counts refreshed as of 2026-09-04. Every tracked issue and PR was re-checked against its live state, and release claims were checked against tags and changelogs rather than inferred from merge dates.
|
thanks for your suggestions so far. Also I've added a stab at query caching which seems not too hard here --> mbeijen@28b67f5 it might go in a separate PR or maybe we can even embed it here, what do you think? The main thing about caching QUERY requests is that the cache key is URL, content-type, and body, as specified in the RFC. |
|
Let's keep caching separate for now. If the cache key includes the body, that takes me back to my earlier question about automatic parsing and DoS. |
|
Ok! (Also, we should only parse when responding to the QUERY. For the cache lookup we should NOT parse. Just compare the cache keys.) |
jonbiemond
left a comment
There was a problem hiding this comment.
Thanks for the changes! Just a couple clarification comments.
|
Thanks for the review so far! If there are more comments, please let me know, I'll try to be very responsive! |
sarahboyce
left a comment
There was a problem hiding this comment.
Thank you for the PR!
I decided to look at the docs a bit this morning 👍
| Django treats the :rfc:`QUERY <10008#section-2>` method as safe and | ||
| idempotent, like ``GET`` and ``HEAD``: it's exempt from CSRF protection and | ||
| takes part in conditional request processing. The query content isn't | ||
| parsed into :attr:`HttpRequest.POST`; read it from :attr:`HttpRequest.body` | ||
| and parse it according to the request's ``Content-Type``. |
There was a problem hiding this comment.
I don't think this fits well here. Perhaps we could mention in HttpRequest.body like:
--- a/docs/ref/request-response.txt
+++ b/docs/ref/request-response.txt
@@ -41,7 +41,8 @@ All attributes should be considered read-only, unless stated otherwise.
The raw HTTP request body as a bytestring. This is useful for processing
data in different ways than conventional HTML forms: binary images,
XML payload etc. For processing conventional form data, use
- :attr:`HttpRequest.POST`.
+ :attr:`HttpRequest.POST`. For other request content, such as the content of
+ ``PUT`` or ``QUERY`` requests, use this attribute.There was a problem hiding this comment.
Thanks for the suggestions! I copied them
|
|
||
| Django now recognizes the :rfc:`QUERY <10008>` method, which performs a safe, | ||
| idempotent query with the query content in the request body rather than in the | ||
| URL's query string. |
There was a problem hiding this comment.
| URL's query string. | |
| URL's query string. Use ``QUERY`` for read-only operations whose input is more | |
| naturally expressed in a request body, such as searches with complex, | |
| structured filters. |
Perhaps
| Caching of ``QUERY`` responses is not supported yet. The cache key for a | ||
| ``QUERY`` request must incorporate the request content, which | ||
| :class:`~django.middleware.cache.UpdateCacheMiddleware`, | ||
| :class:`~django.middleware.cache.FetchFromCacheMiddleware`, and | ||
| :func:`~django.views.decorators.cache.cache_page` don't do, so they continue to | ||
| ignore ``QUERY`` requests. |
There was a problem hiding this comment.
I would be tempted to say:
| Caching of ``QUERY`` responses is not supported yet. The cache key for a | |
| ``QUERY`` request must incorporate the request content, which | |
| :class:`~django.middleware.cache.UpdateCacheMiddleware`, | |
| :class:`~django.middleware.cache.FetchFromCacheMiddleware`, and | |
| :func:`~django.views.decorators.cache.cache_page` don't do, so they continue to | |
| ignore ``QUERY`` requests. | |
| :class:`~django.middleware.cache.UpdateCacheMiddleware`, | |
| :class:`~django.middleware.cache.FetchFromCacheMiddleware` and | |
| :func:`~django.views.decorators.cache.cache_page` do not cache ``QUERY`` | |
| responses. Conditional request processing with ``ETag`` or ``Last-Modified`` | |
| validators is supported. |
b6827d1 to
dadc32e
Compare
jonbiemond
left a comment
There was a problem hiding this comment.
Hey @mbeijen, the documentation looks great! Just have a couple thoughts on sentence structure which I hope will improve accessibility.
| and returns a ``Response`` object. Useful for testing endpoints that | ||
| accept a safe, idempotent query in the request body. |
There was a problem hiding this comment.
Do we need the "Useful for..." part? I know it follows the convention of the other methods here. It feels out of place to me.
|
I think your suggestions look good! I committed them via the web UI on my phone, I am traveling for about another week and did not bring my laptop. Thanks for your suggestions! I would really like to see QUERY in Django! |
jacobtylerwalls
left a comment
There was a problem hiding this comment.
Great work. I left very minor feedback. I'm hoping to play with a test project like djangoproject.com and give this a manual test drive, but I don't anticipate having any other substantive feedback.
I would really like to see QUERY in Django!
Me too! 🙌
I suppose I need to start thinking next about how practically we can support this in the cache middleware.
| content available as request.body. | ||
| """ | ||
| request = self.rf.query( | ||
| "/", data=b"q=django", content_type="application/x-www-form-urlencoded" |
There was a problem hiding this comment.
I'm surprised to see our basic test examples here throughout not using the default content_type. Is there a reason you prefer "application/x-www-form-urlencoded" here?
| **extra, | ||
| ): | ||
| """Construct a QUERY request.""" | ||
| data = self._encode_json(data, content_type) |
There was a problem hiding this comment.
Flagging for other reviewers (e.g. @sarahboyce) that we are consciously not raising a mutually exclusive error like we do for GET and HEAD when passing data and query_params together.
From my read of the discussion on #17447, that seems consistent.
I test drove this on a project I used to work on, which had a classic use case for sending a GET-like request by POST in order to supply GeoJSON geometries via the request body. The |
Great! I think I configured the PR to allow changes by contributors. Feel free to make the changes you proposed, they all make sense! Otherwise please wait a bit as I will have no access to my laptop for another week |
|
@mbeijen Enjoy your laptop-free travels! I figure there's no rush, we still have 3 months left till the 6.2 feature freeze. @jacobtylerwalls Speaking of the feature freeze, would we need the caching part of the spec implemented before we included this work in a release? |
|
I think we should aim to have it for 6.2, but I don't see it as a blocker -- this unit of work is useful without caching, and caching can be done manually or downstream (or even using interesting strategies like the I'll open a ticket to discuss the design issues. I'd be happy to prioritize it for review if you wanted to get started, @jonbiemond? |
|
@jacobtylerwalls Sounds good! If you find time to create a ticket that would be awesome. @BenA-SA Are you still interested in working on the caching part of this? You may have the right of first refusal, seeing as you've already begun on it. :) |
|
caching: ticket-37428 |
|
Thanks. I also already implemented caching in a commit i linked earlier |
I've addressed the review comments now |
jacobtylerwalls
left a comment
There was a problem hiding this comment.
Thanks for the updates, this looks great 🥳
I see a couple content_type="application/x-www-form-urlencoded" still in the ClientTest/AsyncClientTest, but maybe it's okay to have them for variety's sake 🤷 .
Nice attention to detail throughout, very easy to review!
| 'login CSRF', where an attacking site tricks a user's browser into logging into | ||
| a site with someone else's credentials, is also covered. | ||
|
|
||
| The first defense against CSRF attacks is to ensure that GET requests (and |
There was a problem hiding this comment.
I'm squashing in a little tweak here, since the linked RFC doesn't mention QUERY.
diff --git a/docs/ref/csrf.txt b/docs/ref/csrf.txt
index a921bdbb41..984b376ab5 100644
--- a/docs/ref/csrf.txt
+++ b/docs/ref/csrf.txt
@@ -86,12 +86,12 @@ The CSRF protection is based on the following things:
This ensures that only forms that have originated from trusted domains can be
used to POST data back.
-It deliberately ignores GET requests (and other requests that are defined as
-'safe' by :rfc:`9110#section-9.2.1`). These requests ought never to have any
-potentially dangerous side effects, and so a CSRF attack with a GET request
-ought to be harmless. :rfc:`9110#section-9.2.1` defines POST, PUT, and DELETE
-as 'unsafe', and all other methods are also assumed to be unsafe, for maximum
-protection.
+It deliberately ignores GET and QUERY requests (and other requests that are
+defined as 'safe' by :rfc:`9110#section-9.2.1`). These requests ought never to
+have any potentially dangerous side effects, and so a CSRF attack with a GET
+request ought to be harmless. :rfc:`9110#section-9.2.1` defines POST, PUT, and
+DELETE as 'unsafe', and all other methods are also assumed to be unsafe, for
+maximum protection.
The CSRF protection cannot protect against man-in-the-middle attacks, so use
:ref:`HTTPS <security-recommendation-ssl>` with
@@ -101,6 +101,10 @@ the HOST header <host-headers-virtual-hosting>` and that there aren't any
(because XSS vulnerabilities already let an attacker do anything a CSRF
vulnerability allows and much worse).
+.. versionchanged:: 6.2
+
+ ``QUERY`` was added to the exempt methods.
+
.. admonition:: Removing the ``Referer`` header
To avoid disclosing the referrer URL to third-party sites, you might wantQUERY (RFC 10008) is a safe, idempotent method that carries the query in the request content rather than in the URI's query component. Treat it as safe throughout: exempt it from CSRF protection, evaluate request preconditions as for GET, accept it in require_safe(), and dispatch it from View. Query content is available as HttpRequest.body and is not parsed into HttpRequest.POST. Caching is left for a follow-up. Per RFC 10008 Section 2.7 the cache key for a QUERY request must incorporate the request content, which the cache middleware and cache_page() don't do, so they continue to ignore QUERY. Thanks Ben Atkinson, Jonathan Biemond, and Jacob Walls for reviews.
7fc3157 to
e85b3c0
Compare
Uncovered lines in changed filesNote: Missing lines are warnings only. Some database-specific lines may not be measured. More information |
Trac ticket number
ticket-37232
Branch description
Implements the HTTP QUERY method (RFC 10008) as a safe, idempotent method
throughout Django: exempt from CSRF protection, evaluated for request
preconditions as GET is, accepted by
require_safe(), and dispatched byViewvia aquery()handler.Client.query()is added to the test client,async client, and both request factories. Query content is exposed as
HttpRequest.bodyand is not parsed intoHttpRequest.POST, per thediscussion in comment 5 on the ticket.
Caching is deliberately left for a follow-up. RFC 10008 Section 2.7 requires
the cache key to incorporate the request content, which the cache middleware
and
cache_page()don't do, so they continue to ignore QUERY. This is statedin the release notes so there are no silent incorrect cache hits.
Three points I'd like reviewer input on:
require_safenow accepts QUERY. The ticket asks for this, but it meansexisting views gain a method they didn't have. Noted in the release notes
with
require_http_methods(["GET", "HEAD"])as the opt-out.RuntimeErrorrather thanblocking the redirect outright. RFC 10008 Section 2.5 states the POST-to-GET
exception does not apply to QUERY, so a compliant client re-sends the
QUERY with its content and the redirect is spec-conformant.
Client.query()defaults tocontent_type="application/octet-stream"tomatch
put()/patch()/delete(), though RFC 10008 Section 2 requires ameaningful Content-Type. Happy to change the default.
Not addressed, and arguably a separate ticket:
follow=Truefollows redirectsas GET for every method, so a 307/308 on QUERY is not replayed as QUERY.
AI Assistance Disclosure (REQUIRED)
reviewed and verified their output.
Claude Code (Opus 5) was used to draft the implementation, tests, and docs.
I reviewed and verified the full diff, and ran the test suite and linters
locally.
Checklist
mainbranch.number, and ends with a period.
for this PR.