bug 1237822: Throw error if tabs.query is used without "tabs" permission with url param r?kmag
MozReview-Commit-ID: 4WpawIYcwnl
--- a/browser/components/extensions/ext-tabs.js
+++ b/browser/components/extensions/ext-tabs.js
@@ -548,16 +548,20 @@ extensions.registerSchemaAPI("tabs", nul
}
return self.tabs.query({windowId});
},
query: function(queryInfo) {
let pattern = null;
if (queryInfo.url !== null) {
+ if (!extension.hasPermission("tabs")) {
+ return Promise.reject({message: 'The "tabs" permission is required to use the query API with the "url" parameter'});
+ }
+
pattern = new MatchPattern(queryInfo.url);
}
function matches(window, tab) {
let props = ["active", "pinned", "highlighted", "status", "title", "index"];
for (let prop of props) {
if (queryInfo[prop] !== null && queryInfo[prop] != tab[prop]) {
return false;
--- a/browser/components/extensions/test/browser/browser_ext_tabs_query.js
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_query.js
@@ -131,8 +131,55 @@ add_task(function* () {
yield extension.startup();
yield extension.awaitFinish("tabs.query");
yield extension.unload();
yield BrowserTestUtils.removeTab(tab1);
yield BrowserTestUtils.removeTab(tab2);
yield BrowserTestUtils.removeTab(tab3);
});
+
+add_task(function* testQueryPermissions() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": [],
+ },
+
+ background: function(x) {
+ browser.tabs.query({currentWindow: true, active: true}).then((tabs) => {
+ browser.test.assertEq(tabs.length, 1, "Expect query to return tabs");
+ browser.test.notifyPass("queryPermissions");
+ }).catch((e) => {
+ browser.test.notifyFail("queryPermissions");
+ });
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("queryPermissions");
+
+ yield extension.unload();
+});
+
+add_task(function* testQueryWithURLPermissions() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": [],
+ },
+
+ background: function(x) {
+ browser.tabs.query({"url": "http://www.bbc.com/"}).then(() => {
+ browser.test.notifyFail("queryWithURLPermissions");
+ }).catch((e) => {
+ browser.test.assertEq('The "tabs" permission is required to use the query API with the "url" parameter',
+ e.message, "Expected permissions error message");
+ browser.test.notifyPass("queryWithURLPermissions");
+ });
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("queryWithURLPermissions");
+
+ yield extension.unload();
+});