Skip to content

Commit d11b449

Browse files
committed
add lua validation in analyze
1 parent c81ac17 commit d11b449

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

analyze.go

+22
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options *
155155
continue
156156
}
157157

158+
// check if the directive is a lua block
159+
////nolint:ineffassign
160+
if options.LexOptions.ExternalLexers != nil {
161+
for _, lexer := range options.LexOptions.ExternalLexers {
162+
if _, ok := lexer.(*LuaLexer); ok {
163+
if (mask&ngxConfBlock) != 0 && term != ";" { // lua block end with ; not {
164+
what = fmt.Sprintf(`Lua directive "%s" has no opening "{"`, stmt.Directive)
165+
continue
166+
}
167+
// *_by_lua_block takes ngxConfNoArgs, set_by_lua_block: ngxConfTake1
168+
// but parse takes a block as an extra argument and we need to +1
169+
if ((mask&ngxConfNoArgs) != 0 && len(stmt.Args) == 1) ||
170+
((mask&ngxConfTake1) != 0 && len(stmt.Args) == 2) {
171+
return nil
172+
} else {
173+
what = fmt.Sprintf(`invalid number of arguments in "%s" directive`, stmt.Directive)
174+
continue
175+
}
176+
}
177+
}
178+
}
179+
158180
// if the directive isn't a block but should be according to the mask
159181
if (mask&ngxConfBlock) != 0 && term != "{" {
160182
what = fmt.Sprintf(`directive "%s" has no opening "{"`, stmt.Directive)

analyze_test.go

+78-1
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ func TestAnalyze_lua(t *testing.T) {
19471947
blockCtx{"http", "location", "location if"},
19481948
false,
19491949
},
1950-
"content_by_lua_file nor ok": {
1950+
"content_by_lua_file not ok": {
19511951
&Directive{
19521952
Directive: "content_by_lua_file",
19531953
Args: []string{"foo/bar.lua"},
@@ -1992,6 +1992,78 @@ func TestAnalyze_lua(t *testing.T) {
19921992
blockCtx{"http"},
19931993
true,
19941994
},
1995+
"set_by_lua ok": {
1996+
&Directive{
1997+
Directive: "set_by_lua",
1998+
Args: []string{"$res", "' return 32 + math.cos(32) '"},
1999+
Line: 5,
2000+
},
2001+
blockCtx{"server"},
2002+
false,
2003+
},
2004+
"set_by_lua not ok": {
2005+
&Directive{
2006+
Directive: "set_by_lua",
2007+
Args: []string{"' return 32 + math.cos(32) '"},
2008+
Line: 5,
2009+
},
2010+
blockCtx{"http"},
2011+
true,
2012+
},
2013+
"set_by_lua_block ok": {
2014+
&Directive{
2015+
Directive: "set_by_lua_block",
2016+
Args: []string{"$res", "{ return tonumber(ngx.var.foo) + 1 }"},
2017+
Line: 5,
2018+
},
2019+
blockCtx{"server"},
2020+
false,
2021+
},
2022+
"set_by_lua_block not ok": {
2023+
&Directive{
2024+
Directive: "set_by_lua_block",
2025+
Args: []string{"{ return tonumber(ngx.var.foo) + 1 }"},
2026+
Line: 5,
2027+
},
2028+
blockCtx{"http"},
2029+
true,
2030+
},
2031+
"content_by_lua ok": {
2032+
&Directive{
2033+
Directive: "content_by_lua",
2034+
Args: []string{"'ngx.say('I need no extra escaping here, for example: \r\nblah')'"},
2035+
Line: 5,
2036+
},
2037+
blockCtx{"location"},
2038+
false,
2039+
},
2040+
"content_by_lua not ok": {
2041+
&Directive{
2042+
Directive: "content_by_lua",
2043+
Args: []string{"'ngx.say('I need no extra escaping here, for example: \r\nblah')'"},
2044+
Line: 5,
2045+
},
2046+
blockCtx{"http"},
2047+
true,
2048+
},
2049+
"content_by_lua_block ok": {
2050+
&Directive{
2051+
Directive: "content_by_lua_block",
2052+
Args: []string{"{ngx.say('I need no extra escaping here, for example: \r\nblah')}"},
2053+
Line: 5,
2054+
},
2055+
blockCtx{"location"},
2056+
false,
2057+
},
2058+
"content_by_lua_block not ok": {
2059+
&Directive{
2060+
Directive: "content_by_lua_block",
2061+
Args: []string{"{ngx.say('I need no extra escaping here, for example: \r\nblah')}"},
2062+
Line: 5,
2063+
},
2064+
blockCtx{"http"},
2065+
true,
2066+
},
19952067
}
19962068

19972069
for name, tc := range testcases {
@@ -2000,6 +2072,11 @@ func TestAnalyze_lua(t *testing.T) {
20002072
t.Parallel()
20012073
err := analyze("nginx.conf", tc.stmt, ";", tc.ctx, &ParseOptions{
20022074
MatchFuncs: []MatchFunc{MatchLua},
2075+
LexOptions: LexOptions{
2076+
ExternalLexers: []ExtLexer{
2077+
&LuaLexer{},
2078+
},
2079+
},
20032080
})
20042081

20052082
if !tc.wantErr && err != nil {

0 commit comments

Comments
 (0)