diff --git a/browser/components/contextualidentity/test/browser/browser.ini b/browser/components/contextualidentity/test/browser/browser.ini index af7bd6dbafaf..30150bde4c8f 100644 --- a/browser/components/contextualidentity/test/browser/browser.ini +++ b/browser/components/contextualidentity/test/browser/browser.ini @@ -12,6 +12,7 @@ support-files = [browser_favicon.js] [browser_forgetaboutsite.js] [browser_forgetAPI_cookie_getCookiesWithOriginAttributes.js] +[browser_restore_getCookiesWithOriginAttributes.js] [browser_forgetAPI_EME_forgetThisSite.js] [browser_forgetAPI_quota_clearStoragesForPrincipal.js] [browser_newtabButton.js] diff --git a/browser/components/contextualidentity/test/browser/browser_restore_getCookiesWithOriginAttributes.js b/browser/components/contextualidentity/test/browser/browser_restore_getCookiesWithOriginAttributes.js new file mode 100644 index 000000000000..35cad08c31c8 --- /dev/null +++ b/browser/components/contextualidentity/test/browser/browser_restore_getCookiesWithOriginAttributes.js @@ -0,0 +1,114 @@ +/* + * Bug 1334587 - A Test case for checking whether forgetting APIs are working for cookies. + */ + +const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu } = Components; + +const TEST_HOST = "example.com"; +const TEST_URL = "http://" + TEST_HOST + "/browser/browser/components/contextualidentity/test/browser/"; + +const USER_CONTEXTS = [ + "default", + "personal", + "work" +]; + +const DELETE_CONTEXT = 1; +const COOKIE_NAME = "userContextId"; + +// +// Support functions. +// + +function* openTabInUserContext(uri, userContextId) { + // Open the tab in the correct userContextId. + let tab = gBrowser.addTab(uri, {userContextId}); + + // Select tab and make sure its browser is focused. + gBrowser.selectedTab = tab; + tab.ownerGlobal.focus(); + + let browser = gBrowser.getBrowserForTab(tab); + yield BrowserTestUtils.browserLoaded(browser); + return {tab, browser}; +} + +function getCookiesForOA(host, userContextId) { + return Services.cookies.getCookiesFromHost(host, {userContextId}); +} + +// +// Test functions. +// + +add_task(function* setup() { + // Make sure userContext is enabled. + yield SpecialPowers.pushPrefEnv({"set": [ + [ "privacy.userContext.enabled", true ], + ]}); +}); + +function checkCookies(ignoreContext = null) { + for (let userContextId of Object.keys(USER_CONTEXTS)) { + if (ignoreContext && userContextId === String(ignoreContext)) { + continue; + } + let enumerator = getCookiesForOA(TEST_HOST, userContextId); + ok(enumerator.hasMoreElements(), "Cookies available"); + + let foundCookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); + is(foundCookie["name"], COOKIE_NAME, "Check cookie name"); + is(foundCookie["value"], USER_CONTEXTS[userContextId], "Check cookie value"); + } +} + +function deleteCookies(onlyContext = null) { + // Using getCookiesWithOriginAttributes() to get all cookies for a certain + // domain by using the originAttributes pattern, and clear all these cookies. + let enumerator = Services.cookies.getCookiesWithOriginAttributes(JSON.stringify({}), TEST_HOST); + while (enumerator.hasMoreElements()) { + let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie); + if (!onlyContext || cookie.originAttributes.userContextId == onlyContext) { + Services.cookies.remove(cookie.host, cookie.name, cookie.path, false, cookie.originAttributes); + } + } +} + +add_task(function* test_cookie_getCookiesWithOriginAttributes() { + let tabs = []; + + for (let userContextId of Object.keys(USER_CONTEXTS)) { + // Load the page in different contexts and set a cookie + // which should only be visible in that context. + let value = USER_CONTEXTS[userContextId]; + + // Open our tab in the given user context. + tabs[userContextId] = yield* openTabInUserContext(TEST_URL + "file_reflect_cookie_into_title.html?" + value, userContextId); + + // Close this tab. + yield BrowserTestUtils.removeTab(tabs[userContextId].tab); + } + + // Check that cookies have been set properly. + for (let userContextId of Object.keys(USER_CONTEXTS)) { + let enumerator = getCookiesForOA(TEST_HOST, userContextId); + ok(enumerator.hasMoreElements(), "Cookies available"); + + let foundCookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); + is(foundCookie["name"], COOKIE_NAME, "Check cookie name"); + is(foundCookie["value"], USER_CONTEXTS[userContextId], "Check cookie value"); + } + checkCookies(); + + deleteCookies(DELETE_CONTEXT); + + checkCookies(DELETE_CONTEXT); + + deleteCookies(); + + // Check that whether cookies has been cleared. + for (let userContextId of Object.keys(USER_CONTEXTS)) { + let e = getCookiesForOA(TEST_HOST, userContextId); + ok(!e.hasMoreElements(), "No Cookie should be here"); + } +}); diff --git a/netwerk/cookie/nsCookieService.cpp b/netwerk/cookie/nsCookieService.cpp index 7588097eaf6a..0cd86ec1337d 100644 --- a/netwerk/cookie/nsCookieService.cpp +++ b/netwerk/cookie/nsCookieService.cpp @@ -1517,13 +1517,13 @@ nsCookieService::TryInitDB(bool aRecreateDB) rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING( "DELETE FROM moz_cookies " - "WHERE name = :name AND host = :host AND path = :path"), + "WHERE name = :name AND host = :host AND path = :path AND originAttributes = :originAttributes"), getter_AddRefs(mDefaultDBState->stmtDelete)); NS_ENSURE_SUCCESS(rv, RESULT_RETRY); rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING( "UPDATE moz_cookies SET lastAccessed = :lastAccessed " - "WHERE name = :name AND host = :host AND path = :path"), + "WHERE name = :name AND host = :host AND path = :path AND originAttributes = :originAttributes"), getter_AddRefs(mDefaultDBState->stmtUpdate)); NS_ENSURE_SUCCESS(rv, RESULT_RETRY); @@ -5004,6 +5004,12 @@ nsCookieService::RemoveCookieFromList(const nsListIter &aIter, aIter.Cookie()->Path()); NS_ASSERT_SUCCESS(rv); + nsAutoCString suffix; + aIter.Cookie()->OriginAttributesRef().CreateSuffix(suffix); + rv = params->BindUTF8StringByName( + NS_LITERAL_CSTRING("originAttributes"), suffix); + NS_ASSERT_SUCCESS(rv); + rv = paramsArray->AddParams(params); NS_ASSERT_SUCCESS(rv); @@ -5183,6 +5189,12 @@ nsCookieService::UpdateCookieInList(nsCookie *aCookie, aCookie->Path()); NS_ASSERT_SUCCESS(rv); + nsAutoCString suffix; + aCookie->OriginAttributesRef().CreateSuffix(suffix); + rv = params->BindUTF8StringByName( + NS_LITERAL_CSTRING("originAttributes"), suffix); + NS_ASSERT_SUCCESS(rv); + // Add our bound parameters to the array. rv = aParamsArray->AddParams(params); NS_ASSERT_SUCCESS(rv);