Bug 1729640 - P7. Add test cases for isolating sites by login related heuristics r=farre

Differential Revision: https://phabricator.services.mozilla.com/D127108
This commit is contained in:
Dimi 2021-11-05 17:11:33 +00:00
Родитель c0b1d1a95c
Коммит 853f95311b
4 изменённых файлов: 174 добавлений и 74 удалений

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

@ -46,6 +46,7 @@ already_AddRefed<LoginDetectionService> LoginDetectionService::GetSingleton() {
return do_AddRef(gLoginDetectionService); return do_AddRef(gLoginDetectionService);
} }
LoginDetectionService::LoginDetectionService() : mIsLoginsLoaded(false) {}
LoginDetectionService::~LoginDetectionService() { UnregisterObserver(); } LoginDetectionService::~LoginDetectionService() { UnregisterObserver(); }
void LoginDetectionService::MaybeStartMonitoring() { void LoginDetectionService::MaybeStartMonitoring() {
@ -111,6 +112,17 @@ NS_IMETHODIMP LoginDetectionService::Init() {
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP LoginDetectionService::IsLoginsLoaded(bool* aResult) {
if (IsIsolateHighValueSiteEnabled()) {
*aResult = mIsLoginsLoaded;
} else {
// When the feature is disabled, just returns true so testcases don't
// block on waiting for us to load logins.
*aResult = true;
}
return NS_OK;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// nsILoginSearchObserver implementation // nsILoginSearchObserver implementation
NS_IMETHODIMP NS_IMETHODIMP
@ -125,6 +137,7 @@ LoginDetectionService::OnSearchComplete(
mozilla::dom::kHighValueHasSavedLoginPermission); mozilla::dom::kHighValueHasSavedLoginPermission);
} }
mIsLoginsLoaded = true;
return NS_OK; return NS_OK;
} }

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

@ -40,7 +40,7 @@ class LoginDetectionService final : public nsILoginDetectionService,
void MaybeStartMonitoring(); void MaybeStartMonitoring();
private: private:
LoginDetectionService() = default; LoginDetectionService();
virtual ~LoginDetectionService(); virtual ~LoginDetectionService();
// Fetch saved logins from the password manager. // Fetch saved logins from the password manager.
@ -50,6 +50,9 @@ class LoginDetectionService final : public nsILoginDetectionService,
void UnregisterObserver(); void UnregisterObserver();
nsCOMPtr<nsIObserverService> mObs; nsCOMPtr<nsIObserverService> mObs;
// Used by testcase to make sure logins are fetched.
bool mIsLoginsLoaded;
}; };
} // namespace mozilla::dom } // namespace mozilla::dom

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

@ -13,4 +13,10 @@ interface nsILoginDetectionService : nsISupports
* called to initialize the login detection service. * called to initialize the login detection service.
*/ */
void init(); void init();
/**
* Returns true if we have loaded logins from the password manager.
* This is now used by testcase only.
*/
bool isLoginsLoaded();
}; };

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

