Skip to content

Commit

Permalink
LibWeb: Don't crash with invalid import maps
Browse files Browse the repository at this point in the history
See:
 - http://wpt.live/import-maps/multiple-import-maps/with-errors.html
(cherry picked from commit c891b83fc079ee0c34ea9ccc71cfed67e3b14cd2)
  • Loading branch information
jamierocks authored and nico committed Nov 2, 2024
1 parent 00e187e commit c45292e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions Tests/LibWeb/Text/expected/HTML/import-maps-invalid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PASS
10 changes: 10 additions & 0 deletions Tests/LibWeb/Text/input/HTML/import-maps-invalid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script type="importmap">
Invalid import map.
</script>
<script type="module">
test(() => {
println("PASS");
});
</script>
26 changes: 19 additions & 7 deletions Userland/Libraries/LibWeb/HTML/Scripting/ImportMapParseResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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
Expand All @@ -41,23 +40,36 @@ 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);
}

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <LibURL/URL.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/Scripting/ImportMap.h>
#include <LibWeb/WebIDL/ExceptionOr.h>

namespace Web::HTML {

Expand All @@ -29,8 +30,8 @@ class ImportMapParseResult
[[nodiscard]] Optional<ImportMap> const& import_map() const { return m_import_map; }
void set_import_map(ImportMap const& value) { m_import_map = value; }

[[nodiscard]] JS::Value error_to_rethrow() const { return m_error_to_rethrow; }
void set_error_to_rethrow(JS::Value value) { m_error_to_rethrow = value; }
[[nodiscard]] Optional<WebIDL::Exception> const& error_to_rethrow() const { return m_error_to_rethrow; }
void set_error_to_rethrow(WebIDL::Exception const& value) { m_error_to_rethrow = value; }

void register_import_map(Window& global);

Expand All @@ -40,13 +41,13 @@ class ImportMapParseResult
virtual void visit_edges(Visitor&) override;

private:
virtual void visit_host_defined_self(JS::Cell::Visitor&) override;
virtual void visit_host_defined_self(Visitor&) override;

// https://html.spec.whatwg.org/multipage/webappapis.html#impr-import-map
Optional<ImportMap> m_import_map;

// https://html.spec.whatwg.org/multipage/webappapis.html#impr-error-to-rethrow
JS::Value m_error_to_rethrow;
Optional<WebIDL::Exception> m_error_to_rethrow;
};

}

0 comments on commit c45292e

Please sign in to comment.