Skip to content

Key localStorage by origin including the port in StorageHolder - #1196

Merged
rbri merged 2 commits into
HtmlUnit:masterfrom
Vidya-m78:localstorage-origin-port
Nov 16, 2026
Merged

rbri merged 2 commits into
HtmlUnit:masterfrom
Vidya-m78:localstorage-origin-port

Conversation

@Vidya-m78

Copy link
Copy Markdown
Contributor

Repro: a page on http://localhost:8080/ runs localStorage.setItem('token', 'secret'); a page from http://localhost:8081/ loaded in the same WebClient gets 'secret' from localStorage.getItem('token') and can also overwrite or clear it. In Chrome and Firefox the second page gets null.
Cause: StorageHolder.getLocalStorage(URL) builds the storage key from scheme and host only, so all ports of a host share one map. localStorage is partitioned by origin (scheme, host, port), so anything a page keeps there (session or CSRF tokens) is open to every other service on the same host.
Fix: add the effective port (the default port when the url has none) to the key and lower-case the host. getLocalStorage(url) keeps working as documented for the url of the page that is loaded later.

StorageHolderTest.localStorageOtherPort fails on master (expected: <null> but was: <secret>) and passes with the change; StorageHolderTest, doc.StorageHolderTest, StorageTest and CodeStyleTest pass, checkstyle is clean. I also added a changes.xml entry. sessionStorage is left alone; it is keyed by the top window by design (getSessionStorage(WebWindow) is the documented pre-population API).

@rbri
rbri merged commit a6b4d66 into HtmlUnit:master Nov 16, 2026
5 checks passed
@sonarqubecloud

Copy link
Copy Markdown

@rbri

rbri commented Nov 16, 2026

Copy link
Copy Markdown
Member

Thanks

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