-
Notifications
You must be signed in to change notification settings - Fork 5k
url: accept Unicode 16.0 IDNA-mapped hostnames and reject invalid Punycode labels #34731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
680953d
url: accept IDNA hostnames reclassified from disallowed to mapped in …
robobun d32f026
[autofix.ci] apply automated fixes
autofix-ci[bot] 45af3b8
ci: retrigger
robobun 7499cbc
scripts: pin SHA-256 for ICU inputs, use absolute output path, trim h…
robobun f8feb90
url: reject invalid Punycode (xn--) labels in special-scheme hosts
robobun 8fa292a
url: handle ICU buffer overflow and validate the blob: origin's inner…
robobun 4ae7db6
test: add ContextJ (xn--0ug) cases to the Punycode harness
robobun f210109
trim multi-line comments to single lines
robobun 1d27a31
URLDecomposition: gate setHost/setHostname commit on urlHostIsValidID…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/usr/bin/env bash | ||
| # Regenerates src/jsc/bindings/icu_uts46_override.nrm: a Unicode 16.0 UTS #46 | ||
| # uts46.nrm in Nrm2 format v4 (readable by the bundled ICU 73/75), swapped in at | ||
| # runtime by bun_icu_decompress.cpp. Drop once the WebKit prebuilt ships ICU 76+. | ||
| set -euo pipefail | ||
|
|
||
| ICU_TOOLCHAIN_TAG=release-75-1 # whose gennorm2 to use (emits Nrm2 format v4) | ||
| ICU_TOOLCHAIN_SRC=icu4c-75_1-src.tgz | ||
| ICU_TOOLCHAIN_SHA256=cb968df3e4d2e87e8b11c49a5d01c787bd13b9545280fc6642f826527618caef | ||
| ICU_DATA_TAG=release-76-1 # whose norm2/uts46.txt to compile | ||
| ICU_DATA_SHA256=fda2c1c636d71db2cc685ca7671aff8efa27e83d84ee322cc3ce1375e53300d8 | ||
|
|
||
| REPO_ROOT="$(cd -- "$(dirname -- "$0")/.." && pwd)" | ||
| OUT="$REPO_ROOT/src/jsc/bindings/icu_uts46_override.nrm" | ||
| WORK="$(mktemp -d)" | ||
| trap 'rm -rf "$WORK"' EXIT | ||
|
|
||
| fetch() { | ||
| curl -fL --proto '=https' --tlsv1.2 "$1" -o "$2" | ||
| echo "$3 $2" | sha256sum -c - | ||
| } | ||
|
|
||
| fetch "https://github.com/unicode-org/icu/releases/download/${ICU_TOOLCHAIN_TAG}/${ICU_TOOLCHAIN_SRC}" \ | ||
| "$WORK/icu.tgz" "$ICU_TOOLCHAIN_SHA256" | ||
| tar -xzf "$WORK/icu.tgz" -C "$WORK" | ||
| pushd "$WORK/icu/source" >/dev/null | ||
| ./configure --enable-static --disable-shared --with-data-packaging=archive \ | ||
| --disable-samples --disable-tests --disable-extras --disable-icuio >/dev/null | ||
| make -j"$(nproc)" >/dev/null | ||
| popd >/dev/null | ||
|
|
||
| fetch "https://raw.githubusercontent.com/unicode-org/icu/${ICU_DATA_TAG}/icu4c/source/data/unidata/norm2/uts46.txt" \ | ||
| "$WORK/icu/source/data/unidata/norm2/uts46.txt" "$ICU_DATA_SHA256" | ||
|
|
||
| LD_LIBRARY_PATH="$WORK/icu/source/lib:$WORK/icu/source/stubdata" \ | ||
| "$WORK/icu/source/bin/gennorm2" -o "$OUT" \ | ||
| -s "$WORK/icu/source/data/unidata/norm2" nfc.txt uts46.txt | ||
|
|
||
| fmt=$(od -An -t u1 -j 16 -N 1 "$OUT" | tr -d ' ') | ||
| [ "$fmt" = "4" ] || { echo "error: $OUT has Nrm2 format version $fmt, expected 4" >&2; exit 1; } | ||
|
|
||
| echo "wrote $OUT ($(wc -c <"$OUT") bytes, md5 $(md5sum "$OUT" | cut -d' ' -f1))" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #include "root.h" | ||
| #include "BunIDNA.h" | ||
|
|
||
| #include <unicode/uidna.h> | ||
| #include <wtf/URL.h> | ||
| #include <wtf/URLParser.h> | ||
| #include <wtf/Vector.h> | ||
| #include <wtf/text/StringView.h> | ||
| #include <wtf/text/WTFString.h> | ||
|
|
||
| namespace Bun { | ||
|
|
||
| bool domainHasACELabel(WTF::StringView domain) | ||
| { | ||
| unsigned labelStart = 0; | ||
| while (true) { | ||
| if (domain.substring(labelStart).startsWithIgnoringASCIICase("xn--"_s)) | ||
| return true; | ||
| size_t dot = domain.find('.', labelStart); | ||
| if (dot == WTF::notFound) | ||
| return false; | ||
| labelStart = static_cast<unsigned>(dot) + 1; | ||
| } | ||
| } | ||
|
|
||
| WTF::String domainToASCII(WTF::StringView domain) | ||
| { | ||
| std::array<char16_t, WTF::URLParser::hostnameBufferLength> stackBuffer; | ||
| WTF::Vector<char16_t> heapBuffer; | ||
| std::span<char16_t> buffer { stackBuffer }; | ||
| while (true) { | ||
| UErrorCode error = U_ZERO_ERROR; | ||
| UIDNAInfo processingDetails = UIDNA_INFO_INITIALIZER; | ||
| int32_t length = uidna_nameToASCII(&WTF::URLParser::internationalDomainNameTranscoder(), domain.upconvertedCharacters(), domain.length(), buffer.data(), static_cast<int32_t>(buffer.size()), &processingDetails, &error); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if (U_SUCCESS(error) && !(processingDetails.errors & ~WTF::URLParser::allowedNameToASCIIErrors) && length > 0) | ||
| return WTF::String { buffer.first(static_cast<size_t>(length)) }; | ||
| // ICU preflight: on overflow `length` is the required size; retry once with a heap buffer. | ||
| if (error != U_BUFFER_OVERFLOW_ERROR || length <= 0 || !heapBuffer.isEmpty()) | ||
| return {}; | ||
| heapBuffer.grow(static_cast<size_t>(length)); | ||
| buffer = heapBuffer.mutableSpan(); | ||
| } | ||
| } | ||
|
|
||
| bool urlHostIsValidIDNA(const WTF::URL& url) | ||
| { | ||
| if (!url.hasSpecialScheme()) | ||
| return true; | ||
| auto host = url.host(); | ||
| if (!domainHasACELabel(host)) | ||
| return true; | ||
| return !domainToASCII(host).isNull(); | ||
| } | ||
|
|
||
| } // namespace Bun | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
|
|
||
| #include "root.h" | ||
|
|
||
| #include <wtf/Forward.h> | ||
|
|
||
| namespace Bun { | ||
|
|
||
| // Does any dot-separated label start with "xn--" (ASCII case-insensitive)? | ||
| bool domainHasACELabel(WTF::StringView domain); | ||
|
|
||
| // https://url.spec.whatwg.org/#concept-domain-to-ascii (beStrict = false); null on failure. | ||
| WTF::String domainToASCII(WTF::StringView domain); | ||
|
|
||
| // WTF::URLParser skips UTS-46 for all-ASCII hosts, so an invalid "xn--" label parses; re-check it here. | ||
| bool urlHostIsValidIDNA(const WTF::URL&); | ||
|
|
||
| } // namespace Bun |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.