Skip to content

Check the offset of the range an Azure endpoint returns against the requested one - #120955

Merged
alexey-milovidov merged 2 commits into
masterfrom
azure-read-range-check
Sep 20, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
azure-read-range-check

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Related: #115706
Related: #120419

A successful Download is not enough to trust the body: an Azure Blob Storage endpoint, or a proxy in front of it, that ignores the requested range answers 200 OK with the whole object from byte 0, and ReadBufferFromAzureBlobStorage consumed that body as if it started at the requested offset. This happens on the sequential path (a read after a seek, or a download reopened after a retry) and in readBigAt alike. The caller then received the wrong bytes under the right offsets: silent data corruption.

The start of the returned range is now checked against the requested offset before the body is read, in both places, and a mismatch is reported as HTTP_RANGE_NOT_SATISFIABLE. The SDK reports a 200 OK response as a range starting at 0, so a full-object response is accepted exactly when the request started at 0, where it is a correct answer, the same way ReadWriteBufferFromHTTP treats it.

src/Disks/tests/gtest_azure_read_buffer_range_check.cpp drives a positioned read at offset 40 and a sequential read after a seek to 40 against a fake endpoint that ignores ranges, and asserts the error; a positioned read at offset 0 against the same endpoint, and a sequential read against a well-behaved one, are the controls.

This is one of the independent parts of #115706, which was reverted in #120419.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Reads from Azure Blob Storage now verify that the returned range starts at the requested offset, so an endpoint or proxy that ignores the Range of a request fails the read with HTTP_RANGE_NOT_SATISFIABLE instead of delivering the wrong bytes.


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

Version info

  • Merged into: 26.10.1.313-master (included in 26.10 and later)

…equested one

A successful `Download` is not enough to trust the body: an endpoint, or a proxy in front
of it, that ignores the requested range answers `200 OK` with the whole object from byte 0,
and the reader consumed that body as if it started at the requested offset - both on the
sequential path (after a seek or on a reopen) and in `readBigAt`. The caller then got the
wrong bytes under the right offsets, which is silent data corruption.

The start of the returned range (`Content-Range`, which the SDK reports as 0 for a
`200 OK` response) is now checked against the requested offset before the body is read,
and a mismatch is `HTTP_RANGE_NOT_SATISFIABLE`. A full-object response is accepted exactly
when the request started at 0, where it is a correct answer, the same as in
`ReadWriteBufferFromHTTP`.

`src/Disks/tests/gtest_azure_read_buffer_range_check.cpp` drives a positioned read and a
sequential read after a seek against a fake endpoint that ignores ranges, and a read at
offset 0 and a well-behaved endpoint as controls.

This is one of the independent parts of
#115706, which was reverted in
#120419.
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ec702c4]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR adds a left-bound range check to Azure blob downloads and new gtests around endpoints that ignore byte ranges. It catches the wrong-start cases the new helper checks, but the sequential reader still does not satisfy its advertised exact right-bound contract, so the patch is not complete yet.

Findings

❌ Blockers

  • [src/Disks/IO/ReadBufferFromAzureBlobStorage.h:53, src/Disks/IO/ReadBufferFromAzureBlobStorage.cpp:286,352] supportsRightBoundedReads() promises EOF exactly at read_until_position, but the new guard only checks the returned start offset. If a bounded request starts at 0 and the endpoint replies with the whole object (or any response that starts at the requested offset but ignores the right bound), initialize still sets total_size = data_stream->Length() + offset, and nextImpl can expose bytes past the logical end before the following call throws LOGICAL_ERROR. That leaves callers such as CachedOnDiskReadBufferFromFile and ReadBufferFromFileView observing out-of-range bytes while this class advertises that no wrapper is needed. Suggested fix: when read_until_position is active, either reject responses whose visible length exceeds read_until_position - offset, or clamp total_size to read_until_position before the first read, and add a regression test for setReadUntilPosition() at offset 0 (ideally also for a 206 with the correct left bound but an oversized end).
Final Verdict
  • Status: ⚠️ Request changes
  • Fix the bounded sequential-read overrun so ReadBufferFromAzureBlobStorage actually honors supportsRightBoundedReads().
  • Add a focused regression test that covers setReadUntilPosition() / bounded reads starting at 0 against an endpoint that ignores the requested end of the range.

LLVM Coverage Report

Measured on commit ec702c4.

Metric Baseline Current Δ
Lines 88.40% 88.50% +0.10%
Functions 91.80% 91.80% +0.00%
Branches 80.60% 80.70% +0.10%

Changed lines: Changed C/C++ lines covered: 85/87 (97.70%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 19, 2026
/// would hand the caller the wrong bytes under the right offsets - silent data corruption - so
/// the start of the returned range is checked against the requested one before the body is read.
/// The SDK reports a `200 OK` response as the range starting at 0, so a full-object response is
/// accepted exactly when the request started at 0, where it is a correct answer, the same as in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

supportsRightBoundedReads() promises that setReadUntilPosition will stop exactly at the requested bound, but this helper now also accepts a full-object 200 OK whenever the request starts at 0. That is still wrong for bounded reads: initialize stores total_size = data_stream->Length() + offset, and nextImpl then uses that full length directly, so setReadUntilPosition(16) can expose bytes past 16 before the next call notices and throws LOGICAL_ERROR. Please either reject whole-object responses when a shorter right bound is active, or clamp the visible stream length to the requested bound, and add a regression test for offset == 0 with read_until_position < blob_size.

@clickhouse-gh clickhouse-gh Bot added the comp-azure Azure Blob Storage integration (credentials, multipart, Azure-specific IO). label Sep 19, 2026
alexey-milovidov added a commit that referenced this pull request Sep 19, 2026
The `ignore_range` mode of the fake endpoint - answering every request with `200 OK` and the
blob from byte 0, the way a proxy that does not understand ranges does - and the
`HTTP_RANGE_NOT_SATISFIABLE` and `LOGICAL_ERROR` error codes are left over from
#115706, where the check of the range an endpoint
returns lived in the same change as the premature end. No test here uses them.

That check is #120955, and it brings its own
range-ignoring endpoint in `src/Disks/tests/gtest_azure_read_buffer_range_check.cpp`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@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.

Perfect.

@alexey-milovidov alexey-milovidov self-assigned this Sep 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 20, 2026
Merged via the queue into master with commit 0b02fc1 Sep 20, 2026
175 of 177 checks passed
@alexey-milovidov
alexey-milovidov deleted the azure-read-range-check branch September 20, 2026 23:38
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 20, 2026
alexey-milovidov added a commit that referenced this pull request Sep 25, 2026
…reopen range check

The AI review asked that a reopened download be rejected when it does not start at
the current offset, and that the data a response hands out be capped at
`read_until_position`. The first half is `checkReturnedRange` from
#120955, now on master and merged here;
`AzureBoundedRead.RangeIgnoringEndpointIsRejectedOnReopen` pins that it applies to
the reopen added by this PR. For the second half, `nextImpl` no longer reads past
the right bound when a response runs longer than requested (e.g. a whole-object
`200 OK` answering a ranged request at offset 0); before, the caller received the
extra bytes and the next call threw `Attempt to read beyond right offset`.
`AzureBoundedRead.LongerResponseIsCutAtTheRightBound` fails without the cap.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Oct 4, 2026
…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]>
alexey-milovidov added a commit that referenced this pull request Oct 4, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-azure Azure Blob Storage integration (credentials, multipart, Azure-specific IO). pr-bugfix Pull request with bugfix, not backported by default 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.

2 participants