Skip to content

Validate the cookie domain for document.cookie and public suffixes - #1200

Merged
rbri merged 1 commit into
HtmlUnit:masterfrom
Vidya-m78:document-cookie-domain-check
Nov 18, 2026
Merged

rbri merged 1 commit into
HtmlUnit:masterfrom
Vidya-m78:document-cookie-domain-check

Conversation

@Vidya-m78

Copy link
Copy Markdown
Contributor

Repro: on a page from https://www.attacker.co.uk/ run document.cookie = 'sid=x; domain=victim.example; path=/'. HtmlUnit stores the cookie and sends it with every request to victim.example. domain=co.uk is accepted the same way and is sent to every *.co.uk site.
Cause: HttpClientConverter.parseCookie(), the parser behind document.cookie and <meta http-equiv=set-cookie>, only calls CookieSpec.parse() and never CookieSpec.validate(), so the domain-match check HttpClient runs for Set-Cookie headers is skipped. HtmlUnitDomainHandler has no public suffix check on either path (RFC 6265 section 5.3, steps 5 and 6).
Fix: validate in parseCookie(), and reject a multi-label public suffix domain in HtmlUnitDomainHandler.validate() unless it is the host itself. Single-label domains like com keep the existing handling in match(). The public suffix list bundled with httpclient 4.5.14 has no private-section entries such as github.io, so those are not covered.

CookieManager2Test.documentCookieForeignDomain and HttpClientConverterTest.parseCookieDomain fail without the fix. CookieManager4Test/5Test need the htmlunit-dev.org hosts setup, which I could not run locally.

HttpClientConverter.parseCookie() (used by document.cookie and meta
http-equiv set-cookie) never called CookieSpec.validate(), so the
domain attribute was not checked against the page. A page from
attacker.example could set a cookie with domain=victim.example and
HtmlUnit sent it to victim.example. HttpClient does this check for
Set-Cookie headers already.

HtmlUnitDomainHandler.validate() now also rejects a domain that is a
public suffix (e.g. co.uk) unless it is the host itself
(RFC 6265 section 5.3, step 5).
@sonarqubecloud

Copy link
Copy Markdown

@rbri

rbri commented Nov 18, 2026

Copy link
Copy Markdown
Member

Great, many thanks for working on all these...

@rbri
rbri merged commit defea4f into HtmlUnit:master Nov 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants