head.js (6537B)
1 const { PermissionTestUtils } = ChromeUtils.importESModule( 2 "resource://testing-common/PermissionTestUtils.sys.mjs" 3 ); 4 5 const BEHAVIOR_ACCEPT = Ci.nsICookieService.BEHAVIOR_ACCEPT; 6 const BEHAVIOR_REJECT = Ci.nsICookieService.BEHAVIOR_REJECT; 7 8 const PERM_DEFAULT = Ci.nsICookiePermission.ACCESS_DEFAULT; 9 const PERM_ALLOW = Ci.nsICookiePermission.ACCESS_ALLOW; 10 const PERM_DENY = Ci.nsICookiePermission.ACCESS_DENY; 11 12 const TEST_DOMAIN = "https://example.com/"; 13 const TEST_PATH = "browser/netwerk/cookie/test/browser/"; 14 const TEST_TOP_PAGE = TEST_DOMAIN + TEST_PATH + "file_empty.html"; 15 16 // Helper to eval() provided cookieJarAccessAllowed and cookieJarAccessDenied 17 // toString()ed optionally async function in freshly created tabs with 18 // BEHAVIOR_ACCEPT and BEHAVIOR_REJECT configured, respectively, in a number of 19 // permutations. This includes verifying that changing the permission while the 20 // page is open still results in the state of the permission when the 21 // document/global was created still applying. Code will execute in the 22 // ContentTask.spawn frame-script context, use content to access the underlying 23 // page. 24 this.CookiePolicyHelper = { 25 runTest(testName, config) { 26 // Testing allowed to blocked by cookie behavior 27 this._createTest( 28 testName, 29 config.cookieJarAccessAllowed, 30 config.cookieJarAccessDenied, 31 config.prefs, 32 { 33 fromBehavior: BEHAVIOR_ACCEPT, 34 toBehavior: BEHAVIOR_REJECT, 35 fromPermission: PERM_DEFAULT, 36 toPermission: PERM_DEFAULT, 37 } 38 ); 39 40 // Testing blocked to allowed by cookie behavior 41 this._createTest( 42 testName, 43 config.cookieJarAccessDenied, 44 config.cookieJarAccessAllowed, 45 config.prefs, 46 { 47 fromBehavior: BEHAVIOR_REJECT, 48 toBehavior: BEHAVIOR_ACCEPT, 49 fromPermission: PERM_DEFAULT, 50 toPermission: PERM_DEFAULT, 51 } 52 ); 53 54 // Testing allowed to blocked by cookie permission 55 this._createTest( 56 testName, 57 config.cookieJarAccessAllowed, 58 config.cookieJarAccessDenied, 59 config.prefs, 60 { 61 fromBehavior: BEHAVIOR_REJECT, 62 toBehavior: BEHAVIOR_REJECT, 63 fromPermission: PERM_ALLOW, 64 toPermission: PERM_DEFAULT, 65 } 66 ); 67 68 // Testing blocked to allowed by cookie permission 69 this._createTest( 70 testName, 71 config.cookieJarAccessDenied, 72 config.cookieJarAccessAllowed, 73 config.prefs, 74 { 75 fromBehavior: BEHAVIOR_ACCEPT, 76 toBehavior: BEHAVIOR_ACCEPT, 77 fromPermission: PERM_DENY, 78 toPermission: PERM_DEFAULT, 79 } 80 ); 81 }, 82 83 _createTest(testName, goodCb, badCb, prefs, config) { 84 add_task(async _ => { 85 info("Starting " + testName + ": " + config.toSource()); 86 87 await SpecialPowers.flushPrefEnv(); 88 89 // Allow eval below. 90 await SpecialPowers.pushPrefEnv({ 91 set: [["security.allow_eval_with_system_principal", true]], 92 }); 93 94 if (prefs) { 95 await SpecialPowers.pushPrefEnv({ set: prefs }); 96 } 97 98 // Let's set the first cookie pref. 99 PermissionTestUtils.add(TEST_DOMAIN, "cookie", config.fromPermission); 100 await SpecialPowers.pushPrefEnv({ 101 set: [["network.cookie.cookieBehavior", config.fromBehavior]], 102 }); 103 104 // Let's open a tab and load content. 105 let tab = BrowserTestUtils.addTab(gBrowser, TEST_TOP_PAGE); 106 gBrowser.selectedTab = tab; 107 108 let browser = gBrowser.getBrowserForTab(tab); 109 await BrowserTestUtils.browserLoaded(browser); 110 111 // Let's create an iframe. 112 await SpecialPowers.spawn( 113 browser, 114 [{ url: TEST_TOP_PAGE }], 115 async obj => { 116 return new content.Promise(resolve => { 117 let ifr = content.document.createElement("iframe"); 118 ifr.setAttribute("id", "iframe"); 119 ifr.src = obj.url; 120 ifr.onload = () => resolve(); 121 content.document.body.appendChild(ifr); 122 }); 123 } 124 ); 125 126 // Let's exec the "good" callback. 127 info( 128 "Executing the test after setting the cookie behavior to " + 129 config.fromBehavior + 130 " and permission to " + 131 config.fromPermission 132 ); 133 await SpecialPowers.spawn( 134 browser, 135 [{ callback: goodCb.toString() }], 136 async obj => { 137 let runnableStr = `(() => {return (${obj.callback});})();`; 138 let runnable = eval(runnableStr); // eslint-disable-line no-eval 139 await runnable(content); 140 141 let ifr = content.document.getElementById("iframe"); 142 await runnable(ifr.contentWindow); 143 } 144 ); 145 146 // Now, let's change the cookie settings 147 PermissionTestUtils.add(TEST_DOMAIN, "cookie", config.toPermission); 148 await SpecialPowers.pushPrefEnv({ 149 set: [["network.cookie.cookieBehavior", config.toBehavior]], 150 }); 151 152 // We still want the good callback to succeed. 153 info( 154 "Executing the test after setting the cookie behavior to " + 155 config.toBehavior + 156 " and permission to " + 157 config.toPermission 158 ); 159 await SpecialPowers.spawn( 160 browser, 161 [{ callback: goodCb.toString() }], 162 async obj => { 163 let runnableStr = `(() => {return (${obj.callback});})();`; 164 let runnable = eval(runnableStr); // eslint-disable-line no-eval 165 await runnable(content); 166 167 let ifr = content.document.getElementById("iframe"); 168 await runnable(ifr.contentWindow); 169 } 170 ); 171 172 // Let's close the tab. 173 BrowserTestUtils.removeTab(tab); 174 175 // Let's open a new tab and load content again. 176 tab = BrowserTestUtils.addTab(gBrowser, TEST_TOP_PAGE); 177 gBrowser.selectedTab = tab; 178 179 browser = gBrowser.getBrowserForTab(tab); 180 await BrowserTestUtils.browserLoaded(browser); 181 182 // Let's exec the "bad" callback. 183 info("Executing the test in a new tab"); 184 await SpecialPowers.spawn( 185 browser, 186 [{ callback: badCb.toString() }], 187 async obj => { 188 let runnableStr = `(() => {return (${obj.callback});})();`; 189 let runnable = eval(runnableStr); // eslint-disable-line no-eval 190 await runnable(content); 191 } 192 ); 193 194 // Let's close the tab. 195 BrowserTestUtils.removeTab(tab); 196 197 // Cleanup. 198 await new Promise(resolve => { 199 Services.clearData.deleteData( 200 Ci.nsIClearDataService.CLEAR_ALL, 201 resolve 202 ); 203 }); 204 }); 205 }, 206 };