LibWeb: Don't crash with invalid import maps

See:
 - http://wpt.live/import-maps/multiple-import-maps/with-errors.html
This commit is contained in:
Jamie Mansfield 2024-08-04 14:08:32 +01:00 committed by Andreas Kling
parent 040653311e
commit c891b83fc0
Notes: github-actions[bot] 2024-08-06 14:26:37 +00:00
4 changed files with 35 additions and 11 deletions

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/ModuleRequest.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/Scripting/ImportMapParseResult.h>
#include <LibWeb/HTML/Window.h>
@ -23,15 +24,13 @@ JS::NonnullGCPtr<ImportMapParseResult> ImportMapParseResult::create(JS::Realm& r
{
// 1. Let result be an import map parse result whose import map is null and whose error to rethrow is null.
auto result = realm.heap().allocate<ImportMapParseResult>(realm);
result->set_error_to_rethrow(JS::js_null());
// 2. Parse an import map string given input and baseURL, catching any exceptions.
auto import_map = parse_import_map_string(realm, input, base_url);
// 2.1. If this threw an exception, then set result's error to rethrow to that exception.
// FIXME: rethrow the original exception
if (import_map.is_exception())
result->set_error_to_rethrow(JS::Error::create(realm, "Failed to parse import map string"sv));
result->set_error_to_rethrow(import_map.exception());
// 2.2. Otherwise, set result's import map to the return value.
else
@ -41,7 +40,7 @@ JS::NonnullGCPtr<ImportMapParseResult> ImportMapParseResult::create(JS::Realm& r
return result;
}
void ImportMapParseResult::visit_host_defined_self(JS::Cell::Visitor& visitor)
void ImportMapParseResult::visit_host_defined_self(Visitor& visitor)
{
visitor.visit(*this);
}
@ -49,15 +48,28 @@ void ImportMapParseResult::visit_host_defined_self(JS::Cell::Visitor& visitor)
void ImportMapParseResult::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_error_to_rethrow);
if (m_error_to_rethrow.has_value()) {
m_error_to_rethrow.value().visit(
[&](WebIDL::SimpleException const&) {
// ignore
},
[&](JS::NonnullGCPtr<WebIDL::DOMException> exception) {
visitor.visit(exception);
},
[&](JS::Completion const& completion) {
if (completion.value().has_value())
visitor.visit(completion.value().value());
});
}
}
// https://html.spec.whatwg.org/multipage/webappapis.html#register-an-import-map
void ImportMapParseResult::register_import_map(Window& global)
{
// 1. If result's error to rethrow is not null, then report the exception given by result's error to rethrow and return.
if (!m_error_to_rethrow.is_null()) {
HTML::report_exception(m_error_to_rethrow, global.realm());
if (m_error_to_rethrow.has_value()) {
auto completion = Web::Bindings::dom_exception_to_throw_completion(global.vm(), m_error_to_rethrow.value());
HTML::report_exception(completion, global.realm());
return;
}