Repository navigation
Add support for the HTTP QUERY method - #57973
Conversation
|
Both rack/rack#2474 and puma/puma#3973 are related to this proposal. While those particular PRs might not be the ones to add support for Are there additional considerations to incorporate into the framework to support a fragmented and incremental roll-out of support for architectural dependencies (or other Rack web servers that are not |
|
Thanks for opening those, @seanpdoyle. This PR doesn't depend on either of them. Rails reads On incremental roll-out, each layer already degrades on its own:
The CHANGELOG entry documents the Puma opt-in for this reason. I can expand the routing guide with a short note on server support (Puma config, nginx mainline, CloudFront gap) and link the Rack/Puma PRs, either in this PR or as a follow-up. Two things I'd leave as deliberate follow-ups rather than v1 scope: the |
| # query 'bacon', to: 'food#bacon' | ||
| def query(*path_or_actions, as: DEFAULT, to: nil, controller: nil, action: nil, on: nil, defaults: nil, constraints: nil, anchor: nil, format: nil, path: nil, internal: nil, **mapping, &block) | ||
| if path_or_actions.grep(Hash).any? && (deprecated_options = path_or_actions.extract_options!) | ||
| as = assign_deprecated_option(deprecated_options, :as, :query) if deprecated_options.key?(:as) |
There was a problem hiding this comment.
Do we have the luxury of not doing the deprecated options handling since this is a new method? Or does something enforce all methods to support this signature?
There was a problem hiding this comment.
Good call, nothing enforces the signature; the shim is per-method copy-paste from 3b4255e, there purely for backward compatibility with pre-existing hash-based routes. Since query is new in 8.2 (the release where hash support is slated for removal), there are no legacy callers to protect. Dropped the shim and updated the branch. (query "search" => "search#index" still works via **mapping, and positional hashes fall through to match's own deprecation until the 8.2 removal.)
9477c44 to
dd98f8d
Compare
35123c7 to
ab5690b
Compare
beb14e9 to
bd5a141
Compare
RFC 10008 defines QUERY as a safe and idempotent HTTP method that
conveys the query in the request content instead of the URL query
string. It is registered in the IANA HTTP Method Registry (safe: yes,
idempotent: yes) and is intended for queries too large or structured
for a query string: complex filters, search APIs, GraphQL, and
JSON-RPC style endpoints.
This registers QUERY as a known HTTP method in ActionDispatch::Request
and wires it through the framework:
* Add the RFC10008 constant to ActionDispatch::Request::HTTP_METHODS,
so QUERY requests no longer raise
ActionController::UnknownHttpMethod.
* Add ActionDispatch::Request#query?, mirroring the other verb
predicates (Rack::Request::Helpers does not define one for QUERY).
* Add a QUERY verb matcher to Journey (VerbMatchers::VERBS), so
`via: :query` routes match without the Unknown fallback.
* Add the `query` routing DSL method to
ActionDispatch::Routing::Mapper::HttpHelpers:
query "search", to: "search#index"
match "filter", to: "search#filter", via: :query
* Add the `query` request helper to integration tests:
query "/search", params: { filters: { q: "video" } }, as: :json
* Exempt QUERY requests from forgery protection, like GET and HEAD.
HTML forms cannot issue QUERY requests, and cross-origin QUERY
requests always require a CORS preflight (QUERY is not a
CORS-safelisted method), so the forgery surface is strictly smaller
than GET's. This exemption is kept in an isolated hunk so it is easy
to change if the exemption is not wanted.
Body parameter parsing needs no changes: parsing is keyed on
Content-Type rather than on the request method, so JSON and
form-encoded QUERY bodies already parse into params.
There are no behavior changes for existing applications: QUERY
requests previously raised ActionController::UnknownHttpMethod (405)
before reaching any application code.
Note that the application server must also accept the method. Puma
currently allows only the eight standard methods by default; QUERY can
be enabled with its `supported_http_methods` option.
References:
- RFC 10008: https://www.rfc-editor.org/rfc/rfc10008.html
- IANA HTTP Method Registry:
https://www.iana.org/assignments/http-methods/http-methods.xhtml
- Rails core forum proposal:
https://discuss.rubyonrails.org/t/proposal-support-for-the-http-query-method-rfc-10008/91255
bd5a141 to
87d4dec
Compare
QUERY is safe and idempotent per RFC 10008 §2, so Request#safe_method? now includes it alongside RFC 9110 §9.2.1's GET, HEAD, OPTIONS, and TRACE.
* Add "query" to the integration Runner delegation list so top-level query calls reset the HTML document and copy session variables, and so follow_redirect! can re-issue QUERY after a 307/308 (it dispatches via public_send on the request method). Cover 307-preserves / 303-switches-to-GET redirect behavior. * Add the query helper to ActionController::TestCase, mirroring the integration helper. TestRequest#assign_parameters already routes non-GET methods through the body-encoding branch, so params land in request_parameters with no query string. * Document that both helpers default the Content-Type to application/x-www-form-urlencoded, as RFC 10008 requires QUERY requests to declare one; pass as: :json for a JSON body. * Cover params-to-body encoding (urlencoded and as: :json) end to end.
* Mapper tests for the query helper (verb, defaults, format). * query works on resource collections/members through the existing scope machinery: query :search, on: :collection. * Pin that Journey's HEAD->GET fallback does not extend to QUERY: a via: :get route never matches a QUERY request. * Add RFC10008 to the TestHttpMethods matrix.
* Cross-origin JavaScript response protection covers QUERY exactly as GET: mark_for_same_origin_verification! includes query?, so a non-XHR cross-origin QUERY returning JavaScript raises InvalidCrossOriginRequest. * SSL middleware: document that QUERY intentionally receives a 307 (not 301) when redirecting to HTTPS — 301 permits clients to rewrite the method to GET, which would drop the QUERY body (RFC 10008 redirect semantics). * Conditional requests: no code change needed — fresh_when/stale? are verb-agnostic. Pin that QUERY with a matching If-None-Match gets 304 Not Modified and a fresh QUERY gets 200 + ETag. Deliberate non-changes: ActionDispatch::Static keeps serving files for GET/HEAD only, and implicit-render behavior is untouched — QUERY is a programmatic method, not a browser navigation. Rack::ConditionalGet handles QUERY upstream on rack main (rack/rack#2474, unreleased); controller-level freshness covers it in the meantime.
|
Nice work! I've rebased onto current main and pushed the remainder of QUERY support:
One behavior guard: the CSRF exemption applies only to requests that natively arrived as QUERY, not via method override. Rack method-override removal: rack/rack#2502 |
Rack merged QUERY into Rack::MethodOverride's override whitelist (rack/rack#2474, unreleased). Once a rack release ships it, an ordinary cross-origin form POST carrying _method=query would be dispatched as QUERY by the default middleware stack — and, without this change, inherit QUERY's CSRF exemption. That would silently void the structural rationale for the exemption: HTML forms cannot emit QUERY, and cross-origin QUERY is always preflighted. Neither is true of a form POST. Key the exemption on the method the request actually arrived with: request.method reports the pre-override method, so native QUERY remains exempt while a tunneled POST is verified like any other POST. The cross-origin JavaScript response protection uses the same predicate. This defends the invariant ahead of rack's release rather than reacting after it ships.
* Routes drawn via: :all (and custom constraints that don't check the request method) now receive QUERY requests, where previously such requests 405'd before routing ran. Note this in the CHANGELOG entry and pin it with a test — it's the one observable behavior change for existing applications. * Cover request.method/method_symbol reporting the pre-override method for a POST masquerading as QUERY, matching the existing masquerading tests and underpinning the native-QUERY CSRF exemption. * Guides: show query on resource collections in the routing guide, and place QUERY alongside GET in the security guide's GET-vs-POST checklist.
QUERY is safe and idempotent per RFC 10008, so the database selector middleware routes it to the replica like GET and HEAD. A QUERY arriving within the configured write window still reads from the primary, same as any other read.
No code change needed — method: :query matches via request.method_symbol now that QUERY is a recognized method. The default method: :get deliberately does not match QUERY. Form helper QUERY generation (button_to/form_with method: :query via _method tunneling) is deliberately not included: a POST tunnel offers none of QUERY's intermediary-visible caching or retry semantics, and it would make QUERY endpoints reachable through an ordinary cross-origin form POST, undermining the structural CSRF protections that justify QUERY's forgery-protection exemption.
* testing: query request helper with a body-sending example, and query in the xhr: true helper list * action_controller_overview: body params framing, request predicate table (query?, safe_method?/unsafe_method?), and a 'Handling HTTP QUERY Requests' section covering routing, params, CSRF, and conditional-request behavior * caching_with_rails: conditional requests apply to QUERY via fresh_when/stale? (Rack::ConditionalGet remains GET/HEAD-only in released rack) * action_controller_advanced_topics: CSRF safe-method exemption * routing: query in the member/collection verb list
4c03500 to
636ed8e
Compare
|
@magnogouveia The changes to the guides mention RFC 10008 each time QUERY is mentioned. This makes the guides more verbose and harder to read. |
Summary
Registers the HTTP QUERY method (RFC 10008, June 2026, Proposed Standard) as a known HTTP method and wires it through Action Pack:
Motivation
QUERY is registered in the IANA HTTP Method Registry as safe and idempotent. It behaves like GET but carries the query in the request content, for queries too large or structured for a URL query string: complex filters, search endpoints, GraphQL, JSON-RPC.
Rails currently rejects the method before it reaches application code:
request_methodraisesActionController::UnknownHttpMethod(rendered as a 405),request.get?raises instead of returning false, andmatch ..., via: :querydraws a route that can never match.Proposed beforehand on the rails-core forum by Muhammet Dilmaç in Proposal: Support for the HTTP QUERY method (RFC 10008); this implements that proposal. Node.js core parses QUERY since 21.7.2, nginx supports it in recent mainline, and Spring has an open PR (spring-projects/spring-framework#34993).
Detail
ActionDispatch::Request:RFC10008constant appended toHTTP_METHODS, plus aquery?predicate (Rack::Request::Helpersdoes not define one).QUERYadded toVerbMatchers::VERBS.queryverb helper alongsideget/post/patch/put/delete.queryrequest helper. NoActionController::TestCasewrapper, mirroringoptions;process(:index, method: "QUERY")works there.Body parameter parsing needs no changes: parsing is keyed on Content-Type rather than the request method, so JSON and form-encoded QUERY bodies already parse into
params.No behavior changes for existing applications: QUERY requests previously raised before reaching any application code, so no framework default is needed.
Out of scope here: the
Accept-Queryresponse header, content-based cache keys for QUERY responses (RFC 10008 §2.7),resourcesintegration, method override, and aRack::Request#query?upstream addition.Note on servers: Puma only allows the eight standard methods by default and answers QUERY with a 501 unless its
supported_http_methodsoption is extended. That is a deployment concern outside Rails; this change makes Rails ready for the method without changing any server behavior.Checklist
[Fix #issue-number]