browser_sanitize-cookie-exceptions.js (6864B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const { SiteDataTestUtils } = ChromeUtils.importESModule( 5 "resource://testing-common/SiteDataTestUtils.sys.mjs" 6 ); 7 const { PermissionTestUtils } = ChromeUtils.importESModule( 8 "resource://testing-common/PermissionTestUtils.sys.mjs" 9 ); 10 11 const oneHour = 3600000000; 12 13 add_task(async function sanitizeWithExceptionsOnShutdown() { 14 info( 15 "Test that cookies that are marked as allowed from the user do not get \ 16 cleared when cleaning on shutdown is done" 17 ); 18 19 await SpecialPowers.pushPrefEnv({ 20 set: [ 21 ["browser.sanitizer.loglevel", "All"], 22 ["privacy.sanitize.sanitizeOnShutdown", true], 23 ], 24 }); 25 26 // Clean up before start 27 await new Promise(resolve => { 28 Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve); 29 }); 30 31 let originALLOW = "https://mozilla.org"; 32 PermissionTestUtils.add( 33 originALLOW, 34 "cookie", 35 Ci.nsICookiePermission.ACCESS_ALLOW 36 ); 37 38 let originDENY = "https://example123.com"; 39 PermissionTestUtils.add( 40 originDENY, 41 "cookie", 42 Ci.nsICookiePermission.ACCESS_DENY 43 ); 44 45 SiteDataTestUtils.addToCookies({ origin: originALLOW }); 46 ok( 47 SiteDataTestUtils.hasCookies(originALLOW), 48 "We have cookies for " + originALLOW 49 ); 50 51 SiteDataTestUtils.addToCookies({ origin: originDENY }); 52 ok( 53 SiteDataTestUtils.hasCookies(originDENY), 54 "We have cookies for " + originDENY 55 ); 56 57 await Sanitizer.runSanitizeOnShutdown(); 58 59 ok( 60 SiteDataTestUtils.hasCookies(originALLOW), 61 "We should have cookies for " + originALLOW 62 ); 63 64 ok( 65 !SiteDataTestUtils.hasCookies(originDENY), 66 "We should not have cookies for " + originDENY 67 ); 68 }); 69 70 add_task(async function sanitizeNoExceptionsInTimeRange() { 71 info( 72 "Test that no exceptions are made when not clearing on shutdown, e.g. clearing within a range" 73 ); 74 75 // Clean up before start 76 await new Promise(resolve => { 77 Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve); 78 }); 79 80 let originALLOW = "https://mozilla.org"; 81 PermissionTestUtils.add( 82 originALLOW, 83 "cookie", 84 Ci.nsICookiePermission.ACCESS_ALLOW 85 ); 86 87 let originDENY = "https://bar123.com"; 88 PermissionTestUtils.add( 89 originDENY, 90 "cookie", 91 Ci.nsICookiePermission.ACCESS_DENY 92 ); 93 94 SiteDataTestUtils.addToCookies({ origin: originALLOW }); 95 ok( 96 SiteDataTestUtils.hasCookies(originALLOW), 97 "We have cookies for " + originALLOW 98 ); 99 100 SiteDataTestUtils.addToCookies({ origin: originDENY }); 101 ok( 102 SiteDataTestUtils.hasCookies(originDENY), 103 "We have cookies for " + originDENY 104 ); 105 106 let to = Date.now() * 1000; 107 let from = to - oneHour; 108 await Sanitizer.sanitize(["cookies"], { range: [from, to] }); 109 110 ok( 111 !SiteDataTestUtils.hasCookies(originALLOW), 112 "We should not have cookies for " + originALLOW 113 ); 114 115 ok( 116 !SiteDataTestUtils.hasCookies(originDENY), 117 "We should not have cookies for " + originDENY 118 ); 119 }); 120 121 add_task(async function sanitizeWithExceptionsOnStartup() { 122 info( 123 "Test that cookies that are marked as allowed from the user do not get \ 124 cleared when cleaning on startup is done, for example after a crash" 125 ); 126 127 await SpecialPowers.pushPrefEnv({ 128 set: [ 129 ["browser.sanitizer.loglevel", "All"], 130 ["privacy.sanitize.sanitizeOnShutdown", true], 131 ], 132 }); 133 134 // Clean up before start 135 await new Promise(resolve => { 136 Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve); 137 }); 138 139 let originALLOW = "https://mozilla.org"; 140 PermissionTestUtils.add( 141 originALLOW, 142 "cookie", 143 Ci.nsICookiePermission.ACCESS_ALLOW 144 ); 145 146 let originDENY = "https://example123.com"; 147 PermissionTestUtils.add( 148 originDENY, 149 "cookie", 150 Ci.nsICookiePermission.ACCESS_DENY 151 ); 152 153 SiteDataTestUtils.addToCookies({ origin: originALLOW }); 154 ok( 155 SiteDataTestUtils.hasCookies(originALLOW), 156 "We have cookies for " + originALLOW 157 ); 158 159 SiteDataTestUtils.addToCookies({ origin: originDENY }); 160 ok( 161 SiteDataTestUtils.hasCookies(originDENY), 162 "We have cookies for " + originDENY 163 ); 164 165 let pendingSanitizations = [ 166 { 167 id: "shutdown", 168 itemsToClear: ["cookies"], 169 options: {}, 170 }, 171 ]; 172 Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, true); 173 Services.prefs.setStringPref( 174 Sanitizer.PREF_PENDING_SANITIZATIONS, 175 JSON.stringify(pendingSanitizations) 176 ); 177 178 await Sanitizer.onStartup(); 179 180 ok( 181 SiteDataTestUtils.hasCookies(originALLOW), 182 "We should have cookies for " + originALLOW 183 ); 184 185 ok( 186 !SiteDataTestUtils.hasCookies(originDENY), 187 "We should not have cookies for " + originDENY 188 ); 189 }); 190 191 add_task(async function sanitizeWithSessionExceptionsOnShutdown() { 192 info( 193 "Test that cookies that are marked as allowed on session is cleared when sanitizeOnShutdown is false" 194 ); 195 196 await SpecialPowers.pushPrefEnv({ 197 set: [ 198 ["browser.sanitizer.loglevel", "All"], 199 ["privacy.sanitize.sanitizeOnShutdown", false], 200 ], 201 }); 202 203 // Clean up before start 204 await new Promise(resolve => { 205 Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve); 206 }); 207 208 let originAllowSession = "https://mozilla.org"; 209 PermissionTestUtils.add( 210 originAllowSession, 211 "cookie", 212 Ci.nsICookiePermission.ACCESS_SESSION 213 ); 214 215 SiteDataTestUtils.addToCookies({ origin: originAllowSession }); 216 ok( 217 SiteDataTestUtils.hasCookies(originAllowSession), 218 "We have cookies for " + originAllowSession 219 ); 220 221 await Sanitizer.runSanitizeOnShutdown(); 222 223 ok( 224 !SiteDataTestUtils.hasCookies(originAllowSession), 225 "We should not have cookies for " + originAllowSession 226 ); 227 }); 228 229 add_task(async function sanitizeWithManySessionExceptionsOnShutdown() { 230 info( 231 "Test that lots of allowed on session exceptions are cleared when sanitizeOnShutdown is false" 232 ); 233 234 await SpecialPowers.pushPrefEnv({ 235 set: [ 236 ["privacy.sanitize.sanitizeOnShutdown", false], 237 ["dom.quotaManager.backgroundTask.enabled", true], 238 ], 239 }); 240 241 // Clean up before start 242 await new Promise(resolve => { 243 Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve); 244 }); 245 246 info("Setting cookies"); 247 248 const origins = new Array(300) 249 .fill(0) 250 .map((v, i) => `https://mozilla${i}.org`); 251 252 for (const origin of origins) { 253 PermissionTestUtils.add( 254 origin, 255 "cookie", 256 Ci.nsICookiePermission.ACCESS_SESSION 257 ); 258 SiteDataTestUtils.addToCookies({ origin }); 259 } 260 261 ok( 262 origins.every(origin => SiteDataTestUtils.hasCookies(origin)), 263 "All origins have cookies" 264 ); 265 266 info("Running sanitization"); 267 268 await Sanitizer.runSanitizeOnShutdown(); 269 270 ok( 271 origins.every(origin => !SiteDataTestUtils.hasCookies(origin)), 272 "All origins lost cookies" 273 ); 274 });