зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1637619 - [remote] Implement Network.getAllCookies r=remote-protocol-reviewers,whimboo
Differential Revision: https://phabricator.services.mozilla.com/D75115
This commit is contained in:
Родитель
9e0ee8988a
Коммит
dcaf3bd40f
|
@ -155,6 +155,25 @@ class Network extends Domain {
|
|||
Services.io.offline = offline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all browser cookies.
|
||||
*
|
||||
* Depending on the backend support, will return detailed cookie information in the cookies field.
|
||||
*
|
||||
* @param {Object} options
|
||||
*
|
||||
* @return {Array<Cookie>}
|
||||
* Array of cookie objects.
|
||||
*/
|
||||
async getAllCookies(options = {}) {
|
||||
const cookies = [];
|
||||
for (const cookie of Services.cookies.cookies) {
|
||||
cookies.push(_buildCookie(cookie));
|
||||
}
|
||||
|
||||
return { cookies };
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all browser cookies for the current URL.
|
||||
*
|
||||
|
@ -192,29 +211,7 @@ class Network extends Domain {
|
|||
continue;
|
||||
}
|
||||
|
||||
const data = {
|
||||
name: cookie.name,
|
||||
value: cookie.value,
|
||||
domain: cookie.host,
|
||||
path: cookie.path,
|
||||
expires: cookie.isSession ? -1 : cookie.expiry,
|
||||
// The size is the combined length of both the cookie name and value
|
||||
size: cookie.name.length + cookie.value.length,
|
||||
httpOnly: cookie.isHttpOnly,
|
||||
secure: cookie.isSecure,
|
||||
session: cookie.isSession,
|
||||
};
|
||||
|
||||
if (cookie.sameSite) {
|
||||
const sameSiteMap = new Map([
|
||||
[Ci.nsICookie.SAMESITE_LAX, "Lax"],
|
||||
[Ci.nsICookie.SAMESITE_STRICT, "Strict"],
|
||||
]);
|
||||
|
||||
data.sameSite = sameSiteMap.get(cookie.sameSite);
|
||||
}
|
||||
|
||||
cookies.push(data);
|
||||
cookies.push(_buildCookie(cookie));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -462,6 +459,40 @@ function getLoadContext(httpChannel) {
|
|||
return loadContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a CDP Network.Cookie from our internal cookie values
|
||||
*
|
||||
* @param {nsICookie} cookie
|
||||
*
|
||||
* @returns {Network.Cookie}
|
||||
* A CDP Cookie
|
||||
*/
|
||||
function _buildCookie(cookie) {
|
||||
const data = {
|
||||
name: cookie.name,
|
||||
value: cookie.value,
|
||||
domain: cookie.host,
|
||||
path: cookie.path,
|
||||
expires: cookie.isSession ? -1 : cookie.expiry,
|
||||
// The size is the combined length of both the cookie name and value
|
||||
size: cookie.name.length + cookie.value.length,
|
||||
httpOnly: cookie.isHttpOnly,
|
||||
secure: cookie.isSecure,
|
||||
session: cookie.isSession,
|
||||
};
|
||||
|
||||
if (cookie.sameSite) {
|
||||
const sameSiteMap = new Map([
|
||||
[Ci.nsICookie.SAMESITE_LAX, "Lax"],
|
||||
[Ci.nsICookie.SAMESITE_STRICT, "Strict"],
|
||||
]);
|
||||
|
||||
data.sameSite = sameSiteMap.get(cookie.sameSite);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a array of possibly repeating header names, merge the values for
|
||||
* duplicate headers into a comma-separated list, or in some cases a
|
||||
|
|
|
@ -15,6 +15,7 @@ support-files =
|
|||
|
||||
[browser_deleteCookies.js]
|
||||
[browser_emulateNetworkConditions.js]
|
||||
[browser_getAllCookies.js]
|
||||
[browser_getCookies.js]
|
||||
skip-if = (os == 'win' && os_version == '10.0' && ccov) # Bug 1605650
|
||||
[browser_navigationEvents.js]
|
||||
|
|
|
@ -0,0 +1,225 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
"use strict";
|
||||
|
||||
const SJS_PATH = "/browser/remote/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 DEFAULT_URL = `${DEFAULT_HOST}${SJS_PATH}`;
|
||||
|
||||
add_task(async function noCookiesWhenNoneAreSet({ client }) {
|
||||
const { Network } = client;
|
||||
const { cookies } = await Network.getAllCookies();
|
||||
is(cookies.length, 0, "No cookies have been found");
|
||||
});
|
||||
|
||||
add_task(async function noCookiesForPristineContext({ client }) {
|
||||
const { Network } = client;
|
||||
await loadURL(DEFAULT_URL);
|
||||
|
||||
try {
|
||||
const { cookies } = await Network.getAllCookies();
|
||||
is(cookies.length, 0, "No cookies have been found");
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(async function allCookiesFromHostWithPort({ client }) {
|
||||
const { Network } = client;
|
||||
const PORT_URL = `${DEFAULT_HOST}:8000${SJS_PATH}?name=id&value=1`;
|
||||
await loadURL(PORT_URL);
|
||||
|
||||
const cookie = {
|
||||
name: "id",
|
||||
value: "1",
|
||||
};
|
||||
|
||||
try {
|
||||
const { cookies } = await Network.getAllCookies();
|
||||
is(cookies.length, 1, "All cookies have been found");
|
||||
assertCookie(cookies[0], cookie);
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(async function allCookiesFromMultipleOrigins({ client }) {
|
||||
const { Network } = client;
|
||||
await loadURL(`${ALT_HOST}${SJS_PATH}?name=users&value=password`);
|
||||
await loadURL(`${SECURE_HOST}${SJS_PATH}?name=secure&value=password`);
|
||||
await loadURL(`${DEFAULT_URL}?name=foo&value=bar`);
|
||||
|
||||
const cookie1 = { name: "foo", value: "bar", domain: "example.org" };
|
||||
const cookie2 = { name: "secure", value: "password", domain: "example.com" };
|
||||
const cookie3 = { name: "users", value: "password", domain: "example.net" };
|
||||
|
||||
try {
|
||||
const { cookies } = await Network.getAllCookies();
|
||||
cookies.sort((a, b) => a.name.localeCompare(b.name));
|
||||
is(cookies.length, 3, "All cookies have been found");
|
||||
assertCookie(cookies[0], cookie1);
|
||||
assertCookie(cookies[1], cookie2);
|
||||
assertCookie(cookies[2], cookie3);
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(async function secure({ client }) {
|
||||
const { Network } = client;
|
||||
await loadURL(`${SECURE_HOST}${SJS_PATH}?name=foo&value=bar&secure`);
|
||||
|
||||
const cookie = {
|
||||
name: "foo",
|
||||
value: "bar",
|
||||
domain: "example.com",
|
||||
secure: true,
|
||||
};
|
||||
|
||||
try {
|
||||
// Cookie returned for secure protocols
|
||||
let result = await Network.getAllCookies();
|
||||
is(result.cookies.length, 1, "The secure cookie has been found");
|
||||
assertCookie(result.cookies[0], cookie);
|
||||
|
||||
// For unsecure protocols the secure cookies are also returned
|
||||
await loadURL(DEFAULT_URL);
|
||||
result = await Network.getAllCookies();
|
||||
is(result.cookies.length, 1, "The secure cookie has been found");
|
||||
assertCookie(result.cookies[0], cookie);
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(async function expiry({ client }) {
|
||||
const { Network } = client;
|
||||
const date = new Date();
|
||||
date.setDate(date.getDate() + 3);
|
||||
|
||||
const encodedDate = encodeURI(date.toUTCString());
|
||||
await loadURL(`${DEFAULT_URL}?name=foo&value=bar&expiry=${encodedDate}`);
|
||||
|
||||
const cookie = {
|
||||
name: "foo",
|
||||
value: "bar",
|
||||
expires: Math.floor(date.getTime() / 1000),
|
||||
session: false,
|
||||
};
|
||||
|
||||
try {
|
||||
const { cookies } = await Network.getAllCookies();
|
||||
is(cookies.length, 1, "A single cookie has been found");
|
||||
assertCookie(cookies[0], cookie);
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(async function session({ client }) {
|
||||
const { Network } = client;
|
||||
await loadURL(`${DEFAULT_URL}?name=foo&value=bar`);
|
||||
|
||||
const cookie = {
|
||||
name: "foo",
|
||||
value: "bar",
|
||||
expiry: -1,
|
||||
session: true,
|
||||
};
|
||||
|
||||
try {
|
||||
const { cookies } = await Network.getAllCookies();
|
||||
is(cookies.length, 1, "A single cookie has been found");
|
||||
assertCookie(cookies[0], cookie);
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(async function path({ client }) {
|
||||
const { Network } = client;
|
||||
const PATH = "/browser/remote/test/browser/";
|
||||
const PARENT_PATH = "/browser/remote/test/";
|
||||
|
||||
await loadURL(`${DEFAULT_URL}?name=foo&value=bar&path=${PATH}`);
|
||||
|
||||
const cookie = {
|
||||
name: "foo",
|
||||
value: "bar",
|
||||
path: PATH,
|
||||
};
|
||||
|
||||
try {
|
||||
console.log("Check exact path");
|
||||
await loadURL(`${DEFAULT_HOST}${PATH}`);
|
||||
let result = await Network.getAllCookies();
|
||||
is(result.cookies.length, 1, "A single cookie has been found");
|
||||
assertCookie(result.cookies[0], cookie);
|
||||
|
||||
console.log("Check sub path");
|
||||
await loadURL(`${DEFAULT_HOST}${SJS_PATH}`);
|
||||
result = await Network.getAllCookies();
|
||||
is(result.cookies.length, 1, "A single cookie has been found");
|
||||
assertCookie(result.cookies[0], cookie);
|
||||
|
||||
console.log("Check parent path");
|
||||
await loadURL(`${DEFAULT_HOST}${PARENT_PATH}`);
|
||||
result = await Network.getAllCookies();
|
||||
is(result.cookies.length, 1, "A single cookie has been found");
|
||||
assertCookie(result.cookies[0], cookie);
|
||||
|
||||
console.log("Check non matching path");
|
||||
await loadURL(`${DEFAULT_HOST}/foo/bar`);
|
||||
result = await Network.getAllCookies();
|
||||
is(result.cookies.length, 1, "A single cookie has been found");
|
||||
assertCookie(result.cookies[0], cookie);
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(async function httpOnly({ client }) {
|
||||
const { Network } = client;
|
||||
await loadURL(`${DEFAULT_URL}?name=foo&value=bar&httpOnly`);
|
||||
|
||||
const cookie = {
|
||||
name: "foo",
|
||||
value: "bar",
|
||||
httpOnly: true,
|
||||
};
|
||||
|
||||
try {
|
||||
const { cookies } = await Network.getAllCookies();
|
||||
is(cookies.length, 1, "A single cookie has been found");
|
||||
assertCookie(cookies[0], cookie);
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(async function sameSite({ client }) {
|
||||
const { Network } = client;
|
||||
for (const value of ["Lax", "Strict"]) {
|
||||
console.log(`Test cookie with sameSite=${value}`);
|
||||
await loadURL(`${DEFAULT_URL}?name=foo&value=bar&sameSite=${value}`);
|
||||
|
||||
const cookie = {
|
||||
name: "foo",
|
||||
value: "bar",
|
||||
sameSite: value,
|
||||
};
|
||||
|
||||
try {
|
||||
const { cookies } = await Network.getAllCookies();
|
||||
is(cookies.length, 1, "A single cookie has been found");
|
||||
assertCookie(cookies[0], cookie);
|
||||
} finally {
|
||||
Services.cookies.removeAll();
|
||||
}
|
||||
}
|
||||
});
|
Загрузка…
Ссылка в новой задаче