-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cors) match configured origins as a regular expression
Use the ngx.re API to match configured origins as regular expressions against the client Origin header. In cases where a single origin is configured for the plugin, set the ACAO header if the configuration contains only non-PCRE metacharacters; otherwise, treat the single configured origin as a though multiple origins were configured, by iterating through the array and setting the ACAO header based on the client Origin.
- Loading branch information
Showing
5 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
local validate_entity = require("kong.dao.schemas_validation").validate_entity | ||
local cors_schema = require "kong.plugins.cors.schema" | ||
|
||
describe("cors schema", function() | ||
it("validates '*'", function() | ||
local ok, err = validate_entity({ origins = { "*" } }, cors_schema) | ||
|
||
assert.True(ok) | ||
assert.is_nil(err) | ||
end) | ||
|
||
it("validates what looks like a domain", function() | ||
local ok, err = validate_entity({ origins = { "example.com" } }, cors_schema) | ||
|
||
assert.True(ok) | ||
assert.is_nil(err) | ||
end) | ||
|
||
it("validates what looks like a regex", function() | ||
local ok, err = validate_entity({ origins = { [[.*\.example(?:-foo)?\.com]] } }, cors_schema) | ||
|
||
assert.True(ok) | ||
assert.is_nil(err) | ||
end) | ||
|
||
describe("errors", function() | ||
it("with invalid regex in origins", function() | ||
local mock_origins = { [[.*.example.com]], [[invalid_**regex]] } | ||
local ok, err = validate_entity({ origins = mock_origins }, cors_schema) | ||
|
||
assert.False(ok) | ||
assert.same(err.origins, "origin " .. mock_origins[2] .. " is not a valid regex") | ||
end) | ||
end) | ||
end) |