Repository navigation
Bound a sequential Azure read by the locally set right bound, not by the response length - #120910
Conversation
…the response length `ReadBufferFromAzureBlobStorage::initialize` derived `total_size` from `BodyStream::Length`, which is the `Content-Length` chosen by the remote endpoint. With `read_until_position = 100`, `offset = 0` and a 64-byte reading buffer, an endpoint that answers the 100-byte ranged request with `Content-Length = 128` made `nextImpl` hand out 64 bytes twice and only trip the right-bound check on the following call, so 28 bytes from outside the requested range had already escaped into the caller. With a reading buffer larger than the range, all 28 escape in a single call. Derive the bound of the current download from `read_until_position`, which is set locally by the caller, and let the length of the response only shorten it. The size of the blob from the same response is not used for this: it comes from the same untrusted place, and an endpoint that answers a ranged request with `200 OK` would make it truncate a legitimate read. `src/Disks/tests/gtest_azure_read_buffer.cpp` gains regressions that drive `nextImpl` through `setReadUntilPosition` against a fake Azure HTTP transport whose response is longer than the requested range, with a reading buffer both smaller and larger than the range. Without the change they fail with `Attempt to read beyond right offset (128 > 99)`. This is one of the independent parts of #115706, which was reverted in #120419. The positioned read path, `readBigAt`, was fixed separately in #119054.
|
Workflow [PR], commit [1d6b7c4] Summary: ✅
AI ReviewSummaryThis PR hardens Azure object reads around one invariant: the bytes a caller may observe must be bounded by locally known metadata, not by whatever Final Verdict
LLVM Coverage ReportMeasured on commit 1d6b7c4.
Changed lines: Changed C/C++ lines covered: 188/205 (91.71%) · Uncovered code |
`ReadBufferFromAzureBlobStorage::setReadUntilPosition` only set `read_until_position` and dropped `initialized`, leaving `working_buffer` and `offset` alone. The bytes that the previous, wider bound allowed to buffer stayed in the working buffer, so a caller that tightened the bound after a partial read still got them: fill 64 bytes for a 100-byte bound, consume 10, call `setReadUntilPosition(20)`, and `ReadBuffer::read` keeps returning bytes `10..63` before the `read_until_position < offset` guard fires on the later refill. That breaks the `supportsRightBoundedReads` contract, which promises that the new bound applies before any further bytes are exposed. Give up the buffered bytes and rewind `offset` to the position the caller has read up to, the same way `setReadUntilEnd` and `ReadBufferFromS3::setReadUntilPosition` do, so the next `nextImpl` downloads the range under the new bound. Setting the same bound again is now a no-op instead of a forced re-download. `src/Disks/tests/gtest_azure_read_buffer.cpp` gains a range-aware fake transport and regressions that tighten the bound after a partial read (with a well-behaved and with an overlong endpoint) and check that `eof` is reported exactly at the new bound, plus a widening case as a control. Without the change the tightening cases read 54 bytes past the new bound and then throw `Attempt to read beyond right offset`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 @groeneai, investigate the failures and provide a fix in a separate PR (if a fix is already in progress, link it here):
Both are unrelated to this PR. The only server-side code it changes is
I have merged |
|
Both rows are covered by an open fix of mine, so no new PR is needed: #121090 ("Perf CI: fail a Measured on
Each query is inside its own bar, which is 1.5x the 0.99 quantile of #121090 restores the conjunction: the dashboard is consulted only for queries the shard's own report published as slower ( One difference from the same ask on #120488: there the learned thresholds were missing (the CIDB fetch for them timed out, and the empty file leaves the bare 0.15/0.25 floors), so that dashboard row did sit in the in-tree slower list and #121090 alone does not clear it. Here the fetch succeeded, which is why the bars above are 10.6 and 0.91 instead of 0.15; the fix for the timeout is a separate change under review on my side and I will link it on #120488. |
… range is a bound `ReadBufferFromAzureBlobStorage` used `0` as the "no bound" sentinel for `read_until_position`, so `setReadUntilPosition(0)` - the empty range `[0, 0)` - was taken for an unbounded read: `nextImpl` and `initialize` both tested the bound for being nonzero, and the reader delivered the contents of the blob instead of reporting EOF, contradicting `supportsRightBoundedReads`. The bound is now a `std::optional<size_t>`, which is empty only when the caller has not set one or has called `setReadUntilEnd`. The single call site that passed a literal `0` for "no bound", `AzureObjectStorage::readObject`, passes `std::nullopt`. Three regressions in `gtest_azure_read_buffer.cpp`: `EmptyRange` (a bound of zero reports EOF without issuing a download at all), `EmptyRangeThenReadUntilEnd` (`setReadUntilEnd` still lifts it) and `TightenedToEmptyRangeAfterPartialRead` (a bound below the current position followed by a `seek`, which `ReadBuffer::setReadUntilPosition` recommends supporting). All three fail on a control binary that keeps the old zero sentinel. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Thanks @groeneai — noted, #121090 covers both performance rows, so nothing to do here on that front. On the new head I have pushed |
…d offset The endpoint chooses the range it answers a ranged request with. It is allowed to ignore the requested range and answer `200 OK` with the whole blob, and a misbehaving one can answer with a different range altogether. In either case the body was handed to the caller as if it were the bytes at the requested offset: after `seek(100)` and `setReadUntilPosition(200)` a response starting at byte `0` delivered bytes `0..99` under offsets `100..199`, a wrong slice of the blob. `checkResponseStartsAtRequestedOffset` compares the range of the response with the offset that was requested and rejects a mismatch, in both `initialize` (the sequential path) and `readBigAt`. There is no fallback that skips the unwanted prefix: only the correct slice is accepted. New gtests `AzureReadUntilPosition.RejectsResponseStartingAtWrongOffset` and `AzureReadBigAt.RejectsResponseStartingAtWrongOffset` read from a nonzero offset through `RangeResponseTransport`, which answers every request with the blob from its beginning, and `AzureReadUntilPosition.SeekToNonZeroOffsetWithHonestEndpoint` pins that an endpoint honouring the range still works. A control binary with the check removed fails both rejection tests; 15/15 `AzureRead*` pass with it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Pushed It adopts the AI-Review Blocker — a ranged response was still trusted to start at the requested offset, so after It also merges |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units40 translation units recompiled, 345 s compile time in total, 40 of them have a recent master baseline. |
Conflict in `src/Disks/IO/ReadBufferFromAzureBlobStorage.cpp`: master has landed `checkReturnedRange`, which does exactly what the `checkResponseStartsAtRequestedOffset` helper added here did - it compares `ContentRange.Offset` of the response with the requested offset and throws, in both `initialize` and `readBigAt`. Resolved by keeping master's helper and dropping the duplicate one, along with the two gtests that only pinned that behaviour, which master's new `gtest_azure_read_buffer_range_check.cpp` covers. The rest of this PR - bounding a sequential read by the locally set `read_until_position` instead of the response length - is unaffected. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…metadata `AzureObjectStorage::readObject` passed `std::nullopt` as `read_until_position`, so a plain whole-object read had no trusted right bound at all and its size came from the length of the response, which is exactly what this PR set out to stop trusting. An endpoint answering a request for a 100-byte object with 128 bytes handed the caller 28 bytes past the end of the object on the default path used by `ObjectStorageSourceReader::open` and `readSmallObjectAndGetObjectMetadata`. `StoredObject::bytes_size` is the locally known bound for that case, so it is now threaded through as the initial `read_until_position`, with the `UnknownSize` sentinel (and `0`) mapped to `std::nullopt` exactly as in `S3ObjectStorage::readObject`. Two gtests on the `readObject` path: `AzureReadObject` checks that a 100-byte object served by a 128-byte response yields 100 bytes, and that an object of unknown size still reads to the end. A control build keeping `std::nullopt` fails the first with `Which is: 128` against `Which is: 100`; 19/19 `AzureRead*` pass with the fix. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 @groeneai, investigate the failure and provide a fix in a separate PR (if a fix is already in progress, link it here):
It is unrelated to this PR. The throw is in The two closed CI-matching issues for this message, #119408 and #113292, were both closed on 2026-09-13 with no closing commit, and the first has been cross-referenced by several PRs hitting it since, so the family looks like it is recurring on master rather than fixed. It may need a fresh issue. Pushed in the meantime: merged |
|
Fix in progress: #110020, and it targets exactly the throw you pointed at. I verified it against this shape instead of assuming. On master Why the family keeps recurring: #110020 is green and mergeable, open since 2026-07-10, and in those 73 days no human review has ever been requested on it. The only earlier On the two closed issues, the measured picture supports your reading. Both were closed at 2026-09-13T08:54:17Z and 08:54:18Z, one second apart, by you, with I would reopen #113292 rather than file a fresh issue. It is titled for this exact shape, a |
`AzureObjectStorage::readObject` mapped both `StoredObject::UnknownSize` and `0` to "no right bound". `UnknownSize` is the default-constructed value of `StoredObject::bytes_size`, so it is the only sentinel a caller that does not know the size leaves behind; `bytes_size == 0` describes a genuinely empty object and is a bound like any other. With `0` taken for "unknown", an empty object was read unbounded, so an endpoint answering the `GET` with a non-empty body handed the caller bytes of an object whose trusted metadata says it has none - exactly the leak this change is about, on the one input where the bound is tightest. `S3ObjectStorage::readObject` keeps mapping `0` to "no bound" because `ReadBufferFromS3::read_until_position` is a plain `size_t` whose `0` already means "unbounded", so it cannot express the empty range at all. `ReadBufferFromAzureBlobStorage` can, since the bound became a `std::optional<size_t>` in an earlier round of this review. New regression `AzureReadObject.EmptyObjectReadsAsEmpty`: a `StoredObject` of size `0` against an endpoint serving 128 bytes must read as empty. A control build keeping the old condition fails it; 20/20 `AzureRead*` pass with the fix. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 @groeneai — your reading is right on both counts, thanks for measuring it rather than assuming. I have reopened #113292 rather than filing a fresh one, for the reasons you gave: it is titled for exactly this shape, it carries a deterministic reproducer that needs no fuzzer, and a new issue would split its 35 cross-references. #119408 can stay closed as the fuzzer-reported duplicate of it. My close on 2026-09-13 was a sweep, not a fix landing, as the one-second gap between the two shows. On #110020 — green, mergeable, and open since 2026-07-10 with no human review ever requested is not a state it should be in for a fix to a recurring logical error. Requesting devcrafter and KochetovNicolai was the right move; I will look at it myself as well. Pushed The other finding of that round — threading No failed checks on the previous head |
Address the review of ClickHouse#123379: - `.claude/tools/fetch_ci_report.js` with a PR URL expanded the all-green `PR` workflow into all its ~146 green job reports even when the `MergeQueueCI` run of a PR rejected by the merge queue contributed the failed job, burying the one failure that matters. An all-green top-level report is now expanded only when no other workflow has failed jobs; for the merge-queue rejection of ClickHouse#120910 the output shrinks from 147 reports to the single failed `Build (amd_binary)` job. - The `continue-pr` and `continue-pr-auto` skills suggested building the log URL as `.../mergequeueci/<job>/job.log`, but Praktika stores attached files under normalized sub-result directories, so such a URL returns `AccessDenied`. They now say to follow the `links` of the failed results (as printed by `fetch_ci_report.js`, also with `--links`) or to use `--download-logs`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nly the core bound fix The original PR #120910 builds on Azure reader changes that are not in 26.3 (#119054, #120955, #120963, `s3_validate_etag_on_read`, `LazyReadFromObjectStorageSource`, the `gtest_azure_read_buffer` fake transport). Resolved by taking the 26.3 tree for every touched path and porting only the fix itself to `ReadBufferFromAzureBlobStorage`: - the bound of the current download (`total_size`) is the locally set `read_until_position`, and the `Content-Length` of the response can only shorten it; - `setReadUntilPosition` discards the bytes buffered under the previous, wider bound and rewinds `offset`, so a tightened bound takes effect immediately. Not backported: `If-Match` pinning with `azure_validate_etag_on_read` and the `AZURE_OBJECT_CHANGED_DURING_READ` error code, the `std::optional` right bound, the `file_size` constructor argument, and the tests that depend on those (the gtest file does not exist in 26.3, `05259_lazy_materialization_azure_etag_pinned`, the integration test). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #120910 to 26.3: Bound a sequential Azure read by the locally set right bound, not by the response length
…y set right bound, not by the response length
Resolve the conflicts of the cherry-pick of #120910 to 26.7. The release branch lacks several prerequisites of the original change, so only its own part is kept: - `checkReturnedRange`, the premature-end-of-response handling and the lazy `getBlobClient` are not on 26.7; the `ETag` pinning and the local right bound are applied to the existing `blob_client` code instead. - Lazy materialization from object storage is not on 26.7, so the changes to `LazyReadFromObjectStorageSource`, `canUseLazyMaterialization` and the test `05259_lazy_materialization_azure_etag_pinned` are dropped. - `gtest_azure_read_buffer.cpp` is not on 26.7 (it came with #119054, which is not backported), so it is not added here. - The new setting `azure_validate_etag_on_read` is recorded in the "26.8" block of `SettingsChangesHistory.cpp`, as other backported settings are.
Cherry pick #120910 to 26.7: Bound a sequential Azure read by the locally set right bound, not by the response length
…y set right bound, not by the response length
…o 26.8 The cherry-pick of #120910 conflicted because 26.8 lacks the prerequisite Azure read fixes that landed on `master` first: #119054 (lazy blob client, `readBigAt` fix, and the creation of `gtest_azure_read_buffer.cpp`), #120955 (`checkReturnedRange`) and #120963 (reopen on a premature end of a bounded read). Resolution: - `ReadBufferFromAzureBlobStorage` is the 26.8 version with the changes of #120910 applied on top: `read_until_position` becomes `std::optional<size_t>`, the current download is bounded by `getTotalSizeOfCurrentDownload`, `setReadUntilPosition` drops the bytes buffered past a tighter bound, and every `Download` is pinned with `If-Match` to `expected_etag`, with the `ETag` of the response checked and `412` reported as `AZURE_OBJECT_CHANGED_DURING_READ`. The `file_size` argument is reported by `tryGetFileSize`. - `AzureObjectStorage::readObject` uses `connection_params.getContainer` as in 26.8. - `AZURE_OBJECT_CHANGED_DURING_READ` keeps the code 1021 from `master`; `END` is raised to it. - `azure_validate_etag_on_read` is declared in the 26.8 style, with its history entry in the `26.10` section of `SettingsChangesHistory.cpp`. - `gtest_azure_read_buffer.cpp` is added with the tests of #120910 only. The tests that belong to the prerequisites (`AzureReadBigAt.DoesNotTrustResponseLength`, `AzureReadBigAt.ReturnsAccumulatedCountOnTruncatedResponse`, `AzureReadUntilPosition.ShortRangeResponse`) are dropped, the `AzureObjectStorage` constructor takes the 26.8 `auth_method` argument, and `AzureReadBigAt.RejectsReplacedBlobThroughIfMatch` calls `getFileSize` first, because in 26.8 that is what creates the blob client for `readBigAt`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #120910 to 26.8: Bound a sequential Azure read by the locally set right bound, not by the response length
…y set right bound, not by the response length
Master's ClickHouse#120910 pins Azure reads to the listed generation inside ReadBufferFromAzureBlobStorage itself, so master's version of that buffer replaces the branch's own If-Match code. It reports a replaced blob as AZURE_OBJECT_CHANGED_DURING_READ, so the queue now treats that code like FILE_CHANGED_DURING_READ and S3_OBJECT_CHANGED_DURING_READ; otherwise a rewritten Azure object would be charged to the retry budget. Master also made read_until_position an optional where 0 is an empty range, so the read-and-write copy fallback now passes no bound instead of 0. createReadBuffer pins under either per-backend validate setting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #120910 to 26.8: Bound a sequential Azure read by the locally set right bound, not by the response length
Backport #120910 to 26.3: Bound a sequential Azure read by the locally set right bound, not by the response length
Backport #120910 to 26.7: Bound a sequential Azure read by the locally set right bound, not by the response length
Related: #115706
Related: #120419
The sequential read path of
ReadBufferFromAzureBlobStoragetrusted the remote endpoint about how much data the current download may deliver:initializederivedtotal_sizefromBodyStream::Length, which is theContent-Lengthchosen by the endpoint. Withread_until_position = 100,offset = 0and a 64-byte reading buffer, an endpoint that answers the 100-byte ranged request withContent-Length = 128madenextImplhand out 64 bytes twice and only trip the right-bound check (Attempt to read beyond right offset) on the following call, so 28 bytes from outside the requested range had already escaped into the caller. With a reading buffer larger than the range, all 28 escape in a single call. The layers above (ReadBufferFromRemoteFSGather,AsynchronousBoundedReadBuffer, the caches) lay out the next object of a file, or the next segment, right behind the right bound, so these bytes are delivered under offsets that belong to something else.The bound of the current download is now derived from
read_until_position, which is set locally by the caller, and theContent-Lengthof the response can only shorten it. The size of the blob from the same response is deliberately not used for this: it comes from the same untrusted place, and an endpoint that answers a ranged request with200 OKwould make it truncate a legitimate read. A read without a right bound keeps its previous behaviour.setReadUntilPositionalso gave up nothing when the caller tightened the bound: it only setread_until_positionand droppedinitialized, so the bytes that the previous, wider bound allowed to buffer stayed in the working buffer and were still handed out before the right-bound check could fire on the next refill. It now discards them and rewindsoffsetto the position the caller has read up to, the same waysetReadUntilEndandReadBufferFromS3::setReadUntilPositiondo, so a new bound applies before any further byte is exposed, assupportsRightBoundedReadspromises.src/Disks/tests/gtest_azure_read_buffer.cppgains regressions that drivenextImplthroughsetReadUntilPositionagainst a fake Azure HTTP transport whose response is longer than the requested range, with a reading buffer both smaller and larger than the range, plus the exact and short responses as controls. Without the change the overlong cases fail withAttempt to read beyond right offset (128 > 99).AzureObjectStorage::readObjectsets no right bound of its own, so a whole-object read had its size decided by the response as well. It now passes the size recorded in the metadata,StoredObject::bytes_size, as the bound (UnknownSizeis the only "not determined" sentinel;0is a genuinely empty object, which is read without a request and whose metadata comes from the properties inreadSmallObjectAndGetObjectMetadata). The same size is handed to the buffer as itsfile_size, sogetFileSizereports the size the read is bounded by instead of asking the endpoint with a liveGetProperties, which would describe the current generation and makeCachedInMemoryReadBufferFromFilethrowUNEXPECTED_END_OF_FILEon a blob grown since the listing.A locally known size is only a correct bound for the generation of the object it was recorded for: a blob replaced with a longer one after the listing would otherwise be silently truncated to the stale size. So, as
ReadBufferFromS3does, everyDownloadis pinned toStoredObject::etagwith theIf-Matchaccess condition, theETagof the response is checked against it for endpoints that ignore the condition, and the412 Precondition Failedis reported as the newAZURE_OBJECT_CHANGED_DURING_READerror, which is not retried. The zero-byte path checks theETagof the properties instead. A blob listing spells theETagwithout the quotes that theETagheader of a download carries, so both spellings are canonicalized to the quoted one (ReadBufferFromAzureBlobStorage::quotedETag) before they are compared or sent asIf-Match. An object whose size is listed as zero is read without a download, so its generation is checked on the properties instead, inreadObjectas well as inreadSmallObjectAndGetObjectMetadata.The pin is controlled for table reads by a new setting,
azure_validate_etag_on_read(default on), the Azure counterpart ofs3_validate_etag_on_read; it also refreshes the metadata placeholder ofazureBlobStorageCluster, which skips the metadata probe. That placeholder is never used as a bound:createReadBufferpassesStoredObject::UnknownSizeunless the metadata was really fetched, because the placeholder'ssize_bytes == 0would otherwise read every file as empty on the initiator whencluster_table_function_split_granularity = 'bucket'.This is one of the independent parts of #115706, which was reverted in #120419; it contains only the code related to this defect. The positioned read path,
readBigAt, was fixed separately in #119054.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a buffer over-read on the sequential read path from Azure Blob Storage: an endpoint that answers a ranged request with more data than was requested could deliver bytes from outside the requested range to the caller. The end of a read is now decided by the locally set right bound, not by the
Content-Lengthof the response, and a read is pinned to the listed generation of the blob withIf-Match, so an object replaced during a read fails withAZURE_OBJECT_CHANGED_DURING_READinstead of being truncated (new settingazure_validate_etag_on_read, enabled by default).Workflow [PR]
Sync PR [sync-upstream/pr/120910]
Version info
26.10.1.1483-master(included in26.10and later)26.8.17.4,26.7.22.4,26.3.42.2