browser_storage_cookies_navigation.js (5426B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 add_task(async function () { 8 // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default" 9 await SpecialPowers.pushPrefEnv({ 10 set: [["network.cookie.sameSite.laxByDefault", false]], 11 }); 12 13 const URL1 = buildURLWithContent( 14 "example.com", 15 `<h1>example.com</h1>` + `<script>document.cookie = "lorem=ipsum";</script>` 16 ); 17 const URL2 = buildURLWithContent( 18 "example.net", 19 `<h1>example.net</h1>` + 20 `<iframe></iframe>` + 21 `<script>document.cookie = "foo=bar";</script>` 22 ); 23 const URL_IFRAME = buildURLWithContent( 24 "example.org", 25 `<h1>example.org</h1>` + 26 `<script>document.cookie = "hello=world; SameSite=None; Secure; Partitioned;";</script>` 27 ); 28 29 // open tab 30 await openTabAndSetupStorage(URL1); 31 const doc = gPanelWindow.document; 32 33 // Check first domain 34 // check that both host appear in the storage tree 35 checkTree(doc, ["cookies", "https://example.com"]); 36 // check the table for values 37 await selectTreeItem(["cookies", "https://example.com"]); 38 checkCookieData("lorem", "ipsum"); 39 40 // NOTE: No need to clean up cookies since Services.cookies.removeAll() from 41 // the registered clean up function will remove all of them. 42 43 // Check second domain 44 await navigateTo(URL2); 45 // wait for storage tree refresh, and check host 46 info("Waiting for storage tree to refresh and show correct host…"); 47 await waitUntil( 48 () => 49 isInTree(doc, ["cookies", "https://example.net"]) && 50 !isInTree(doc, ["cookies", "https://example.com"]) 51 ); 52 53 ok( 54 !isInTree(doc, ["cookies", "https://example.com"]), 55 "example.com item is not in the tree anymore" 56 ); 57 58 // check the table for values 59 // NOTE: there's an issue with the TreeWidget in which `selectedItem` is set 60 // but we have nothing selected in the UI. See Bug 1712706. 61 // Here we are forcing selecting a different item first. 62 await selectTreeItem(["cookies"]); 63 await selectTreeItem(["cookies", "https://example.net"]); 64 info("Waiting for table data to update and show correct values"); 65 await waitUntil(() => hasCookieData("foo", "bar")); 66 67 // reload the current page, and check again 68 await reloadBrowser(); 69 // wait for storage tree refresh, and check host 70 info("Waiting for storage tree to refresh and show correct host…"); 71 await waitUntil(() => isInTree(doc, ["cookies", "https://example.net"])); 72 // check the table for values 73 // NOTE: there's an issue with the TreeWidget in which `selectedItem` is set 74 // but we have nothing selected in the UI. See Bug 1712706. 75 // Here we are forcing selecting a different item first. 76 await selectTreeItem(["cookies"]); 77 await selectTreeItem(["cookies", "https://example.net"]); 78 info("Waiting for table data to update and show correct values"); 79 await waitUntil(() => hasCookieData("foo", "bar")); 80 81 // make the iframe navigate to a different domain 82 const onStorageTreeUpdated = gUI.once("store-objects-edit"); 83 await SpecialPowers.spawn( 84 gBrowser.selectedBrowser, 85 [URL_IFRAME], 86 async function (url) { 87 const iframe = content.document.querySelector("iframe"); 88 const onIframeLoaded = new Promise(loaded => 89 iframe.addEventListener("load", loaded, { once: true }) 90 ); 91 iframe.src = url; 92 await onIframeLoaded; 93 } 94 ); 95 info("Waiting for storage tree to update"); 96 await onStorageTreeUpdated; 97 98 info("Waiting for storage tree to refresh and show correct host…"); 99 await waitUntil(() => isInTree(doc, ["cookies", "https://example.org"])); 100 info("Checking cookie data"); 101 await selectTreeItem(["cookies", "https://example.org"]); 102 checkCookieData("hello", "world"); 103 104 info( 105 "Navigate to the first URL to check that the multiple hosts in the current document are all removed" 106 ); 107 await navigateTo(URL1); 108 ok(true, "navigated"); 109 await waitUntil(() => isInTree(doc, ["cookies", "https://example.com"])); 110 ok( 111 !isInTree(doc, ["cookies", "https://example.net"]), 112 "host of previous document (example.net) is not in the tree anymore" 113 ); 114 ok( 115 !isInTree(doc, ["cookies", "https://example.org"]), 116 "host of iframe in previous document (example.org) is not in the tree anymore" 117 ); 118 119 // indexedDB is slightly more asynchronously fetched than the others 120 // and is emitted as a Resource to the frontend late. navigateTo isn't waiting for 121 // the full initialization of the storage panel on reload. 122 // To avoid exception when navigating back wait for indexed DB to be updated. 123 await waitUntil(() => isInTree(doc, ["indexedDB", "https://example.com"])); 124 125 info("Navigate backward to test bfcache navigation"); 126 gBrowser.goBack(); 127 await waitUntil( 128 () => 129 isInTree(doc, ["cookies", "https://example.net"]) && 130 isInTree(doc, ["cookies", "https://example.org"]) 131 ); 132 133 ok( 134 !isInTree(doc, ["cookies", "https://example.com"]), 135 "host of previous document (example.com) is not in the tree anymore" 136 ); 137 138 info("Check that the Cookies node still has the expected label"); 139 is( 140 getTreeNodeLabel(doc, ["cookies"]), 141 "Cookies", 142 "Cookies item is properly displayed" 143 ); 144 145 SpecialPowers.clearUserPref("network.cookie.sameSite.laxByDefault"); 146 });