-
Notifications
You must be signed in to change notification settings - Fork 5k
url: reject invalid Punycode (xn--) labels in special-scheme hosts #33201
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
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,61 @@ | ||
| #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); | ||
| if (U_SUCCESS(error) && !(processingDetails.errors & ~WTF::URLParser::allowedNameToASCIIErrors) && length > 0) | ||
| return WTF::String { buffer.first(static_cast<size_t>(length)) }; | ||
| // ICU's preflight convention: on overflow, `length` is the required | ||
| // size. Retry once so a domain longer than the stack buffer (a host | ||
| // the spec places no length limit on) is not treated as invalid. | ||
| 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) | ||
| { | ||
| // Only special-scheme URLs have a domain host; the host of every other | ||
| // scheme is opaque and the URL Standard does not apply IDNA to it. | ||
| if (!url.hasSpecialScheme()) | ||
| return true; | ||
| // A parsed special host is already ASCII; UTS-46 "domain to ASCII" can | ||
| // only still fail on it when a label uses the Punycode "xn--" prefix. | ||
| 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,24 @@ | ||
| #pragma once | ||
|
|
||
| #include "root.h" | ||
|
|
||
| #include <wtf/Forward.h> | ||
|
|
||
| namespace Bun { | ||
|
|
||
| // Whether any dot-separated label of `domain` starts with the Punycode ACE | ||
| // prefix "xn--" (ASCII case-insensitive). Those are the only all-ASCII | ||
| // labels that UTS-46 "domain to ASCII" can reject. | ||
| bool domainHasACELabel(WTF::StringView domain); | ||
|
|
||
| // UTS-46 "domain to ASCII" with the URL Standard's options (beStrict = false): | ||
| // https://url.spec.whatwg.org/#concept-domain-to-ascii | ||
| // Returns a null String when `domain` is not a valid IDNA domain. | ||
| WTF::String domainToASCII(WTF::StringView domain); | ||
|
|
||
| // WTF::URLParser never runs UTS-46 on an all-ASCII host, so an "xn--" label | ||
| // that does not decode to a valid IDNA label parses successfully. The URL | ||
| // Standard's host parser requires that to fail; re-check such hosts 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
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.
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.