Skip to content

Commit 24e28e4

Browse files
authored
fix: apply path validation for prefix pattern during router creation (#54)
1 parent 37d79f8 commit 24e28e4

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

.github/workflows/examples.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ jobs:
2626
luaVersion: ${{ matrix.luaVersion }}
2727

2828
- name: install LuaRocks
29-
uses: leafo/gh-actions-luarocks@v4
29+
uses: leafo/gh-actions-luarocks@v5
30+
with:
31+
luarocksVersion: "3.12.0"
3032

3133
- name: install dependencies
3234
run: |

.github/workflows/test.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ jobs:
2929
luaVersion: ${{ matrix.luaVersion }}
3030

3131
- name: install LuaRocks
32-
uses: leafo/gh-actions-luarocks@v4
32+
uses: leafo/gh-actions-luarocks@v5
33+
with:
34+
luarocksVersion: "3.12.0"
3335

3436
- name: install dependencies
3537
run: |
@@ -83,7 +85,9 @@ jobs:
8385
openrestyVersion: ${{ matrix.openrestyVersion }}
8486

8587
- name: install LuaRocks
86-
uses: leafo/gh-actions-luarocks@v4
88+
uses: leafo/gh-actions-luarocks@v5
89+
with:
90+
luarocksVersion: "3.12.0"
8791

8892
- name: install dependencies
8993
run: |

spec/router_spec.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,50 @@ describe("Router", function()
5656
})
5757
assert.is_nil(router)
5858
assert.equal("unable to process route(index 1): invalid methond", err)
59+
60+
router, err = Router.new({
61+
{
62+
paths = { "abc" },
63+
handler = "abc",
64+
},
65+
})
66+
assert.is_nil(router)
67+
assert.equal("invalid route(index 1): path must starts with /", err)
68+
69+
router, err = Router.new({
70+
{
71+
paths = { "/a/{*b}/c" },
72+
handler = "",
73+
},
74+
})
75+
assert.is_nil(router)
76+
assert.equal("invalid route(index 1): invalid prefix pattern", err)
77+
router, err = Router.new({
78+
{
79+
paths = { "/a/{*}/c" },
80+
handler = "",
81+
},
82+
})
83+
assert.is_nil(router)
84+
assert.equal("invalid route(index 1): invalid prefix pattern", err)
85+
router, err = Router.new({
86+
{
87+
paths = { "/a/{*b}/{*c}" },
88+
handler = "",
89+
},
90+
})
91+
assert.is_nil(router)
92+
assert.equal("invalid route(index 1): invalid prefix pattern", err)
93+
router, err = Router.new({
94+
{
95+
paths = { "/a/{*}/{*}" },
96+
handler = "",
97+
},
98+
})
99+
assert.is_nil(router)
100+
assert.equal("invalid route(index 1): invalid prefix pattern", err)
101+
102+
59103
end)
60104
it("new() with opts argument", function()
61105
local router, err = Router.new({}, {

src/route.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
local ipairs = ipairs
66
local str_byte = string.byte
7+
local str_find = string.find
78
local BYTE_SLASH = str_byte("/")
89

910
local Route = {}
@@ -17,7 +18,11 @@ function Route.new(route)
1718

1819
for _, path in ipairs(route.paths) do
1920
if str_byte(path) ~= BYTE_SLASH then
20-
return nil, "path must start with /"
21+
return nil, "path must starts with /"
22+
end
23+
local _, pattern_idx = str_find(path, "{%*[^}]*}")
24+
if pattern_idx ~= nil and pattern_idx ~= #path then
25+
return nil, "invalid prefix pattern"
2126
end
2227
end
2328

0 commit comments

Comments
 (0)