Skip to content

Commit

Permalink
Ensure Vary header is set when using CORS with origin
Browse files Browse the repository at this point in the history
When CORS requirements are more complicated than setting `Access-Control-Allow-Origin` to `*` then we set the `Vary` to `Origin`.
This avoids caching the wrong response.
  • Loading branch information
boekkooi-lengoo committed Mar 10, 2023
1 parent 69df734 commit 4f20cfb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 3 additions & 4 deletions apisix/plugins/cors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,6 @@ local function set_cors_headers(conf, ctx)
end

core.response.set_header("Access-Control-Allow-Origin", ctx.cors_allow_origins)
if ctx.cors_allow_origins ~= "*" then
core.response.add_header("Vary", "Origin")
end

core.response.set_header("Access-Control-Allow-Methods", allow_methods)
core.response.set_header("Access-Control-Max-Age", conf.max_age)
core.response.set_header("Access-Control-Expose-Headers", conf.expose_headers)
Expand Down Expand Up @@ -308,6 +304,9 @@ function _M.header_filter(conf, ctx)
conf.allow_origins_by_metadata, ctx, req_origin
)
end
if conf.allow_origins ~= "*" then
core.response.add_header("Vary", "Origin")
end
if allow_origins then
ctx.cors_allow_origins = allow_origins
set_cors_headers(conf, ctx)
Expand Down
4 changes: 2 additions & 2 deletions t/plugin/cors3.t
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Origin: http://foo.example.org
hello world
--- response_headers
Access-Control-Allow-Origin:
Vary:
Vary: Origin
Access-Control-Allow-Methods:
Access-Control-Allow-Headers:
Access-Control-Expose-Headers:
Expand Down Expand Up @@ -254,7 +254,7 @@ Origin: http://foo.example.org
hello world
--- response_headers
Access-Control-Allow-Origin:
Vary:
Vary: Origin
Access-Control-Allow-Methods:
Access-Control-Allow-Headers:
Access-Control-Expose-Headers:
Expand Down

0 comments on commit 4f20cfb

Please sign in to comment.