Bug 1605362 - Network.getCookies() returns all cookies of current page and all of its sub frames as well. r=webdriver-reviewers,jdescottes

Added check in Network.getCookies() to skip duplicate cookies
Also added tests.
Created html files for running tests which set cookies
- remote/cdp/test/browser/network/doc_get_cookies_page.html
- remote/cdp/test/browser/network/doc_get_cookies_frame.html
Added tests to /remote/cdp/test/browser/network/browser_getCookies.js
Updated Puppeteer tests-
- changed Page.setCookie should set secure same-site cookies from a frame spec to PASS

Differential Revision: https://phabricator.services.mozilla.com/D122382
This commit is contained in:
Nafees Nehar 2021-08-17 19:24:45 +00:00
parent 40c63c264c
commit 7b783556f9
6 changed files with 75 additions and 5 deletions

View File

@ -185,7 +185,9 @@ class Network extends Domain {
*/
async getCookies(options = {}) {
// Bug 1605354 - Add support for options.urls
const urls = [this.session.target.url];
const urls = this.session.target.browsingContext
.getAllBrowsingContextsInSubtree()
.map(context => context.currentURI.spec);
const cookies = [];
for (let url of urls) {
@ -209,7 +211,20 @@ class Network extends Domain {
continue;
}
cookies.push(_buildCookie(cookie));
const builtCookie = _buildCookie(cookie);
const duplicateCookie = cookies.some(value => {
return (
value.name === builtCookie.name &&
value.path === builtCookie.path &&
value.domain === builtCookie.domain
);
});
if (duplicateCookie) {
continue;
}
cookies.push(builtCookie);
}
}

View File

@ -8,6 +8,8 @@ support-files =
head.js
doc_empty.html
doc_frameset.html
doc_get_cookies_frame.html
doc_get_cookies_page.html
doc_networkEvents.html
file_networkEvents.js
file_framesetEvents.js

View File

@ -3,12 +3,13 @@
"use strict";
const SJS_PATH = "/browser/remote/cdp/test/browser/network/sjs-cookies.sjs";
const DEFAULT_HOST = "http://example.org";
const ALT_HOST = "http://example.net";
const SECURE_HOST = "https://example.com";
const BASE_PATH = "/browser/remote/cdp/test/browser/network";
const SJS_PATH = `${BASE_PATH}/sjs-cookies.sjs`;
const DEFAULT_URL = `${DEFAULT_HOST}${SJS_PATH}`;
add_task(async function noCookiesWhenNoneAreSet({ client }) {
@ -68,6 +69,26 @@ add_task(async function allCookiesFromCurrentURL({ client }) {
}
});
add_task(async function allCookiesIncludingSubFrames({ client }) {
const GET_COOKIES_PAGE_URL = `${DEFAULT_HOST}${BASE_PATH}/doc_get_cookies_page.html`;
const { Network } = client;
await loadURL(GET_COOKIES_PAGE_URL);
const cookie_page = { name: "page", value: "mainpage", path: BASE_PATH };
const cookie_frame = { name: "frame", value: "subframe", path: BASE_PATH };
try {
const { cookies } = await Network.getCookies();
cookies.sort((a, b) => a.name.localeCompare(b.name));
is(cookies.length, 2, "All cookies have been found including subframe");
assertCookie(cookies[0], cookie_frame);
assertCookie(cookies[1], cookie_page);
} finally {
Services.cookies.removeAll();
}
});
add_task(async function secure({ client }) {
const { Network } = client;
await loadURL(`${SECURE_HOST}${SJS_PATH}?name=foo&value=bar&secure`);

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Sub Frame to get cookies</title>
</head>
<body>
<div id="content">Example Sub Frame</div>
<script type="text/javascript">
document.cookie = "frame=subframe";
</script>
</body>
</html>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Page to get cookies</title>
</head>
<body>
<iframe src="doc_get_cookies_frame.html"></iframe>
<script type="text/javascript">
document.cookie = "page=mainpage";
</script>
</body>
</html>

View File

@ -234,7 +234,7 @@
"FAIL"
],
"Cookie specs Page.setCookie should set secure same-site cookies from a frame (cookies.spec.ts)": [
"FAIL"
"PASS"
],
"Cookie specs Page.deleteCookie should work (cookies.spec.ts)": [
"FAIL"