Commit 4f79553f authored by Ayu Ishii's avatar Ayu Ishii Committed by Commit Bot

CookieStore: Add wpt for path behavior

This adds changes to the Modification API path behavior
to match the explainer[1].

1. Rejects relative paths (not starting with /)
2. Adds / to the end of paths that don't end in /

[1]https://github.com/WICG/cookie-store/blob/master/explainer.md#paths

Change-Id: Iefaef4e60f718727da45f99c8d6504a387560fcb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2131031Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Commit-Queue: Ayu Ishii <ayui@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755384}
parent c9e8ebd9
This module implements the
[Async Cookies API](https://github.com/WICG/async-cookies-api). The
[Cookie Store API](https://github.com/WICG/cookie-store). The
implementation is under active development, and its progress is tracked in
[this OWP Launch bug](https://crbug.com/729800
[this OWP Launch bug](https://crbug.com/729800)
......@@ -105,6 +105,17 @@ base::Optional<CanonicalCookie> ToCanonicalCookie(
domain = cookie_url_host;
}
String path = options->path();
if (!path.IsEmpty()) {
if (!path.StartsWith("/")) {
exception_state.ThrowTypeError("Cookie path must start with \"/\"");
return base::nullopt;
}
if (!path.EndsWith("/")) {
path = path + String("/");
}
}
// Although the Cookie Store API spec always defaults the "secure" cookie
// attribute to true, we only default to true on cryptographically secure
// origins, where only secure cookies may be written, and to false otherwise,
......@@ -145,7 +156,7 @@ base::Optional<CanonicalCookie> ToCanonicalCookie(
}
return CanonicalCookie::Create(
name, value, domain, options->path(), base::Time() /*creation*/, expires,
name, value, domain, path, base::Time() /*creation*/, expires,
base::Time() /*last_access*/, secure, false /*http_only*/, same_site,
CanonicalCookie::kDefaultPriority, source_scheme_enum);
}
......
This directory contains tests for the
[Async Cookies API](https://github.com/WICG/cookie-store).
[Cookie Store API](https://github.com/WICG/cookie-store).
## Note on cookie naming conventions
......
......@@ -169,6 +169,28 @@ promise_test(async testCase => {
assert_array_equals(Object.keys(cookie).sort(), kCookieListItemKeys);
}, 'CookieListItem - cookieStore.set with path set to the current directory');
promise_test(async testCase => {
const currentUrl = new URL(self.location.href);
const currentPath = currentUrl.pathname;
const currentDirectory = currentPath.substr(0, currentPath.lastIndexOf('/'));
await cookieStore.delete({ name: 'cookie-name', path: currentDirectory });
await cookieStore.set('cookie-name', 'cookie-value',
{ path: currentDirectory });
testCase.add_cleanup(async () => {
await cookieStore.delete({ name: 'cookie-name', path: currentDirectory });
});
const cookie = await cookieStore.get('cookie-name');
assert_equals(cookie.name, 'cookie-name');
assert_equals(cookie.value, 'cookie-value');
assert_equals(cookie.domain, null);
assert_equals(cookie.path, currentDirectory + '/');
assert_equals(cookie.expires, null);
assert_equals(cookie.secure, true);
assert_equals(cookie.sameSite, 'strict');
assert_array_equals(Object.keys(cookie).sort(), kCookieListItemKeys);
}, 'CookieListItem - cookieStore.set adds / to path if it does not end with /');
promise_test(async testCase => {
await cookieStore.delete('cookie-name');
......
......@@ -139,6 +139,21 @@ promise_test(async testCase => {
assert_equals(cookie.value, 'cookie-value');
}, 'cookieStore.delete with path set to subdirectory of the current directory');
promise_test(async testCase => {
const currentUrl = new URL(self.location.href);
const currentPath = currentUrl.pathname;
const currentDirectory = currentPath.substr(0, currentPath.lastIndexOf('/'));
await cookieStore.set(
'cookie-name', 'cookie-value', { path: currentDirectory + '/' });
testCase.add_cleanup(async () => {
await cookieStore.delete({ name: 'cookie-name', path: currentDirectory });
});
await cookieStore.delete({ name: 'cookie-name', path: currentDirectory });
const cookie = await cookieStore.get('cookie-name');
assert_equals(cookie, null);
}, 'cookieStore.delete with missing / at the end of path');
promise_test(async testCase => {
await cookieStore.set('cookie-name', 'cookie-value');
testCase.add_cleanup(async () => {
......
......@@ -259,6 +259,34 @@ promise_test(async testCase => {
assert_equals(cookies[0].value, 'cookie-new-value');
}, 'cookieStore.set default path is /');
promise_test(async testCase => {
const currentUrl = new URL(self.location.href);
const currentPath = currentUrl.pathname;
const currentDirectory = currentPath.substr(0, currentPath.lastIndexOf('/'));
await cookieStore.delete({ name: 'cookie-name', path: currentDirectory });
await cookieStore.set(
'cookie-name', 'cookie-value', { path: currentDirectory });
testCase.add_cleanup(async () => {
await cookieStore.delete({ name: 'cookie-name', path: currentDirectory });
});
const cookie = await cookieStore.get('cookie-name');
assert_equals(cookie.name, 'cookie-name');
assert_equals(cookie.value, 'cookie-value');
assert_equals(cookie.path, currentDirectory + '/');
}, 'cookieStore.set adds / to path that does not end with /');
promise_test(async testCase => {
const currentUrl = new URL(self.location.href);
const currentPath = currentUrl.pathname;
const currentDirectory =
currentPath.substr(0, currentPath.lastIndexOf('/') + 1);
const invalidPath = currentDirectory.substr(1);
await promise_rejects_js(testCase, TypeError, cookieStore.set(
'cookie-name', 'cookie-value', { path: invalidPath }));
}, 'cookieStore.set with path that does not start with /');
promise_test(async testCase => {
await cookieStore.set('cookie-name', 'old-cookie-value');
testCase.add_cleanup(async () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment