Skip to content

Add url_base setting for resolving relative URLs in url table function - #101113

Merged
alexey-milovidov merged 48 commits into
masterfrom
url_base_setting
May 5, 2026
Merged

alexey-milovidov merged 48 commits into
masterfrom
url_base_setting

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 29, 2026 •

Copy link
Copy Markdown
Member

Add url_base setting for resolving relative URLs in the url table function and URL table engine. Implements item 2 from #59617.

When url_base is set, relative URLs are resolved following RFC 3986 semantics (same as the HTML <base> tag):

  • Path-relative (data.csv with base https://example.com/def/): https://example.com/def/data.csv
  • Host-relative (/test/data.csv with base https://example.com/def/): https://example.com/test/data.csv
  • Scheme-relative (//other.com/data.csv with base https://example.com/def/): https://other.com/data.csv
  • Absolute URLs are passed through unchanged

Resolution is done via string manipulation (no Poco::URI construction) to allow malformed URLs. Query strings and fragment identifiers in the base URL are handled correctly.

Changelog category (leave one):

  • New Feature

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

Add url_base setting to resolve relative URLs in the url table function and URL table engine, following RFC 3986 semantics.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

New setting url_base (String, default empty). When set, relative URLs passed to the url table function or URL engine are resolved against this base URL:

  • SET url_base = 'https://example.com/data/' then SELECT * FROM url('file.csv') reads from https://example.com/data/file.csv
  • Supports path-relative, host-relative, and scheme-relative URLs per RFC 3986

Version info

  • Merged into: 26.5.1.313

…ction and `URL` engine

Implements item 2 from #59617

The new `url_base` setting allows using relative URLs in the `url` table
function and `URL` table engine. Resolution follows RFC 3986 semantics
(same as HTML `<base>` tag) via string manipulation (no Poco::URI
construction, to allow malformed URLs):

- Path-relative (`data.csv`): merged with the base path
- Host-relative (`/path`): uses scheme and authority from base
- Scheme-relative (`//host/path`): uses scheme from base
- Absolute URLs are passed through unchanged

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [44c8d09]

Summary: ✅


AI Review

Summary

This PR introduces the url_base setting and wires it into both the url table function and the URL table engine, including RFC 3986-style relative resolution, persistence behavior for engine arguments, and broad stateless coverage. I did not find new correctness, safety, or performance blockers in the final diff, but there is still a PR metadata inconsistency that should be fixed before merge.

Findings

💡 Nits

  • [PR description] The PR template still has Documentation is written (mandatory for new features) unchecked, but this PR now contains user documentation updates in docs/en/sql-reference/table-functions/url.md and docs/en/engines/table-engines/special/url.md.
    Suggested fix: Mark the documentation checkbox as checked in the PR body.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ✅
PR metadata quality ⚠️ Documentation checkbox is inconsistent with the actual diff
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ⚠️ Request changes

Minimum required action:

  1. Update the PR template metadata and check Documentation is written (mandatory for new features).

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Mar 29, 2026
Comment thread src/Storages/StorageURL.cpp Resolved
When `url_base` is authority-only (e.g. `https://example.com`), `rfind('/')`
was matching the slash in `https://`, producing `https://data.csv` instead of
`https://example.com/data.csv`. Now we check if there is a `/` after the
authority first, and if not, append `"/" + url`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Storages/StorageURL.cpp Resolved
return base_before_query.substr(0, authority_end) + url;
}

/// Path-relative URL: merge with the base path per RFC 3986.

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.

RFC 3986 handling is also incorrect for query-only / fragment-only relative references.

Concrete examples with base = "https://example.com/dir/file.csv":

  • url = "?x=1" should resolve to https://example.com/dir/file.csv?x=1, but current logic returns https://example.com/dir/?x=1
  • url = "#frag" should resolve to https://example.com/dir/file.csv#frag, but current logic returns https://example.com/dir/#frag

These are valid relative references and are used in signed-URL workflows where preserving the full base path is important.

Please add dedicated handling for leading ? and # before the path-merge branch, plus tests for both cases.

alexey-milovidov and others added 2 commits March 30, 2026 02:06
Address review feedback: when the URL starts with `?` or `#`, preserve
the full base path instead of stripping the last path component.

Also fix CI test timeout by replacing `example.com` with `.invalid` TLD
domains (RFC 2606) to ensure immediate DNS failure without connection
timeouts.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
return base_before_query + url;

/// Fragment-only relative reference: #frag → append to full base path (before any existing query/fragment).
if (url.starts_with("#"))

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.

resolveURLBase incorrectly handles fragment-only references when url_base already has a query string.

Concrete trace:

  • base = "https://example.com/dir/file.csv?token=abc"
  • url = "#frag"
  • query_or_fragment = base.find_first_of("?#", authority_start) points to ?
  • base_before_query = "https://example.com/dir/file.csv"
  • returned value is https://example.com/dir/file.csv#frag

Expected (RFC 3986) result is https://example.com/dir/file.csv?token=abc#frag (query must be preserved; only fragment is replaced).

Please keep the base query for #... references, e.g. strip only existing fragment for that branch.

alexey-milovidov and others added 2 commits March 30, 2026 03:39
…olution

- Rename test from 04068 to 04070 to avoid number collision with
  `04068_constant_fold_union_intersect`
- Add `http_connection_timeout = 1, http_max_tries = 1` settings to
  all test queries to avoid slow DNS resolution retries for `.invalid`
  domains (test was taking 132s)
- Fix `resolveURLBase` to preserve the base query string for
  fragment-only relative references (`#frag`) per RFC 3986
- Add test case for fragment-only reference with a base that has
  a query string

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Storages/StorageURL.cpp Outdated
return url;

/// If the URL already contains a scheme, return as-is.
if (url.find("://") != String::npos)

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.

resolveURLBase misclassifies some relative URLs as absolute because it checks for "://" anywhere in the string.

Concrete trace with:

  • base = "https://base.invalid/dir/"
  • url = "data.csv?next=https://other.invalid/a"

Current flow:

  • url.find("://") != npos is true (because of query value https://...)
  • function returns url unchanged
  • result becomes data.csv?next=https://other.invalid/a (still relative)

Expected result is https://base.invalid/dir/data.csv?next=https://other.invalid/a.

This breaks valid relative references that include embedded absolute URLs in query/fragment parameters (common in signed redirects).

Please detect an absolute URL only when the scheme is at the beginning (or use proper RFC URI parsing for this check), instead of searching for "://" anywhere.

…efore path/query/fragment

The previous check `url.find("://")` searched for `://` anywhere in the URL string,
which incorrectly classified relative URLs with embedded absolute URLs in query
parameters (e.g. `data.csv?next=https://other/a`) as absolute.

Now only considers `://` as a scheme indicator if it appears before the first
`/`, `?`, or `#` character, matching RFC 3986 semantics.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
$CLICKHOUSE_CLIENT --query "SELECT * FROM url('/other/file.csv', CSV, 'c String') SETTINGS url_base = 'https://base.invalid/def/?redirect=http://x.invalid/', $FAST" 2>&1 | grep -oF 'https://base.invalid/other/file.csv'

# Path-relative URL with authority-only base (no path): https://base.invalid + data.csv → https://base.invalid/data.csv
$CLICKHOUSE_CLIENT --query "SELECT * FROM url('data.csv', CSV, 'c String') SETTINGS url_base = 'https://base.invalid', $FAST" 2>&1 | grep -oF 'https://base.invalid/data.csv'

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.

The current test set does not cover path-relative references with dot-segments (./ / ../). Since the PR description promises RFC 3986-like behavior, please add explicit cases like:

  • base https://base.invalid/dir/ + ../a.csv → https://base.invalid/a.csv
  • base https://base.invalid/dir/ + ./a.csv → https://base.invalid/dir/a.csv

Without these cases, regressions in remove_dot_segments behavior are easy to miss.

alexey-milovidov and others added 2 commits March 30, 2026 07:44
…e` setting

Dot-segments are intentionally passed through without normalization,
matching the behavior documented in the review discussion.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
# Path-relative URL: data.csv with base https://base.invalid/def/ → https://base.invalid/def/data.csv
$CLICKHOUSE_CLIENT --query "SELECT * FROM url('data.csv', CSV, 'c String') SETTINGS url_base = 'https://base.invalid/def/', $FAST" 2>&1 | grep -oF 'https://base.invalid/def/data.csv'

# Host-relative URL: /test/data.csv with base https://base.invalid/def/ → https://base.invalid/test/data.csv

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.

This test covers url(...) resolution only, but the PR also changes StorageURL::getConfiguration for the URL table engine. Please add at least one engine-level case (e.g. CREATE TABLE ... ENGINE=URL('data.csv', ...) SETTINGS url_base='https://base.invalid/dir/') and assert that the resolved request URL is https://base.invalid/dir/data.csv.

Without an engine-path assertion, a regression in URL engine wiring could pass this suite.

Comment thread src/Core/Settings.cpp Outdated
The base URL used to resolve relative URLs in the [url](../../sql-reference/table-functions/url.md) table function and the [URL](../../engines/table-engines/special/url.md) table engine.

When set, relative URLs are resolved as follows:
- Path-relative URL (e.g. `data.csv`): appended to the base URL path.

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.

The docs for url_base currently say path-relative URLs are "appended to the base URL path", but this is not always true.

For a base without trailing slash (e.g. https://example.com/def), data.csv resolves to https://example.com/data.csv (last path segment replacement per RFC 3986 merge), not https://example.com/def/data.csv.

Please reword this bullet to describe path merge semantics, including the trailing-slash distinction, to avoid misleading users.

Comment thread src/Storages/StorageURL.cpp Outdated
/// Only check the prefix before any '/', '?', or '#' to avoid false positives
/// from embedded URLs in query parameters (e.g. "data.csv?next=https://other/a").
auto first_special = url.find_first_of("/?#");
auto scheme_in_url = url.find("://");

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.

resolveURLBase still misses absolute-URL schemes that do not use //, so they get incorrectly merged with url_base.

Concrete trace:

  • base = "https://base.invalid/dir/"
  • url = "http:data.csv" (valid absolute URI reference: scheme ":" path-rootless)
  • first_special = npos, scheme_in_url = npos (because check is only "://")
  • code falls through to path-relative merge and returns https://base.invalid/dir/http:data.csv

This silently rewrites an absolute reference into a relative one. Since the PR explicitly supports malformed URLs via string manipulation, the absolute check should detect a leading RFC scheme (^[A-Za-z][A-Za-z0-9+.-]*: before / ? #) rather than only "://".

alexey-milovidov and others added 2 commits April 9, 2026 20:29
The test relied on the resolved URL appearing in the error output from
failed HTTP requests, but `ReadWriteBufferFromHTTP` only includes the URL
in a DEBUG-level log message ("Failed to make request to '<url>'"), not in
the rethrown exception. With the default `--send_logs_level=warning`, these
debug logs are not sent to the client, so `grep` could not find the
resolved URLs in the output.

Fix by using `--send_logs_level=debug` and a helper function that extracts
the first match via `head -1` to get exactly one output line per test case.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Add a URL engine test case to verify `url_base` resolution works for
  `ENGINE = URL(...)` tables (not just the `url` table function).
- Clarify `url_base` docs: explain path merge semantics including trailing
  slash distinction, document query-only and fragment-only references, and
  note that dot segments are preserved without normalization.
- Improve absolute URL detection in `resolveURLBase` to recognize any RFC
  3986 scheme (`[A-Za-z][A-Za-z0-9+.-]*:`) instead of only `://`. This
  correctly handles scheme-only absolute URIs like `http:path`.

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

/// Resolve relative URLs against the url_base setting.
const auto & url_base = context->getSettingsRef()[Setting::url_base].value;
filename = StorageURL::resolveURLBase(filename, url_base);

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.

TableFunctionURL::parseArgumentsImpl still infers format from filename before applying url_base in the named-collection branch, so format = auto can be derived from the wrong path.

Concrete case:

  • named collection: url='?x=1', format='auto'
  • setting: url_base='https://base.invalid/dir/file.csv'
  • expected resolved URL: https://base.invalid/dir/file.csv?x=1 (CSV path)

Current flow computes format from unresolved filename='?x=1' (empty path), then resolves URL later at this line. This can miss extension-based format detection and choose the wrong parser.

Please resolve filename against url_base before the format == "auto" inference (or recompute format from the resolved URL in that branch).

Move auto-format derivation from filename to after `url_base` resolution,
so that relative references like `?x=1` can inherit the format from the
resolved URL path.
return base_without_fragment + url;
}

/// Path-relative URL: merge with the base path per RFC 3986.

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.

resolveURLBase misses the RFC 3986 R.empty case (empty relative reference), so url = '' is resolved incorrectly.

Concrete trace:

  • base = "https://base.invalid/dir/file.csv?token=abc#frag"
  • url = ""
  • code skips "?" / "#" branches and enters path-relative merge
  • result: https://base.invalid/dir/

Expected result for empty reference is the base URI without fragment: https://base.invalid/dir/file.csv?token=abc.

This can silently target a different resource when URL is passed from a templated/empty setting. Please add an explicit if (url.empty()) branch that returns base without fragment (preserving path and query), and add a test for this case.

Comment thread src/Storages/StorageURL.cpp Outdated

/// Scheme-relative URL: //host/path → prepend scheme from base.
if (url.starts_with("//"))
return base.substr(0, scheme_end + 1) + url;

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.

resolveURLBase still skips RFC 3986 dot-segment normalization for the //host/path branch.

Concrete trace:

  • base = "http://base.invalid/dir/file.csv"
  • url = "//other.invalid/a/../b.csv"
  • current code returns http://other.invalid/a/../b.csv

Per RFC 3986 (R.authority branch), remove_dot_segments must still be applied to R.path, so the expected result is http://other.invalid/b.csv.

Please normalize this branch too (e.g. build http: + url and run normalizeDotSegmentsInURL before returning).

For a `url_base` like `http://base.invalid/dir/file.csv` and a
scheme-relative reference like `//other.invalid/a/../b.csv`, the
result must be `http://other.invalid/b.csv` per RFC 3986 (the
`R.authority` branch still applies `remove_dot_segments` to `R.path`).

The existing implementation in `resolveURLBase` already normalized
dot segments for host-relative and path-relative branches but skipped
the scheme-relative branch. Apply the same `normalizeDotSegmentsInURL`
helper there and add a regression test.

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

ASTs key_value_args = {make_intrusive<ASTIdentifier>("url"), make_intrusive<ASTLiteral>(uri)};
args.push_back(makeASTOperator("equals", std::move(key_value_args)));

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.

Materializing the resolved URL into engine args for the named-collection form can leak secrets from the named collection into table metadata.

Concrete trace:

  1. CREATE NAMED COLLECTION nc AS url='http://user:[email protected]/data.csv', format='CSV'.
  2. CREATE TABLE t (c String) ENGINE = URL(nc).
  3. This branch appends url = '<resolved>' as a literal to args, so SHOW CREATE TABLE t now contains user:pass@....

Before this change, URL(nc) kept credentials encapsulated in the named collection. Exposing them via table metadata is a security regression.

Please avoid persisting the resolved URL literal for named-collection arguments (or redact credentials before persisting), and use a runtime resolution strategy for restart safety instead.

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.

There is still one credential-leak path here even with the current guard: credentials can come from url_base rather than from the named collection itself.

Concrete trace:

  1. SET url_base = 'http://user:[email protected]/dir/';
  2. CREATE NAMED COLLECTION nc AS url='persist.csv', format='CSV';
  3. CREATE TABLE t (c String) ENGINE = URL(nc);
  4. This code appends url = 'http://user:[email protected]/dir/persist.csv' to engine args.
  5. SHOW CREATE TABLE t now exposes user:pass@....

So we still materialize secrets into metadata for relative named-collection URLs when url_base contains them. Could we skip materialization when resolved uri contains userinfo / obvious query secrets and keep runtime resolution for that case?

alexey-milovidov and others added 5 commits May 2, 2026 09:40
In `addInferredEngineArgsToCreateQuery`, the named-collection branch
unconditionally injected `url='<resolved>'` into the engine args, so any
named collection whose `url` was already absolute (e.g. one containing
`user:pass@host`) would leak its URL contents into table metadata
visible via `SHOW CREATE TABLE`.

Mirror the positional-form behavior: only materialize the resolved URL
when `url_base` actually changed it. The check is done by re-resolving
the named collection and comparing its `url` to `uri`.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Comment thread src/Storages/StorageURL.cpp Outdated
configuration.url = resolveURLBase(configuration.url, url_base);

if (configuration.format == "auto")
configuration.format = FormatFactory::instance().tryGetFormatFromFileName(Poco::URI(configuration.url).getPath()).value_or("auto");

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.

resolveURLBase intentionally works via string manipulation to tolerate malformed inputs, but this Poco::URI(configuration.url) parse in the format="auto" path can still throw for resolved URLs that contain invalid URI characters (for example, a relative input like 'file name.csv' under url_base). That turns URL resolution into an unexpected exception path.

Could we guard this inference with try/catch (or use a non-throwing path extraction) and fall back to "auto" when URI parsing fails? The same pattern exists in TableFunctionURL::parseArgumentsImpl.

`resolveURLBase` intentionally works via string manipulation to tolerate
malformed inputs, so the resolved URL may contain characters that
`Poco::URI` rejects (for example, a space in a relative reference like
`'file name.csv'` under `url_base`). The format-auto path called
`Poco::URI(url).getPath()` directly, turning any URI parse failure into
an unexpected exception.

Wrap the call with `try`/`catch` and fall back to format `"auto"` when
parsing fails, both in `StorageURL::getConfiguration` and
`TableFunctionURL::parseArgumentsImpl`.
The empty catch is intentional: when `Poco::URI` cannot parse the
resolved URL (because `resolveURLBase` permits malformed input), we
deliberately fall back to keeping the format as `auto`. Add explicit
`NOLINT(bugprone-empty-catch)` on both call sites to fix the `arm_tidy`
build.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/PRs/101113/9023796618046f4f144cfb23c7bd3190f2aadf3b/build_arm_tidy/build_clickhouse/build_clickhouse.log
Even with the existing guard against copying named-collection fields into
the CREATE TABLE AST, credentials introduced via `url_base` (for example
`SET url_base = 'http://user:pass@base/dir/'`) would be materialized into
the engine args by `addInferredEngineArgsToCreateQuery`, exposing them via
`SHOW CREATE TABLE`.

Skip materialization whenever the resolved URL contains a userinfo
component. Persistence in this case relies on `url_base` being set with
the same value at attach/restart time.

Also add tests covering both the named-collection and positional engine-arg
forms.
@clickhouse-gh

clickhouse-gh Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.00% -0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 90.53% (153/169) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 5, 2026
Merged via the queue into master with commit 3828d2d May 5, 2026
325 of 326 checks passed
@alexey-milovidov
alexey-milovidov deleted the url_base_setting branch May 5, 2026 21:27
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 5, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request May 7, 2026
`LockedTopicPartitionInfo::lock` (held in `permanent_locks` and `tmp_locks`)
is a `zkutil::EphemeralNodeHolder`, which stores a **raw** `ZooKeeper &`
reference captured at construction. Its destructor calls
`zookeeper.expired()` and possibly `zookeeper.tryRemove()`.

`KeeperHandlingConsumer::setKeeper()` assigned the new `keeper` shared_ptr
*before* clearing the lock maps. If the consumer was the last shared owner
of the previous `ZooKeeper` session, that assignment would synchronously
destroy the old session, and the subsequent `permanent_locks.clear()` /
`tmp_locks.clear()` would then access a dangling reference -> SIGSEGV.

This is the same hazard already documented and handled in
`StorageKafka2::partialShutdown()` for `replica_is_active_node`
(see the comment at `StorageKafka2.cpp` lines 221-225). This commit
mirrors that ordering for the per-consumer lock holders.

Caught by `Stress test (arm_release)` on PR ClickHouse#101113 (which is
unrelated to Kafka/Keeper -- the crash surfaced under stress-test
ZK fault injection):

  zkutil::EphemeralNodeHolder::~EphemeralNodeHolder() @ 0x15aa9699
  DB::KeeperHandlingConsumer::setKeeper(...)            @ 0x15a9985c
  DB::StorageKafka2::streamToViews(unsigned long)       @ 0x15b0cf1c

Report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101113&sha=4cf4eb894643c2dac6b52dea6126f1a3f2e75cd2&name_0=PR&name_1=Stress%20test%20%28arm_release%29

Related issue: ClickHouse#84616 (sibling crash in StorageKafka2 on Keeper session loss).

STID: 3555-2986
Signed-off-by: Groene AI <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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