Fixing Duplicate CORS Headers from IIS URL Rewrite Outbound Rules Behind ARR

A single-page app loads from your IIS site, calls /api/orders on a separate upstream service behind ARR, and the browser console answers with “has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header contains multiple values”. The API itself returns exactly one origin header. ARR is the one duplicating it, because it caches response headers per upstream and replays them, appending the upstream value to whatever the rewrite rule already set. The fix lives in outbound rules and in how the proxy treats header collections, not in application code.

This scenario is common when the front end and the API end up on different backends but share one public hostname, and the upstream already sets CORS on its own. Administrators then bolt on a rewrite rule to make it work, and the two layers stack. Before you touch URL Rewrite, it helps to know what your current IIS host can proxy cheaply with respect to CPU and memory; the sizing notes for IIS and .NET hosting on a Windows VPS cover the baseline.

Confirm the duplication before editing rules

Open Developer Tools, Network tab, and look at the raw response headers rather than the console message. One Access-Control-Allow-Origin line containing https://app.example.com, https://app.example.com means two writers. A line containing * alongside a real origin means the upstream sends a wildcard and a rule adds a specific value. Both break browsers identically, so the diagnosis has to come from the response itself.

From the server side, run curl -sI -H "Origin: https://app.example.com" https://www.example.com/api/orders and count the headers. Then request the upstream directly on its private address and repeat the check. If the proxy shows two and the upstream shows one, ARR or the rewrite module is responsible. If both show two, the upstream application is emitting CORS twice, typically from both a middleware and a proxy-level filter.

Why ARR breaks the obvious rule

The natural approach is an outbound rule that sets the header on every response, with a condition on the upstream path. It works for the first request and then appears to stop. The reason is that URL Rewrite outbound rules run against HTTP_RESPONSE_HEADERS as a collection, and setting an existing key through a server variable with replace does not always remove values ARR already wrote from the cached upstream response. Adding a second rule with REPLACE mode can produce duplicates rather than replacements.

A stale cached copy also explains responses that change depending on which backend node answers. If ARR caches the upstream response and stores the header collection, a rule that mutates it in place can leave the cache holding both old and new values. Disabling the ARR cache for the API path, or at least for responses carrying an Origin echo, removes one whole class of confusing behaviour while you test.

Stop the upstream from setting CORS at all

The cleanest arrangement is one writer. Turn off CORS in the upstream application or in its own IIS level and let only the reverse proxy emit the headers, or the reverse. For ASP.NET Core that means removing app.UseCors() together with the service registration and any endpoint-level RequireCors attributes, then restarting the app pool so the middleware pipeline is rebuilt.

If you cannot change the upstream, an outbound rule can strip its headers before adding the correct ones. Use a rule with an empty action value in Replace mode on the header name, matched against a condition on {RESPONSE_HEADER} rather than the URL. Strip Access-Control-Allow-Origin, Access-Control-Allow-Credentials, Access-Control-Allow-Headers, and Access-Control-Allow-Methods, then write each one back from a server variable bound to the request Origin.

Echoing the Origin without opening the door

Writing back the incoming Origin value verbatim for any origin turns your API into an open relay that any site can read, as long as it does not rely on cookies. With Allow-Credentials: true the same mistake is worse: any origin gets authenticated access. Gate the echo on a condition that checks {HTTP_ORIGIN} against a comma-separated pattern of your own hosts, and return no CORS headers at all for anything else.

Also handle the preflight. An OPTIONS request to a proxied API must return 204 with allow-methods and allow-headers, and ARR will pass it upstream where it may 405. A rule that terminates OPTIONS at the proxy answers directly and keeps first-paint latency down.

Exposing headers the browser cannot see

CORS hides response headers from JavaScript by default. If the SPA reads a correlation ID, a rate-limit counter, or a pagination total, the response needs Access-Control-Expose-Headers listing those names. This is another header the proxy can set centrally, and it saves you from asking every API team to remember it. Keep the list short and explicit; wildcards do not work for credentialed requests.

Watch for the interaction with Vary. A response that varies by Origin must say so, otherwise an intermediary or the browser cache can serve the wrong origin’s headers to the next caller. ARR caching plus an echoed origin without Vary: Origin is a reliable way to produce intermittent CORS failures that only appear on the second request from a second browser tab.

Test with the failure modes in the matrix

Build a small matrix and check each row with curl before involving the browser: allowed origin with credentials, allowed origin without, unlisted origin, no origin header, and a preflight. Record the status code and every Access-Control-* header for each. If the unlisted origin row shows any allow header, the condition is not matching and the rule is echoing blindly.

Repeat the matrix after a worker process recycle and after a five-minute idle period. A warm ARR cache can hide the bug until the first quiet moment, which is exactly when users notice. Logging the outbound rule name in a debug header during testing shortens the loop considerably.

Keeping the rule set readable

Put all CORS changes in a single section of web.config with comments naming the upstream and the date, and export a backup before each change with appcmd list config /section:system.webServer/rewrite. When the API moves to a direct binding later, the rules become dead weight and someone needs to know they were the proxy’s workaround rather than an application requirement.

If the proxy and the API eventually share a host, the simplest state is no CORS at all: same origin, no headers, no preflight. Plan for that end state, and treat the outbound rules as a transition. For teams building this layout on a single machine, compare Windows VPS configurations sized for IIS and reverse proxy duties so the proxy is not competing with the app for the same two cores.

Leave a Comment