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 <darshan.sen@postman.com>

PR-URL: #39060
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
  • Loading branch information
RaisinTen authored and nodejs-github-bot committed Jul 18, 2021
1 parent fdf625b commit 00cac65
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/node_url.cc
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;
url->path.emplace_back("");
}
break;
case kPath:
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
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
103 changes: 103 additions & 0 deletions test/fixtures/wpt/url/resources/setters_tests.json
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": "me@example.net"
}
},
{
"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

0 comments on commit 00cac65

Please sign in to comment.