Skip to content

Commit 8c5cd14

Browse files
TimothyGuaddaleax
authored andcommitted
src: avoid dereference without existence check
Currently the URL API is only used from the JS binding, which always initializes `base` regardless of `has_base`. Therefore, there is no actual security risk right now, but would be had we made other C++ parts of Node.js use this API. An earlier version of this patch was created by Bradley Farias <[email protected]>. PR-URL: #14591 Refs: #14369 (comment) Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 3c46ef4 commit 8c5cd14

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/node_url.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ void URL::Parse(const char* input,
12831283
}
12841284
break;
12851285
case kNoScheme:
1286-
cannot_be_base = base->flags & URL_FLAGS_CANNOT_BE_BASE;
1286+
cannot_be_base = has_base && (base->flags & URL_FLAGS_CANNOT_BE_BASE);
12871287
if (!has_base || (cannot_be_base && ch != '#')) {
12881288
url->flags |= URL_FLAGS_FAILED;
12891289
return;

test/cctest/test_url.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "gtest/gtest.h"
55

66
using node::url::URL;
7+
using node::url::URL_FLAGS_FAILED;
78

89
class URLTest : public ::testing::Test {
910
protected:
@@ -20,6 +21,7 @@ class URLTest : public ::testing::Test {
2021
TEST_F(URLTest, Simple) {
2122
URL simple("https://example.org:81/a/b/c?query#fragment");
2223

24+
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
2325
EXPECT_EQ(simple.protocol(), "https:");
2426
EXPECT_EQ(simple.host(), "example.org");
2527
EXPECT_EQ(simple.port(), 81);
@@ -32,6 +34,7 @@ TEST_F(URLTest, Simple2) {
3234
const char* input = "https://example.org:81/a/b/c?query#fragment";
3335
URL simple(input, strlen(input));
3436

37+
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
3538
EXPECT_EQ(simple.protocol(), "https:");
3639
EXPECT_EQ(simple.host(), "example.org");
3740
EXPECT_EQ(simple.port(), 81);
@@ -40,10 +43,17 @@ TEST_F(URLTest, Simple2) {
4043
EXPECT_EQ(simple.fragment(), "fragment");
4144
}
4245

46+
TEST_F(URLTest, NoBase1) {
47+
URL error("123noscheme");
48+
EXPECT_TRUE(error.flags() & URL_FLAGS_FAILED);
49+
}
50+
4351
TEST_F(URLTest, Base1) {
4452
URL base("http://example.org/foo/bar");
45-
URL simple("../baz", &base);
53+
ASSERT_FALSE(base.flags() & URL_FLAGS_FAILED);
4654

55+
URL simple("../baz", &base);
56+
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
4757
EXPECT_EQ(simple.protocol(), "http:");
4858
EXPECT_EQ(simple.host(), "example.org");
4959
EXPECT_EQ(simple.path(), "/baz");
@@ -52,6 +62,7 @@ TEST_F(URLTest, Base1) {
5262
TEST_F(URLTest, Base2) {
5363
URL simple("../baz", "http://example.org/foo/bar");
5464

65+
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
5566
EXPECT_EQ(simple.protocol(), "http:");
5667
EXPECT_EQ(simple.host(), "example.org");
5768
EXPECT_EQ(simple.path(), "/baz");
@@ -63,6 +74,7 @@ TEST_F(URLTest, Base3) {
6374

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

77+
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
6678
EXPECT_EQ(simple.protocol(), "http:");
6779
EXPECT_EQ(simple.host(), "example.org");
6880
EXPECT_EQ(simple.path(), "/baz");

0 commit comments

Comments
 (0)