Repository navigation
Add URL path equality and prefix comparison - #929
Conversation
Cookies needs to compare URL paths, both for equality and for whether one path contains another. Comparing serializations is not right for either: an opaque path can serialize identically to a list of URL path segments, and one path's serialization can be a prefix of another's without covering whole segments. Also let the URL path serializer take a URL path, so that callers holding one no longer have to inline it, and record that a URL path segment never contains U+002F (/), which is what makes the prefix comparison meaningful. Closes #814. Tests: web-platform-tests/wpt#62165
domenic
left a comment
There was a problem hiding this comment.
This all feels a little unclean from the pure URL Standard perspective, but I appreciate this is needed for cookies and you're trying to balance.
No really substantial comments, although I think my final comment on the note is worth acting on.
| @@ -3220,15 +3220,18 @@ these steps. They return an <a>ASCII string</a>. | |||
|
|
|||
| <div algorithm> | |||
| <p>The <dfn export lt="URL path serializer|URL path serializing">URL path serializer</dfn> takes a | |||
There was a problem hiding this comment.
This feels subjectively icky. I'd rather have two algorithms which take well-defined types, with one delegating to the other. It'd be a bonus if the one that takes a path could stay non-exported.
There was a problem hiding this comment.
I've decided to keep this as-is. I don't think it's too bad. (I did investigate only having one, but that impacts too many downstream specifications.)
| <li><p>If <var>A</var>'s <a for=list>size</a> is not <var>B</var>'s <a for=list>size</a>, then | ||
| return false. | ||
|
|
||
| <li><p><a for=list>For each</a> <var>index</var> of <var>A</var>'s <a for=list>indices</a>: if |
There was a problem hiding this comment.
I guess whatwg/infra#710 would be useful here, but stalled.
| <p class=note>As <a for=/>URL path segments</a> never contain U+002F (/), this can only be true when | ||
| <var>B</var> covers whole segments of <var>A</var>. That makes it suitable for deciding whether one | ||
| <a for=/>URL path</a> is contained by another, which comparing serializations is not: the | ||
| serialization of « "<code>foo</code>" » is a prefix of that of « "<code>foobar</code>" ». |
There was a problem hiding this comment.
This note is helpful, but still a little confusing, in part because of how it moves between "starts with", "is a prefix", and "contains". Suggested, including some notable cross-links:
As URL path segments never contain U+002F (/), A starts with B only when B covers whole segments of A. This makes it suitable for deciding whether one URL path is "contained" by another. Comparing [=URL path serializer|serializations=] is not appropriate: the serialization of « "foobar" » [=string/starts with=] the serialization of « "foo" », but conceptually, a URL with the former path does not contain a URL with the latter path.
This may further benefit from a second note, or maybe warning, explaining that the concept of paths "containing" each other is not really a good precedent, and is only used by legacy APIs like cookies (or service worker?) to impose a kind of path-based authority system that is discordant with the same origin policy used by the rest of the platform.
There was a problem hiding this comment.
This is great. I've reused a bunch of your suggested words here.
Path-Matches compared serialized paths, which made a cookie path of /foo match a request path of /foobar, and passed URL paths to the URL path serializer, which takes a URL. Compare URL path segments instead, using the operations URL now provides. The Path attribute was split into a list that kept its leading empty segment, which is not how a URL path is represented, so such a path matched nothing and no "__Host-" prefixed cookie could be set. Isomorphic decode the attribute, drop that segment, and return failure if the result is not an ASCII string, rather than storing a path no request could ever carry. That check applies to the Path attribute that wins, so it happens once all attributes have been processed. Cookie Default Path removed an item from the URL path it was given, or replaced that path's first item, and it is called with a request URL's path. Operate on a clone instead. Also stop passing a path to Store a Cookie, which takes no such argument. Tests: web-platform-tests/wpt#62165 Depends on whatwg/url#929.
Cookies need to compare URL paths, both for equality and for whether one path contains another. Comparing serializations is not great: an opaque path can serialize identically to a list of URL path segments, and one path's
serialization can be a prefix of another's without covering whole segments.
Also let the URL path serializer take a URL path and record that the URL path segments in a list never contain U+002F (/), which is what makes the prefix comparison meaningful.
Tests: web-platform-tests/wpt#62165
Closes #814.
💥 AccessDenied: Access Denied 💥
PR Preview failed to build. (Last tried on Sep 7, 2026, 1:12 PM UTC).
More
If you don't have enough information above to solve the error by yourself, please file an issue.