fetch: Origin header added to non-GET/HEAD requests breaks OAuth2 client_credentials against Microsoft Entra ID (AADSTS9002326)
Summary
Since v0.9.0-beta, fetch() adds an Origin header to every request whose method is not GET or HEAD. The value is the origin of the request URL itself. Server-side OAuth2 client_credentials token requests to Microsoft Entra ID (Azure AD) are rejected because of that header, so any Lambda on LLRT v0.9.0-beta that obtains a token from Entra ID with fetch() now fails. The same code works on v0.8.1-beta and on Node.js, Deno and Bun, none of which send Origin from a server-side fetch().
Environment
- LLRT v0.9.0-beta,
llrt-lambda-*-no-sdkbinary on AWS Lambda - Works on LLRT v0.8.1-beta
Reproduction
// Any confidential-client token request to the Entra ID v2.0 token endpoint.
const body = new URLSearchParams({
grant_type: 'client_credentials',
client_id: '<client-id>',
client_secret: '<client-secret>',
scope: 'api://<app-id-uri>/.default',
}).toString();
const res = await fetch('https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
body,
});
console.log(res.status, await res.text());Actual result (400):
{
"error": "invalid_request",
"error_description": "AADSTS9002326: Cross-origin token redemption is permitted only for the 'Single-Page Application' client-type. Request origin: 'https://login.microsoftonline.com'. ...",
"error_codes": [9002326]
}Expected result: 200 with an access token, as on LLRT v0.8.1-beta and other server-side runtimes.
Equivalent curl that reproduces the failure is simply the same request plus -H 'Origin: https://login.microsoftonline.com'. Remove that one header and it succeeds.
Root cause
build_request in modules/llrt_fetch/src/fetch.rs (introduced in #1553):
// Per spec, the Origin header is included for non-CORS-safelisted methods
// (anything other than GET/HEAD). Browsers compute origin from the caller;
// here we approximate with the initial request URI's origin.
if !is_get_or_head && !detected_headers.contains(ORIGIN.as_str()) {
if let Some(origin) = uri_origin(initial_uri) {
req = req.header(ORIGIN, origin);
}
}The Fetch spec's Origin behaviour is defined in terms of the caller's origin (the environment settings object). A server-side runtime has no such origin, which is why Node (undici), Deno and Bun omit the header entirely. Using the target URL's own origin is not an approximation of that; it presents every POST as a cross-origin browser request from the target server itself. Entra ID, and any other identity provider that gates on Origin, rejects it.
Why it cannot be worked around from JS
Originis on the forbidden request header list inmodules/llrt_fetch/src/headers.rs, so a caller-supplied value is silently dropped and the computed one is added anyway.- The logic does not consult
mode, somode: 'no-cors'has no effect. - There is no
http.request/https.requestclient to fall back to.
The only workaround is to pin the Lambda to v0.8.1-beta.
Suggested fix
Do not add Origin to outgoing requests by default. If browser-parity in WPT is the goal, gate it behind an explicit opt-in (for example an environment variable or a request init option) so server-side callers are unaffected.
Happy to open a PR if a maintainer can confirm the preferred direction.
Source: awslabs/llrt