summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_cookies_sameSite.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /browser/components/sessionstore/test/browser_cookies_sameSite.js
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/sessionstore/test/browser_cookies_sameSite.js')
-rw-r--r--browser/components/sessionstore/test/browser_cookies_sameSite.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/browser/components/sessionstore/test/browser_cookies_sameSite.js b/browser/components/sessionstore/test/browser_cookies_sameSite.js
new file mode 100644
index 0000000000..bc680dac25
--- /dev/null
+++ b/browser/components/sessionstore/test/browser_cookies_sameSite.js
@@ -0,0 +1,89 @@
+"use strict";
+
+const TEST_HTTP_URL = "http://example.com";
+const TEST_HTTPS_URL = "https://example.com";
+const MAX_EXPIRY = Math.pow(2, 62);
+
+function getSingleCookie() {
+ let cookies = Array.from(Services.cookies.cookies);
+ Assert.equal(cookies.length, 1, "expected one cookie");
+ return cookies[0];
+}
+
+async function verifyRestore(url, sameSiteSetting) {
+ Services.cookies.removeAll();
+
+ // Make sure that sessionstore.js can be forced to be created by setting
+ // the interval pref to 0.
+ await SpecialPowers.pushPrefEnv({
+ set: [["browser.sessionstore.interval", 0]],
+ });
+
+ let tab = BrowserTestUtils.addTab(gBrowser, url);
+ await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+
+ // Add a cookie with specific same-site setting.
+ let r = Math.floor(Math.random() * MAX_EXPIRY);
+ Services.cookies.add(
+ url,
+ "/",
+ "name" + r,
+ "value" + r,
+ false,
+ false,
+ true,
+ MAX_EXPIRY,
+ {},
+ sameSiteSetting,
+ url.startsWith("https:")
+ ? Ci.nsICookie.SCHEME_HTTPS
+ : Ci.nsICookie.SCHEME_HTTP
+ );
+ await TabStateFlusher.flush(tab.linkedBrowser);
+
+ // Get the sessionstore state for the window.
+ let state = ss.getBrowserState();
+
+ // Verify our cookie got set.
+ let cookie = getSingleCookie();
+
+ // Remove the cookie.
+ Services.cookies.removeAll();
+
+ // Restore the window state.
+ await setBrowserState(state);
+
+ // At this point, the cookie should be restored.
+ let cookie2 = getSingleCookie();
+
+ is(
+ cookie2.sameSite,
+ cookie.sameSite,
+ "cookie same-site flag successfully restored"
+ );
+
+ is(
+ cookie2.schemeMap,
+ cookie.schemeMap,
+ "cookie schemeMap flag successfully restored"
+ );
+
+ // Clean up.
+ Services.cookies.removeAll();
+ BrowserTestUtils.removeTab(gBrowser.tabs[1]);
+}
+
+/**
+ * Tests that cookie.sameSite flag is stored and restored correctly by
+ * sessionstore.
+ */
+add_task(async function() {
+ // Test for various possible values of cookie.sameSite and schemeMap.
+ await verifyRestore(TEST_HTTP_URL, Ci.nsICookie.SAMESITE_NONE);
+ await verifyRestore(TEST_HTTP_URL, Ci.nsICookie.SAMESITE_LAX);
+ await verifyRestore(TEST_HTTP_URL, Ci.nsICookie.SAMESITE_STRICT);
+
+ await verifyRestore(TEST_HTTPS_URL, Ci.nsICookie.SAMESITE_NONE);
+ await verifyRestore(TEST_HTTPS_URL, Ci.nsICookie.SAMESITE_LAX);
+ await verifyRestore(TEST_HTTPS_URL, Ci.nsICookie.SAMESITE_STRICT);
+});