Skip to content

Do not let the request body extend the query of a custom HTTP handler - #126642

Merged
alexey-milovidov merged 15 commits into
masterfrom
fix-handler-request-body-passthrough
Nov 6, 2026
Merged

alexey-milovidov merged 15 commits into
masterfrom
fix-handler-request-body-passthrough

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Related: https://github.com/ClickHouse/clickhouse-private/issues/76671#issuecomment-6259244186

A request to a SQL-defined handler (CREATE HANDLER) whose query uses {_request_body:String} could also pass param__request_body in the URL. The body was then left unread, and the HTTP layer appended it to the handler's query text, as it does for the default handler. So a body such as UNION ALL SELECT 1+1 became part of the handler's SQL. No new privileges were gained, because the HTTP interface runs arbitrary queries anyway. But a custom handler is expected to run only its own query.

This change does the following:

  • For SQL-defined handlers, the request body is appended to the query text only when it is the query's data: an INSERT, or an INSERT ... SELECT reading from input. Otherwise the body is never appended, including when _request_body is bound some other way, such as from a URL REGEXP capture group.
  • Passing _request_body in the URL (as _request_body or param__request_body) to a predefined or SQL-defined handler now throws a BAD_ARGUMENTS exception. For a SQL-defined handler whose URL REGEXP has a named capture group _request_body, the parameter is bound from that capture and the body is never read for it, so such a handler may use GET and does not require Content-Length.
  • For configuration-defined predefined_query_handler rules, whose query is not analyzed and whose body is always appended, binding _request_body from a named capture group of url / headers regular expressions also throws a BAD_ARGUMENTS exception.
  • The query of a SQL-defined handler that takes the body as data is parsed from the stored text alone, so the body is only its data and is never parsed as SQL, even for an INSERT ... SELECT ... FROM input(...) without a literal FORMAT.
  • A _request_body left in a named HTTP session (SET param__request_body = ...) is ignored by handlers using it: the parameter is bound from the current request only.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The request body of a SQL-defined HTTP handler (CREATE HANDLER) is appended to the handler's query only as the data of an INSERT, so it can never extend the handler's SQL. The _request_body query parameter of custom HTTP handlers can no longer be passed in the URL.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/126642]

Version info

  • Merged into: 26.11.1.1560-master (included in 26.11 and later)

A request to a SQL-defined handler whose query uses `_request_body` could pass
`param__request_body` in the URL. The body was then left unread and appended to
the handler's query text, so a body such as `UNION ALL SELECT 1+1` extended the
handler's SQL.

Now the body is appended to the query of a SQL-defined handler only as the data
of an `INSERT` (or of the `input` table function), and `_request_body` cannot be
passed in the URL of a predefined or SQL-defined handler.

ClickHouse/clickhouse-private#76671 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mintlify

mintlify Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
ClickHouse-docs 🟢 Ready View Preview Nov 4, 2026, 7:32 AM

@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [38724bb]

Summary: ✅


AI Review

Summary

This PR prevents HTTP request bodies from extending the stored SQL of CREATE HANDLER, preserves uploaded data for body-consuming INSERT queries, and binds _request_body from the current request. At head 38724bb9a1157f1443ae9d214f17d05f43444375, the five prior findings are addressed and no new actionable findings remain.

Missing context / blind spots
  • ⚠️ Current-head integration results for test_http_handlers_config::test_predefined_query_handler were unavailable during review. A passing result would confirm the configuration-defined handler regressions. The changed stateless tests 04825_handler_request_body_with_form_params and 05407_handler_request_body_not_appended_to_query passed in the current-head fast-test report; no local tests were run.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... labels Oct 19, 2026
Comment thread src/Server/HTTPHandler.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 38724bb9a with master 7f11488e0 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

24 translation units recompiled, 132 s compile time in total, 24 of them have a recent master baseline.

Job report

alexey-milovidov and others added 2 commits October 21, 2026 09:56
…iguration-defined handler

A named capture group of `url_regexp` or `headers_regexp` of a `predefined_query_handler` could bind
`_request_body`. The body was then left unread and appended to the handler's query text, so a body such
as `UNION ALL SELECT 1+1` still extended the handler's SQL. Such a capture is now rejected with
`BAD_ARGUMENTS` whenever the handler appends the body to its query. SQL-defined handlers append the body
only to an `INSERT`, which cannot use `_request_body`, so a `URL REGEXP` capture may still bind it there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…figuration-defined handler

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 2 commits October 23, 2026 06:16
…ad the body

A `CREATE HANDLER` whose `URL REGEXP` has a named capture group `_request_body`
binds the parameter from the URL, so the handler never reads the request body
for it. But `consumes_request_body` was still derived from the query using
`_request_body`, so such a handler was rejected with `GET`, and every request
without `Content-Length` was answered with `411 Length Required`.

Drop a captured `_request_body` from the parameters bound by the handler layer
in `makeSQLDefinedHandler`, so the body-consumption contract matches the
execution, and the body is never read for it. Rejecting such handlers at
creation instead would make already stored handlers fail to load.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 2 commits October 25, 2026 22:03
…rom a `URL REGEXP` capture

A SQL-defined handler binding `_request_body` from a named capture group of its `URL REGEXP` drops it from
`receive_params`, so that the body is never read for it. That also removed the only signal
`PredefinedQueryHandler::customizeQueryParam` used to reject `_request_body` passed in the URL query string:
`?param__request_body=...` was silently ignored and `?_request_body=...` fell through to the generic path.

