Bug 1643736 - Adding appropriate check before passing cookieStoreId to GeckoViewTabBridge.createNewTab() r=robwu,agi

Differential Revision: https://phabricator.services.mozilla.com/D100151
This commit is contained in:
ankushduacodes 2021-01-14 16:57:45 +00:00
parent 31ff3b6a71
commit 066250f7d6
5 changed files with 182 additions and 2 deletions

View File

@ -331,6 +331,13 @@ this.tabs = class extends ExtensionAPI {
}
}
cookieStoreId = getUserContextIdForCookieStoreId(
extension,
cookieStoreId,
false // TODO bug 1372178: support creation of private browsing tabs
);
cookieStoreId = cookieStoreId ? cookieStoreId.toString() : undefined;
const nativeTab = await GeckoViewTabBridge.createNewTab({
extensionId: context.extension.id,
createProperties: {

View File

@ -1,4 +1,4 @@
browser.tabs.create({
url: "https://www.mozilla.org/en-US/",
cookieStoreId: "1",
cookieStoreId: "firefox-container-1",
});

View File

@ -84,6 +84,11 @@ global.getContainerForCookieStoreId = function(storeId) {
}
let containerId = storeId.substring(CONTAINER_STORE.length);
if (AppConstants.platform === "android") {
return parseInt(containerId, 10);
} // TODO: Bug 1643740, support ContextualIdentityService on Android
if (ContextualIdentityService.getPublicIdentityFromId(containerId)) {
return parseInt(containerId, 10);
}

View File

@ -152,13 +152,14 @@ skip-if = xorigin # JavaScript Error: "SecurityError: Permission denied to acces
scheme=https
[test_ext_storage_smoke_test.html]
[test_ext_streamfilter_multiple.html]
skip-if =
skip-if =
!debug # Bug 1628642
os == 'linux' # Bug 1628642
[test_ext_streamfilter_processswitch.html]
[test_ext_subframes_privileges.html]
skip-if = os == 'android' || verify # bug 1489771
[test_ext_tabs_captureTab.html]
[test_ext_tabs_create_cookieStoreId.html]
[test_ext_tabs_query_popup.html]
[test_ext_tabs_sendMessage.html]
[test_ext_test.html]

View File

@ -0,0 +1,167 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test tabs.create(cookieStoreId)</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
add_task(async function no_cookies_permission() {
await SpecialPowers.pushPrefEnv({
set: [["privacy.userContext.enabled", true]],
});
let extension = ExtensionTestUtils.loadExtension({
async background() {
await browser.test.assertRejects(
browser.tabs.create({ cookieStoreId: "firefox-container-1" }),
/No permission for cookieStoreId/,
"cookieStoreId requires cookies permission"
);
browser.test.sendMessage("done");
},
});
await extension.startup();
await extension.awaitMessage("done");
await extension.unload();
await SpecialPowers.popPrefEnv();
});
add_task(async function invalid_cookieStoreId() {
await SpecialPowers.pushPrefEnv({
set: [["privacy.userContext.enabled", true]],
});
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["tabs", "cookies"],
},
async background() {
await browser.test.assertRejects(
browser.tabs.create({ cookieStoreId: "not-firefox-container-1" }),
/Illegal cookieStoreId/,
"cookieStoreId must be valid"
);
await browser.test.assertRejects(
browser.tabs.create({ cookieStoreId: "firefox-private" }),
/Illegal to set private cookieStoreId in a non-private window/,
"cookieStoreId cannot be private in a non-private window"
);
browser.test.sendMessage("done");
},
});
await extension.startup();
await extension.awaitMessage("done");
await extension.unload();
await SpecialPowers.popPrefEnv();
});
add_task(async function perma_private_browsing_mode() {
await SpecialPowers.pushPrefEnv({
set: [["browser.privatebrowsing.autostart", true]],
});
let extension = ExtensionTestUtils.loadExtension({
incognitoOverride: "spanning",
manifest: {
permissions: ["tabs", "cookies"],
},
async background() {
await browser.test.assertRejects(
browser.tabs.create({ cookieStoreId: "firefox-container-1" }),
/Contextual identities are unavailable in permanent private browsing mode/,
"cookieStoreId cannot be a container tab ID in perma-private browsing mode"
);
browser.test.sendMessage("done");
},
});
await extension.startup();
await extension.awaitMessage("done");
await extension.unload();
await SpecialPowers.popPrefEnv();
});
add_task(async function userContext_disabled() {
await SpecialPowers.pushPrefEnv({
set: [["privacy.userContext.enabled", false]],
});
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["tabs", "cookies"],
},
async background() {
await browser.test.assertRejects(
browser.tabs.create({ cookieStoreId: "firefox-container-1" }),
/Contextual identities are currently disabled/,
"cookieStoreId cannot be a container tab ID when contextual identities are disabled"
);
browser.test.sendMessage("done");
},
});
await extension.startup();
await extension.awaitMessage("done");
await extension.unload();
await SpecialPowers.popPrefEnv();
});
add_task(async function valid_cookieStoreId() {
await SpecialPowers.pushPrefEnv({
set: [["privacy.userContext.enabled", true]],
});
const testCases = [
{
description: "no explicit URL",
createProperties: {
cookieStoreId: "firefox-container-1",
},
expectedCookieStoreId: "firefox-container-1",
},
{
description: "pass explicit url",
createProperties: {
url: "about:blank",
cookieStoreId: "firefox-container-1",
},
expectedCookieStoreId: "firefox-container-1",
},
];
async function background(testCases) {
for (let { createProperties, expectedCookieStoreId } of testCases) {
let tab = await browser.tabs.create(createProperties);
browser.test.assertEq(
expectedCookieStoreId,
tab.cookieStoreId,
"Expected cookieStoreId for container tab"
);
await browser.tabs.remove(tab.id);
}
browser.test.sendMessage("done");
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["tabs", "cookies"],
},
background: `(${background})(${JSON.stringify(testCases)})`,
});
await extension.startup();
await extension.awaitMessage("done");
await extension.unload();
await SpecialPowers.popPrefEnv();
});
</script>
</body>
</html>