Skip to content

NLB-4932: add support for headers-more #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -2725,3 +2725,29 @@ func MatchLua(directive string) ([]uint, bool) {
masks, matched := LuaDirectives[directive]
return masks, matched
}

// nginx headers_more directives
// [https://github.com/openresty/headers-more-nginx-module]
//
//nolint:gochecknoglobals
var headersMoreDirectives = map[string][]uint{
"more_set_headers": {
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConf1More,
},
"more_clear_headers": {
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConf1More,
},
"more_set_input_headers": {
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConf1More,
},
"more_clear_input_headers": {
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConf1More,
},
}

// MatchHeadersMore is a match function for parsing an NGINX config that contains the
// Headers-More module.
func MatchHeadersMore(directive string) ([]uint, bool) {
masks, matched := headersMoreDirectives[directive]
return masks, matched
}
83 changes: 83 additions & 0 deletions analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2269,3 +2269,86 @@ func TestAnalyze_mgmt(t *testing.T) {
})
}
}

//nolint:funlen
func TestAnalyze_headers_more(t *testing.T) {
t.Parallel()
testcases := map[string]struct {
stmt *Directive
ctx blockCtx
wantErr bool
}{
"more_set_headers ok": {
&Directive{
Directive: "more_set_headers",
Args: []string{"Server: my_server"},
Line: 5,
},
blockCtx{"http", "location", "location if"},
false,
},
"more_set_headers multiple args ok": {
&Directive{
Directive: "more_set_headers",
Args: []string{"-s", "404", "-s", "500 503", "Foo: bar"},
Line: 5,
},
blockCtx{"http", "location", "location if"},
false,
},
"more_set_headers not ok": {
&Directive{
Directive: "more_set_headers",
Args: []string{"Server: my_server"},
Line: 5,
},
blockCtx{"stream"},
true,
},
"more_clear_headers ok": {
&Directive{
Directive: "more_clear_headers",
Args: []string{"X-Hidden-*"},
Line: 5,
},
blockCtx{"http", "location", "location if"},
false,
},
"more_set_input_headers ok": {
&Directive{
Directive: "more_set_input_headers",
Args: []string{"Authorization: $http_authorization"},
Line: 5,
},
blockCtx{"http", "location", "location if"},
false,
},
"more_clear_input_headers ok": {
&Directive{
Directive: "more_set_input_headers",
Args: []string{"-t", "'text/plain'", "Foo: ", "Baz: "},
Line: 5,
},
blockCtx{"http", "location", "location if"},
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{
MatchFuncs: []MatchFunc{MatchHeadersMore},
})

if !tc.wantErr && err != nil {
t.Fatal(err)
}

if tc.wantErr && err == nil {
t.Fatal("expected error, got nil")
}
})
}
}
Loading