Bug 1877596 - Use original URL as base URL for internal scheme also in worker. r=jonco,dom-storage-reviewers,janv,dom-worker-reviewers,smaug

Differential Revision: https://phabricator.services.mozilla.com/D200129
This commit is contained in:
Tooru Fujisawa 2024-02-05 01:12:24 +00:00
parent cd8a3ca314
commit 21b4e958e9
13 changed files with 127 additions and 37 deletions

View file

@ -156,6 +156,11 @@ dom/workers/test/importScripts_worker_imported3.js
dom/workers/test/invalid.js
dom/workers/test/threadErrors_worker1.js
# Tests the module loader's path handling.
# Dynamic imports contains non-optimal paths.
dom/workers/test/xpcshell/data/base_uri_worker.js
dom/workers/test/xpcshell/data/base_uri_module.mjs
# Bug 1527075: This directory is linted in github repository
intl/l10n/

View file

@ -10,25 +10,11 @@ async function ensureUtilsChild() {
return;
}
try {
const { UtilsChild: importedUtilsChild } = await import(
"/dom/quota/test/modules/worker/UtilsChild.js"
);
const { UtilsChild: importedUtilsChild } = await import(
"/dom/quota/test/modules/worker/UtilsChild.mjs"
);
UtilsChild = importedUtilsChild;
throw Error("Please switch to dynamic module import");
} catch (e) {
if (e.message == "Please switch to dynamic module import") {
throw e;
}
importScripts("/dom/quota/test/modules/worker/UtilsChild.js");
const { UtilsChild: importedUtilsChild } = globalThis.importUtilsChild();
UtilsChild = importedUtilsChild;
}
UtilsChild = importedUtilsChild;
}
const Utils = {

View file

@ -24,7 +24,7 @@ function _recvMessage() {
});
}
const _UtilsChild = {
export const UtilsChild = {
async getCachedOriginUsage() {
_sendMessage({
op: "getCachedOriginUsage",
@ -50,7 +50,3 @@ const _UtilsChild = {
return _recvMessage();
},
};
function importUtilsChild() {
return { UtilsChild: _UtilsChild };
}

View file

@ -82,5 +82,5 @@ TESTING_JS_MODULES.dom.quota.test.modules.worker += [
"modules/system/worker/head.js",
"modules/system/worker/ModuleLoader.js",
"modules/system/worker/Utils.js",
"modules/system/worker/UtilsChild.js",
"modules/system/worker/UtilsChild.mjs",
]

View file

@ -3675,11 +3675,6 @@ int32_t ScriptLoader::PhysicalSizeOfMemoryInGB() {
return mPhysicalSizeOfMemory;
}
static bool IsInternalURIScheme(nsIURI* uri) {
return uri->SchemeIs("moz-extension") || uri->SchemeIs("resource") ||
uri->SchemeIs("chrome");
}
bool ScriptLoader::ShouldApplyDelazifyStrategy(ScriptLoadRequest* aRequest) {
// Full parse everything if negative.
if (StaticPrefs::dom_script_loader_delazification_max_size() < 0) {
@ -3866,13 +3861,7 @@ nsresult ScriptLoader::PrepareLoadedRequest(ScriptLoadRequest* aRequest,
rv = channel->GetOriginalURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
// Fixup moz-extension: and resource: URIs, because the channel URI will
// point to file:, which won't be allowed to load.
if (uri && IsInternalURIScheme(uri)) {
aRequest->mBaseURL = uri;
} else {
channel->GetURI(getter_AddRefs(aRequest->mBaseURL));
}
aRequest->SetBaseURLFromChannelAndOriginalURI(channel, uri);
if (aRequest->IsModuleRequest()) {
ModuleLoadRequest* request = aRequest->AsModuleRequest();

View file

@ -188,7 +188,7 @@ nsresult NetworkLoadHandler::DataReceivedFromNetwork(nsIStreamLoader* aLoader,
rv = channel->GetOriginalURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
channel->GetURI(getter_AddRefs(loadContext->mRequest->mBaseURL));
loadContext->mRequest->SetBaseURLFromChannelAndOriginalURI(channel, uri);
// Figure out what we actually loaded.
nsCOMPtr<nsIURI> finalURI;

View file

@ -0,0 +1,23 @@
// This file is for testing the module loader's path handling.
// ESLint rules that modifies path shouldn't be applied.
export const obj = {};
export async function doImport() {
// This file is loaded as resource://test/data/base_uri_module.mjs
// Relative/absolute paths should be resolved based on the URI, instead of
// file: path.
const namespaceWithURI = await import(
"resource://test/data/base_uri_module2.mjs"
);
const namespaceWithCurrentDir = await import("./base_uri_module2.mjs");
const namespaceWithParentDir = await import("../data/base_uri_module2.mjs");
const namespaceWithAbsoluteDir = await import("/data/base_uri_module2.mjs");
return {
equal1: namespaceWithURI.obj2 == namespaceWithCurrentDir.obj2,
equal2: namespaceWithURI.obj2 == namespaceWithParentDir.obj2,
equal3: namespaceWithURI.obj2 == namespaceWithAbsoluteDir.obj2,
};
}

View file

@ -0,0 +1 @@
export const obj2 = {};

View file

@ -0,0 +1,27 @@
// This file is for testing the module loader's path handling.
// ESLint rules that modifies path shouldn't be applied.
onmessage = async event => {
// This file is loaded as resource://test/data/base_uri_worker.js
// Relative/absolute paths should be resolved based on the URI, instead of
// file: path.
const namespaceWithURI = await import(
"resource://test/data/base_uri_module.mjs"
);
const namespaceWithCurrentDir = await import("./base_uri_module.mjs");
const namespaceWithParentDir = await import("../data/base_uri_module.mjs");
const namespaceWithAbsoluteDir = await import("/data/base_uri_module.mjs");
postMessage({
scriptToModule: {
equal1: namespaceWithURI.obj == namespaceWithCurrentDir.obj,
equal2: namespaceWithURI.obj == namespaceWithParentDir.obj,
equal3: namespaceWithURI.obj == namespaceWithAbsoluteDir.obj,
},
moduleToModuleURI: await namespaceWithURI.doImport(),
moduleToModuleCurrent: await namespaceWithCurrentDir.doImport(),
moduleToModuleParent: await namespaceWithParentDir.doImport(),
moduleToModuleAbsolute: await namespaceWithAbsoluteDir.doImport(),
});
};

View file

@ -0,0 +1,35 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
add_task(async function testSyncImportBeforeAsyncImportDependencyInWorker() {
const worker = new ChromeWorker("resource://test/data/base_uri_worker.js");
const { promise, resolve } = Promise.withResolvers();
worker.onmessage = event => {
resolve(event.data);
};
worker.postMessage("");
const result = await promise;
Assert.ok(result.scriptToModule.equal1);
Assert.ok(result.scriptToModule.equal2);
Assert.ok(result.scriptToModule.equal3);
Assert.ok(result.moduleToModuleURI.equal1);
Assert.ok(result.moduleToModuleURI.equal2);
Assert.ok(result.moduleToModuleURI.equal3);
Assert.ok(result.moduleToModuleCurrent.equal1);
Assert.ok(result.moduleToModuleCurrent.equal2);
Assert.ok(result.moduleToModuleCurrent.equal3);
Assert.ok(result.moduleToModuleParent.equal1);
Assert.ok(result.moduleToModuleParent.equal2);
Assert.ok(result.moduleToModuleParent.equal3);
Assert.ok(result.moduleToModuleAbsolute.equal1);
Assert.ok(result.moduleToModuleAbsolute.equal2);
Assert.ok(result.moduleToModuleAbsolute.equal3);
});

View file

@ -4,6 +4,9 @@ support-files = [
"data/worker.js",
"data/worker_fileReader.js",
"data/chrome.manifest",
"data/base_uri_worker.js",
"data/base_uri_module.mjs",
"data/base_uri_module2.mjs",
]
["test_ext_redirects_sw_scripts.js"]
@ -18,6 +21,8 @@ firefox-appdir = "browser"
["test_fileReader.js"]
["test_import_base_uri.js"]
["test_remoteworker_launch_new_process.js"]
# The following firefox-appdir make sure that this xpcshell test will run
# with e10s enabled (which is needed to make sure that the test case is

View file

@ -197,6 +197,22 @@ bool ScriptLoadRequest::IsMarkedForBytecodeEncoding() const {
return !!mScriptForBytecodeEncoding;
}
static bool IsInternalURIScheme(nsIURI* uri) {
return uri->SchemeIs("moz-extension") || uri->SchemeIs("resource") ||
uri->SchemeIs("chrome");
}
void ScriptLoadRequest::SetBaseURLFromChannelAndOriginalURI(
nsIChannel* aChannel, nsIURI* aOriginalURI) {
// Fixup moz-extension: and resource: URIs, because the channel URI will
// point to file:, which won't be allowed to load.
if (aOriginalURI && IsInternalURIScheme(aOriginalURI)) {
mBaseURL = aOriginalURI;
} else {
aChannel->GetURI(getter_AddRefs(mBaseURL));
}
}
//////////////////////////////////////////////////////////////
// ScriptLoadRequestList
//////////////////////////////////////////////////////////////

View file

@ -203,6 +203,13 @@ class ScriptLoadRequest : public nsISupports,
const LoadedScript* getLoadedScript() const { return mLoadedScript.get(); }
LoadedScript* getLoadedScript() { return mLoadedScript.get(); }
/*
* Set the request's mBaseURL, based on aChannel.
* aOriginalURI is the result of aChannel->GetOriginalURI.
*/
void SetBaseURLFromChannelAndOriginalURI(nsIChannel* aChannel,
nsIURI* aOriginalURI);
const ScriptKind mKind; // Whether this is a classic script or a module
// script.