Skip to content

Support bearer-token authentication in BuilderRWBufferFromHTTP - #110313

Closed
serxa wants to merge 20 commits into
masterfrom
http-builder-bearer-token
Closed

serxa wants to merge 20 commits into
masterfrom
http-builder-bearer-token

Conversation

@serxa

@serxa serxa commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

BuilderRWBufferFromHTTP had no way to authenticate with a bearer token: callers spliced an Authorization: Bearer entry into the header list themselves and had to remember not to pass HTTP Basic credentials at the same time, because both occupy the same header. create now takes the credential directly, in three overloads: Basic credentials, a bearer token, or both — with a non-empty token taking precedence, so the either/or rule lives in one place instead of in every caller. create also appends the bearer header to a local copy of the header list rather than mutating the builder, so a builder can be reused without carrying over a stale token.

Every bearer-token call site of the class is migrated to the new overloads:

  • HTTPBasedCatalogUtils and UnityCatalog: built the header by hand and passed never-used HTTP Basic credentials alongside; now call create with the token.
  • RestCatalog, OneLakeCatalog, BigLakeCatalog: getAuthHeaders now returns an AuthHeaders (a bearer token plus extra headers). The bearer goes to create; only genuine extra headers (a user-supplied auth_header, x-goog-user-project, User-Agent) stay in the header list. OneLakeCatalog's pre-obtained token also moved from a spliced header to the bearer.
  • PaimonRestCatalog: the bearer token provider (the dlf request-signing branch keeps its own signed headers).

Out of scope — different transport (plain Poco requests or S3-client headers), not BuilderRWBufferFromHTTP: OpenAIProvider, CloudJWTProvider, ArrowFlight AuthMiddleware, GCS diskSettings.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...


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

serxa and others added 2 commits July 13, 2026 19:00
Callers that authenticate with a bearer token currently splice an
`Authorization: Bearer` entry into the header list themselves and must
also remember not to pass HTTP Basic credentials, because both occupy the
same header. The builder now owns that rule: `withBearerToken` attaches
the header, and a non-empty token takes precedence over the Basic
credentials passed to `create`.

Co-Authored-By: Claude Fable 5 <[email protected]>
UnityCatalog spliced the bearer into the header list itself and passed
never-used HTTP Basic credentials alongside. The catalog helpers now take
the token and hand it to the builder, which owns the header formatting and
the bearer-over-Basic precedence.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [18a2816]

Summary: ✅


AI Review

Summary

This PR centralizes bearer-token handling in BuilderRWBufferFromHTTP and routes the Unity/Paimon/OneLake catalog callers through that path. The earlier stale-builder-state bug and the missing http_forbid_headers validation on the new bearer-token callers are fixed, but the shared validation is still incomplete for bare carriage returns, so malformed Authorization bytes can still reach the outbound request path.

Findings
❌ Blockers
  • [src/Databases/DataLake/HTTPBasedCatalogUtils.cpp:23-33, src/Common/HTTPHeaderFilter.cpp:18-25, src/IO/ReadWriteBufferFromHTTP.cpp:838-840, base/poco/Net/src/MessageHeader.cpp:59-65] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/110313#discussion_r3786089726] validateBearerToken now guards every Unity/Paimon bearer-token request, but it delegates to HTTPHeaderFilter::checkAndNormalizeHeaders, which still rejects only '\n'. A token like token\rX-Injected: evil therefore passes validation, is turned into Authorization: Bearer token\rX-Injected: evil, and Poco writes that value verbatim into the outgoing headers. The PR's new shared bearer-token path therefore still accepts malformed header material and can emit a syntactically broken or split Authorization header.
    Suggested fix: reject bare '\r' in HTTPHeaderFilter (or, less ideally, in both validateBearerToken and RestCatalog::validateAuthHeaders) and add a regression case beside ValidateBearerTokenRejectsMalformedHeader for token\rX-Injected: evil.
Final Verdict

Changes requested: the bearer-token validation introduced by this refactor is still incomplete for bare carriage returns.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.80% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.20% +0.00%

