Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<body>
<release version="5.8.0" date="xxxx xx, 2026" description="Chrome/Edge 157, Bugfixes">
<action type="fix" dev="Vidya m">
document.cookie (and meta http-equiv set-cookie) no longer creates HttpOnly cookies and no longer
replaces or deletes an existing HttpOnly cookie (RFC 6265 section 5.3).
The localStorage is separated by origin; pages from the same host but a different port no longer
share the same storage. Method document.cookie (and meta http-equiv set-cookie) no longer creates
HttpOnly cookies and no longer replaces or deletes an existing HttpOnly cookie (RFC 6265 section 5.3).
</action>
<action type="fix" dev="Vidya m">
History.pushState() and History.replaceState() throw a SecurityError if the url is not same origin
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/htmlunit/StorageHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -65,7 +66,12 @@ public Map<String, String> getStore(final Type storageType, final Page page) {
*/
public Map<String, String> getLocalStorage(final URL url) {
synchronized (localStorage_) {
final String key = url.getProtocol() + "://" + url.getHost();
// the storage is per origin (scheme, host and port)
int port = url.getPort();
if (port == -1) {
port = url.getDefaultPort();
}
final String key = url.getProtocol() + "://" + url.getHost().toLowerCase(Locale.ROOT) + ":" + port;
return localStorage_.computeIfAbsent(key, k -> new LinkedHashMap<>());
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/htmlunit/StorageHolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.htmlunit;

import java.net.URL;
import java.util.Map;

import org.htmlunit.html.HtmlPage;
Expand Down Expand Up @@ -82,6 +83,38 @@ public void populateLocalStorage() throws Exception {
assertEquals("Tom", page.getTitleText());
}

/**
* The local storage is per origin, pages from the same host but another port
* must not see the data.
* @throws Exception in case of error
*/
@Test
public void localStorageOtherPort() throws Exception {
final URL otherPortUrl = new URL("http://localhost:" + PORT2 + "/");

final String html = DOCTYPE_HTML
+ "<html><body>\n"
+ "<script>\n"
+ " localStorage.setItem('token', 'secret');"
+ "</script>\n"
+ "</body></html>";
loadPage(html);

final String html2 = DOCTYPE_HTML
+ "<html><body>\n"
+ "<script>\n"
+ " document.title = '' + localStorage.getItem('token');"
+ "</script>\n"
+ "</body></html>";
getMockWebConnection().setResponse(otherPortUrl, html2);
final HtmlPage page = getWebClient().getPage(otherPortUrl);
assertEquals("null", page.getTitleText());

final WebClient webClient = getWebClient();
assertEquals("secret", webClient.getStorageHolder().getLocalStorage(URL_FIRST).get("token"));
assertEquals(0, webClient.getStorageHolder().getLocalStorage(otherPortUrl).size());
}

/**
* @throws Exception in case of error
*/
Expand Down
Loading