Track separately whether the query uses `_request_body` (`uses_request_body_param`) and use it for the URL guard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126642&sha=3a5c6fbe4edac34e2bd50bfded3aa03db8ef8bb3&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20sequential%29 (05218_text_index_substreams_opened_lazily) and provide a fix in a separate PR. It fails across many unrelated PRs and on master; the fix appears to be in progress in #127781. I merged the latest master into this branch.

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is my open #127781 (a separate, test-only PR).

The run here at 3a5c6fbe has the same signature: the diff is in_range_above_threshold 1 -> 0, and the runner's diagnosis names --use_columns_cache 1 as the culprit (19/19 fail with the same settings, 20/20 pass without randomization). The 05218 table uses compact parts, and since #127287 the columns cache also serves them, so the warm in-range query opens fewer files than the control expects. #127781 pins use_columns_cache = 0 in the test's query settings. Its previous head was approved with green CI; the latest push only adds a one-line comment in the test.

Comment thread src/Server/HTTPHandler.cpp Resolved
alexey-milovidov and others added 3 commits October 27, 2026 19:35
Two remaining paths let the request body extend the SQL of a custom HTTP handler:

- A SQL-defined handler whose query takes the body as data but has no literal data
  boundary, such as `INSERT INTO t SELECT x FROM input('x UInt64')` without `FORMAT`:
  the body was concatenated to the query text before parsing, so a body like
  `UNION ALL SELECT 99 FORMAT TSV` became part of the query. Now the query of a
  SQL-defined handler that feeds the body to the query is parsed from the stored
  text alone (`parse_query_from_initial_buffer`), and the body is only its data.

- A `_request_body` query parameter left in a named HTTP session by
  `SET param__request_body = ...` made the handler skip reading the body. A
  configuration-defined handler then appended the unread body to its query, and a
  SQL-defined handler used the stale value. Such an inherited value is now dropped
  by a handler using `_request_body`, so it is bound from the current request only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The `TabSeparated` output escapes single quotes, so use a body without them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. #127781 is merged, and I merged the latest master into this branch, so 05218_text_index_substreams_opened_lazily should pass on the next run. I also addressed the two AI-review findings (input handlers without FORMAT, and _request_body inherited from a named session) and added regressions for both.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The remaining failures at 4931e133 are unrelated to this change:

@groeneai, if any of these has no fix in progress yet, please investigate it and provide a fix in a separate PR.

@groeneai

Copy link
Copy Markdown
Collaborator

Status per line:

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 28, 2026
…es without rabbitmqctl

`rabbitmqctl close_all_connections test` timed out after 300 s in 3 runs of
`Integration tests (arm_binary, 2/4)` (master 524a0c2, ClickHouse#127389, ClickHouse#126642). The
close never reached the broker: no `Closing connection ... because "test"` line
in its log, and ClickHouse's AMQP connection stayed open for 5 minutes until the
test dropped the database. The broker passed its `rabbitmq-diagnostics ping`
healthcheck 29/29 times in each window, so a new CLI process again never got to
its RPC, like the `list_queues` call before.

Bounding the call and asking again does not fit here: the drop must happen while
most of the backlog is still queued (`view_rows() < messages_num // 2`), which
leaves about 9 s after the 500-row trigger at ~490 rows/s, and a timed-out
`docker exec` leaves the CLI running in the container, where a late close would
force a second reconnect.

Close through the management API instead:
`DELETE /api/connections/username/root` with `X-Reason: test` reaches the same
`rabbit_networking:close_connection(Pid, "test")` for every connection of the
broker's only user, synchronously before the 204 reply, and starts no CLI process.

Validated: an injected never-returning close fails the unfixed test after 300 s
with the CI error; without the DELETE the test fails waiting for the reconnect;
the pre-ClickHouse#125105 binary still fails on the duplicates bound (9500, 9400); 50/50
runs pass with `204` (p50 0.1 s) and ClickHouse logs `CONNECTION_FORCED - test`
then `Connection restored`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator

test_rabbitmq_reconnect_drops_stale_deliveries: pushed to #126746 as 9c18fa2. The connections are now closed through the RabbitMQ management API (DELETE /api/connections/username/root) instead of rabbitmqctl close_all_connections, so that step starts no CLI process. In all 3 hung runs the close never reached the broker (no Closing connection line in its log).

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai for the status. I merged the latest master (047484d8) into this branch, which pulls in #127865 for 05307_json_shared_typed_paths_concurrent_readers. The remaining red checks (test_rabbitmq_reconnect_drops_stale_deliveries, the Build profile diff LogClusterNotReady error, and CH Inc sync) are unrelated to this change. Fixes for them are in progress in #126746, #126200 and #127837.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The only failure at b594ae40 was 05397_clickhouse_local_locked_path_untouched in Fast test (arm_darwin), which is unrelated to this change. Master already skips that test on macOS (59f220cb8187), so I merged the latest master into this branch (d9d7fc4b) to pull that in.

# Conflicts:
#	src/Server/HTTPHandler.cpp
#	src/Server/HTTPHandler.h
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The only failure at 32bd9a4f was 05443_gdeflate_codec (Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel)), which is unrelated to this change. The GDeflate codec and its test were reverted on master in #128947, so I merged the latest master into this branch.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Nov 6, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 6, 2026
Merged via the queue into master with commit 1bc8e69 Nov 6, 2026
185 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-handler-request-body-passthrough branch November 6, 2026 20:49
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 6, 2026

This branch was successfully deployed

1 active deployment
staging - docs — 38724bb9 Deployed Nov 4, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants