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) returning correct Access-Control-Allow-Credentials #2104

Merged
merged 1 commit into from
Feb 20, 2017
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
4 changes: 3 additions & 1 deletion kong/plugins/cors/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ local function configure_origin(ngx, conf)
end

local function configure_credentials(ngx, conf)
if (conf.credentials) then
if conf.origin == nil or conf.origin == "*" then
ngx.header["Access-Control-Allow-Credentials"] = "false"
elseif conf.credentials then
ngx.header["Access-Control-Allow-Credentials"] = "true"
end
end
Expand Down
1 change: 1 addition & 0 deletions kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ server {
default_type application/json;
content_by_lua_block {
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Credentials'] = 'false'
if ngx.req.get_method() == 'OPTIONS' then
ngx.header['Access-Control-Allow-Methods'] = 'GET,HEAD,PUT,PATCH,POST,DELETE'
ngx.header['Access-Control-Allow-Headers'] = 'Content-Type'
Expand Down
36 changes: 32 additions & 4 deletions spec/03-plugins/04-cors/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ describe("Plugin: cors (access)", function()
hosts = { "cors4.com" },
upstream_url = "http://mockbin.com"
})
local api5 = assert(helpers.dao.apis:insert {
name = "api-5",
hosts = { "cors5.com" },
upstream_url = "http://mockbin.com"
})

assert(helpers.dao.plugins:insert {
name = "cors",
Expand Down Expand Up @@ -64,6 +69,14 @@ describe("Plugin: cors (access)", function()
api_id = api4.id
})

assert(helpers.dao.plugins:insert {
name = "cors",
api_id = api5.id,
config = {
origin = "*"
}
})

assert(helpers.start_kong())
client = helpers.proxy_client()
end)
Expand All @@ -86,7 +99,22 @@ describe("Plugin: cors (access)", function()
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.equal("false", res.headers["Access-Control-Allow-Credentials"])
assert.is_nil(res.headers["Access-Control-Max-Age"])
end)
it("gives appropriate defaults when origin is explicitly set to *", function()
local res = assert(client:send {
method = "OPTIONS",
headers = {
["Host"] = "cors5.com"
}
})
assert.res_status(204, res)
assert.equal("GET,HEAD,PUT,PATCH,POST,DELETE", 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.equal("false", res.headers["Access-Control-Allow-Credentials"])
assert.is_nil(res.headers["Access-Control-Max-Age"])
end)
it("accepts config options", function()
Expand Down Expand Up @@ -132,7 +160,7 @@ describe("Plugin: cors (access)", function()
assert.is_nil(res.headers["Access-Control-Allow-Methods"])
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.equal("false", res.headers["Access-Control-Allow-Credentials"])
assert.is_nil(res.headers["Access-Control-Max-Age"])
end)
it("accepts config options", function()
Expand Down Expand Up @@ -163,7 +191,7 @@ describe("Plugin: cors (access)", function()
assert.is_nil(res.headers["Access-Control-Allow-Methods"])
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.equal("false", res.headers["Access-Control-Allow-Credentials"])
assert.is_nil(res.headers["Access-Control-Max-Age"])
end)
it("works with 40x responses returned by another plugin", function()
Expand All @@ -178,7 +206,7 @@ describe("Plugin: cors (access)", function()
assert.is_nil(res.headers["Access-Control-Allow-Methods"])
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.equal("false", res.headers["Access-Control-Allow-Credentials"])
assert.is_nil(res.headers["Access-Control-Max-Age"])
end)
end)
Expand Down