Skip to content

Commit 771c900

Browse files
Added pattern to allow all ports
1 parent 8461ba8 commit 771c900

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ For the list of allowed origins, you can specify patterns such as:
5454
| schema, domain name, and port | http://mydomain.com:8080 | allow only HTTP of mydomain.com from port 8080 |
5555
| dot and domain name | .mydomain.com | allow ALL subdomains of mydomain.com |
5656
| dot, domain name, and port | .mydomain.com:443 | allow ALL subdomains of mydomain.com from default HTTPS source port |
57+
| port is * | http://mydomain.com:* | allow only HTTP of mydomain.com on ANY ports |
5758

5859
## Examples
5960

lib/cors.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ M.build_pattern = function(pattern)
5656
pattern = "//" .. pattern
5757
end
5858

59+
-- an asterisk for the port means allow all ports
60+
if string.find(pattern, "[:]+%*$") ~= nil then
61+
pattern = pattern:gsub("[:]+%*$", "[:]+[0-9]+")
62+
end
63+
5964
-- escape dots in pattern
6065
pattern = pattern:gsub("%.", "%%.")
6166

tests/cors_tests.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ function test_build_pattern_6()
9191
luaunit.assertEquals(result, "%.test%.com:8080$")
9292
end
9393

94+
function test_build_pattern_7()
95+
local result = cors.build_pattern("http://test.com:*")
96+
luaunit.assertEquals(result, "http://test%.com[:]+[0-9]+$")
97+
end
98+
9499
function test_get_allowed_origin_case_1()
95100
local result = cors.get_allowed_origin("http://test.com", {"http://test.com"})
96101
luaunit.assertEquals(result, "http://test.com")
@@ -146,5 +151,11 @@ function test_get_allowed_origin_case_11()
146151
luaunit.assertEquals(result, "*")
147152
end
148153

154+
155+
function test_get_allowed_origin_case_12()
156+
local result = cors.get_allowed_origin("http://test.com:8080", {"http://test.com:*"})
157+
luaunit.assertEquals(result, "http://test.com:8080")
158+
end
159+
149160
-- this line must go at the end
150161
os.exit(luaunit.LuaUnit.run())

0 commit comments

Comments
 (0)