Repository navigation
Make base58 respect query cancellation and limit input size to 10 KB - #106428
Conversation
The generic (variable-length) `base58` encoder and decoder in `Common/Base58.cpp` use a big-integer base conversion whose cost is quadratic in the input length. They ran to completion without ever checking for cancellation, so a single large value could keep a thread busy for many minutes, ignoring `max_execution_time` (which is only checked between pipeline blocks, not inside one `executeImpl` call on a single huge value) and ignoring `KILL QUERY`. The server-side AST fuzzer (the `Stress test (experimental, serverfuzz, ...)` checks) repeatedly hit this: it runs fuzzed queries synchronously in the connection handler's finish-callback, and a fuzzed `base58Encode`/`base58Decode` over an enlarged argument blocked the handler indefinitely. The query stayed in the process list with `is_cancelled = 1` and the run failed with "Hung check failed, possible deadlock found" across many architectures. `encodeBase58` and `decodeBase58` now accept an optional cancellation callback that is invoked periodically inside their inner loops; `FunctionBaseXXConversion` builds it from the query's `QueryStatus` (`checkTimeLimit`, like `FunctionSleep`), so a long-running `base58` conversion now throws `TIMEOUT_EXCEEDED` or `QUERY_WAS_CANCELLED` promptly. The fixed-size 32/64-byte paths and `base32`/`base64` are linear and unaffected. CI report: https://play.clickhouse.com/play?user=play#U0VMRUNUIGNoZWNrX25hbWUsIHRlc3RfbmFtZSwgcmVwb3J0X3VybApGUk9NIGNoZWNrcwpXSEVSRSAxCiAgICBBTkQgY2hlY2tfc3RhcnRfdGltZSA+PSBub3coKSAtIElOVEVSVkFMIDI0IEhPVVIKICAgIEFORCAoaGVhZF9yZWYgPSAnbWFzdGVyJyBBTkQgc3RhcnRzV2l0aChoZWFkX3JlcG8sICdDbGlja0hvdXNlLycpKQ== Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
In addition to the cancellation check, reject base58 inputs larger than 10 KB. The generic base58 conversion is quadratic in the input length, and base58 is meant for short data (keys, hashes, addresses), so an oversized value is almost certainly a mistake or fuzzer input rather than a legitimate use. A hard limit gives an immediate, clear error instead of a long-running conversion. The limit is enforced in `FunctionBaseXXConversion` via a `Traits::max_input_size` constant (`base32`/`base64` are linear and set it to 0, i.e. unlimited). `base58Encode`/`base58Decode` throw `TOO_LARGE_STRING_SIZE`; `tryBase58Decode` keeps its "empty string on error" contract and returns an empty string for oversized input. The 10 KB limit leaves a large margin over the 32/64-byte values base58 is normally used for. The cancellation check from the previous commit is still needed: many in-limit values in a single block (one `executeImpl` call) can still exceed `max_execution_time`, which is otherwise only checked between pipeline blocks. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [b4b24d1] Summary: ✅
AI ReviewSummaryThis PR makes the generic Final VerdictStatus: ✅ Approve |
…ellation-and-size-limit
The hard 10 KB cap on `base58Encode`, `base58Decode` and `tryBase58Decode` rejected inputs that previous releases accepted, applied unconditionally on upgraded clusters. Gate it behind the new setting `function_base58_max_input_size` (default 10000, `0` disables the limit) so the protective behavior is on by default while `compatibility` with versions before 26.6 restores the previous unlimited behavior. The limit is only enforced for the quadratic `base58` conversion (the only function whose compile-time `Traits::max_input_size` is non-zero); the linear `base32`/`base64` functions ignore the setting. The error message now reports the effective runtime limit. Also renumber the test to 04319 to avoid a number collision introduced by the merge with master. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Updated:
Built clean and verified locally: default cap rejects >10 KB with The remaining 16 red checks are unrelated arm performance-comparison noise ( |
…ellation-and-size-limit
…tion size check Use `base32` instead of `base64` in the test, since `base64` is not built in the Fast test image (the test only needs a linear, unaffected function). Two review fixes in the conversion itself: - The cancellation check in `encodeBase58`/`decodeBase58` is now driven by the accumulated inner-loop work, not the number of outer iterations. The inner loop is `idx` iterations and `idx` grows with the input, so checking once per fixed number of source bytes could leave billions of inner-loop iterations between checks when the size limit is disabled or raised. Worst-case latency is now one inner-loop pass. - `QueryStatus::checkTimeLimit` returns `false` (instead of throwing) for the `break` overflow mode. A single scalar value has no meaningful partial result, so the callback now turns a `break` request into a hard `TIMEOUT_EXCEEDED` stop. And one in the function layer: the input-size limit is now checked before allocating the result buffer. `getBufferSize` reserves roughly the whole input (about twice for encode), so an oversized block could surface as `MEMORY_LIMIT_EXCEEDED` before `TOO_LARGE_STRING_SIZE`. Input lengths are validated up front; the throwing decoder rejects oversized rows before any allocation, and the empty-string decoder sizes the buffer from the in-limit rows alone. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Pushed d27565a (after merging master):
Built locally and verified all |
…ellation-and-size-limit
The arm_tidy build failed with `cppcoreguidelines-init-variables`: `variable 'max_result_size' is not initialized` in `FunctionBaseXXConversion.h`. Initialize it to 0. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106428&sha=d27565af2eb16335626e0d89804858a1685f7bb8&name_0=PR&name_1=Build%20%28arm_tidy%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 166/188 (88.30%) | Lost baseline coverage: none · Uncovered code |
The generic (variable-length)
base58encoder and decoder use a big-integer base conversion whose cost is quadratic in the input length. They ran to completion without ever checking for cancellation, so a single large value could keep a thread busy for many minutes, ignoringmax_execution_time(which is only checked between pipeline blocks, not inside oneexecuteImplcall on a single value) and ignoringKILL QUERY.This was repeatedly hit by the server-side AST fuzzer in the
Stress test (experimental, serverfuzz, ...)checks, which runs fuzzed queries synchronously in the connection handler's finish-callback: a fuzzedbase58Encode/base58Decodeover an enlarged argument blocked the handler, the query stayed in the process list withis_cancelled = 1, and the run failed withHung check failed, possible deadlock foundacross many architectures.Two complementary fixes:
QueryStatus::checkTimeLimit, likeFunctionSleep), so many in-limit values in a single block still respect the time limit andKILL QUERY.base58inputs are limited to 10 KB by default.base58is meant for short data (keys, hashes, addresses), so larger inputs are almost certainly a mistake or fuzzer input.base58Encode/base58DecodethrowTOO_LARGE_STRING_SIZE;tryBase58Decodekeeps its "empty string on error" contract.base32/base64are linear and unaffected.The size limit is configurable through the new setting
function_base58_max_input_size(default10000;0disables the limit). Because this rejects inputs that previous releases accepted, the change is registered inSettingsChangesHistoryfor26.6with a compatibility value of0, socompatibilitywith versions before26.6restores the previous unlimited behavior on upgraded clusters.CI report: https://play.clickhouse.com/play?user=play (
checkstable,Stress test (experimental, serverfuzz, ...),test_name = 'Hung check failed, possible deadlock found'onmaster).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Functions
base58Encode,base58DecodeandtryBase58Decodenow respectmax_execution_timeand query cancellation on large inputs, and reject inputs larger than 10 KB instead of running for a very long time. The limit is configurable via the new settingfunction_base58_max_input_size(0disables it).Documentation entry for user-facing changes
🤖 Generated with Claude Code
Version info
26.6.1.525