Skip to content

Commit e918570

Browse files
Trottdanielleadams
authored andcommitted
url: preserve null char in WHATWG URL errors
A null character in the middle of an invalid URL was resulting in an error message that truncated the input string. This preserves the entire input string in the error message. Refs: #39592 PR-URL: #42263 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 245577e commit e918570

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

lib/internal/url.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ function onParseComplete(flags, protocol, username, password,
549549
initSearchParams(this[searchParams], query);
550550
}
551551

552-
function onParseError(flags, input) {
552+
function onParseError(input, flags) {
553553
throw new ERR_INVALID_URL(input);
554554
}
555555

@@ -627,7 +627,8 @@ class URL {
627627
}
628628
this[context] = new URLContext();
629629
parse(input, -1, base_context, undefined,
630-
FunctionPrototypeBind(onParseComplete, this), onParseError);
630+
FunctionPrototypeBind(onParseComplete, this),
631+
FunctionPrototypeBind(onParseError, this, input));
631632
}
632633

633634
get [special]() {
@@ -740,7 +741,8 @@ class URL {
740741
// toUSVString is not needed.
741742
input = `${input}`;
742743
parse(input, -1, undefined, undefined,
743-
FunctionPrototypeBind(onParseComplete, this), onParseError);
744+
FunctionPrototypeBind(onParseComplete, this),
745+
FunctionPrototypeBind(onParseError, this, input));
744746
}
745747

746748
// readonly

src/node_url.cc

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,12 @@ URLHost::~URLHost() {
142142
XX(ARG_FRAGMENT) \
143143
XX(ARG_COUNT) // This one has to be last.
144144

145-
#define ERR_ARGS(XX) \
146-
XX(ERR_ARG_FLAGS) \
147-
XX(ERR_ARG_INPUT) \
148-
149145
enum url_cb_args {
150146
#define XX(name) name,
151147
ARGS(XX)
152148
#undef XX
153149
};
154150

155-
enum url_error_cb_args {
156-
#define XX(name) name,
157-
ERR_ARGS(XX)
158-
#undef XX
159-
};
160-
161151
#define TWO_CHAR_STRING_TEST(bits, name, expr) \
162152
template <typename T> \
163153
bool name(const T ch1, const T ch2) { \
@@ -1679,12 +1669,8 @@ void Parse(Environment* env,
16791669
SetArgs(env, argv, url);
16801670
cb->Call(context, recv, arraysize(argv), argv).FromMaybe(Local<Value>());
16811671
} else if (error_cb->IsFunction()) {
1682-
Local<Value> argv[2] = { undef, undef };
1683-
argv[ERR_ARG_FLAGS] = Integer::NewFromUnsigned(isolate, url.flags);
1684-
argv[ERR_ARG_INPUT] =
1685-
String::NewFromUtf8(env->isolate(), input).ToLocalChecked();
1686-
error_cb.As<Function>()->Call(context, recv, arraysize(argv), argv)
1687-
.FromMaybe(Local<Value>());
1672+
Local<Value> flags = Integer::NewFromUnsigned(isolate, url.flags);
1673+
USE(error_cb.As<Function>()->Call(context, recv, 1, &flags));
16881674
}
16891675
}
16901676

test/parallel/test-url-null-char.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
assert.throws(
6+
() => { new URL('a\0b'); },
7+
{ input: 'a\0b' }
8+
);

0 commit comments

Comments
 (0)