Skip to content
Permalink
Browse files

Disable prompts to save logins while on extension pages (#74)

- Add getLoginSavingEnabled and setLoginSavingEnabled methods to the
  Logins experiment API

- Disable login saving for extension pages on background startup

Fixes #57
  • Loading branch information...
lmorchard committed Feb 11, 2019
1 parent 29c1fd9 commit 10feb59a9194d400ccd49b4a9b548dd81b4ba972
@@ -0,0 +1,14 @@
/* 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/. */

export async function initializeEnvironment() {
// Issue #57: Disable login saving for extension pages.
const origin = browser.extension.getURL("").slice(0, -1);
const savingEnabled = await browser.experiments.logins
.getLoginSavingEnabled(origin);
if (savingEnabled) {
await browser.experiments.logins
.setLoginSavingEnabled(origin, false);
}
}
@@ -8,8 +8,10 @@ import updateBrowserAction from "./browser-action";
import initializeMessagePorts from "./message-ports";
import { initializeTelemetry } from "./telemetry";
import { initializeDataStore } from "./datastore";
import { initializeEnvironment } from "./environment";

Promise.resolve().then(async () => {
await initializeEnvironment();
initializeTelemetry();
initializeMessagePorts();
await initializeDataStore();
@@ -49,6 +49,20 @@ this.logins = class extends ExtensionAPI {
// - fooLogin = login as vanilla JS object
// - fooLoginInfo = login as nsILoginInfo
// - fooBag = (subset of) login fields as nsIPropertyBag
getLoginSavingEnabled(origin) {
try {
return Services.logins.getLoginSavingEnabled(origin);
} catch (ex) {
throw new ExtensionError(ex);
}
},
setLoginSavingEnabled(origin, isEnabled) {
try {
return Services.logins.setLoginSavingEnabled(origin, isEnabled);
} catch (ex) {
throw new ExtensionError(ex);
}
},
getAll() {
const logins = getLogins();
return logins;
@@ -199,6 +199,32 @@
}
],
"functions": [
{
"name": "getLoginSavingEnabled",
"type": "function",
"description": "Reports whether or not saving login information is enabled for a host.",
"async": true,
"parameters": [{
"name": "aHost",
"type": "string",
"description": "The origin for which to check the status of login saving"
}]
},
{
"name": "setLoginSavingEnabled",
"type": "function",
"description": "Enables or disables storing logins for a specified host. When login storing is disabled, the Login Manager won't prompt the user to store logins for that host. Existing logins are not affected.",
"async": true,
"parameters": [{
"name": "aHost",
"type": "string",
"description": "The origin for which to modify the login saving permission"
},{
"name": "isEnabled",
"type": "boolean",
"description": "The value to which to set login saving for the origin"
}]
},
{
"name": "getAll",
"type": "function",
@@ -46,6 +46,20 @@ describe("logins API", () => {
let By;
let until;

const enableRememberSignons = async () => {
await webext.inChrome();
await driver.executeScript(`
Services.prefs.setBoolPref("signon.rememberSignons", true);
`);
};

const disableRememberSignons = async () => {
await webext.inChrome();
await driver.executeScript(`
Services.prefs.setBoolPref("signon.rememberSignons", false);
`);
};

const addLogin = async () => {
await webext.inChrome();
await driver.executeScript(`
@@ -235,6 +249,45 @@ describe("logins API", () => {
expect(logins[0].timesUsed).to.equal(2);
});

describe("browser.experiments.logins.{set,get}LoginSavingEnabled", () => {
const commonTest = async (button, expected) => {
await loadTestPage();
await clickButton(button ? "enable-saving" : "disable-saving");
const results = await driver.wait(until.elementLocated(
By.id("saving-results")
), 1000);
await driver.wait(
until.elementTextContains(
results,
expected ? "true" : "false"
),
5000
);
};
describe("with pref signon.rememberSignons = true", () => {
before(async () => {
await enableRememberSignons();
});
after(async () => {
await disableRememberSignons();
});
it("disables login saving for origin when given false", async () => {
await commonTest(false, false);
});
it("enables login saving for origin when given true", async () => {
await commonTest(true, true);
});
});
describe("with pref signon.rememberSignons = false", () => {
before(async () => {
await disableRememberSignons();
});
it("does not enable login saving for origin when given true", async () => {
await commonTest(true, false);
});
});
});

describe("browser.experiments.logins.onAdded", () => {
it("should fire if the mock login is added", async () => {
await loadTestPage();
@@ -40,4 +40,10 @@ <h1>Logins API Test File</h1>
<button id="clear-register-listeners-results">Clear results</button>
<pre id="register-listeners-results"></pre>

<p>Click the Enable / Disable buttons to change login saving permission for
this extension</p>
<button id="enable-saving">Enable Saving</button>
<button id="disable-saving">Disable Saving</button>
<pre id="saving-results"></pre>

<script src="logins-api.js"></script>
@@ -76,3 +76,18 @@ document.querySelector("#clear-register-listeners-results").addEventListener("cl
}
log(events);
});

const changeSaving = val => {
const origin = window.location.origin;
const log = getLogger("saving");
browser.experiments.logins
.setLoginSavingEnabled(origin, val)
.then(() =>
browser.experiments.logins
.getLoginSavingEnabled(origin))
.then(log, log);
};
document.querySelector("#enable-saving")
.addEventListener("click", () => changeSaving(true));
document.querySelector("#disable-saving")
.addEventListener("click", () => changeSaving(false));
@@ -0,0 +1,39 @@
/* 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/. */

import { expect } from "chai";
import sinon from "sinon";

import "test/unit/mocks/browser";

import {
initializeEnvironment,
} from "src/background/environment";

describe("background > environment", () => {
it("disables login saving for extension pages", async () => {
sinon
.stub(browser.experiments.logins, "getLoginSavingEnabled")
.resolves(true);
sinon
.stub(browser.experiments.logins, "setLoginSavingEnabled")
.resolves(undefined);
sinon
.stub(browser.extension, "getURL")
.returns("moz-extension://8675309/");

await initializeEnvironment();

expect(browser.experiments.logins.getLoginSavingEnabled.called).to.be.true;
expect(browser.experiments.logins.setLoginSavingEnabled.called).to.be.true;

const args = browser.experiments.logins.setLoginSavingEnabled.firstCall.args;
expect(args[0]).to.equal("moz-extension://8675309");
expect(args[1]).to.equal(false);

browser.experiments.logins.getLoginSavingEnabled.restore();
browser.experiments.logins.setLoginSavingEnabled.restore();
browser.extension.getURL.restore();
});
});
@@ -197,6 +197,8 @@ window.browser = {

experiments: {
logins: {
async getLoginSavingEnabled() { },
async setLoginSavingEnabled() { },
async getAll() { return []; },
async add(login) {
browser.experiments.logins.onAdded.getListener()({ login });

0 comments on commit 10feb59

Please sign in to comment.
You can’t perform that action at this time.