HtmlUnit 5.9.0: Native fetch(), better Intl, and stricter Cookies
HtmlUnit 5.9.0 moves several core parts of the library closer to real browsers. It adds a first native fetch() implementation, a much more complete Intl implementation, and a long list of security rules for cookies, CORS, referrers, and local files. Some of these changes are incompatible with earlier versions, so they come first.
Breaking changes
- Local files: A page that was not loaded from a file can no longer load local files. This applies to scripts, images, stylesheets, workers,
importScripts(), navigation, andwindow.open(). Like browsers, the load fails. If your tests depend on the old behavior,WebClientOptions.setLocalFileAccessFromRemotePagesAllowed(true)restores it, but real browsers block this, so use it sparingly. - File URLs:
URL.hrefand anchorhrefvalues serialize file URLs without a host, for examplefile:///C:/a. - Protocol setters:
location.protocolthrows aSyntaxErrorfor an invalid scheme and navigates only to http(s) URLs. - Redirects: A redirect to a non-http(s) URL is now a network error.
- Markup:
asXml()writes empty HTML elements with an end tag. If you read back markup written byasXml()before 5.8.0, enableWebClientOptions.setHtmlFragmentSelfClosingTagsAllowed(). Thanks to Ronny Shapiro for this option. - Chrome 158: The
XSLTProcessorJavaScript object is no longer supported. - CSP sandbox: In
htmlunit-csp, thesandboxdirective no longer makesallowsExternalScript(),allowsInlineScript(),allowsScriptAsAttribute(), orallowsFormAction()returnfalse. The user agent applies the sandbox flags itself.
Native fetch()
fetch() now runs natively instead of only through the polyfill. This release covers the basics:
- Requests to the page’s origin,
data:andblob:URLs, redirects, abort signals, and the redirect modeserrorandmanual - Cross-origin requests in
corsmode, including preflight requests, credentials modes, and filtering of response headers throughAccess-Control-Expose-Headers no-corsmode with opaque responses and restricted request headers- Cache modes, subresource integrity, and the body size limit for
keepaliverequests Request,Response, andHeaders, includingclone(), the body methods,Response.json(), andResponse.redirect()AbortControllerandAbortSignal, includingAbortSignal.timeout()andAbortSignal.any()fetch()in dedicated workers
The polyfill is still available as an option and replaces the native implementation when enabled.
Intl
Intl.NumberFormatgainsformatToParts(),formatRange(), andformatRangeToParts(), plus support for percent, currency, unit, scientific, engineering, and compact notation, along with rounding, grouping, and sign options.Intl.Collatorsupports options such asusage,sensitivity,numeric, andcaseFirst. The German phonebook and traditional Spanish collations are included.resolvedOptions()reports the resolved locale, including relevant Unicode extensions.- Locale arguments are canonicalized the way
Intl.getCanonicalLocales()does it, and unsupported locales are skipped.
Cookies and origins
- SameSite is enforced. Strict cookies are not sent cross-site, and Lax cookies are sent only on top-level navigations.
- Secure cookies set from an insecure origin are ignored, and insecure pages cannot overwrite secure cookies.
- Prefixes:
__Secure-and__Host-cookies must follow their rules. - Hidden values:
Set-CookieandSet-Cookie2headers are no longer visible throughgetResponseHeader(), soHttpOnlycookie values are no longer readable from JavaScript. Thanks to Vidya m. - Domains: Public suffixes such as
github.ioare treated as public suffixes, anddocument.cookiecan no longer set cookies for another domain. Thanks to Vidya m. - Credentials in URLs:
location.href, theRefererandOriginheaders, anddocument.referrerno longer contain the user name and password. Thanks to Vidya m. - Referrer policy is supported, with
strict-origin-when-cross-originas the default.
CORS
- Cross-origin requests send a preflight when
Accept,Accept-Language,Content-Language, orContent-Typeis not CORS-safelisted. SimpleRangeheaders do not trigger one. Thanks to Vidya m. - Forbidden request headers such as
Origin,DNT, andSet-Cookieare ignored insetRequestHeader(). Method override headers are ignored when they name a forbidden method. Thanks to Vidya m. navigator.sendBeacon()with a non-safelisted body type is a CORS request. Thanks to Vidya m.
Content Security Policy
- A
sandboxdirective in an enforced policy sandboxes the document, the same way thesandboxattribute of an iframe does. form-actionalso applies to redirects from form submissions, and the path of its sources is ignored for redirect targets. Redirect targets for scripts, styles, images, and connections ignore the path too.- Data URLs are matched by scheme sources such as
data:. - Violation reports can be turned off with
WebClientOptions.setContentSecurityPolicyReportsEnabled(false). - When simulating Chrome/Edge, a WebSocket blocked by
connect-srcfires only the error event.
Iframes, documents, and windows
- The
sandboxattribute of iframes is supported. Withoutallow-same-origin, the document gets an opaque origin, which means no cookies and no storage. - The
srcdocattribute is supported, and the document uses theabout:srcdocURL. window.originis available.window.isSecureContextistruefor loopback addresses such as127.0.0.1and[::1].
JavaScript engine
Together with the Rhino team:
- Holes are skipped in the array returned by a
flatMapcallback. for...inskips properties shadowed by non-enumerable ones.Number.prototype.toPrecisionis fixed for small values that need padding zeros.
Thank you
Big thanks to Vidya m for the many security and cookie fixes in this release, to Ronny Shapiro for the markup compatibility option, and to the Rhino team for their steady JavaScript work.
Version 5.9.0 is available on Maven Central. Check out the full changes report for the complete list of changes.
Happy testing!
— RBRi
Resources:
- Changes Report: Detailed release notes
- Details about HtmlUnit Security: Security considerations
- Maven Central: Download
- GitHub Repository: Source code