diff --git a/analyze_nplus_latest_directives.go b/analyze_nplus_latest_directives.go index 2b0e6f73..66cec3bc 100644 --- a/analyze_nplus_latest_directives.go +++ b/analyze_nplus_latest_directives.go @@ -695,8 +695,8 @@ var ngxPlusLatestDirectives = map[string][]uint{ ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake12, }, "keyval": { - ngxHTTPMainConf | ngxConfTake3 | ngxConfTake2, - ngxStreamMainConf | ngxConfTake3 | ngxConfTake2, + ngxHTTPMainConf | ngxConfTake3 | ngxConfTake4, + ngxStreamMainConf | ngxConfTake3 | ngxConfTake4, }, "keyval_zone": { ngxHTTPMainConf | ngxConf1More, @@ -754,7 +754,7 @@ var ngxPlusLatestDirectives = map[string][]uint{ ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1, }, "limit_req_zone": { - ngxHTTPMainConf | ngxConfTake3 | ngxConfTake2, + ngxHTTPMainConf | ngxConfTake3 | ngxConfTake4, }, "lingering_close": { ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxConfTake1, diff --git a/analyze_test.go b/analyze_test.go index 57b86f72..105bfb07 100644 --- a/analyze_test.go +++ b/analyze_test.go @@ -2601,3 +2601,40 @@ func TestAnalyze_directiveSources_defaultBehavior(t *testing.T) { }) } } + +func TestAnalyze_limit_req_zone(t *testing.T) { + t.Parallel() + testcases := map[string]struct { + stmt *Directive + ctx blockCtx + wantErr bool + }{ + "limit_req_zone ok http": { + &Directive{ + Directive: "limit_req_zone", + Args: []string{"$binary_remote_addr", "zone=one:10m", "rate=1r/s", "sync"}, + Line: 5, + }, + blockCtx{"http"}, + false, + }, + } + + for name, tc := range testcases { + tc := tc + t.Run(name, func(t *testing.T) { + t.Parallel() + err := analyze("nginx.conf", tc.stmt, ";", tc.ctx, &ParseOptions{ + DirectiveSources: []MatchFunc{NgxPlusLatestDirectivesMatchFn, AppProtectWAFv4DirectivesMatchFn}, + }) + + if !tc.wantErr && err != nil { + t.Fatal(err) + } + + if tc.wantErr && err == nil { + t.Fatal("expected error, got nil") + } + }) + } +} diff --git a/parse_test.go b/parse_test.go index 9db35559..d0628b40 100644 --- a/parse_test.go +++ b/parse_test.go @@ -2012,6 +2012,100 @@ var parseFixtures = []parseFixture{ }, }, }}, + {"limit-req-zone", "", ParseOptions{SingleFile: true}, Payload{ + Status: "ok", + Errors: []PayloadError{}, + Config: []Config{ + { + File: getTestConfigPath("limit-req-zone", "nginx.conf"), + Status: "ok", + Errors: []ConfigError{}, + Parsed: Directives{ + { + Directive: "user", + Args: []string{"nginx"}, + Line: 1, + Block: nil, + }, + { + Directive: "worker_processes", + Args: []string{"auto"}, + Line: 2, + Block: nil, + }, + { + Directive: "error_log", + Args: []string{"/var/log/nginx/error.log", "notice"}, + Line: 4, + Block: nil, + }, + { + Directive: "pid", + Args: []string{"/var/run/nginx.pid"}, + Line: 5, + Block: nil, + }, + { + Directive: "events", + Args: []string{}, + Line: 7, + Block: Directives{ + { + Directive: "worker_connections", + Args: []string{"1024"}, + Line: 8, + }, + }, + }, + { + Directive: "http", + Args: []string{}, + Line: 11, + Block: Directives{ + { + Directive: "include", + Args: []string{"/etc/nginx/mime.types"}, + Line: 12, + }, + { + Directive: "default_type", + Args: []string{"application/octet-stream"}, + Line: 13, + }, + { + Directive: "limit_req_zone", + Args: []string{"$binary_remote_addr", "zone=one:10m", "rate=1r/s", "sync"}, + Line: 15, + }, + { + Directive: "log_format", + Args: []string{"main", + "$remote_addr - $remote_user [$time_local] \"$request\" ", + "$status $body_bytes_sent \"$http_referer\" ", + "\"$http_user_agent\" \"$http_x_forwarded_for\"", + }, + Line: 17, + }, + { + Directive: "access_log", + Args: []string{"/var/log/nginx/access.log", "main"}, + Line: 21, + }, + { + Directive: "sendfile", + Args: []string{"on"}, + Line: 23, + }, { + Directive: "keepalive_timeout", + Args: []string{"65"}, + Line: 25, + }, + }, + }, + }, + }, + }, + }}, } func TestParse(t *testing.T) { diff --git a/testdata/configs/limit-req-zone/nginx.conf b/testdata/configs/limit-req-zone/nginx.conf new file mode 100644 index 00000000..c13aad1e --- /dev/null +++ b/testdata/configs/limit-req-zone/nginx.conf @@ -0,0 +1,26 @@ +user nginx; +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s sync; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + + keepalive_timeout 65; +} \ No newline at end of file