Skip to content

Return the explicit port of a URL with an IPv6 host from portRFC - #118632

Merged
alexey-milovidov merged 2 commits into
masterfrom
port-rfc-ipv6-host-118487
Sep 8, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
port-rfc-ipv6-host-118487

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Closes: #118487
Related: #118482

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):

portRFC now returns the explicit port of a URL whose host is an IPv6 address in brackets, such as http://[2001:db8::1]:8080/, instead of the default port.

Description

portRFC('http://[2001:db8::1]:8080/') returned 0, and with an explicit default it returned that default, so the answer was indistinguishable from a URL that has no port. domainRFC parses the very same authority correctly, so the RFC-conformant family disagreed with itself.

getURLHostRFC returns an IP-literal host without its brackets (RFC 3986, 3.2.2: IP-literal = "[" ( IPv6address / IPvFuture ) "]"), so the character right after the host is ], not the : of the port, and extractPort gave up there. It now skips the closing bracket.

port is left as it is: its host parser does not accept a bracketed host at all, so it has no port to return there.

The portRFC half of the IPv6 section of 01284_port asserted the old answers - it was added as a known limitation of getURLHost - and now expects the port.


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

`portRFC('http://[2001:db8::1]:8080/')` returned `0`, and with an explicit
default it returned that default, so the answer was indistinguishable from a URL
that has no port. `domainRFC` parses the very same authority correctly, so the
RFC-conformant family disagreed with itself.

`getURLHostRFC` returns an IP-literal host without its brackets (RFC 3986,
3.2.2: `IP-literal = "[" ( IPv6address / IPvFuture ) "]"`), so the character
right after the host is `]`, not the `:` of the port, and `extractPort` gave up
there. Skip the closing bracket.

`port` is left as it is: its host parser does not accept a bracketed host at
all, so it has no port to return there.

The `portRFC` half of the IPv6 section of `01284_port` asserted the old answers
- it was added as a known limitation of `getURLHost` - and now expects the port.

Closes: #118487

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

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [618a330]

Summary: ✅


AI Review

Summary

This PR fixes portRFC so it can read the explicit port that follows a bracketed IP-literal host, and it extends getURLHostRFC so the same path also works when the authority contains userinfo@[host]. I reviewed the current diff and prior discussion, traced the updated parser logic in getURLHostRFC and extractPort, and did not find any remaining correctness or compatibility issues in the changed behavior.

Final Verdict

✅ Approve. The previously reported userinfo@[IPv6] gap is fixed in the current code, and the added regression coverage matches the bug fix this PR claims.

LLVM Coverage Report

Measured on commit 618a330.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.20% 81.10% -0.10%

Changed lines: Changed C/C++ lines covered: 30/31 (96.77%) · 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 7, 2026
Comment thread src/Functions/URL/port.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Sep 7, 2026
`portRFC('http://user:password@[2001:db8::1]:8080/')` still returned the default
port, and `domainRFC` returned an empty host for it: `getURLHostRFC` entered
IP-literal mode only when the bracket was the first character of the authority,
while RFC 3986 allows a userinfo before it (3.2: `authority = [ userinfo "@" ]
host [ ":" port ]`).

The bracket is recognized after `@` now, and a complete IP-literal is returned
as the host whether or not a userinfo preceded it. Inside an IP-literal `@` is
not a userinfo separator at all - it is not allowed there - so it makes the host
invalid, which is what `02845_domain_rfc_support_ipv6` pins for
`[2001::db@81]:80`.

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

Copy link
Copy Markdown
Member Author

Addressed: getURLHostRFC now recognizes an IP-literal host after the userinfo, so portRFC('http://user:password@[2001:db8::1]:8080/') returns 8080 and domainRFC of the same URL returns the host. A complete IP-literal is returned as the host whether or not a userinfo preceded it.

Inside an IP-literal @ is not a userinfo separator - it is not allowed there - so it now makes the host invalid, which is what 02845_domain_rfc_support_ipv6 pins for [2001::db@81]:80; without that guard the reordered return started answering 81 for it. The new cases are in 05137_port_rfc_ipv6_host, and 00398_url_functions, 02845_domain_rfc_support_ipv6 and the rest of the URL suite pass.

@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 618a3303b with master 6bdba314e (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

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

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

Job report

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

Nice.

@alexey-milovidov alexey-milovidov self-assigned this Sep 8, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 8, 2026
Merged via the queue into master with commit eb23047 Sep 8, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the port-rfc-ipv6-host-118487 branch September 8, 2026 20:58
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 8, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

pull Bot pushed a commit to SINHASantos/ClickHouse that referenced this pull request Sep 17, 2026
ClickHouse#118632 taught getURLHostRFC to parse a bracketed IPv6 host, including
after a userinfo@ prefix, but several related bugs remain in the same
parser, on top of that fix:

- A bracketed host with an embedded IPv4 tail, e.g. "::ffff:192.168.0.1"
  or a NAT64 literal "64:ff9b::192.0.2.33", was rejected outright: any
  '.' while scanning inside brackets returned an empty host, even
  though this is a valid IPv6 address form and parseIPv6Whole already
  supports it. This is the bug reported in
  ClickHouse#118890.

- The bracket contents were never validated as an actual IPv6 address,
  so a non-IPv6 string like "2001db81" was silently accepted as the
  host. Validate it with parseIPv6Whole (already used elsewhere in the
  codebase, e.g. isIPAddressInRange.cpp).

- Nothing following the closing ']' was checked, so
  "[2001:db8::1]evil.com" returned "2001:db8::1" as the host. Only end
  of input, ':', '/', '?', or '#' may legally follow it.

- colon_pos, dot_pos, and has_terminator_after_colon carried over
  across '@', so a colon or dot in the userinfo (e.g. "user:password@")
  could validate a malformed host afterward using state that actually
  describes the userinfo, not the host - reset all three at '@'.

- userinfo may contain more than one ':' (RFC 3986), but the second
  one was read as the host:port separator before the parser ever
  reached '@', rejecting valid authorities like
  "user:pass:word@[2001:db8::1]:80".

- A second raw '@' was accepted as if it were another userinfo
  separator, returning the segment in between as the host:
  "[email protected]@evil.com" returned "paypal.com" instead of being
  rejected - userinfo cannot legally contain an unescaped '@' at all,
  so a second one makes the whole authority unparseable.

- A host starting with a literal '.', e.g. ".com", was accepted by
  checkAndReturnHost (shared by domain() and domainRFC()), since it
  only checked for a dot with nothing after it, not nothing before it
  either.

Closes: ClickHouse#118890
Related: ClickHouse#118632
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... 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.

portRFC returns the default port instead of the explicit port for URLs with a bracketed IPv6 host

3 participants