Skip to content

Check the header values before skipping the CORS preflight - #1207

Merged
rbri merged 2 commits into
HtmlUnit:masterfrom
Vidya-m78:cors-safelisted-header-values
Nov 21, 2026
Merged

rbri merged 2 commits into
HtmlUnit:masterfrom
Vidya-m78:cors-safelisted-header-values

Conversation

@Vidya-m78

Copy link
Copy Markdown
Contributor

CorsLoader.isPreflightHeader() decides whether a cross-origin XMLHttpRequest or fetch() needs a preflight. It accepts Accept, Accept-Language and Content-Language whatever their value, and any Content-Type that only starts with text/plain, multipart/form-data or application/x-www-form-urlencoded. A page can send a credentialed cross-origin POST with Content-Type: application/x-www-form-urlencoded+json without a preflight. Servers that take application/*+json (Spring's Jackson converter, ASP.NET Core) parse that body as JSON, so an API that relies on the preflight for CSRF protection gets the state-changing request with the victim's cookies. Browsers check the value (CORS-safelisted request-header in the Fetch standard) and send the preflight first. If the preflight fails, the request is never sent.

The fix reuses Headers.isNoCorsSafelistedRequestHeader(), which already does this check for no-cors requests: the MIME type essence, the CORS-unsafe bytes, the characters allowed in the language headers and the 128 byte limit. The three new XMLHttpRequestCORSTest cases fail on master with POST cookie=sweet reaching the server and pass with the change. fetch() goes through the same helper. The XMLHttpRequest, fetch, Headers and Request tests pass and checkstyle is clean. I also added a changes.xml entry.

Vidya-m78 and others added 2 commits November 21, 2026 14:39
CorsLoader.isPreflightHeader() treated Accept, Accept-Language and
Content-Language as safelisted whatever their value and accepted every
Content-Type that starts with one of the three form types. A cross-origin
XMLHttpRequest or fetch() with e.g. 'Content-Type:
application/x-www-form-urlencoded+json' was sent with credentials and
without a preflight.

Use the CORS-safelisted request-header check of the Fetch standard
(Headers.isNoCorsSafelistedRequestHeader()) for these headers.
@rbri
rbri merged commit 982cf74 into HtmlUnit:master Nov 21, 2026
5 checks passed
@rbri

rbri commented Nov 21, 2026

Copy link
Copy Markdown
Member

Thanks, will do some follow up, looks like togethere we are driving this thing really forward...

@sonarqubecloud

Copy link
Copy Markdown

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