Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions src/jsc/bindings/URLDecomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,16 @@ String URLDecomposition::pathname() const
return fullURL().path().toString();
}

void URLDecomposition::setPathname(StringView value)
void URLDecomposition::setPathname(const String& value)
{
auto fullURL = this->fullURL();
if (fullURL.hasOpaquePath())
return;
fullURL.setPath(value);
// The basic URL parser removes ASCII tab and newline before anything else.
// URL::setPath only drops them in its reparse, after choosing the leading "/"
// (and the "/." guard) from the raw value, so "\n/a/b" reparsed as "//a/b".
Comment thread
robobun marked this conversation as resolved.
Resolved
String path = value.removeCharacters([](char16_t c) { return c == '\t' || c == '\n' || c == '\r'; });
fullURL.setPath(path);
setFullURL(fullURL);
}

Expand Down
2 changes: 1 addition & 1 deletion src/jsc/bindings/URLDecomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class URLDecomposition {
void setPort(StringView);

WEBCORE_EXPORT String pathname() const;
void setPathname(StringView);
void setPathname(const String&);

WEBCORE_EXPORT String search() const;
void setSearch(const String&);
Expand Down
36 changes: 36 additions & 0 deletions test/js/web/url/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,42 @@ describe("url", () => {
// A path whose first segment merely starts with "." gets no guard.
expect(util.inspect(new URL("foo:/.foo"), { showHidden: true })).toContain("pathname_start: 4,");
});

it("pathname setter removes ASCII tab and newline before looking at the leading slash", () => {
// https://url.spec.whatwg.org/#concept-basic-url-parser removes tab, LF and CR from the value first, so
// "\n/x" is the same assignment as "/x". Expected values are Node's. On a host-less URL the unstripped
// value used to reparse as "//evil.com/y", which made evil.com the host.
const cases: [href: string, value: string, expected: { href: string; host: string; pathname: string }][] = [
["foo:/a", "\n/evil.com/y", { href: "foo:/evil.com/y", host: "", pathname: "/evil.com/y" }],
["foo:/a", "\t/evil.com/y", { href: "foo:/evil.com/y", host: "", pathname: "/evil.com/y" }],
["foo:/a", "\r/evil.com/y", { href: "foo:/evil.com/y", host: "", pathname: "/evil.com/y" }],
["foo:/a", "\t\n\r/evil.com/y", { href: "foo:/evil.com/y", host: "", pathname: "/evil.com/y" }],
// Two leading slashes on a host-less URL get the /. guard instead of becoming an empty host.
["foo:/a", "\n//evil.com/y", { href: "foo:/.//evil.com/y", host: "", pathname: "//evil.com/y" }],
["foo:/a", "\tevil.com/y", { href: "foo:/evil.com/y", host: "", pathname: "/evil.com/y" }],
["foo:/.//p", "\t/x", { href: "foo:/x", host: "", pathname: "/x" }],
["foo:/.//p", "\t//x", { href: "foo:/.//x", host: "", pathname: "//x" }],
["foo://host/a", "\n/x/y", { href: "foo://host/x/y", host: "host", pathname: "/x/y" }],
// A value that strips down to "" empties the path of a non-special URL with a host.
["foo://host/a", "\t", { href: "foo://host", host: "host", pathname: "" }],
["http://h/", "\t/x", { href: "http://h/x", host: "h", pathname: "/x" }],
["http://h/", "\n/x", { href: "http://h/x", host: "h", pathname: "/x" }],
["http://h/", "\r/x", { href: "http://h/x", host: "h", pathname: "/x" }],
["http://h/", "\t//x", { href: "http://h//x", host: "h", pathname: "//x" }],
["http://h/", "\t\\x", { href: "http://h/x", host: "h", pathname: "/x" }],
["http://h/", "\tx", { href: "http://h/x", host: "h", pathname: "/x" }],
["http://h/", "\t/a\tb/\nc", { href: "http://h/ab/c", host: "h", pathname: "/ab/c" }],
["http://h/", "/x\n", { href: "http://h/x", host: "h", pathname: "/x" }],
["file:///a", "\t/x", { href: "file:///x", host: "", pathname: "/x" }],
];
const results = cases.map(([href, value]) => {
const url = new URL(href);
url.pathname = value;
return [href, value, { href: url.href, host: url.host, pathname: url.pathname }];
});
expect(results).toEqual(cases);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});

it("works", () => {
const inputs = [
[
Expand Down
Loading