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

src: fix leading backslash bug in URL #36613

Closed
wants to merge 1 commit into from
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
6 changes: 5 additions & 1 deletion src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ void URL::Parse(const char* input,
const char ch = p < end ? p[0] : kEOL;
bool special = (url->flags & URL_FLAGS_SPECIAL);
bool cannot_be_base;
const bool special_back_slash = (special && ch == '\\');
bool special_back_slash = (special && ch == '\\');

switch (state) {
case kSchemeStart:
Expand Down Expand Up @@ -1477,6 +1477,7 @@ void URL::Parse(const char* input,
url->flags &= ~URL_FLAGS_SPECIAL;
special = false;
}
special_back_slash = (special && ch == '\\');
buffer.clear();
if (has_state_override)
return;
Expand Down Expand Up @@ -1521,6 +1522,7 @@ void URL::Parse(const char* input,
url->flags &= ~URL_FLAGS_SPECIAL;
special = false;
}
special_back_slash = (special && ch == '\\');
if (base->flags & URL_FLAGS_HAS_PATH) {
url->flags |= URL_FLAGS_HAS_PATH;
url->path = base->path;
Expand All @@ -1544,6 +1546,7 @@ void URL::Parse(const char* input,
url->flags |= URL_FLAGS_SPECIAL;
special = true;
state = kFile;
special_back_slash = (special && ch == '\\');
continue;
}
break;
Expand Down Expand Up @@ -1573,6 +1576,7 @@ void URL::Parse(const char* input,
url->flags &= ~URL_FLAGS_SPECIAL;
special = false;
}
special_back_slash = (special && ch == '\\');
switch (ch) {
case kEOL:
if (base->flags & URL_FLAGS_HAS_USERNAME) {
Expand Down
46 changes: 46 additions & 0 deletions test/cctest/test_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,52 @@ TEST_F(URLTest, Base3) {
EXPECT_EQ(simple.path(), "/baz");
}

TEST_F(URLTest, Base4) {
const char* input = "\\x";
const char* base = "http://example.org/foo/bar";

URL simple(input, strlen(input), base, strlen(base));

EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.path(), "/x");
}

TEST_F(URLTest, Base5) {
const char* input = "/x";
const char* base = "http://example.org/foo/bar";

URL simple(input, strlen(input), base, strlen(base));

EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.path(), "/x");
}

TEST_F(URLTest, Base6) {
const char* input = "\\\\x";
const char* base = "http://example.org/foo/bar";

URL simple(input, strlen(input), base, strlen(base));

EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "x");
}

TEST_F(URLTest, Base7) {
const char* input = "//x";
const char* base = "http://example.org/foo/bar";

URL simple(input, strlen(input), base, strlen(base));

EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "x");
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we should always add WPTs for bug fixes to the WHATWG URL parser, so that other implementations (e.g., browsers) can similarly benefit. I've upstreamed two of these tests in web-platform-tests/wpt#29271.

/cc @joyeecheung

TEST_F(URLTest, TruncatedAfterProtocol) {
char input[2] = { 'q', ':' };
URL simple(input, sizeof(input));
Expand Down