Skip to content

Do not let XMLHttpRequest.setRequestHeader() set the Origin header - #1206

Merged
rbri merged 2 commits into
HtmlUnit:masterfrom
Vidya-m78:xhr-forbidden-origin-header
Nov 21, 2026
Merged

rbri merged 2 commits into
HtmlUnit:masterfrom
Vidya-m78:xhr-forbidden-origin-header

Conversation

@Vidya-m78

Copy link
Copy Markdown
Contributor

A page can forge the Origin header of an XMLHttpRequest
XMLHttpRequest.isAuthorizedHeader() checks the name against its own list, and that list has no Origin, so setRequestHeader('Origin', ...) replaces the header that open() sets. CorsLoader.load() and isPreflightAuthorized() compare Access-Control-Allow-Origin with the Origin header of the request, and Origin never triggers a preflight. A page from http://attacker.example/ that runs

xhr.open('GET', 'https://api.victim.example/me', false);
xhr.withCredentials = true;
xhr.setRequestHeader('Origin', 'https://victim.example');
xhr.send();

sends the victim's cookies, gets Access-Control-Allow-Origin: https://victim.example back from an API that only trusts its own front end, and reads xhr.responseText. The forged Origin also gets past server-side CSRF checks that rely on that header. Browsers ignore Origin in setRequestHeader(), and the Headers class of fetch() already rejects it. I also added the other names from the Fetch standard's forbidden request-header list that were missing (Access-Control-Request-Headers, Access-Control-Request-Method, DNT, Set-Cookie).

XMLHttpRequestCORSTest.setRequestHeaderOriginDoesNotPassCorsCheck fails on master for all browsers (expected: <NetworkError/DOMException§4§0§§> but was: <4§200§origin=http://localhost:22223;cookie=c=1;auth=null§>) and passes with the change, as does setRequestHeaderOriginIgnored (the server gets the origin of the page). The XMLHttpRequest, fetch, Headers and CodeStyle tests pass and checkstyle is clean. I also added a changes.xml entry.

Vidya-m78 and others added 2 commits November 21, 2026 13:26
XMLHttpRequest.isAuthorizedHeader() did not reject Origin, so a page
could replace the Origin header that open() sets. CorsLoader compares
Access-Control-Allow-Origin with the Origin header of the request (also
for the preflight) and Origin does not trigger a preflight, so a page
from another origin that set Origin to the origin a server trusts could
send a request with credentials to that server and read the response.

The list now also contains the other forbidden request header names of
the Fetch standard that were missing (Access-Control-Request-Headers,
Access-Control-Request-Method, DNT, Set-Cookie); the Headers class of
fetch() already rejects all of them.
@rbri
rbri merged commit 8150d9a into HtmlUnit:master Nov 21, 2026
5 checks passed
@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