Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cors): ACAO header was not sent when conf.origins has multiple #13334

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/fix-cors-wildcard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**CORS**: Fixed an issue where the `Access-Control-Allow-Origin` header was not sent when `conf.origins` has multiple entries but includes `*`."
type: bugfix
scope: Plugin
5 changes: 5 additions & 0 deletions kong/plugins/cors/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ local function configure_origin(conf, header_filter)
cached_domains = {}

for _, entry in ipairs(conf.origins) do
if entry == "*" then
set_header("Access-Control-Allow-Origin", "*")
return true
end

local domain
local maybe_regex, _, err = re_find(entry, "[^A-Za-z0-9.:/-]", "jo")
if err then
Expand Down
48 changes: 46 additions & 2 deletions spec/03-plugins/13-cors/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ for _, strategy in helpers.each_strategy() do
hosts = { "cors13.test" },
})

local route14 = bp.routes:insert({
hosts = { "cors14.test" },
})

local mock_upstream = bp.services:insert {
host = helpers.mock_upstream_hostname,
port = helpers.mock_upstream_port,
Expand Down Expand Up @@ -451,6 +455,15 @@ for _, strategy in helpers.each_strategy() do
}
}

bp.plugins:insert {
name = "cors",
route = { id = route14.id },
config = {
preflight_continue = false,
origins = { "foo.bar", "*" }
}
}

bp.plugins:insert {
name = "cors",
route = { id = route_timeout.id },
Expand Down Expand Up @@ -613,7 +626,7 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(res.headers["Vary"])
end)

it("gives appropriate defaults when origin is explicitly set to *", function()
it("gives appropriate defaults when origin is explicitly set to * and config.credentials=true", function()
local res = assert(proxy_client:send {
method = "OPTIONS",
headers = {
Expand All @@ -633,6 +646,25 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(res.headers["Access-Control-Max-Age"])
end)

it("gives * wildcard when origin has multiple entries and have * included", function()
local res = assert(proxy_client:send {
method = "OPTIONS",
headers = {
["Host"] = "cors14.test",
["Origin"] = "http://www.example.net",
["Access-Control-Request-Method"] = "GET",
}
})
assert.res_status(200, res)
assert.equal("0", res.headers["Content-Length"])
assert.equal(CORS_DEFAULT_METHODS, res.headers["Access-Control-Allow-Methods"])
assert.equal("*", res.headers["Access-Control-Allow-Origin"])
assert.is_nil(res.headers["Access-Control-Allow-Headers"])
assert.is_nil(res.headers["Access-Control-Expose-Headers"])
assert.is_nil(res.headers["Access-Control-Allow-Credentials"])
assert.is_nil(res.headers["Access-Control-Max-Age"])
end)

it("accepts config options", function()
local res = assert(proxy_client:send {
method = "OPTIONS",
Expand Down Expand Up @@ -1032,7 +1064,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal("Origin", res.headers["Vary"])
end)

it("responds with * when config.credentials=false", function()
it("responds with * when origin is explicitly set to * and config.credentials=false", function()
local res = assert(proxy_client:send {
method = "GET",
headers = {
Expand All @@ -1046,6 +1078,18 @@ for _, strategy in helpers.each_strategy() do
assert.is_nil(res.headers["Vary"])
end)

it("responds with * when origin has multiple entries and have * included", function()
local res = assert(proxy_client:send {
method = "GET",
headers = {
["Host"] = "cors14.test",
["Origin"] = "http://www.example.net"
}
})
assert.res_status(200, res)
assert.equals("*", res.headers["Access-Control-Allow-Origin"])
end)

it("removes upstream ACAO header when no match is found", function()
local res = proxy_client:get("/response-headers", {
query = ngx.encode_args({
Expand Down
Loading