Skip to content

Commit 0f58d3c

Browse files
committed
src: support domains with empty labels
Follow the spec of domainToASCII/domainToUnicode in whatwg, and synchronise WPT url test data. Refs: web-platform-tests/wpt#5397 PR-URL: #12707 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 6ed791c commit 0f58d3c

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed

src/node_i18n.cc

+17
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ int32_t ToUnicode(MaybeStackBuffer<char>* buf,
461461
&status);
462462
}
463463

464+
// UTS #46's ToUnicode operation applies no validation of domain name length
465+
// (nor a flag requesting it to do so, like VerifyDnsLength for ToASCII). For
466+
// that reason, unlike ToASCII below, ICU4C correctly accepts long domain
467+
// names. However, ICU4C still sets the EMPTY_LABEL error in contrary to UTS
468+
// #46. Therefore, explicitly filters out that error here.
469+
info.errors &= ~UIDNA_ERROR_EMPTY_LABEL;
470+
464471
if (U_FAILURE(status) || (!lenient && info.errors != 0)) {
465472
len = -1;
466473
buf->SetLength(0);
@@ -500,6 +507,16 @@ int32_t ToASCII(MaybeStackBuffer<char>* buf,
500507
&status);
501508
}
502509

510+
// The WHATWG URL "domain to ASCII" algorithm explicitly sets the
511+
// VerifyDnsLength flag to false, which disables the domain name length
512+
// verification step in ToASCII (as specified by UTS #46). Unfortunately,
513+
// ICU4C's IDNA module does not support disabling this flag through `options`,
514+
// so just filter out the errors that may be caused by the verification step
515+
// afterwards.
516+
info.errors &= ~UIDNA_ERROR_EMPTY_LABEL;
517+
info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
518+
info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
519+
503520
if (U_FAILURE(status) || (!lenient && info.errors != 0)) {
504521
len = -1;
505522
buf->SetLength(0);

test/fixtures/url-idna.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,18 @@ module.exports = {
182182
ascii: 'xn--vitnam-jk8b.icom.museum',
183183
unicode: 'việtnam.icom.museum'
184184
},
185-
// long URL
186-
{
187-
ascii: `${`${'a'.repeat(63)}.`.repeat(3)}com`,
188-
unicode: `${`${'a'.repeat(63)}.`.repeat(3)}com`
189-
}
190-
],
191-
invalid: [
192185
// long label
193186
{
194-
url: `${'a'.repeat(64)}.com`,
195-
mode: 'ascii'
187+
ascii: `${'a'.repeat(64)}.com`,
188+
unicode: `${'a'.repeat(64)}.com`,
196189
},
197190
// long URL
198191
{
199-
url: `${`${'a'.repeat(63)}.`.repeat(4)}com`,
200-
mode: 'ascii'
201-
},
192+
ascii: `${`${'a'.repeat(64)}.`.repeat(4)}com`,
193+
unicode: `${`${'a'.repeat(64)}.`.repeat(4)}com`
194+
}
195+
],
196+
invalid: [
202197
// invalid character
203198
{
204199
url: '\ufffd.com',

test/fixtures/url-tests.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
/* WPT Refs:
4-
https://github.com/w3c/web-platform-tests/blob/3eff1bd/url/urltestdata.json
4+
https://github.com/w3c/web-platform-tests/blob/3afae94/url/urltestdata.json
55
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
66
*/
77
module.exports =
@@ -3789,6 +3789,52 @@ module.exports =
37893789
"search": "",
37903790
"hash": ""
37913791
},
3792+
"Domains with empty labels",
3793+
{
3794+
"input": "http://./",
3795+
"base": "about:blank",
3796+
"href": "http://./",
3797+
"origin": "http://.",
3798+
"protocol": "http:",
3799+
"username": "",
3800+
"password": "",
3801+
"host": ".",
3802+
"hostname": ".",
3803+
"port": "",
3804+
"pathname": "/",
3805+
"search": "",
3806+
"hash": ""
3807+
},
3808+
{
3809+
"input": "http://../",
3810+
"base": "about:blank",
3811+
"href": "http://../",
3812+
"origin": "http://..",
3813+
"protocol": "http:",
3814+
"username": "",
3815+
"password": "",
3816+
"host": "..",
3817+
"hostname": "..",
3818+
"port": "",
3819+
"pathname": "/",
3820+
"search": "",
3821+
"hash": ""
3822+
},
3823+
{
3824+
"input": "http://0..0x300/",
3825+
"base": "about:blank",
3826+
"href": "http://0..0x300/",
3827+
"origin": "http://0..0x300",
3828+
"protocol": "http:",
3829+
"username": "",
3830+
"password": "",
3831+
"host": "0..0x300",
3832+
"hostname": "0..0x300",
3833+
"port": "",
3834+
"pathname": "/",
3835+
"search": "",
3836+
"hash": ""
3837+
},
37923838
"Broken IPv6",
37933839
{
37943840
"input": "http://[www.google.com]/",

0 commit comments

Comments
 (0)