Skip to content

Commit d9fe598

Browse files
authored
Merge pull request #29 from dizeee/dash-match-escape
Escape dashes in origin pattern
2 parents 771c900 + d6fc6b8 commit d9fe598

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/cors.lua

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

59+
-- escape dots and dashes in pattern
60+
pattern = pattern:gsub("([%.%-])", "%%%1")
61+
5962
-- an asterisk for the port means allow all ports
6063
if string.find(pattern, "[:]+%*$") ~= nil then
6164
pattern = pattern:gsub("[:]+%*$", "[:]+[0-9]+")
6265
end
6366

64-
-- escape dots in pattern
65-
pattern = pattern:gsub("%.", "%%.")
66-
6767
-- append end character
6868
pattern = pattern .. "$"
6969
return pattern

tests/cors_tests.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ function test_build_pattern_7()
9696
luaunit.assertEquals(result, "http://test%.com[:]+[0-9]+$")
9797
end
9898

99+
function test_build_pattern_8()
100+
local result = cors.build_pattern("te-st.com")
101+
luaunit.assertEquals(result, "//te%-st%.com$")
102+
end
103+
99104
function test_get_allowed_origin_case_1()
100105
local result = cors.get_allowed_origin("http://test.com", {"http://test.com"})
101106
luaunit.assertEquals(result, "http://test.com")
@@ -151,11 +156,15 @@ function test_get_allowed_origin_case_11()
151156
luaunit.assertEquals(result, "*")
152157
end
153158

154-
155159
function test_get_allowed_origin_case_12()
156160
local result = cors.get_allowed_origin("http://test.com:8080", {"http://test.com:*"})
157161
luaunit.assertEquals(result, "http://test.com:8080")
158162
end
159163

164+
function test_get_allowed_origin_case_13()
165+
local result = cors.get_allowed_origin("https://te-st.com", {"te-st.com"})
166+
luaunit.assertEquals(result, "https://te-st.com")
167+
end
168+
160169
-- this line must go at the end
161-
os.exit(luaunit.LuaUnit.run())
170+
os.exit(luaunit.LuaUnit.run())

0 commit comments

Comments
 (0)