browser_sessionStorage_navigation.js (8512B)
1 "use strict"; 2 3 const DIRPATH = getRootDirectory(gTestPath).replace( 4 "chrome://mochitests/content/", 5 "" 6 ); 7 const PATH = DIRPATH + "file_empty.html"; 8 9 const ORIGIN1 = "https://example.com"; 10 const ORIGIN2 = "https://example.org"; 11 const URL1 = `${ORIGIN1}/${PATH}`; 12 const URL2 = `${ORIGIN2}/${PATH}`; 13 const URL1_WITH_COOP_COEP = `${ORIGIN1}/${DIRPATH}file_coop_coep.html`; 14 15 add_task(async function () { 16 await BrowserTestUtils.withNewTab(URL1, async function (browser) { 17 const key = "key"; 18 const value = "value"; 19 20 info( 21 `Verifying sessionStorage is preserved after navigating to a ` + 22 `cross-origin site and then navigating back` 23 ); 24 25 await SpecialPowers.pushPrefEnv({ 26 set: [ 27 [ 28 "privacy.partition.always_partition_third_party_non_cookie_storage", 29 false, 30 ], 31 ], 32 }); 33 34 BrowserTestUtils.startLoadingURIString(browser, URL1); 35 await BrowserTestUtils.browserLoaded(browser); 36 37 await SpecialPowers.spawn( 38 browser, 39 [ORIGIN1, key, value], 40 async (ORIGIN, key, value) => { 41 is(content.window.origin, ORIGIN, `Navigate to ${ORIGIN} as expected`); 42 43 let value1 = content.window.sessionStorage.getItem(key); 44 is( 45 value1, 46 null, 47 `SessionStorage for ${key} in ${content.window.origin} is null ` + 48 `since it's the first visit` 49 ); 50 51 content.window.sessionStorage.setItem(key, value); 52 53 let value2 = content.window.sessionStorage.getItem(key); 54 is( 55 value2, 56 value, 57 `SessionStorage for ${key} in ${content.window.origin} is set ` + 58 `correctly` 59 ); 60 } 61 ); 62 63 BrowserTestUtils.startLoadingURIString(browser, URL2); 64 await BrowserTestUtils.browserLoaded(browser); 65 66 await SpecialPowers.spawn( 67 browser, 68 [ORIGIN2, key, value], 69 async (ORIGIN, key) => { 70 is(content.window.origin, ORIGIN, `Navigate to ${ORIGIN} as expected`); 71 72 let value1 = content.window.sessionStorage.getItem(key); 73 is( 74 value1, 75 null, 76 `SessionStorage for ${key} in ${content.window.origin} is null ` + 77 `since it's the first visit` 78 ); 79 } 80 ); 81 82 BrowserTestUtils.startLoadingURIString(browser, URL1); 83 await BrowserTestUtils.browserLoaded(browser); 84 85 await SpecialPowers.spawn( 86 browser, 87 [ORIGIN1, key, value], 88 async (ORIGIN, key, value) => { 89 is(content.window.origin, ORIGIN, `Navigate to ${ORIGIN} as expected`); 90 91 let value1 = content.window.sessionStorage.getItem(key); 92 is( 93 value1, 94 value, 95 `SessionStorage for ${key} in ${content.window.origin} is preserved` 96 ); 97 } 98 ); 99 100 info(`Verifying sessionStorage is preserved for ${URL1} after navigating`); 101 102 BrowserTestUtils.startLoadingURIString(browser, URL2); 103 await BrowserTestUtils.browserLoaded(browser); 104 105 await SpecialPowers.spawn( 106 browser, 107 [ORIGIN2, ORIGIN1, URL1, key, value], 108 async (ORIGIN, iframeORIGIN, iframeURL, key, value) => { 109 is(content.window.origin, ORIGIN, `Navigate to ${ORIGIN} as expected`); 110 111 let iframe = content.document.createElement("iframe"); 112 iframe.src = iframeURL; 113 content.document.body.appendChild(iframe); 114 await ContentTaskUtils.waitForEvent(iframe, "load"); 115 116 await content.SpecialPowers.spawn( 117 iframe, 118 [iframeORIGIN, key, value], 119 async function (ORIGIN, key, value) { 120 is( 121 content.window.origin, 122 ORIGIN, 123 `Navigate to ${ORIGIN} as expected` 124 ); 125 126 // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5) 127 // Acquire storage access permission here so that the iframe has 128 // first-party access to the sessionStorage. Without this, it is 129 // isolated and this test will always fail 130 SpecialPowers.wrap(content.document).notifyUserGestureActivation(); 131 await SpecialPowers.addPermission( 132 "storageAccessAPI", 133 true, 134 content.window.location.href 135 ); 136 await SpecialPowers.wrap(content.document).requestStorageAccess(); 137 138 let value1 = content.window.sessionStorage.getItem(key); 139 is( 140 value1, 141 value, 142 `SessionStorage for ${key} in ${content.window.origin} is ` + 143 `preserved` 144 ); 145 } 146 ); 147 } 148 ); 149 150 info(`Verifying SSCache is loaded to the content process only once`); 151 152 BrowserTestUtils.startLoadingURIString(browser, URL1); 153 await BrowserTestUtils.browserLoaded(browser); 154 155 await SpecialPowers.spawn( 156 browser, 157 [ORIGIN1, URL1, key, value], 158 async (ORIGIN, iframeURL, key, value) => { 159 is(content.window.origin, ORIGIN, `Navigate to ${ORIGIN} as expected`); 160 161 let iframe = content.document.createElement("iframe"); 162 iframe.src = iframeURL; 163 content.document.body.appendChild(iframe); 164 await ContentTaskUtils.waitForEvent(iframe, "load"); 165 166 await content.SpecialPowers.spawn( 167 iframe, 168 [ORIGIN, key, value], 169 async function (ORIGIN, key, value) { 170 is( 171 content.window.origin, 172 ORIGIN, 173 `Load an iframe to ${ORIGIN} as expected` 174 ); 175 176 let value1 = content.window.sessionStorage.getItem(key); 177 is( 178 value1, 179 value, 180 `SessionStorage for ${key} in ${content.window.origin} is ` + 181 `preserved.` 182 ); 183 184 // When we are here, it means we didn't hit the assertion for 185 // ensuring a SSCache can only be loaded on the content process 186 // once. 187 } 188 ); 189 } 190 ); 191 192 info( 193 `Verifying the sessionStorage for a tab shares between ` + 194 `cross-origin-isolated and non cross-origin-isolated environments` 195 ); 196 const anotherKey = `anotherKey`; 197 const anotherValue = `anotherValue;`; 198 199 BrowserTestUtils.startLoadingURIString(browser, URL1_WITH_COOP_COEP); 200 await BrowserTestUtils.browserLoaded(browser); 201 202 await SpecialPowers.spawn( 203 browser, 204 [ORIGIN1, key, value, anotherKey, anotherValue], 205 async (ORIGIN, key, value, anotherKey, anotherValue) => { 206 is(content.window.origin, ORIGIN, `Navigate to ${ORIGIN} as expected`); 207 ok( 208 content.window.crossOriginIsolated, 209 `The window is cross-origin-isolated.` 210 ); 211 212 let value1 = content.window.sessionStorage.getItem(key); 213 is( 214 value1, 215 value, 216 `SessionStorage for ${key} in ${content.window.origin} was ` + 217 `propagated to COOP+COEP process correctly.` 218 ); 219 220 let value2 = content.window.sessionStorage.getItem(anotherKey); 221 is( 222 value2, 223 null, 224 `SessionStorage for ${anotherKey} in ${content.window.origin} ` + 225 `hasn't been set yet.` 226 ); 227 228 content.window.sessionStorage.setItem(anotherKey, anotherValue); 229 230 let value3 = content.window.sessionStorage.getItem(anotherKey); 231 is( 232 value3, 233 anotherValue, 234 `SessionStorage for ${anotherKey} in ${content.window.origin} ` + 235 `was set as expected.` 236 ); 237 } 238 ); 239 240 BrowserTestUtils.startLoadingURIString(browser, URL1); 241 await BrowserTestUtils.browserLoaded(browser); 242 243 await SpecialPowers.spawn( 244 browser, 245 [ORIGIN1, key, value, anotherKey, anotherValue], 246 async (ORIGIN, key, value, anotherKey, anotherValue) => { 247 is(content.window.origin, ORIGIN, `Navigate to ${ORIGIN} as expected`); 248 ok( 249 !content.window.crossOriginIsolated, 250 `The window is not cross-origin-isolated.` 251 ); 252 253 let value1 = content.window.sessionStorage.getItem(key); 254 is( 255 value1, 256 value, 257 `SessionStorage for ${key} in ${content.window.origin} is ` + 258 `preserved.` 259 ); 260 261 let value2 = content.window.sessionStorage.getItem(anotherKey); 262 is( 263 value2, 264 anotherValue, 265 `SessionStorage for ${anotherKey} in ${content.window.origin} was ` + 266 `propagated to non-COOP+COEP process correctly.` 267 ); 268 } 269 ); 270 }); 271 });