Skip to content

Commit b060803

Browse files
committed
Cookie Store API: add branch for empty string path
https://bugs.webkit.org/show_bug.cgi?id=297268 Reviewed by NOBODY (OOPS!). Implement whatwg/cookiestore#283 with tests upstreamed at web-platform-tests/wpt#54264
1 parent 203bbd0 commit b060803

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
PASS CookieListItem - cookieStore.set with empty string path defaults to current URL
3+
PASS CookieListItem - cookieStore.set with empty string path defaults to current URL with __host- prefix
4+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- This file is required for WebKit test infrastructure to run the templated test -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// META: title=Cookie Store API: set()'s path option
2+
// META: script=/resources/testdriver.js
3+
// META: script=/resources/testdriver-vendor.js
4+
5+
promise_test(async testCase => {
6+
const currentUrl = new URL(self.location.href);
7+
const currentPath = currentUrl.pathname;
8+
await cookieStore.delete({ name: 'cookie-name', path: currentPath });
9+
10+
await cookieStore.set({ name: 'cookie-name', value: 'cookie-value', path: '' });
11+
testCase.add_cleanup(async () => {
12+
await cookieStore.delete({ name: 'cookie-name', path: currentPath });
13+
});
14+
15+
const internalCookie = await test_driver.get_named_cookie('cookie-name');
16+
assert_equals(internalCookie.path, currentPath);
17+
}, 'CookieListItem - cookieStore.set with empty string path defaults to current URL');
18+
19+
promise_test(async testCase => {
20+
const currentUrl = new URL(self.location.href);
21+
const currentPath = currentUrl.pathname;
22+
return promise_rejects_js(testCase, TypeError, cookieStore.set({ name: '__host-cookie-name', value: 'cookie-value', path: '' }));
23+
}, 'CookieListItem - cookieStore.set with empty string path defaults to current URL with __host- prefix');

LayoutTests/imported/w3c/web-platform-tests/cookiestore/w3c-import.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ List of files:
5353
/LayoutTests/imported/w3c/web-platform-tests/cookiestore/cookieStore_set_domain_parsing.sub.https.html
5454
/LayoutTests/imported/w3c/web-platform-tests/cookiestore/cookieStore_set_domain_parsing.tentative.sub.https.html
5555
/LayoutTests/imported/w3c/web-platform-tests/cookiestore/cookieStore_set_limit.https.any.js
56+
/LayoutTests/imported/w3c/web-platform-tests/cookiestore/cookieStore_set_path.https.window.js
5657
/LayoutTests/imported/w3c/web-platform-tests/cookiestore/cookieStore_special_names.https.any.js
5758
/LayoutTests/imported/w3c/web-platform-tests/cookiestore/cookieStore_subscribe_arguments.https.any.js
5859
/LayoutTests/imported/w3c/web-platform-tests/cookiestore/cookieStore_subscriptions_empty.https.window.js

Source/WebCore/Modules/cookie-store/CookieStore.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -442,25 +442,18 @@ void CookieStore::set(CookieInit&& options, Ref<DeferredPromise>&& promise)
442442
}
443443
}
444444

445-
if (cookie.name.startsWithIgnoringASCIICase("__Host-"_s)) {
446-
if (!options.domain.isNull()) {
447-
promise->reject(Exception { ExceptionCode::TypeError, "If the cookie name begins with \"__Host-\", the domain must not be specified."_s });
448-
return;
449-
}
450-
451-
if (!options.path.isNull() && options.path != "/"_s) {
452-
promise->reject(Exception { ExceptionCode::TypeError, "If the cookie name begins with \"__Host-\", the path must either not be specified or be \"/\"."_s });
453-
return;
454-
}
455-
}
456-
457445
cookie.domain = options.domain.isNull() ? domain : options.domain;
458446
if (!cookie.domain.isNull()) {
459447
if (cookie.domain.startsWith('.')) {
460448
promise->reject(Exception { ExceptionCode::TypeError, "The domain must not begin with a '.'"_s });
461449
return;
462450
}
463451

452+
if (cookie.name.startsWithIgnoringASCIICase("__Host-"_s)) {
453+
promise->reject(Exception { ExceptionCode::TypeError, "If the cookie name begins with \"__Host-\", the domain must not be specified."_s });
454+
return;
455+
}
456+
464457
if (!host.endsWith(cookie.domain) || (host.length() > cookie.domain.length() && !host.substring(0, host.length() - cookie.domain.length()).endsWith('.'))) {
465458
promise->reject(Exception { ExceptionCode::TypeError, "The domain must domain-match current host"_s });
466459
return;
@@ -485,17 +478,24 @@ void CookieStore::set(CookieInit&& options, Ref<DeferredPromise>&& promise)
485478
}
486479

487480
cookie.path = WTFMove(options.path);
488-
if (!cookie.path.isNull()) {
489-
if (!cookie.path.startsWith('/')) {
490-
promise->reject(Exception { ExceptionCode::TypeError, "The path must begin with a '/'"_s });
491-
return;
492-
}
481+
ASSERT(!cookie.path.isNull());
482+
if (cookie.path.isEmpty())
483+
cookie.path = url.path().toString();
493484

494-
// FIXME: <rdar://85515842> Obtain the encoded length without allocating and encoding.
495-
if (cookie.path.utf8().length() > maximumAttributeValueSize) {
496-
promise->reject(Exception { ExceptionCode::TypeError, makeString("The size of the path must not be greater than "_s, maximumAttributeValueSize, " bytes"_s) });
497-
return;
498-
}
485+
if (!cookie.path.startsWith('/')) {
486+
promise->reject(Exception { ExceptionCode::TypeError, "The path must begin with a '/'"_s });
487+
return;
488+
}
489+
490+
if (options.path != "/"_s && cookie.name.startsWithIgnoringASCIICase("__Host-"_s)) {
491+
promise->reject(Exception { ExceptionCode::TypeError, "If the cookie name begins with \"__Host-\", the path must either not be specified or be \"/\"."_s });
492+
return;
493+
}
494+
495+
// FIXME: <rdar://85515842> Obtain the encoded length without allocating and encoding.
496+
if (cookie.path.utf8().length() > maximumAttributeValueSize) {
497+
promise->reject(Exception { ExceptionCode::TypeError, makeString("The size of the path must not be greater than "_s, maximumAttributeValueSize, " bytes"_s) });
498+
return;
499499
}
500500

501501
if (options.expires) {

0 commit comments

Comments
 (0)