Bug 1345474 - Check incognito access for webNavigation api, r=rpl

Differential Revision: https://phabricator.services.mozilla.com/D12337

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Shane Caraveo 2018-12-12 14:34:19 +00:00
Родитель 0758a6a1dc
Коммит 2f65e17f02
4 изменённых файлов: 118 добавлений и 4 удалений

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

@ -145,7 +145,7 @@ class WebNavigationEventManager extends EventManager {
fire.async(data2);
};
WebNavigation[eventName].addListener(listener, filters);
WebNavigation[eventName].addListener(listener, filters, context);
return () => {
WebNavigation[eventName].removeListener(listener);
};

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

@ -125,6 +125,8 @@ skip-if = os == 'android' && debug # bug 1397615
skip-if = os == 'android' && debug # bug 1397615
[test_ext_webnavigation_filters.html]
skip-if = (os == 'android' && debug) || (verify && (os == 'linux' || os == 'mac')) # bug 1397615
[test_ext_webnavigation_incognito.html]
skip-if = os == 'android' # bug 1513544
[test_ext_webrequest_auth.html]
skip-if = os == 'android'
[test_ext_webrequest_background_events.html]

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

@ -0,0 +1,106 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for simple WebExtension</title>
<meta charset="utf-8">
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/AddTask.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.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 webnav_test_incognito() {
// Monitor will fail if it gets any event.
let monitor = ExtensionTestUtils.loadExtension({
manifest: {
"permissions": ["webNavigation", "*://mochi.test/*"],
},
incognitoOverride: "not_allowed",
background() {
const EVENTS = [
"onTabReplaced",
"onBeforeNavigate",
"onCommitted",
"onDOMContentLoaded",
"onCompleted",
"onErrorOccurred",
"onReferenceFragmentUpdated",
"onHistoryStateUpdated",
];
function onEvent(event, details) {
browser.test.fail(`not_allowed - Got ${event} ${details.url} ${details.frameId} ${details.parentFrameId}`);
}
let listeners = {};
for (let event of EVENTS) {
listeners[event] = onEvent.bind(null, event);
browser.webNavigation[event].addListener(listeners[event]);
}
browser.test.onMessage.addListener(async (message, tabId) => {
// try to access the private window
await browser.test.assertRejects(browser.webNavigation.getAllFrames({tabId}),
/Invalid tab ID/,
"should not be able to get incognito frames");
await browser.test.assertRejects(browser.webNavigation.getFrame({tabId, frameId: 0}),
/Invalid tab ID/,
"should not be able to get incognito frames");
browser.test.notifyPass("completed");
});
},
});
// extension loads a private window and waits for the onCompleted event.
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["tabs", "webNavigation", "*://mochi.test/*"],
},
async background() {
const BASE = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest";
const url = BASE + "/file_WebNavigation_page1.html";
let window;
browser.webNavigation.onCompleted.addListener(async (details) => {
if (details.url !== url) {
return;
}
browser.test.log(`spanning - Got onCompleted ${details.url} ${details.frameId} ${details.parentFrameId}`);
browser.test.sendMessage("completed");
});
browser.test.onMessage.addListener(async () => {
await browser.windows.remove(window.id);
browser.test.notifyPass("done");
});
window = await browser.windows.create({url, incognito: true});
let tabs = await browser.tabs.query({active: true, windowId: window.id});
browser.test.sendMessage("tabId", tabs[0].id);
},
});
await monitor.startup();
await extension.startup();
await extension.awaitMessage("completed");
let tabId = await extension.awaitMessage("tabId");
await monitor.sendMessage("tab", tabId);
await monitor.awaitFinish("completed");
await extension.sendMessage("close");
await extension.awaitFinish("done");
await extension.unload();
await monitor.unload();
});
</script>
</body>
</html>

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

@ -10,6 +10,8 @@ ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.defineModuleGetter(this, "BrowserWindowTracker",
"resource:///modules/BrowserWindowTracker.jsm");
ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm");
// Maximum amount of time that can be passed and still consider
// the data recent (similar to how is done in nsNavHistory,
@ -63,7 +65,7 @@ var Manager = {
this.createdNavigationTargetByOuterWindowId.clear();
},
addListener(type, listener, filters) {
addListener(type, listener, filters, context) {
if (this.listeners.size == 0) {
this.init();
}
@ -72,7 +74,7 @@ var Manager = {
this.listeners.set(type, new Map());
}
let listeners = this.listeners.get(type);
listeners.set(listener, filters);
listeners.set(listener, {filters, context});
},
removeListener(type, listener) {
@ -436,7 +438,11 @@ var Manager = {
details[prop] = extra[prop];
}
for (let [listener, filters] of listeners) {
for (let [listener, {filters, context}] of listeners) {
if (context && !context.privateBrowsingAllowed &&
PrivateBrowsingUtils.isBrowserPrivate(browser)) {
continue;
}
// Call the listener if the listener has no filter or if its filter matches.
if (!filters || filters.matches(extra.url)) {
listener(details);