Skip to content

Commit

Permalink
url: prevent pathname setter from erasing path of path-only URLs
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
RaisinTen committed Jun 26, 2021
1 parent 1317252 commit 143d369
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,8 @@ void URL::Parse(const char* input,
if (ch != '/') {
continue;
}
} else if (has_state_override && url->host.empty()) {
url->path.emplace_back("");
}
break;
case kPath:
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Last update:
- html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/5873f2d8f1/html/webappapis/timers
- interfaces: https://github.com/web-platform-tests/wpt/tree/79fa4cf76e/interfaces
- resources: https://github.com/web-platform-tests/wpt/tree/972ca5b669/resources
- 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/master/docs/git-node.md#git-node-wpt
[`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/main/docs/git-node.md#git-node-wpt
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

0 comments on commit 143d369

Please sign in to comment.