Repository navigation
Commit 8705d89
authored
tls: normalize the hostname for server identity checks (#42977)
### Problem
- `tls.checkServerIdentity("foo。bar.example.com", cert)` accepts a
certificate for `*.example.com`. IDNA maps U+3002, U+FF0E and U+FF61 to
".", so a resolver looks up `foo.bar.example.com`, two labels deep. Node
fixed this as CVE-2026-48618.
- The cause: a label split on ASCII "." only, in `splitHost`
(`src/js/node/tls.ts:352`) and the native `match_hostname`
(`src/boringssl/lib.rs:302`). Canary accepts it in `tls.connect`,
`https.request`, `fetch` (`tls.serverName`) and `Bun.connect`.
- Only a raw `host` or `servername` string reaches it. A URL host is
already ASCII.
### Fix
- Port nodejs/node 1efb4ff51a and its follow-up 1d87a24050:
`checkServerIdentity` matches DNS names on
`url.domainToASCII(hostname)`. IP literals and the error text keep the
hostname as typed, because `domainToASCII("::1")` is `""`.
`domainToASCII` moves to `internal/url`, as in Node.
- `check_x509_server_identity` runs the same conversion when the host
has a non-ASCII byte. A host that does not convert matches nothing.
ASCII hosts are unchanged.
- One deliberate difference from Node: `*` does not match an empty
label. The conversion turns `。example.com` into `.example.com`, which
Node accepts for `*.example.com`.
- Verified: `test/js/web/fetch/fetch.tls.wildcard.test.ts` (23 of 100
fail on canary) and the vendored `test-tls-check-server-identity.js`,
synced to Node v26.8.2. Self-reviewed: 3 concerns raised, 1 addressed, 2
kept with the reason in Notes.
### Background
- `checkServerIdentity` compares the host with each DNS name in the
certificate, label by label. A left-most `*` stands for exactly one
label.
- IDNA (UTS #46) maps a Unicode domain name to ASCII.
`url.domainToASCII` applies it.
- Bun has two matchers with Node's rules: the JS one in `node:tls`, and
a Rust port that every native TLS client calls.
<details><summary>Notes</summary>
**Repro**
```js
const tls = require("tls");
const err = tls.checkServerIdentity("foo\u3002bar.example.com", { subjectaltname: "DNS:*.example.com", subject: {} });
console.log(err ? "REJECTED: " + err.reason : "ACCEPTED");
```
| runtime | output |
| --- | --- |
| Node v26.3.0 | `ACCEPTED` |
| Bun canary 1.4.3 (09bb546, c6b7fcb) | `ACCEPTED` |
| Node v26.6.0, v26.8.2 | `REJECTED: Host: foo。bar.example.com. is not
in the cert's altnames: DNS:*.example.com` |
| this branch | same as Node v26.8.2 |
The CVE fix shipped in Node v26.5.0. That release stopped matching IPv6
hosts against IP SANs (nodejs/node#64144). v26.6.0 fixed it with
1d87a24050. This PR ports both, so `::1` keeps matching `IP
Address:0:0:0:0:0:0:0:1`.
**End to end, server certificate `DNS:*.example.com`, client dials
127.0.0.1**
| entry point | `foo。bar.example.com` on canary | on this branch |
| --- | --- | --- |
| `tls.connect({ servername })` | authorized |
`ERR_TLS_CERT_ALTNAME_INVALID` |
| `https.request({ servername })` | 200 | `ERR_TLS_CERT_ALTNAME_INVALID`
|
| `fetch(url, { tls: { serverName } })` | 200 |
`ERR_TLS_CERT_ALTNAME_INVALID` |
| `Bun.connect({ tls: { serverName } })` | authorized |
`ERR_TLS_CERT_ALTNAME_INVALID` |
U+FF0E and U+FF61 give the same results. `bar.example.com` and
`bücher.example.com` stay accepted on all four.
**Comparison with Node on a 28-row table** (full stops in SAN and CN
paths, trailing ideographic full stop, U-labels, IPv4 and IPv6 hosts,
hosts that `domainToASCII` rejects). This branch gives the same verdict,
`reason` and `host` as Node v26.6.0 on all 28 rows, and as Node v26.8.2
on 27. The other row is `xn--a.example.com` against
`DNS:xn--a.example.com`: Node v26.7.0 moved to ada 4.0.0, where
`domainToASCII` no longer rejects an invalid punycode label. Bun's
`url.domainToASCII` still returns `""` for it, as ada 3.4.4 does, so the
host matches nothing. That difference belongs to `node:url`.
**Empty first label**
`domainToASCII("。example.com")` is `.example.com`. Node's `check()` lets
`*` match the empty first label, so Node accepts that host for
`*.example.com`:
| host against `DNS:*.example.com` | Node v26.3.0 | Node v26.8.2 | Bun
canary | this branch |
| --- | --- | --- | --- | --- |
| `.example.com` | accepted | accepted | accepted | rejected |
| `。example.com` | rejected | accepted | rejected | rejected |
A plain port would make Bun newly accept `。example.com`. A name with an
empty label is not a name below `*.example.com` (OpenSSL's
`wildcard_match` also needs one character), and glibc does not resolve
`.localhost`. So both matchers reject an empty first label. This is the
one place where `check()` differs from Node, and the tests mark the
rows.
**Reach**
- On Linux, Bun's resolver returns `ENOTFOUND` for a host with one of
these full stops, so a raw `host` does not dial. The check is reached
through `servername`, a custom `lookup`, a proxy agent that connects
first, or a direct call to `tls.checkServerIdentity`.
- On Windows, the resolver already maps the full stop: on canary
a8e4e90, `dns.lookup("127。0。0。1")` returns `127.0.0.1` and
`net.connect({ host: "127。0。0。1", port })` connects to 127.0.0.1. There
a raw `host` is enough.
- #35590 adds the IDNA mapping to `dns.lookup` on every platform.
**Native matcher**
- Every native caller goes through `check_x509_server_identity`: fetch
and its HTTPS proxy tunnel, `Bun.connect`, WebSocket, Postgres, MySQL,
Valkey. The conversion is in that one function.
- `Bun__domainToASCII` wraps the same `parseDomainAsHost` that backs
`url.domainToASCII`. It can run off the JS thread. The HTTP thread
already calls `WTF::URL` for redirects (`bun_url::href_from_string`).
- The conversion only runs for a host with a non-ASCII byte.
`tls.checkServerIdentity` converts every host, as Node does. So the two
matchers differ on an ASCII host that is not a valid URL host. Example:
`a b.example.com` against `*.example.com` is rejected by
`tls.checkServerIdentity` and still accepted by the native matcher. No
resolver maps an ASCII name, so the label split is correct there. The
conversion also rejects ASCII hosts that work today, for example `db.1`
(a numeric last label must parse as IPv4), so the native ASCII path
stays as it is.
- A host is an IP address only as typed, as in Node. `127.0.0.1`
converts to `127.0.0.1` and is still matched as a DNS name. One test
pins this for both matchers.
- The native mismatch reason printed the host through the escaping that
is meant for certificate names. `Bun.connect` showed `Host:
"foo\u00e3\^@\^Bbar.example.com".` for a UTF-8 host. It now prints the
host as typed, as Node does. The `Bun.connect` tests assert the full
message.
- `X509Certificate#checkHost`, the HTTP/3 client and `node:quic` use
OpenSSL rules (`X509_check_host`), where `*` only spans letters, digits
and "-". They already reject these hosts. The test table records
`checkHost` as `undefined` for each new row.
**Tests**
- `fetch.tls.wildcard.test.ts`: `tls.connect` and `Bun.connect` end to
end for the three code points, new rows in the `checkServerIdentity ==
fetch` table, and a table for `tls.checkServerIdentity` (SAN, CN
fallback, error text, `err.host`, trailing U+3002, U-label against an
A-label SAN, IPv6 host, empty first label). Canary a8e4e90: 77 pass,
23 fail. With the fix: 100 pass.
- `test/js/node/test/parallel/test-tls-check-server-identity.js` is
byte-identical to Node v26.8.2. It fails on canary at the U+3002 row.
- Also ran with the debug build: `test/js/node/tls/`,
`test/js/node/url/`, `fetch.tls.test.ts`, `fetch.tls.ipv6.test.ts`,
`node-https-checkServerIdentity.test.ts`, `valkey-tls-verify.test.ts`,
`test/js/bun/net/socket.test.ts`, `test/internal/source-lints/`, and the
262 vendored `test-tls-*`, `test-https-*`, `test-x509-*` files.
</details>
<!-- robobun:evidence:begin -->
---
**[human-review]** gate passed · iteration 0 · 7 files touched
<details><summary>fails on main (without fix)</summary>
```console
ASAN without fix: 23 FAILED
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/pr_gate.xml" test/js/web/fetch/fetch.tls.wildcard.test.ts
bun test v1.4.3 (c6b7fcb)
test/js/web/fetch/fetch.tls.wildcard.test.ts:
(pass) TLS wildcard hostname verification > should reject multi-label wildcard match (sub.foo.example.com vs *.example.com) [645.61ms]
(pass) TLS wildcard hostname verification > should accept valid single-label wildcard match (foo.example.com vs *.example.com) [392.23ms]
(pass) TLS wildcard hostname verification > should reject bare domain for wildcard cert (example.com vs *.example.com) [386.83ms]
(pass) TLS wildcard hostname verification > should accept exact match for wildcard labels (bar.example.com vs *.example.com) [404.48ms]
(pass) TLS wildcard hostname verification > should reject deeply nested subdomain (a.b.c.example.com vs *.example.com) [393.09ms]
327 | servername,
328 | });
329 | socket.on("secureConnect", () => resolve({ authorized: socket.authorized }));
330 | socket.on("error", resolve);
331 | try {
332 | expect(await promise).toMatchObject({
... (truncated)
release without fix: 7 FAILED
bun test v1.4.3-canary.1 (dd5e81d)
test/js/web/fetch/fetch.tls.wildcard.test.ts:
(pass) TLS certificate name matching: fetch() / checkServerIdentity / checkHost agree > checkServerIdentity == fetch > "a.b.wild.test" [7.83ms]
(pass) TLS certificate name matching: fetch() / checkServerIdentity / checkHost agree > checkServerIdentity == fetch > "wild.test" [7.43ms]
(pass) TLS certificate name matching: fetch() / checkServerIdentity / checkHost agree > checkServerIdentity == fetch > "foo.wild.test" [12.93ms]
(pass) TLS certificate name matching: fetch() / checkServerIdentity / checkHost agree > checkServerIdentity == fetch > "FOO.WILD.TEST" [12.26ms]
(pass) TLS wildcard hostname verification > should reject multi-label wildcard match (sub.foo.example.com vs *.example.com) [37.15ms]
(pass) TLS wildcard hostname verification > should accept valid single-label wildcard match (foo.example.com vs *.example.com) [29.23ms]
(pass) TLS wildcard hostname verification > should reject bare domain for wildcard cert (example.com vs *.example.com) [28.23ms]
(pass) TLS certificate name matching: fetch() / checkServerIdentity / checkHost agree > checkServerIdentity == fetch > "foo.wi
... (truncated)
```
</details>
<details><summary>passes on PR (with fix)</summary>
```console
ASAN with fix: all passed
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/pr_gate.xml" test/js/web/fetch/fetch.tls.wildcard.test.ts
bun test v1.4.3 (c6b7fcb)
test/js/web/fetch/fetch.tls.wildcard.test.ts:
(pass) TLS wildcard hostname verification > should reject multi-label wildcard match (sub.foo.example.com vs *.example.com) [643.60ms]
(pass) TLS wildcard hostname verification > should accept valid single-label wildcard match (foo.example.com vs *.example.com) [380.95ms]
(pass) TLS wildcard hostname verification > should reject bare domain for wildcard cert (example.com vs *.example.com) [373.22ms]
(pass) TLS wildcard hostname verification > should accept exact match for wildcard labels (bar.example.com vs *.example.com) [391.15ms]
(pass) TLS wildcard hostname verification > should reject deeply nested subdomain (a.b.c.example.com vs *.example.com) [381.85ms]
(pass) TLS wildcard hostname verification > should accept case-insensitive wildcard match (FOO.EXAMPLE.COM vs *.example.com) [257.09ms]
(pass) TLS wildcard hostname verification > should accept mixed-case wildcard match (FoO.ExAmPlE.cOm vs *.example.com) [246.58m
... (truncated)
release with fix: all passed
$ bun scripts/build.ts --profile=release
[configured] bun-profile → bun (stripped) in 576ms (unchanged)
ninja: Entering directory `/workspace/bun/build/release'
[1/126] gen cpp.rs (cppbind)
[2/126] gen JS modules (bundle-modules)
Preprocess modules (6548ms)
Bundle modules (44ms)
Postprocesss modules (19ms)
Bundle Functions (402ms)
Generate Code (23ms)
[7.04s] Bundled "src/js" for production
2600 kb
197 internal modules
13 native modules
50 internal functions across 16 files
[2/9] cargo bun_runtime → libbun_runtime.a
�[1m�[33mwarning�[0m�[1m: binary `bun_shim_impl` should have a kebab-case name�[0m
�[1m�[94m|�[0m
�[1m�[94m 1�[0m �[1m�[94m|�[0m /workspace/bun/build/release/rust-target/.../bun_shim_impl
�[1m�[94m|�[0m �[1m�[33m^^^^^^^^^^^^^�[0m
�[1m�[94m|�[0m
�[1m�[94m= �[0m�[1mnote�[0m: `cargo::non_kebab_case_bins` is set to `warn` by default
�[1m�[96mhelp�[0m: to change the binary name to `bun-shim-impl`, convert `bin.name`
�[1m�[94m--> �[0msrc/install/windows-shim/Cargo.toml:41:8
�[1m�[94m|�[0m
�[1m�[94m41�[0m �[91m- �[0mname = �[91m"bun_shim_impl"�[0m
�[1m�[94m41�[0m �[92m+ �[0mname = �[92m"
... (truncated)
```
</details>
<details><summary>diff hotspot</summary>
```
src/boringssl/lib.rs | 38 +++++-
src/js/internal/url.ts | 5 +
src/js/node/tls.ts | 14 +-
src/js/node/url.ts | 3 +-
src/jsc/bindings/NodeURL.cpp | 9 ++
.../parallel/test-tls-check-server-identity.js | 30 +++++
test/js/web/fetch/fetch.tls.wildcard.test.ts | 149 ++++++++++++++++++++-
7 files changed, 241 insertions(+), 7 deletions(-)
```
</details>
**gate history** · 2 passed · 0 rejected · iteration 0
<details><summary>evidence per changed file</summary>
```
file reads edits tests
src/boringssl/lib.rs 6 6 25
src/js/internal/url.ts 1 1 24
src/js/node/tls.ts 4 4 26
src/js/node/url.ts 1 1 24
src/jsc/bindings/NodeURL.cpp 2 2 24
…js/node/test/parallel/test-tls-check-server-identity.js 0 0 32
test/js/web/fetch/fetch.tls.wildcard.test.ts 4 7 24
```
</details>
<!-- robobun:evidence:end -->1 parent ebfad4c commit 8705d89
7 files changed
Lines changed: 241 additions & 7 deletions
File tree
- src
- boringssl
- jsc/bindings
- js
- internal
- node
- test/js
- node/test/parallel
- web/fetch
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
400 | 400 | | |
401 | 401 | | |
402 | 402 | | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
403 | 407 | | |
404 | 408 | | |
405 | 409 | | |
| |||
434 | 438 | | |
435 | 439 | | |
436 | 440 | | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
437 | 446 | | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
438 | 461 | | |
439 | | - | |
440 | 462 | | |
441 | 463 | | |
442 | 464 | | |
| |||
651 | 673 | | |
652 | 674 | | |
653 | 675 | | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
654 | 688 | | |
655 | 689 | | |
656 | 690 | | |
| |||
740 | 774 | | |
741 | 775 | | |
742 | 776 | | |
743 | | - | |
| 777 | + | |
744 | 778 | | |
745 | 779 | | |
746 | 780 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
26 | 31 | | |
27 | 32 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
396 | 397 | | |
397 | 398 | | |
398 | 399 | | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
399 | 403 | | |
400 | 404 | | |
401 | 405 | | |
| |||
450 | 454 | | |
451 | 455 | | |
452 | 456 | | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
453 | 463 | | |
454 | 464 | | |
455 | 465 | | |
| |||
467 | 477 | | |
468 | 478 | | |
469 | 479 | | |
470 | | - | |
| 480 | + | |
471 | 481 | | |
472 | 482 | | |
473 | 483 | | |
474 | 484 | | |
475 | 485 | | |
476 | 486 | | |
477 | | - | |
| 487 | + | |
478 | 488 | | |
479 | 489 | | |
480 | 490 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
30 | | - | |
| 29 | + | |
31 | 30 | | |
32 | 31 | | |
33 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
160 | 169 | | |
161 | 170 | | |
162 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
103 | 124 | | |
104 | 125 | | |
105 | 126 | | |
| |||
381 | 402 | | |
382 | 403 | | |
383 | 404 | | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
384 | 414 | | |
385 | 415 | | |
386 | 416 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
303 | 388 | | |
304 | 389 | | |
305 | 390 | | |
306 | 391 | | |
307 | 392 | | |
308 | 393 | | |
309 | 394 | | |
310 | | - | |
| 395 | + | |
| 396 | + | |
311 | 397 | | |
312 | 398 | | |
313 | 399 | | |
| |||
421 | 507 | | |
422 | 508 | | |
423 | 509 | | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
424 | 529 | | |
425 | 530 | | |
426 | 531 | | |
| |||
437 | 542 | | |
438 | 543 | | |
439 | 544 | | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
440 | 587 | | |
441 | 588 | | |
442 | 589 | | |
| |||
0 commit comments