Bug 1540117 - Part 3: Add unit tests for ensuring that the right set of console messages are captured during the anti-tracking test suite, and also add test coverage for BEHAVIOR_REJECT_FOREIGN; r=baku

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Ehsan Akhgari 2019-08-27 15:10:41 +00:00
Родитель 0e4ddb04a3
Коммит f7f21d4c79
16 изменённых файлов: 243 добавлений и 76 удалений

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

@ -446,6 +446,8 @@ void ReportBlockingToConsole(nsPIDOMWindowOuter* aWindow, nsIURI* aURI,
[doc, sourceLine, lineNumber, columnNumber, uri, aRejectedReason]() {
const char* message = nullptr;
nsAutoCString category;
// When changing this list, please make sure to update the corresponding
// code in antitracking_head.js (inside _createTask).
switch (aRejectedReason) {
case nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION:
message = "CookieBlockedByPermission";

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

@ -94,17 +94,21 @@ this.AntiTracking = {
} else {
options.cookieBehavior = BEHAVIOR_ACCEPT;
}
if ("blockingByAllowList" in callbackNonTracking) {
options.blockingByAllowList = callbackNonTracking.blockingByAllowList;
} else {
options.blockingByAllowList = false;
}
if ("expectedBlockingNotifications" in callbackNonTracking) {
options.expectedBlockingNotifications =
callbackNonTracking.expectedBlockingNotifications;
} else {
options.expectedBlockingNotifications = 0;
}
if ("blockingByAllowList" in callbackNonTracking) {
options.blockingByAllowList = callbackNonTracking.blockingByAllowList;
if (options.blockingByAllowList) {
// If we're on the allow list, there won't be any blocking!
options.expectedBlockingNotifications = 0;
}
} else {
options.blockingByAllowList = false;
}
callbackNonTracking = options.callback;
options.accessRemoval = null;
options.callbackAfterRemoval = null;
@ -169,7 +173,9 @@ this.AntiTracking = {
allowList: false,
callback: callbackTracking,
extraPrefs,
expectedBlockingNotifications: 0,
expectedBlockingNotifications: expectedBlockingNotifications
? Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_ALL
: 0,
runInPrivateWindow,
iframeSandbox,
accessRemoval: null, // only passed with non-blocking callback
@ -205,6 +211,22 @@ this.AntiTracking = {
});
this._createCleanupTask(cleanupFunction);
this._createTask({
name,
cookieBehavior: BEHAVIOR_REJECT_FOREIGN,
allowList: false,
callback: callbackTracking,
extraPrefs,
expectedBlockingNotifications: expectedBlockingNotifications
? Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_FOREIGN
: 0,
runInPrivateWindow,
iframeSandbox,
accessRemoval,
callbackAfterRemoval,
});
this._createCleanupTask(cleanupFunction);
this._createTask({
name,
cookieBehavior: BEHAVIOR_REJECT_TRACKER,
@ -311,6 +333,7 @@ this.AntiTracking = {
"privacy.trackingprotection.annotate_channels",
cookieBehavior != BEHAVIOR_ACCEPT,
],
["privacy.restrict3rdpartystorage.console.lazy", false],
[
"privacy.restrict3rdpartystorage.userInteractionRequiredForHosts",
"tracking.example.com,tracking.example.org",
@ -389,6 +412,40 @@ this.AntiTracking = {
options.extraPrefs
);
let topPage;
if (typeof options.topPage == "string") {
topPage = options.topPage;
} else {
topPage = TEST_TOP_PAGE;
}
let thirdPartyPage, thirdPartyDomainURI;
if (typeof options.thirdPartyPage == "string") {
thirdPartyPage = options.thirdPartyPage;
let url = new URL(thirdPartyPage);
thirdPartyDomainURI = Services.io.newURI(url.origin);
} else {
thirdPartyPage = TEST_3RD_PARTY_PAGE;
thirdPartyDomainURI = Services.io.newURI(TEST_3RD_PARTY_DOMAIN);
}
// It's possible that the third-party domain has been whitelisted through
// extraPrefs, so let's try annotating it here and adjust our blocking
// expectations as necessary.
if (
options.expectedBlockingNotifications ==
Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER
) {
if (
!(await AntiTracking._isThirdPartyPageClassifiedAsTracker(
topPage,
thirdPartyDomainURI
))
) {
options.expectedBlockingNotifications = 0;
}
}
let cookieBlocked = 0;
let listener = {
onContentBlockingEvent(webProgress, request, event) {
@ -397,13 +454,14 @@ this.AntiTracking = {
}
},
};
win.gBrowser.addProgressListener(listener);
function prepareTestEnvironmentOnPage() {
win.gBrowser.addProgressListener(listener);
let topPage;
if (typeof options.topPage == "string") {
topPage = options.topPage;
} else {
topPage = TEST_TOP_PAGE;
Services.console.reset();
}
if (!options.allowList) {
prepareTestEnvironmentOnPage();
}
info("Creating a new tab");
@ -417,6 +475,8 @@ this.AntiTracking = {
info("Disabling content blocking for this page");
win.gProtectionsHandler.disableForCurrentPage();
prepareTestEnvironmentOnPage();
// The previous function reloads the browser, so wait for it to load again!
await BrowserTestUtils.browserLoaded(browser);
}
@ -426,12 +486,6 @@ this.AntiTracking = {
typeof options.accessRemoval == "string" &&
options.cookieBehavior == BEHAVIOR_REJECT_TRACKER &&
!options.allowList;
let thirdPartyPage;
if (typeof options.thirdPartyPage == "string") {
thirdPartyPage = options.thirdPartyPage;
} else {
thirdPartyPage = TEST_3RD_PARTY_PAGE;
}
let id = await ContentTask.spawn(
browser,
{
@ -586,6 +640,47 @@ this.AntiTracking = {
);
}
let allMessages = Services.console.getMessageArray().filter(msg => {
try {
// Select all messages that the anti-tracking backend could generate.
return msg
.QueryInterface(Ci.nsIScriptError)
.category.startsWith("cookieBlocked");
} catch (e) {
return false;
}
});
let expectedCategory = "";
// When changing this list, please make sure to update the corresponding
// code in ReportBlockingToConsole().
switch (options.expectedBlockingNotifications) {
case Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_BY_PERMISSION:
expectedCategory = "cookieBlockedPermission";
break;
case Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER:
expectedCategory = "cookieBlockedTracker";
break;
case Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_ALL:
expectedCategory = "cookieBlockedAll";
break;
case Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_FOREIGN:
expectedCategory = "cookieBlockedForeign";
break;
}
if (expectedCategory == "") {
is(allMessages.length, 0, "No console messages should be generated");
} else {
ok(allMessages.length != 0, "Some console message should be generated");
}
for (let msg of allMessages) {
is(
msg.category,
expectedCategory,
"Message should be of expected category"
);
}
if (options.allowList) {
info("Enabling content blocking for this page");
win.gProtectionsHandler.enableForCurrentPage();
@ -917,4 +1012,41 @@ this.AntiTracking = {
}
});
},
async _isThirdPartyPageClassifiedAsTracker(topPage, thirdPartyDomainURI) {
let channel;
await new Promise((resolve, reject) => {
channel = NetUtil.newChannel({
uri: thirdPartyDomainURI,
loadingPrincipal: Services.scriptSecurityManager.createContentPrincipal(
thirdPartyDomainURI,
{}
),
securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
});
channel
.QueryInterface(Ci.nsIHttpChannelInternal)
.setTopWindowURIIfUnknown(Services.io.newURI(topPage));
function Listener() {}
Listener.prototype = {
onStartRequest(request) {},
onDataAvailable(request, stream, off, cnt) {},
onStopRequest(request, st) {
let status = request.QueryInterface(Ci.nsIHttpChannel).responseStatus;
if (status == 200) {
resolve();
} else {
reject();
}
},
};
let listener = new Listener();
channel.asyncOpen(listener);
});
return channel.QueryInterface(Ci.nsIHttpChannel).isTrackingResource();
},
};

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

@ -90,9 +90,12 @@ AntiTracking.runTestInNormalAndPrivateMode(
document.cookie = "name=value";
if (
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT
[
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
)
) {
is(document.cookie, "", "No cookies for me");
} else {

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

@ -51,10 +51,12 @@ AntiTracking.runTest(
/* import-globals-from storageAccessAPIHelpers.js */
await callRequestStorageAccess();
let shouldThrow =
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
let shouldThrow = [
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
);
await caches.open("wow").then(
_ => {

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

@ -45,10 +45,12 @@ AntiTracking.runTestInNormalAndPrivateMode(
/* import-globals-from storageAccessAPIHelpers.js */
await callRequestStorageAccess();
let shouldThrow =
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
let shouldThrow = [
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
);
let hasThrown;
try {
@ -62,7 +64,7 @@ AntiTracking.runTestInNormalAndPrivateMode(
is(
hasThrown,
shouldThrow,
"IDB should be allowed if not in BEHAVIOR_REJECT"
"IDB should be allowed if not in cookieBehavior pref value BEHAVIOR_REJECT/BEHAVIOR_REJECT_FOREIGN"
);
},
// non-blocking callback

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

@ -119,9 +119,12 @@ AntiTracking.runTestInNormalAndPrivateMode(
await callRequestStorageAccess();
if (
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT
[
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
)
) {
blob = new Blob([blockCode.toString() + "; blockCode();"]);
} else {

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

@ -44,9 +44,12 @@ AntiTracking.runTestInNormalAndPrivateMode(
await callRequestStorageAccess();
if (
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT
[
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
)
) {
is(window.localStorage, null, "LocalStorage is null");
try {

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

@ -117,9 +117,12 @@ AntiTracking.runTestInNormalAndPrivateMode(
await callRequestStorageAccess();
if (
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT
[
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
)
) {
try {
new BroadcastChannel("hello");
@ -208,9 +211,12 @@ AntiTracking.runTestInNormalAndPrivateMode(
await callRequestStorageAccess();
if (
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT
[
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
)
) {
blob = new Blob([blockingCode.toString() + "; blockingCode();"]);
} else {

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

@ -24,9 +24,12 @@ AntiTracking.runTest(
await callRequestStorageAccess();
if (
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT
[
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
)
) {
await navigator.serviceWorker
.register("empty.js")

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

@ -3,10 +3,11 @@
AntiTracking.runTestInNormalAndPrivateMode(
"sessionStorage",
async _ => {
let shouldThrow =
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
let shouldThrow = [
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
);
let hasThrown;
try {
@ -36,8 +37,7 @@ AntiTracking.runTestInNormalAndPrivateMode(
},
[],
true,
true,
0
true
);
AntiTracking.runTestInNormalAndPrivateMode(
@ -46,10 +46,11 @@ AntiTracking.runTestInNormalAndPrivateMode(
/* import-globals-from storageAccessAPIHelpers.js */
await noStorageAccessInitially();
let shouldThrow =
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
let shouldThrow = [
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
);
let hasThrown;
try {

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

@ -44,9 +44,12 @@ AntiTracking.runTestInNormalAndPrivateMode(
await callRequestStorageAccess();
if (
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT
[
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
)
) {
try {
new SharedWorker("a.js", "foo");

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

@ -7,10 +7,12 @@ AntiTracking.runTestInNormalAndPrivateMode(
/* import-globals-from storageAccessAPIHelpers.js */
await noStorageAccessInitially();
let shouldThrow =
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
let shouldThrow = [
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
);
is(
window.localStorage == null,

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

@ -3,10 +3,12 @@
AntiTracking.runTest(
"localStorage with a tracker that is whitelisted via a pref",
async _ => {
let shouldThrow =
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
let shouldThrow = [
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
);
let hasThrown;
try {
@ -32,17 +34,19 @@ AntiTracking.runTest(
[["urlclassifier.trackingAnnotationSkipURLs", "TRACKING.EXAMPLE.ORG"]],
false, // run the window.open() test
false, // run the user interaction test
0, // don't expect blocking notifications
Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, // expect blocking notifications
false
); // run in a normal window
AntiTracking.runTest(
"localStorage with a tracker that is whitelisted via a fancy pref",
async _ => {
let shouldThrow =
SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior"
) == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
let shouldThrow = [
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT,
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
].includes(
SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior")
);
let hasThrown;
try {
@ -73,7 +77,7 @@ AntiTracking.runTest(
],
false, // run the window.open() test
false, // run the user interaction test
0, // don't expect blocking notifications
Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, // expect blocking notifications
false
); // run in a normal window

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

@ -27,7 +27,7 @@ AntiTracking.runTest(
null, // extra prefs
false, // no window open test
false, // no user-interaction test
Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, // expected blocking notifications
0, // expected blocking notifications
false, // private window
"allow-scripts allow-same-origin allow-popups" // iframe sandbox
);

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

@ -75,7 +75,7 @@ AntiTracking.runTest(
[["dom.storage_access.enabled", true]], // extra prefs
false, // no window open test
false, // no user-interaction test
false, // no blocking notifications
Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, // expect blocking notifications
false, // run in normal window
"allow-scripts allow-same-origin allow-popups"
);
@ -98,7 +98,7 @@ AntiTracking.runTest(
[["dom.storage_access.enabled", true]], // extra prefs
false, // no window open test
false, // no user-interaction test
false, // no blocking notifications
0, // no blocking notifications
false, // run in normal window
"allow-scripts allow-same-origin allow-popups allow-storage-access-by-user-activation"
);
@ -125,7 +125,7 @@ AntiTracking.runTest(
[["dom.storage_access.enabled", true]], // extra prefs
false, // no window open test
false, // no user-interaction test
0, // no blocking notifications
Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, // expect blocking notifications
true, // run in private window
null // iframe sandbox
);

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

@ -31,6 +31,7 @@ AntiTracking.runTest(
runExtraTests: false,
cookieBehavior,
blockingByAllowList,
expectedBlockingNotifications,
callback: async _ => {
// Let's load the image twice here as well.
let img = document.createElement("img");