Skip to content

Commit 00cac65

Browse files
RaisinTennodejs-github-bot
authored andcommitted
url: prevent pathname setter from erasing path of path-only URLs
This change prevents the pathname setter from erasing the path of path-only URLs as that would make them cannot-be-a-base URLs. The changes in all files except `src/node_url.cc` have been done by running: ```console git node wpt url ``` Fixes: #39059 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #39060 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
1 parent fdf625b commit 00cac65

File tree

5 files changed

+331
-3
lines changed

5 files changed

+331
-3
lines changed

src/node_url.cc

+3
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,9 @@ void URL::Parse(const char* input,
14771477
if (ch != '/') {
14781478
continue;
14791479
}
1480+
} else if (has_state_override && !(url->flags & URL_FLAGS_HAS_HOST)) {
1481+
url->flags |= URL_FLAGS_HAS_PATH;
1482+
url->path.emplace_back("");
14801483
}
14811484
break;
14821485
case kPath:

test/fixtures/wpt/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Last update:
2222
- interfaces: https://github.com/web-platform-tests/wpt/tree/fcb671ed8b/interfaces
2323
- resources: https://github.com/web-platform-tests/wpt/tree/972ca5b669/resources
2424
- streams: https://github.com/web-platform-tests/wpt/tree/8f60d94439/streams
25-
- url: https://github.com/web-platform-tests/wpt/tree/1fcb39223d/url
25+
- url: https://github.com/web-platform-tests/wpt/tree/77d54aa9e0/url
2626

2727
[Web Platform Tests]: https://github.com/web-platform-tests/wpt
2828
[`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/main/docs/git-node.md#git-node-wpt

test/fixtures/wpt/url/resources/setters_tests.json

+103
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,26 @@
990990
"hostname": "test",
991991
"port": "12"
992992
}
993+
},
994+
{
995+
"comment": "Leading / is not stripped",
996+
"href": "http://example.com/",
997+
"new_value": "///bad.com",
998+
"expected": {
999+
"href": "http://example.com/",
1000+
"host": "example.com",
1001+
"hostname": "example.com"
1002+
}
1003+
},
1004+
{
1005+
"comment": "Leading / is not stripped",
1006+
"href": "sc://example.com/",
1007+
"new_value": "///bad.com",
1008+
"expected": {
1009+
"href": "sc:///",
1010+
"host": "",
1011+
"hostname": ""
1012+
}
9931013
}
9941014
],
9951015
"hostname": [
@@ -1345,6 +1365,26 @@
13451365
"hostname": "",
13461366
"pathname": "//p"
13471367
}
1368+
},
1369+
{
1370+
"comment": "Leading / is not stripped",
1371+
"href": "http://example.com/",
1372+
"new_value": "///bad.com",
1373+
"expected": {
1374+
"href": "http://example.com/",
1375+
"host": "example.com",
1376+
"hostname": "example.com"
1377+
}
1378+
},
1379+
{
1380+
"comment": "Leading / is not stripped",
1381+
"href": "sc://example.com/",
1382+
"new_value": "///bad.com",
1383+
"expected": {
1384+
"href": "sc:///",
1385+
"host": "",
1386+
"hostname": ""
1387+
}
13481388
}
13491389
],
13501390
"port": [
@@ -1571,6 +1611,51 @@
15711611
"pathname": "[email protected]"
15721612
}
15731613
},
1614+
{
1615+
"comment": "Special URLs cannot have their paths erased",
1616+
"href": "file:///some/path",
1617+
"new_value": "",
1618+
"expected": {
1619+
"href": "file:///",
1620+
"pathname": "/"
1621+
}
1622+
},
1623+
{
1624+
"comment": "Non-special URLs can have their paths erased",
1625+
"href": "foo://somehost/some/path",
1626+
"new_value": "",
1627+
"expected": {
1628+
"href": "foo://somehost",
1629+
"pathname": ""
1630+
}
1631+
},
1632+
{
1633+
"comment": "Non-special URLs with an empty host can have their paths erased",
1634+
"href": "foo:///some/path",
1635+
"new_value": "",
1636+
"expected": {
1637+
"href": "foo://",
1638+
"pathname": ""
1639+
}
1640+
},
1641+
{
1642+
"comment": "Path-only URLs cannot have their paths erased",
1643+
"href": "foo:/some/path",
1644+
"new_value": "",
1645+
"expected": {
1646+
"href": "foo:/",
1647+
"pathname": "/"
1648+
}
1649+
},
1650+
{
1651+
"comment": "Path-only URLs always have an initial slash",
1652+
"href": "foo:/some/path",
1653+
"new_value": "test",
1654+
"expected": {
1655+
"href": "foo:/test",
1656+
"pathname": "/test"
1657+
}
1658+
},
15741659
{
15751660
"href": "unix:/run/foo.socket?timeout=10",
15761661
"new_value": "/var/log/../run/bar.socket",
@@ -1667,6 +1752,24 @@
16671752
"pathname": "/%23"
16681753
}
16691754
},
1755+
{
1756+
"comment": "? doesn't mess up encoding",
1757+
"href": "http://example.net",
1758+
"new_value": "/?é",
1759+
"expected": {
1760+
"href": "http://example.net/%3F%C3%A9",
1761+
"pathname": "/%3F%C3%A9"
1762+
}
1763+
},
1764+
{
1765+
"comment": "# doesn't mess up encoding",
1766+
"href": "http://example.net",
1767+
"new_value": "/#é",
1768+
"expected": {
1769+
"href": "http://example.net/%23%C3%A9",
1770+
"pathname": "/%23%C3%A9"
1771+
}
1772+
},
16701773
{
16711774
"comment": "File URLs and (back)slashes",
16721775
"href": "file://monkey/",

0 commit comments

Comments
 (0)