Skip to content

Commit

Permalink
LibWeb: Impose a sane max cookie size
Browse files Browse the repository at this point in the history
Drop cookies larger than 4KiB. This value is the RFC's recommendation:
https://tools.ietf.org/html/rfc6265#section-6.1
  • Loading branch information
trflynn89 authored and awesomekling committed Apr 16, 2021
1 parent da92c0e commit 67884f6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Base/res/html/misc/cookie.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ <h3>Invalid cookies (the browser should reject these):</h3>
<label for=invalid3>The cookie expired in the past</label>
<br /><input id=invalid4 type=button onclick="setCookie(this.value)" value="cookie7=value7; expires=Mon, 23 Jan 1989 08:10:36 GMT" />
<label for=invalid4>The cookie expired in the past</label>
<br /><input id=invalid5 type=button onclick="setTooLargeCookie()" value="cookie10=[more than 4096 chars]" />
<label for=invalid5>The cookie is too large</label>
<br />

<h3>Unretrievable cookies (the browser should accept these but not display them):</h3>
Expand All @@ -31,6 +33,11 @@ <h3>Unretrievable cookies (the browser should accept these but not display them)
document.getElementById('cookies').innerHTML = document.cookie;
}

function setTooLargeCookie() {
const cookie = 'name=' + 'x'.repeat(4 << 10);
setCookie(cookie);
}

document.getElementById('cookies').innerHTML = document.cookie;
</script>
</body>
6 changes: 6 additions & 0 deletions Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

namespace Web::Cookie {

static constexpr size_t s_max_cookie_size = 4096;

static void parse_attributes(ParsedCookie& parsed_cookie, StringView unparsed_attributes);
static void process_attribute(ParsedCookie& parsed_cookie, StringView attribute_name, StringView attribute_value);
static void on_expires_attribute(ParsedCookie& parsed_cookie, StringView attribute_value);
Expand All @@ -43,6 +45,10 @@ static Optional<Core::DateTime> parse_date_time(StringView date_string);
Optional<ParsedCookie> parse_cookie(const String& cookie_string)
{
// https://tools.ietf.org/html/rfc6265#section-5.2

if (cookie_string.length() > s_max_cookie_size)
return {};

StringView name_value_pair;
StringView unparsed_attributes;

Expand Down

0 comments on commit 67884f6

Please sign in to comment.