Skip to content

Truly independent web browser

LibHTTP: Stop percent-decoding the default cookie path

Tim Ledbetterefb849fd402b
diff --git a/Libraries/LibHTTP/Cookie/Cookie.cpp b/Libraries/LibHTTP/Cookie/Cookie.cpp
index 3322259d351..297ce62905d 100644
--- a/Libraries/LibHTTP/Cookie/Cookie.cpp
+++ b/Libraries/LibHTTP/Cookie/Cookie.cpp
@@ -131,15 +131,14 @@ bool path_matches(StringView request_path, StringView cookie_path)
String default_path(URL::URL const& url)
{
// 1. Let uri-path be the path portion of the request-uri if such a portion exists (and empty otherwise).
- auto uri_path = URL::percent_decode(url.serialize_path());
+ auto uri_path = url.serialize_path();
// 2. If the uri-path is empty or if the first character of the uri-path is not a %x2F ("/") character, output
// %x2F ("/") and skip the remaining steps.
if (uri_path.is_empty() || (uri_path[0] != '/'))
return "/"_string;
- StringView uri_path_view = uri_path;
- size_t last_separator = uri_path_view.find_last('/').value();
+ size_t last_separator = uri_path.find_last('/').value();
// 3. If the uri-path contains no more than one %x2F ("/") character, output %x2F ("/") and skip the remaining step.
if (last_separator == 0)
@@ -147,7 +146,6 @@ String default_path(URL::URL const& url)
// 4. Output the characters of the uri-path from the first character up to, but not including, the right-most
// %x2F ("/").
- // FIXME: The path might not be valid UTF-8.
return MUST(String::from_utf8(uri_path.substring_view(0, last_separator)));
}
diff --git a/Tests/LibWeb/Text/expected/cookie-default-path-keeps-percent-encoding.txt b/Tests/LibWeb/Text/expected/cookie-default-path-keeps-percent-encoding.txt
new file mode 100644
index 00000000000..fe884fa41ad
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/cookie-default-path-keeps-percent-encoding.txt
@@ -0,0 +1,2 @@
+/a/%C3%A9/page.html: "no-path=value; relative-path=value"
+/a/%FF/page.html: "no-path=value; relative-path=value"
diff --git a/Tests/LibWeb/Text/input/cookie-default-path-keeps-percent-encoding.html b/Tests/LibWeb/Text/input/cookie-default-path-keeps-percent-encoding.html
new file mode 100644
index 00000000000..969f06d2f7a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/cookie-default-path-keeps-percent-encoding.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ test(() => {
+ const host = `cookie-${crypto.randomUUID()}.example.com`;
+ for (const path of ["/a/%C3%A9/page.html", "/a/%FF/page.html"]) {
+ spoofCurrentURL(`https://${host}${path}`);
+ document.cookie = "no-path=value";
+ document.cookie = "relative-path=value; path=x";
+ println(`${path}: "${document.cookie.split("; ").sort().join("; ")}"`);
+ }
+ });
+</script>