Skip to content
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

url: prevent pathname setter from erasing path of path-only URLs #39060

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,9 @@ void URL::Parse(const char* input,
if (ch != '/') {
continue;
}
} else if (has_state_override && !(url->flags & URL_FLAGS_HAS_HOST)) {
url->flags |= URL_FLAGS_HAS_PATH;
TimothyGu marked this conversation as resolved.
Show resolved Hide resolved
url->path.emplace_back("");
}
break;
case kPath:
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Last update:
- interfaces: https://github.com/web-platform-tests/wpt/tree/fcb671ed8b/interfaces
- resources: https://github.com/web-platform-tests/wpt/tree/972ca5b669/resources
- streams: https://github.com/web-platform-tests/wpt/tree/8f60d94439/streams
- url: https://github.com/web-platform-tests/wpt/tree/1fcb39223d/url
- url: https://github.com/web-platform-tests/wpt/tree/77d54aa9e0/url

[Web Platform Tests]: https://github.com/web-platform-tests/wpt
[`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/main/docs/git-node.md#git-node-wpt
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
103 changes: 103 additions & 0 deletions test/fixtures/wpt/url/resources/setters_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,26 @@
"hostname": "test",
"port": "12"
}
},
{
"comment": "Leading / is not stripped",
"href": "http://example.com/",
"new_value": "///bad.com",
"expected": {
"href": "http://example.com/",
"host": "example.com",
"hostname": "example.com"
}
},
{
"comment": "Leading / is not stripped",
"href": "sc://example.com/",
"new_value": "///bad.com",
"expected": {
"href": "sc:///",
"host": "",
"hostname": ""
}
}
],
"hostname": [
Expand Down Expand Up @@ -1345,6 +1365,26 @@
"hostname": "",
"pathname": "//p"
}
},
{
"comment": "Leading / is not stripped",
"href": "http://example.com/",
"new_value": "///bad.com",
"expected": {
"href": "http://example.com/",
"host": "example.com",
"hostname": "example.com"
}
},
{
"comment": "Leading / is not stripped",
"href": "sc://example.com/",
"new_value": "///bad.com",
"expected": {
"href": "sc:///",
"host": "",
"hostname": ""
}
}
],
"port": [
Expand Down Expand Up @@ -1571,6 +1611,51 @@
"pathname": "[email protected]"
}
},
{
"comment": "Special URLs cannot have their paths erased",
"href": "file:///some/path",
"new_value": "",
"expected": {
"href": "file:///",
"pathname": "/"
}
},
{
"comment": "Non-special URLs can have their paths erased",
"href": "foo://somehost/some/path",
"new_value": "",
"expected": {
"href": "foo://somehost",
"pathname": ""
}
},
{
"comment": "Non-special URLs with an empty host can have their paths erased",
"href": "foo:///some/path",
"new_value": "",
"expected": {
"href": "foo://",
"pathname": ""
}
},
{
"comment": "Path-only URLs cannot have their paths erased",
"href": "foo:/some/path",
"new_value": "",
"expected": {
"href": "foo:/",
"pathname": "/"
}
},
{
"comment": "Path-only URLs always have an initial slash",
"href": "foo:/some/path",
"new_value": "test",
"expected": {
"href": "foo:/test",
"pathname": "/test"
}
},
{
"href": "unix:/run/foo.socket?timeout=10",
"new_value": "/var/log/../run/bar.socket",
Expand Down Expand Up @@ -1667,6 +1752,24 @@
"pathname": "/%23"
}
},
{
"comment": "? doesn't mess up encoding",
"href": "http://example.net",
"new_value": "/?é",
"expected": {
"href": "http://example.net/%3F%C3%A9",
"pathname": "/%3F%C3%A9"
}
},
{
"comment": "# doesn't mess up encoding",
"href": "http://example.net",
"new_value": "/#é",
"expected": {
"href": "http://example.net/%23%C3%A9",
"pathname": "/%23%C3%A9"
}
},
{
"comment": "File URLs and (back)slashes",
"href": "file://monkey/",
Expand Down
Loading