Changed lines: Changed C/C++ lines covered: 73/96 (76.04%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 13, 2026
Comment thread src/IO/ReadWriteBufferFromHTTP.cpp Resolved
The token has one entry point now — the terminal create call — so a
conflicting withBearerToken-plus-create state cannot be constructed.
Three overloads cover the auth modes: Basic credentials, a bearer token,
and both (the non-empty token wins; the two occupy the same
`Authorization` header, so a request carries one or the other).

Co-Authored-By: Claude Fable 5 <[email protected]>
@serxa serxa changed the title Add withBearerToken to BuilderRWBufferFromHTTP Support bearer-token authentication in BuilderRWBufferFromHTTP Jul 13, 2026
serxa and others added 7 commits July 14, 2026 16:40
`create` appended the bearer `Authorization` header to the builder's
`http_header_entries` member, so reusing a builder would carry over a
stale token. Append it to a local copy passed to `ReadWriteBufferFromHTTP`
instead, so `create` no longer mutates the builder.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
`RestCatalog` (with `OneLakeCatalog`, `BigLakeCatalog`) and
`PaimonRestCatalog` built the `Authorization: Bearer` header by hand and
passed unused Basic credentials to `create`. `getAuthHeaders` now returns
an `AuthHeaders` of a bearer token plus extra headers: the bearer goes to
`create` and only genuine extra headers (a user-supplied `auth_header`,
`x-goog-user-project`, `User-Agent`) stay in the header list.
`OneLakeCatalog`'s pre-obtained token likewise moves from a spliced header
to the bearer, so every catalog bearer now flows through `create`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
`.create({})` brace-initializes both one-argument overloads, making such
calls ambiguous. Two overloads remain: `create(credentials)` and
`create(bearer_token, credentials)` (the non-empty token wins). The catalog
helper passes empty credentials explicitly.

Co-Authored-By: Claude Fable 5 <[email protected]>
`.create({})` brace-initializes both one-argument overloads, making such calls
ambiguous. Two overloads remain: `create(credentials)` and
`create(bearer_token, credentials)` (the non-empty token wins). The catalog
call sites pass empty credentials explicitly.

Co-Authored-By: Claude Fable 5 <[email protected]>
A bearer-token overload of `create` would be ambiguous with
`create(credentials)` for `.create({})` call sites, and bearer-only callers
had to pass a throwaway empty credentials object because the buffer stores the
credentials by reference. A distinct `createWithBearerToken` removes the
ambiguity and needs no credentials argument; the single empty-credentials
object now lives once inside the builder instead of at every call site.

Co-Authored-By: Claude Fable 5 <[email protected]>
The previous commit renamed the declarations but left the out-of-line
definitions on the old `create` names; align them.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Databases/DataLake/RestCatalog.cpp Resolved
serxa and others added 2 commits July 14, 2026 18:07
`getAuthHeaders` returned an `AuthHeaders` of a bearer token plus extra
headers, so every `RestCatalog` call site had to unpack it. Match
`PaimonRestCatalog`'s existing shape instead: `getAuthHeaders` appends its
extra headers to a caller-owned list and returns the bearer token. The
struct is gone and the call sites just forward their header list and the
token to `createWithBearerToken`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…aders

`createWithBearerToken` appends the `Authorization: Bearer` header
internally without going through the `HTTPHeaderFilter` check, so a
user-supplied `onelake_bearer_token` that contains a forbidden or
malformed value could reach the request path unchecked.

Add the same validation that `RestCatalog` applies to a user-supplied
`auth_header` in its own constructor: call `validateAuthHeaders` with the
synthetic `Authorization: Bearer <token>` header before storing the
token. This restores the pre-refactor behavior where the value went
through the header list and was subject to `http_forbid_headers` / CR-LF
checks.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread src/IO/ReadWriteBufferFromHTTP.cpp Resolved
serxa and others added 2 commits July 16, 2026 15:29
Resolve the conflict in `RestCatalog.{h,cpp}`: master's `CatalogState`-based
auth refactor (atomic credential snapshots plus the ALTER framework
`prepareSettingsChanges`/`applySettingsChangesToState`, and `getAuthHeaders`
returning `HTTPHeaderEntries` from a state snapshot) superseded this branch's
`getAuthHeaders` simplification, so `RestCatalog.{h,cpp}` are taken from master.

The branch's remaining contribution is preserved: `createWithBearerToken` on
`BuilderRWBufferFromHTTP` and its adoption in `HTTPBasedCatalogUtils`,
`PaimonRestCatalog` and `UnityCatalog` (which master did not refactor).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Construct OneLakeCatalog with a bearer token containing a newline and
assert the constructor rejects it with BAD_ARGUMENTS via
validateAuthHeaders, before any request is issued. Locks the fix that
routes a pre-obtained bearer token through http_forbid_headers
validation instead of splicing it into the header list unchecked.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.10% +0.10%
Branches 78.30% 78.30% +0.00%

Changed lines: Changed C/C++ lines covered: 58/80 (72.50%) · Uncovered code

Full report · Diff report

@serxa
serxa marked this pull request as ready for review July 27, 2026 10:47
@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 34 queries analysed

This is a security/plumbing change confined to data-lake catalog HTTP bearer-token auth (Unity/Paimon/OneLake) and the generic HTTP read-buffer builder. None of that code is on the ClickBench or TPC-H query-execution path, which runs entirely on local tables. All 14 flagged deltas are improvements pointing in the same direction (~8% to ×8.6 faster), which is the signature of a measurement-environment difference between the source and base runs, not a code effect. All 14 are downgraded to not sure; no action needed on these numbers.

clickbench

⚠️ 12 inconclusive

Flagged queries (12 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 13 not_sure 426 336 -21.2% <0.0001 This PR only changes data-lake catalog HTTP bearer-token auth and the HTTP read-buffer builder; ClickBench Q13 runs on local tables and never touches that path, so the -21.2% is environment variance.
⚠️ 14 not_sure 362 323 -10.8% <0.0001 Q14 exercises none of the changed data-lake/HTTP auth code, so the -10.8% cannot be attributed to this PR and reads as run-to-run variance.
⚠️ 15 not_sure 193 160 -17.1% <0.0001 The changed code (catalog bearer-token validation, HTTP buffer builder) is off Q15's execution path, so -17.1% is not a PR effect.
⚠️ 16 not_sure 793 602 -24.1% <0.0001 Q16 does not run any data-lake catalog or HTTP-buffer code, so the -24.1% is unrelated to this PR and looks like environment variance.
⚠️ 17 not_sure 556 439 -21.0% <0.0001 This PR touches only HTTP catalog auth plumbing, which Q17 never executes; the -21.0% is off-path and reads as variance.
⚠️ 18 not_sure 1298 1046 -19.4% <0.0001 Q18 runs on local tables and exercises none of the changed code, so -19.4% cannot plausibly be this PR; the base measurements here were also noisy.
⚠️ 23 not_sure 167 72 ×2.3 faster <0.0001 Even at ×2.3 faster, Q23 never touches the data-lake catalog auth or HTTP buffer code this PR changes, so the speedup is off-path environment variance, not a PR effect.
⚠️ 27 not_sure 176 146 -17.0% <0.0001 Q27 does not exercise the changed HTTP auth path, so the -17.0% is unrelated to this PR.
⚠️ 30 not_sure 279 243 -12.9% <0.0001 The changed data-lake/HTTP code is off Q30's path, so -12.9% is run-to-run variance rather than a real change.
⚠️ 31 not_sure 395 312 -21.0% <0.0001 Q31 never runs the modified catalog auth or HTTP buffer code, so the -21.0% is off-path and not attributable to this PR.
⚠️ 33 not_sure 1254 1157 -7.8% <0.0001 Q33 exercises none of the changed code paths, so -7.8% reads as variance, not a PR effect.
⚠️ 34 not_sure 1256 1155 -8.0% <0.0001 Q34 runs on local tables unrelated to the data-lake/HTTP auth change, so -8.0% is off-path variance.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

⚠️ 2 inconclusive

Flagged queries (2 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 7 not_sure 574 67 ×8.6 faster <0.0001 join_operator: Q7 is a join query on local TPC-H tables; this PR only changes data-lake catalog HTTP auth, which Q7 never executes, so the ×8.6 faster reading is off-path environment variance.
⚠️ 8 not_sure 175 78 ×2.2 faster <0.0001 join_operator: Q8 does not touch the changed HTTP catalog code, so the ×2.2 faster result cannot be a PR effect and is treated as variance.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

Debug info
  • StressHouse run: c5305a36-6488-48b9-ae24-92b458dfb092
  • MIRAI run: 85b129ae-78cc-4f91-9311-47b7cc41a9dd
  • PR check IDs:
    • clickbench_1746157_1786972761
    • clickbench_1746163_1786972761
    • clickbench_1746180_1786972761
    • tpch_adapted_1_official_1746187_1786972761
    • tpch_adapted_1_official_1746200_1786972761
    • tpch_adapted_1_official_1746228_1786972761

Comment thread src/IO/ReadWriteBufferFromHTTP.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 18a281646 with master e654c4469 (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
Binary Master PR Δ
programs/clickhouse-stripped 701.98 MiB 698.95 MiB -3.02 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes

15 object files changed (-16.28 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/ContextTimeSeriesTagsCollector.cpp.o 441.16 KiB 421.89 KiB -19.27 KiB (-4.37%)

737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

createWithBearerToken synthesizes an `Authorization: Bearer <token>` header from a
raw token but did not validate it. Unity and Paimon route user-supplied bearer tokens
/ catalog_credential through this path, so a token with an embedded newline could
smuggle a second header into the request, or send an `Authorization` header that
`http_forbid_headers` forbids. OneLake already validated its auth header explicitly.

Add a shared `DataLake::validateBearerToken` that runs the same `HTTPHeaderFilter`
check on the synthesized header and call it on every bearer-token path.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
serxa and others added 2 commits August 14, 2026 19:57
`HTTPHeaderFilter::checkAndNormalizeHeaders` rejected an embedded `\n` but not a bare
`\r`. A lone CR also terminates a header line, so a value like "token\rX-Injected: evil"
could still smuggle a second header. Reject `\r` as well as `\n`, which closes the gap
for every caller (the bearer-token catalog paths and the OneLake auth-header path).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@vdimir vdimir self-assigned this Aug 17, 2026
@serxa

serxa commented Aug 19, 2026

Copy link
Copy Markdown
Member Author

Superseded by #115400 (attempt 2), which is in the merge queue.

@serxa serxa closed this Aug 19, 2026
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Aug 19, 2026
…pt 2)

Clone of ClickHouse#110313 on a fresh branch off current master — same changes, reconciled onto master. Opened because the original PR's sync pair got wedged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Aug 20, 2026
`HTTPHeaderFilter::checkAndNormalizeHeaders` rejected an embedded `\n` but not a
bare `\r`. A lone CR also terminates a header line, so a value such as
"token\rX-Injected: evil" could still smuggle a second header (request/response
splitting). Reject `\r` alongside `\n` for both header names and values, closing
the gap for every caller of the shared filter.

Deferred from ClickHouse#110313 to keep that
pull request focused on refactoring.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants