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-03-01 20:49:07 +00:00
Родитель 51c498a4b9
Коммит 4b9df6ba3b
9 изменённых файлов: 197 добавлений и 12 удалений

Просмотреть файл

@ -328,8 +328,7 @@ class Tab extends TabBase {
}
get cookieStoreId() {
// Expose the same session context ID that the GeckoView app is sending to us.
return this.window.moduleManager.settings.unsafeSessionContextId;
return getCookieStoreIdForTab(this, this.nativeTab);
}
get height() {

Просмотреть файл

@ -331,6 +331,15 @@ this.tabs = class extends ExtensionAPI {
}
}
if (cookieStoreId) {
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: {

Просмотреть файл

@ -14,6 +14,9 @@
"use strict";
add_task(async function() {
await SpecialPowers.pushPrefEnv({
set: [["privacy.userContext.enabled", true]],
});
const extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
manifest: {
@ -62,20 +65,12 @@ add_task(async function() {
},
{
create: {cookieStoreId: null},
result: {cookieStoreId: null},
result: {cookieStoreId: "firefox-default"},
},
{
create: {cookieStoreId: "firefox-container-1"},
result: {cookieStoreId: "firefox-container-1"},
},
{
create: {cookieStoreId: "😅"},
result: {cookieStoreId: "😅"},
},
{
create: {cookieStoreId: "漢字"},
result: {cookieStoreId: "漢字"},
},
];
async function nextTest() {
@ -149,6 +144,7 @@ add_task(async function() {
await extension.startup();
await extension.awaitFinish("tabs.create");
await extension.unload();
await SpecialPowers.popPrefEnv();
});
</script>

Просмотреть файл

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

Просмотреть файл

@ -624,6 +624,7 @@ class WebExtensionTest : BaseSessionTest() {
// - Verify that request came from right extension
@Test
fun testBrowserTabsCreateWithCookieStoreId() {
sessionRule.setPrefsUntilTestEnd(mapOf("privacy.userContext.enabled" to true));
val tabsCreateResult = GeckoResult<Void>()
var tabsExtension: WebExtension? = null
val tabDelegate = object : WebExtension.TabDelegate {

Просмотреть файл

@ -38,6 +38,11 @@ class Tab {
getActive() {
return this.active;
}
get userContextId() {
return this.browser.ownerGlobal.moduleManager.settings
.unsafeSessionContextId;
}
}
// Because of bug 1410749, we can't use 0, though, and just to be safe

Просмотреть файл

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

Просмотреть файл

@ -163,6 +163,7 @@ skip-if =
skip-if = os == 'android' || verify # bug 1489771
[test_ext_tabs_captureTab.html]
[test_ext_tabs_executeScript_good.html]
[test_ext_tabs_create_cookieStoreId.html]
[test_ext_tabs_query_popup.html]
[test_ext_tabs_permissions.html]
[test_ext_tabs_sendMessage.html]

Просмотреть файл

@ -0,0 +1,169 @@
<!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({
useAddonManager: "permanent",
manifest: {
permissions: ["tabs", "cookies"],
applications: { gecko: { id: "cookiestoreid@tests.mozilla.org" } },
},
background: `(${background})(${JSON.stringify(testCases)})`,
});
await extension.startup();
await extension.awaitMessage("done");
await extension.unload();
await SpecialPowers.popPrefEnv();
});
</script>
</body>
</html>