Commit 08baca77 authored by Hiroki Nakagawa's avatar Hiroki Nakagawa Committed by Commit Bot

Worker: Throw an original Error on importScripts() failure

Before this CL, when a failure on importScripts() happens,
WorkerOrWorkletScriptController::RethrowExceptionFromImportedScript() creates a
new Error and throws it instead of an original Error. This fails web platform
tests:
https://wpt.fyi/results/workers/interfaces/WorkerUtils/importScripts/004.html?label=stable&aligned
https://wpt.fyi/results/workers/interfaces/WorkerUtils/importScripts/006.html?label=stable&aligned

This CL makes the function throw the original Error.

In addition, this CL changes ErrorEvent::CreateSanitizedError() to create a
sanitized error with a null error object instead of an empty error object. This
is because the HTML spec requires the error object for scripts whose errors are
muted must be null, and ExceptionState::RethrowV8Exception() called from
RethrowExceptionFromImportedScript() doesn't throw an exception when its error
object is empty.

Bug: 651778, 655458
Change-Id: I67a91879b4a9fa3fd521797394fe35de89634b7a
Reviewed-on: https://chromium-review.googlesource.com/c/1343465
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarHiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarKouhei Ueno <kouhei@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610011}
parent b1101c25
...@@ -3824,14 +3824,12 @@ crbug.com/655458 external/wpt/workers/interfaces/SharedWorkerGlobalScope/onconne ...@@ -3824,14 +3824,12 @@ crbug.com/655458 external/wpt/workers/interfaces/SharedWorkerGlobalScope/onconne
crbug.com/655458 external/wpt/workers/constructors/SharedWorker/setting-port-members.html [ Failure ] crbug.com/655458 external/wpt/workers/constructors/SharedWorker/setting-port-members.html [ Failure ]
crbug.com/655458 external/wpt/workers/constructors/SharedWorker/connect-event.html [ Failure ] crbug.com/655458 external/wpt/workers/constructors/SharedWorker/connect-event.html [ Failure ]
crbug.com/655458 external/wpt/workers/constructors/Worker/unresolvable-url.html [ Failure ] crbug.com/655458 external/wpt/workers/constructors/Worker/unresolvable-url.html [ Failure ]
crbug.com/655458 external/wpt/workers/interfaces/WorkerUtils/importScripts/004.html [ Failure ]
crbug.com/655458 external/wpt/workers/semantics/reporting-errors/002.html [ Failure ] crbug.com/655458 external/wpt/workers/semantics/reporting-errors/002.html [ Failure ]
crbug.com/655458 external/wpt/workers/semantics/run-a-worker/003.html [ Failure ] crbug.com/655458 external/wpt/workers/semantics/run-a-worker/003.html [ Failure ]
crbug.com/655458 external/wpt/workers/Worker_terminate_event_queue.htm [ Pass Timeout ] crbug.com/655458 external/wpt/workers/Worker_terminate_event_queue.htm [ Pass Timeout ]
crbug.com/655458 external/wpt/workers/interfaces/WorkerUtils/navigator/language.html [ Failure ] crbug.com/655458 external/wpt/workers/interfaces/WorkerUtils/navigator/language.html [ Failure ]
crbug.com/655458 external/wpt/workers/constructors/SharedWorker/interface-objects.html [ Failure ] crbug.com/655458 external/wpt/workers/constructors/SharedWorker/interface-objects.html [ Failure ]
crbug.com/655458 external/wpt/workers/constructors/SharedWorker/unresolvable-url.html [ Failure ] crbug.com/655458 external/wpt/workers/constructors/SharedWorker/unresolvable-url.html [ Failure ]
crbug.com/655458 external/wpt/workers/interfaces/WorkerUtils/importScripts/006.html [ Failure Timeout ]
crbug.com/655458 external/wpt/workers/semantics/multiple-workers/007.html [ Timeout ] crbug.com/655458 external/wpt/workers/semantics/multiple-workers/007.html [ Timeout ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/undefined-arguments.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/undefined-arguments.html [ Failure ]
...@@ -3839,14 +3837,12 @@ crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/worker ...@@ -3839,14 +3837,12 @@ crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/worker
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/setting-port-members.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/setting-port-members.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/connect-event.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/connect-event.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/Worker/unresolvable-url.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/Worker/unresolvable-url.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/interfaces/WorkerUtils/importScripts/004.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/semantics/reporting-errors/002.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/semantics/reporting-errors/002.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/semantics/run-a-worker/003.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/semantics/run-a-worker/003.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/Worker_terminate_event_queue.htm [ Pass Timeout ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/Worker_terminate_event_queue.htm [ Pass Timeout ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/interfaces/WorkerUtils/navigator/language.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/interfaces/WorkerUtils/navigator/language.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/interface-objects.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/interface-objects.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/unresolvable-url.html [ Failure ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/constructors/SharedWorker/unresolvable-url.html [ Failure ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/interfaces/WorkerUtils/importScripts/006.html [ Failure Timeout ]
crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/semantics/multiple-workers/007.html [ Timeout ] crbug.com/655458 virtual/off-main-thread-worker-script-fetch/external/wpt/workers/semantics/multiple-workers/007.html [ Timeout ]
# Off-the-main-thread classic worker script fetch. # Off-the-main-thread classic worker script fetch.
......
...@@ -27,18 +27,18 @@ Loaded resource 1 ...@@ -27,18 +27,18 @@ Loaded resource 1
PASS: Threw NetworkError: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://127.0.0.1:8000/workers/resources/nonexistant' failed to load. when load failed PASS: Threw NetworkError: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://127.0.0.1:8000/workers/resources/nonexistant' failed to load. when load failed
PASS: some resources were loaded despite the network error PASS: some resources were loaded despite the network error
Loaded resource 1 Loaded resource 1
PASS: Threw Error: Uncaught SyntaxError: Unexpected identifier when encountering a syntax error in imported script PASS: Threw SyntaxError: Unexpected identifier when encountering a syntax error in imported script
PASS: some resources were loaded despite the presence of a syntax error PASS: some resources were loaded despite the presence of a syntax error
Testing multiple arguments, with first resource throwing an exception: Testing multiple arguments, with first resource throwing an exception:
Loaded resource 1 Loaded resource 1
First resource throwing an exception First resource throwing an exception
PASS: Propagated 'Error: Uncaught Thrown by first resource' from script PASS: Propagated 'Thrown by first resource' from script
PASS: First resource was executed, and second resource was not PASS: First resource was executed, and second resource was not
Testing multiple arguments, with second resource throwing an exception: Testing multiple arguments, with second resource throwing an exception:
Loaded resource 1 Loaded resource 1
Loaded resource 2 Loaded resource 2
Second resource throwing an exception Second resource throwing an exception
PASS: Propagated 'Error: Uncaught Thrown by second resource' from script PASS: Propagated 'Thrown by second resource' from script
PASS: Both scripts were executed PASS: Both scripts were executed
Testing multiple arguments, with second argument throwing in toString(): Testing multiple arguments, with second argument throwing in toString():
PASS: User error recieved in toString PASS: User error recieved in toString
......
...@@ -27,18 +27,18 @@ Loaded resource 1 ...@@ -27,18 +27,18 @@ Loaded resource 1
PASS: Threw NetworkError: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://127.0.0.1:8000/workers/resources/nonexistant' failed to load. when load failed PASS: Threw NetworkError: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://127.0.0.1:8000/workers/resources/nonexistant' failed to load. when load failed
PASS: some resources were loaded despite the network error PASS: some resources were loaded despite the network error
Loaded resource 1 Loaded resource 1
PASS: Threw Error: Uncaught SyntaxError: Unexpected identifier when encountering a syntax error in imported script PASS: Threw SyntaxError: Unexpected identifier when encountering a syntax error in imported script
PASS: some resources were loaded despite the presence of a syntax error PASS: some resources were loaded despite the presence of a syntax error
Testing multiple arguments, with first resource throwing an exception: Testing multiple arguments, with first resource throwing an exception:
Loaded resource 1 Loaded resource 1
First resource throwing an exception First resource throwing an exception
PASS: Propagated 'Error: Uncaught Thrown by first resource' from script PASS: Propagated 'Thrown by first resource' from script
PASS: First resource was executed, and second resource was not PASS: First resource was executed, and second resource was not
Testing multiple arguments, with second resource throwing an exception: Testing multiple arguments, with second resource throwing an exception:
Loaded resource 1 Loaded resource 1
Loaded resource 2 Loaded resource 2
Second resource throwing an exception Second resource throwing an exception
PASS: Propagated 'Error: Uncaught Thrown by second resource' from script PASS: Propagated 'Thrown by second resource' from script
PASS: Both scripts were executed PASS: Both scripts were executed
Testing multiple arguments, with second argument throwing in toString(): Testing multiple arguments, with second argument throwing in toString():
PASS: User error recieved in toString PASS: User error recieved in toString
......
...@@ -755,6 +755,18 @@ v8::Local<v8::Context> ToV8ContextEvenIfDetached(LocalFrame* frame, ...@@ -755,6 +755,18 @@ v8::Local<v8::Context> ToV8ContextEvenIfDetached(LocalFrame* frame,
return frame->WindowProxy(world)->ContextIfInitialized(); return frame->WindowProxy(world)->ContextIfInitialized();
} }
ScriptState* ToScriptState(ExecutionContext* context, DOMWrapperWorld& world) {
DCHECK(context);
if (auto* document = DynamicTo<Document>(context)) {
if (LocalFrame* frame = document->GetFrame())
return ToScriptState(frame, world);
} else if (auto* scope = DynamicTo<WorkerOrWorkletGlobalScope>(context)) {
if (WorkerOrWorkletScriptController* script = scope->ScriptController())
return script->GetScriptState();
}
return nullptr;
}
ScriptState* ToScriptState(LocalFrame* frame, DOMWrapperWorld& world) { ScriptState* ToScriptState(LocalFrame* frame, DOMWrapperWorld& world) {
v8::HandleScope handle_scope(ToIsolate(frame)); v8::HandleScope handle_scope(ToIsolate(frame));
return ToScriptStateImpl(frame, world); return ToScriptStateImpl(frame, world);
......
...@@ -466,6 +466,7 @@ CORE_EXPORT v8::Local<v8::Context> ToV8ContextEvenIfDetached(LocalFrame*, ...@@ -466,6 +466,7 @@ CORE_EXPORT v8::Local<v8::Context> ToV8ContextEvenIfDetached(LocalFrame*,
// These methods can return nullptr if the context associated with the // These methods can return nullptr if the context associated with the
// ScriptState has already been detached. // ScriptState has already been detached.
CORE_EXPORT ScriptState* ToScriptState(ExecutionContext*, DOMWrapperWorld&);
CORE_EXPORT ScriptState* ToScriptState(LocalFrame*, DOMWrapperWorld&); CORE_EXPORT ScriptState* ToScriptState(LocalFrame*, DOMWrapperWorld&);
// Do not use this method unless you are sure you should use the main world's // Do not use this method unless you are sure you should use the main world's
// ScriptState // ScriptState
......
...@@ -332,7 +332,7 @@ bool WorkerOrWorkletScriptController::Evaluate( ...@@ -332,7 +332,7 @@ bool WorkerOrWorkletScriptController::Evaluate(
return false; return false;
} }
if (sanitize_script_errors == SanitizeScriptErrors::kSanitize) { if (sanitize_script_errors == SanitizeScriptErrors::kSanitize) {
*error_event = ErrorEvent::CreateSanitizedError(world_.get()); *error_event = ErrorEvent::CreateSanitizedError(script_state_);
} else { } else {
*error_event = *error_event =
ErrorEvent::Create(state.error_message, state.location_->Clone(), ErrorEvent::Create(state.error_message, state.location_->Clone(),
...@@ -379,11 +379,10 @@ void WorkerOrWorkletScriptController::DisableEval(const String& error_message) { ...@@ -379,11 +379,10 @@ void WorkerOrWorkletScriptController::DisableEval(const String& error_message) {
void WorkerOrWorkletScriptController::RethrowExceptionFromImportedScript( void WorkerOrWorkletScriptController::RethrowExceptionFromImportedScript(
ErrorEvent* error_event, ErrorEvent* error_event,
ExceptionState& exception_state) { ExceptionState& exception_state) {
const String& error_message = error_event->message();
if (execution_state_) if (execution_state_)
execution_state_->error_event_from_imported_script_ = error_event; execution_state_->error_event_from_imported_script_ = error_event;
exception_state.RethrowV8Exception( exception_state.RethrowV8Exception(
V8ThrowException::CreateError(isolate_, error_message)); error_event->error(script_state_).V8ValueFor(script_state_));
} }
void WorkerOrWorkletScriptController::Trace(blink::Visitor* visitor) { void WorkerOrWorkletScriptController::Trace(blink::Visitor* visitor) {
......
...@@ -38,6 +38,16 @@ ...@@ -38,6 +38,16 @@
namespace blink { namespace blink {
ErrorEvent* ErrorEvent::CreateSanitizedError(ScriptState* script_state) {
// "6. If script's muted errors is true, then set message to "Script error.",
// urlString to the empty string, line and col to 0, and errorValue to null."
// https://html.spec.whatwg.org/multipage/webappapis.html#runtime-script-errors:muted-errors
DCHECK(script_state);
return MakeGarbageCollected<ErrorEvent>(
"Script error.", SourceLocation::Create(String(), 0, 0, nullptr),
ScriptValue::CreateNull(script_state), &script_state->World());
}
ErrorEvent::ErrorEvent() ErrorEvent::ErrorEvent()
: sanitized_message_(), : sanitized_message_(),
location_(SourceLocation::Create(String(), 0, 0, nullptr)), location_(SourceLocation::Create(String(), 0, 0, nullptr)),
......
...@@ -67,11 +67,9 @@ class ErrorEvent final : public Event { ...@@ -67,11 +67,9 @@ class ErrorEvent final : public Event {
const ErrorEventInit* initializer) { const ErrorEventInit* initializer) {
return MakeGarbageCollected<ErrorEvent>(script_state, type, initializer); return MakeGarbageCollected<ErrorEvent>(script_state, type, initializer);
} }
static ErrorEvent* CreateSanitizedError(DOMWrapperWorld* world) {
return MakeGarbageCollected<ErrorEvent>( // Creates an error for a script whose errors are muted.
"Script error.", SourceLocation::Create(String(), 0, 0, nullptr), static ErrorEvent* CreateSanitizedError(ScriptState* script_state);
ScriptValue(), world);
}
ErrorEvent(); ErrorEvent();
ErrorEvent(const String& message, ErrorEvent(const String& message,
......
...@@ -137,8 +137,10 @@ bool ExecutionContext::DispatchErrorEventInternal( ...@@ -137,8 +137,10 @@ bool ExecutionContext::DispatchErrorEventInternal(
if (!target) if (!target)
return false; return false;
if (sanitize_script_errors == SanitizeScriptErrors::kSanitize) if (sanitize_script_errors == SanitizeScriptErrors::kSanitize) {
error_event = ErrorEvent::CreateSanitizedError(error_event->World()); error_event = ErrorEvent::CreateSanitizedError(
ToScriptState(this, *error_event->World()));
}
DCHECK(!in_dispatch_error_event_); DCHECK(!in_dispatch_error_event_);
in_dispatch_error_event_ = true; in_dispatch_error_event_ = true;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment