Skip to content

Commit 6b70bd9

Browse files
committed
src: improve error handling for triggered assertions
1 parent bf452bb commit 6b70bd9

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/node_url.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ void BindingData::PathToFileURL(const FunctionCallbackInfo<Value>& args) {
169169
[[unlikely]] {
170170
CHECK(args[2]->IsString());
171171
Utf8Value hostname(isolate, args[2]);
172-
CHECK(out->set_hostname(hostname.ToStringView()));
172+
// Ada should validate chars in hostname
173+
if(!out->set_hostname(hostname.ToStringView())) {
174+
return ThrowInvalidURL(realm->env(), input.ToStringView(), std::nullopt);
175+
}
173176
}
174177

175178
binding_data->UpdateComponents(out->get_components(), out->type);
@@ -441,7 +444,10 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
441444

442445
std::string_view new_value_view = new_value.ToStringView();
443446
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
444-
CHECK(out);
447+
// If the href cannot be re-parsed, return original url
448+
if (!out) {
449+
return args.GetReturnValue().Set(false);
450+
}
445451

446452
bool result{true};
447453

test/parallel/test-url-format-whatwg.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,6 @@ test('should not crash on URLs with invalid IDN hostnames', () => {
154154
const u = new URL('ws:xn-\u022B');
155155
// doesNotThrow
156156
url.format(u, { fragment: false, unicode: false, auth: false, search: false });
157+
// Same problem with re-parsing
158+
url.port = 80;
157159
});

test/parallel/test-url-pathtofileurl.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,10 @@ for (const { path, expected } of testCases) {
223223
});
224224
}
225225
}
226+
227+
// Regression for forbidden chars in UNC Windows
228+
{
229+
assert.throws(() => url.pathToFileURL('\\\\%\\file', {windows : true}), {
230+
code: 'ERR_INVALID_URL',
231+
});
232+
}

0 commit comments

Comments
 (0)