summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/permissions/browser_canvas_rfp_exclusion.js
blob: a29116e4820e6d26aa71b9db520df28282506aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* 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/.
 *
 * Adapted from browser_canvas_fingerprinting_resistance.js
 */
"use strict";

const kUrl = "https://example.com/";
var gPlacedData = false;

function initTab(performReadbackTest) {
  let contentWindow = content.wrappedJSObject;

  let drawCanvas = (fillStyle, id) => {
    let contentDocument = contentWindow.document;
    let width = 64,
      height = 64;
    let canvas = contentDocument.createElement("canvas");
    if (id) {
      canvas.setAttribute("id", id);
    }
    canvas.setAttribute("width", width);
    canvas.setAttribute("height", height);
    contentDocument.body.appendChild(canvas);

    let context = canvas.getContext("2d");
    context.fillStyle = fillStyle;
    context.fillRect(0, 0, width, height);
    return canvas;
  };

  let canvas = drawCanvas("cyan", "canvas-id-canvas");

  let placedData = canvas.toDataURL();
  if (performReadbackTest) {
    is(
      canvas.toDataURL(),
      placedData,
      "Reading the placed data twice didn't match"
    );
    return placedData;
  }
  return undefined;
}

function disableResistFingerprinting() {
  return SpecialPowers.pushPrefEnv({
    set: [["privacy.resistFingerprinting", false]],
  });
}

function enableResistFingerprinting(RfpNonPbmExclusion, RfpDomainExclusion) {
  if (RfpNonPbmExclusion && RfpDomainExclusion) {
    return SpecialPowers.pushPrefEnv({
      set: [
        ["privacy.resistFingerprinting", true],
        ["privacy.resistFingerprinting.testGranularityMask", 6],
        ["privacy.resistFingerprinting.exemptedDomains", "example.com"],
      ],
    });
  } else if (RfpNonPbmExclusion) {
    return SpecialPowers.pushPrefEnv({
      set: [
        ["privacy.resistFingerprinting", true],
        ["privacy.resistFingerprinting.testGranularityMask", 2],
      ],
    });
  } else if (RfpDomainExclusion) {
    return SpecialPowers.pushPrefEnv({
      set: [
        ["privacy.resistFingerprinting", true],
        ["privacy.resistFingerprinting.testGranularityMask", 4],
        ["privacy.resistFingerprinting.exemptedDomains", "example.com"],
      ],
    });
  }
  return SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.resistFingerprinting", true],
      ["privacy.resistFingerprinting.testGranularityMask", 0],
    ],
  });
}

function extractCanvasData(
  placedData,
  isPbm,
  RfpNonPbmExclusion,
  RfpDomainExclusion
) {
  let contentWindow = content.wrappedJSObject;
  let canvas = contentWindow.document.getElementById("canvas-id-canvas");
  let canvasData = canvas.toDataURL();

  if (RfpDomainExclusion) {
    is(
      canvasData,
      placedData,
      `A: RFP, domain exempted, canvas data == placed data (isPbm: ${isPbm}, RfpNonPbmExclusion: ${RfpNonPbmExclusion}, RfpDomainExclusion: ${RfpDomainExclusion})`
    );
  } else if (!isPbm && RfpNonPbmExclusion) {
    is(
      canvasData,
      placedData,
      `B: RFP, nonPBM exempted, not in PBM, canvas data == placed data (isPbm: ${isPbm}, RfpNonPbmExclusion: ${RfpNonPbmExclusion}, RfpDomainExclusion: ${RfpDomainExclusion})`
    );
  } else if (isPbm && RfpNonPbmExclusion) {
    isnot(
      canvasData,
      placedData,
      `C: RFP, nonPBM exempted, in PBM, canvas data != placed data (isPbm: ${isPbm}, RfpNonPbmExclusion: ${RfpNonPbmExclusion}, RfpDomainExclusion: ${RfpDomainExclusion})`
    );
  } else {
    isnot(
      canvasData,
      placedData,
      `D: RFP, domain not exempted, nonPBM not exempted, canvas data != placed data (isPbm: ${isPbm}, RfpNonPbmExclusion: ${RfpNonPbmExclusion}, RfpDomainExclusion: ${RfpDomainExclusion})`
    );
  }
}

async function populatePlacedData() {
  let win = await BrowserTestUtils.openNewBrowserWindow();
  await disableResistFingerprinting();
  await BrowserTestUtils.withNewTab(
    {
      gBrowser: win.gBrowser,
      url: kUrl,
    },
    async function() {
      let browser = win.gBrowser.selectedBrowser;
      gPlacedData = await SpecialPowers.spawn(
        browser,
        [/* performReadbackTest= */ true],
        initTab
      );
    }
  );
  await BrowserTestUtils.closeWindow(win);
}

async function rfpExclusionTestOnCanvas(
  win,
  placedData,
  isPbm,
  RfpNonPbmExclusion,
  RfpDomainExclusion
) {
  let browser = win.gBrowser.selectedBrowser;
  await SpecialPowers.spawn(
    browser,
    [/* performReadbackTest= */ false],
    initTab
  );
  await SpecialPowers.spawn(
    browser,
    [placedData, isPbm, RfpNonPbmExclusion, RfpDomainExclusion],
    extractCanvasData
  );
}

async function testCanvasRfpExclusion(
  isPbm,
  RfpNonPbmExclusion,
  RfpDomainExclusion
) {
  let win = await BrowserTestUtils.openNewBrowserWindow({
    private: isPbm,
  });
  await enableResistFingerprinting(RfpNonPbmExclusion, RfpDomainExclusion);
  await BrowserTestUtils.withNewTab(
    {
      gBrowser: win.gBrowser,
      url: kUrl,
    },
    rfpExclusionTestOnCanvas.bind(
      null,
      win,
      gPlacedData,
      isPbm,
      RfpNonPbmExclusion,
      RfpDomainExclusion
    )
  );
  await BrowserTestUtils.closeWindow(win);
}

add_task(populatePlacedData.bind(null));
add_task(testCanvasRfpExclusion.bind(null, false, false, false));
add_task(testCanvasRfpExclusion.bind(null, false, false, true));
add_task(testCanvasRfpExclusion.bind(null, false, true, false));
add_task(testCanvasRfpExclusion.bind(null, false, true, true));
add_task(testCanvasRfpExclusion.bind(null, true, false, false));
add_task(testCanvasRfpExclusion.bind(null, true, false, true));
add_task(testCanvasRfpExclusion.bind(null, true, true, false));
add_task(testCanvasRfpExclusion.bind(null, true, true, true));