소스 검색

LibWeb: Don't crash with invalid import maps

See:
 - http://wpt.live/import-maps/multiple-import-maps/with-errors.html
Jamie Mansfield 11 달 전
부모
커밋
c891b83fc0

+ 1 - 0
Tests/LibWeb/Text/expected/HTML/import-maps-invalid.txt

@@ -0,0 +1 @@
+PASS

+ 10 - 0
Tests/LibWeb/Text/input/HTML/import-maps-invalid.html

@@ -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>

+ 19 - 7
Userland/Libraries/LibWeb/HTML/Scripting/ImportMapParseResult.cpp

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

+ 5 - 4
Userland/Libraries/LibWeb/HTML/Scripting/ImportMapParseResult.h

@@ -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 {
 
@@ -29,8 +30,8 @@ public:
     [[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);
 
@@ -40,13 +41,13 @@ protected:
     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;
 };
 
 }