You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CORS middleware duplicates headers when used in chained proxy servers #2853
When building a proxy server using Echo (golang) and enabling the built-in middleware.CORS() on each proxy layer, CORS headers are duplicated in the final response.
This happens in a common microservice / gateway setup where:
multiple Echo-based services act as proxies,
each service enables CORS middleware,
requests pass through several proxy layers before reaching the client.
As a result, the response contains duplicated CORS headers such as:
This is expected behavior rather than a bug in the CORS middleware. The duplication comes from running CORS on more than one layer of the chain: the gateway's CORS middleware sets the headers, and then the reverse proxy copies the upstream's CORS headers on top of them. The middleware can't safely undo that without wrapping the ResponseWriter in security-sensitive code to compensate for a configuration choice — which we'd rather not bake into the default. rs/cors reached the same conclusion for the identical request (rs/cors#45).
The fix belongs at the boundary:
Own CORS at the edge only — internal upstream services behind a gateway generally shouldn't run CORS; let the outermost service own it.
Strip the upstream's CORS headers when proxying, if an upstream must keep CORS for standalone use, via httputil.ReverseProxy.ModifyResponse (delete every Access-Control-* header, and remove only the Origin token from Vary so unrelated caching directives survive).
Activity
Fix CORS header duplication in proxy chains
middleware/cors: prevent duplicate CORS headers in proxy chains
docs(cors): note reverse-proxy header duplication on CORS godoc
docs(cors): add "Behind a reverse proxy" section
vishr commentedon Jun 24, 2026
Thanks for the detailed report.
This is expected behavior rather than a bug in the CORS middleware. The duplication comes from running CORS on more than one layer of the chain: the gateway's CORS middleware sets the headers, and then the reverse proxy copies the upstream's CORS headers on top of them. The middleware can't safely undo that without wrapping the
ResponseWriterin security-sensitive code to compensate for a configuration choice — which we'd rather not bake into the default.rs/corsreached the same conclusion for the identical request (rs/cors#45).The fix belongs at the boundary:
httputil.ReverseProxy.ModifyResponse(delete everyAccess-Control-*header, and remove only theOrigintoken fromVaryso unrelated caching directives survive).Documented here:
CORSgodoc pointer: docs(cors): note reverse-proxy header duplication #3027Closing as working-as-intended once those land — happy to reopen if there's a case this guidance doesn't cover.
docs(cors): add "Behind a reverse proxy" section
docs(cors): note reverse-proxy header duplication on CORS godoc