@ -142,7 +142,105 @@ function mkTestPage({
}; };
} }
const heuristics = [
{
name: "coop",
setup_com: async expected => {
// Set the COOP header, and load
await testTreeRemoteTypes(
"com_set_coop",
mkTestPage({
topOrigin: COM_ORIGIN,
topHeaders: { "Cross-Origin-Opener-Policy": "same-origin" },
comRemoteType: expected.com_high,
orgRemoteType: expected.org_normal,
mozRemoteType: expected.moz_normal,
})
);
},
run_extra_test: async expected => {
// Load with both the COOP and COEP headers set.
await testTreeRemoteTypes(
"com_coop_coep",
mkTestPage({
topOrigin: COM_ORIGIN,
topHeaders: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
},
frameHeaders: {
"Cross-Origin-Embedder-Policy": "require-corp",
"Cross-Origin-Resource-Policy": "cross-origin",
},
comRemoteType: expected.com_coop_coep,
orgRemoteType: expected.org_coop_coep,
mozRemoteType: expected.moz_coop_coep,
})
);
},
},
{
name: "hasSavedLogin",
setup_com: async expected => {
// add .com to the password manager
let LoginInfo = new Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1",
Ci.nsILoginInfo,
"init"
);
Services.logins.addLogin(
new LoginInfo(COM_ORIGIN, "", null, "username", "password", "", "")
);
// Init login detection service to trigger fetching logins
let loginDetection = Cc[
"@mozilla.org/login-detection-service;1"
].createInstance(Ci.nsILoginDetectionService);
loginDetection.init();
await TestUtils.waitForCondition(() => {
let x = loginDetection.isLoginsLoaded();
return x;
}, "waiting for loading logins from the password manager");
},
},
{
name: "isLoggedIn",
setup_com: async expected => {
let p = new Promise(resolve => {
Services.obs.addObserver(function obs() {
Services.obs.removeObserver(
obs,
"passwordmgr-form-submission-detected"
);
resolve();
}, "passwordmgr-form-submission-detected");
});
const TEST_URL = documentURL(
COM_ORIGIN,
{},
`<form>
<input value="username">
<input type="password" value="password">
<input type="submit">
</form>`
);
// submit the form to simulate the login behavior
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
await SpecialPowers.spawn(browser, [], async () => {
content.document.querySelector("form").submit();
});
});
await p;
},
},
];
async function do_tests(expected) { async function do_tests(expected) {
for (let heuristic of heuristics) {
info(`Starting ${heuristic.name} test`);
// Clear all site-specific data, as we don't want to have any high-value site // Clear all site-specific data, as we don't want to have any high-value site
// permissions from any previous iterations. // permissions from any previous iterations.
await new Promise(resolve => await new Promise(resolve =>
@ -170,21 +268,13 @@ async function do_tests(expected) {
}) })
); );
// Set the COOP header, and load info(`Setting up ${heuristic.name} test`);
await testTreeRemoteTypes( await heuristic.setup_com(expected);
"com_set_coop",
mkTestPage({
topOrigin: COM_ORIGIN,
topHeaders: { "Cross-Origin-Opener-Policy": "same-origin" },
comRemoteType: expected.com_high,
orgRemoteType: expected.org_normal,
mozRemoteType: expected.moz_normal,
})
);
// Load again after setting the COOP header // Load again after the heuristic is triggered
info(`Running ${heuristic.name} tests after setup`);
await testTreeRemoteTypes( await testTreeRemoteTypes(
"com_after_coop", `com_after_${heuristic.name}`,
mkTestPage({ mkTestPage({
topOrigin: COM_ORIGIN, topOrigin: COM_ORIGIN,
comRemoteType: expected.com_high, comRemoteType: expected.com_high,
@ -193,9 +283,9 @@ async function do_tests(expected) {
}) })
); );
// Load again after setting the COOP header, with a .org toplevel // Load again with a .org toplevel
await testTreeRemoteTypes( await testTreeRemoteTypes(
"org_after_coop", `org_after_${heuristic.name}`,
mkTestPage({ mkTestPage({
topOrigin: ORG_ORIGIN, topOrigin: ORG_ORIGIN,
comRemoteType: expected.com_high, comRemoteType: expected.com_high,
@ -204,24 +294,12 @@ async function do_tests(expected) {
}) })
); );
// Load with both the COOP and COEP headers set. // Run heuristic dependent tests
await testTreeRemoteTypes( if (heuristic.run_extra_test) {
"com_coop_coep", info(`Running extra tests for ${heuristic.name}`);
mkTestPage({ await heuristic.run_extra_test(expected);
topOrigin: COM_ORIGIN, }
topHeaders: { }
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
},
frameHeaders: {
"Cross-Origin-Embedder-Policy": "require-corp",
"Cross-Origin-Resource-Policy": "cross-origin",
},
comRemoteType: expected.com_coop_coep,
orgRemoteType: expected.org_coop_coep,
mozRemoteType: expected.moz_coop_coep,
})
);
} }
add_task(async function test_isolate_nothing() { add_task(async function test_isolate_nothing() {