From 04a89ce2343be1ce91e80d4c5fa597014961afad Mon Sep 17 00:00:00 2001 From: Desmond Ho Date: Fri, 5 Oct 2018 12:24:37 +0800 Subject: [PATCH] UPT: annotation enhancement for resty-lua-waf --- .../nginx-configuration/annotations.md | 40 ++++++++++++++++ .../ingress/annotations/luarestywaf/main.go | 37 ++++++++++---- .../annotations/luarestywaf/main_test.go | 11 ++++- rootfs/etc/nginx/template/nginx.tmpl | 14 ++++++ test/e2e/annotations/luarestywaf.go | 48 +++++++++++++++++++ 5 files changed, 140 insertions(+), 10 deletions(-) diff --git a/docs/user-guide/nginx-configuration/annotations.md b/docs/user-guide/nginx-configuration/annotations.md index f1eb69e1b..6a61d21be 100644 --- a/docs/user-guide/nginx-configuration/annotations.md +++ b/docs/user-guide/nginx-configuration/annotations.md @@ -80,6 +80,9 @@ You can add these Kubernetes annotations to specific Ingress objects to customiz |[nginx.ingress.kubernetes.io/lua-resty-waf-debug](#lua-resty-waf)|"true" or "false"| |[nginx.ingress.kubernetes.io/lua-resty-waf-ignore-rulesets](#lua-resty-waf)|string| |[nginx.ingress.kubernetes.io/lua-resty-waf-extra-rules](#lua-resty-waf)|string| +|[nginx.ingress.kubernetes.io/lua-resty-waf-allow-unknown-content](#lua-resty-waf)|"true" or "false"| +|[nginx.ingress.kubernetes.io/lua-resty-waf-score](#lua-resty-waf)|number| +|[nginx.ingress.kubernetes.io/lua-resty-waf-disable-multipart-body](#lua-resty-waf)|"true" or "false"| |[nginx.ingress.kubernetes.io/enable-influxdb](#influxdb)|"true" or "false"| |[nginx.ingress.kubernetes.io/influxdb-measurement](#influxdb)|string| |[nginx.ingress.kubernetes.io/influxdb-port](#influxdb)|string| @@ -558,6 +561,43 @@ It is also possible to configure custom WAF rules per ingress using the `nginx.i nginx.ingress.kubernetes.io/lua-resty-waf-extra-rules: '[=[ { "access": [ { "actions": { "disrupt" : "DENY" }, "id": 10001, "msg": "my custom rule", "operator": "STR_CONTAINS", "pattern": "foo", "vars": [ { "parse": [ "values", 1 ], "type": "REQUEST_ARGS" } ] } ], "body_filter": [], "header_filter":[] } ]=]' ``` +Since the default allowed contents were `"text/html", "text/json", "application/json"` +We can enable the following annotation for allow all contents type: + + +```yaml +nginx.ingress.kubernetes.io/lua-resty-waf-allow-unknown-content: "true" +``` + +The default score of lua-resty-waf is 5, which usually triggered if hitting 2 default rules, you can modify the score threshold with following annotation: + + +```yaml +nginx.ingress.kubernetes.io/lua-resty-waf-score: "10" +``` + +When you enabled HTTPS in the endpoint and since resty-lua will return 500 error when processing "multipart" contents +Reference for this [issue](https://github.com/p0pr0ck5/lua-resty-waf/issues/166) +You may enable the following annotation for work around: + +```yaml +nginx.ingress.kubernetes.io/lua-resty-waf-disable-multipart-body: "true" +``` + +For details on how to write WAF rules, please refer to [https://github.com/p0pr0ck5/lua-resty-waf](https://github.com/p0pr0ck5/lua-resty-waf). + + +```yaml +nginx.ingress.kubernetes.io/lua-resty-waf-allow-unknown-content: "true" +``` + +The default score of lua-resty-waf is 5, which usually triggered if hitting 2 default rules, you can modify the score threshold with following annotation: + + +```yaml +nginx.ingress.kubernetes.io/lua-resty-waf-score: "10" +``` + For details on how to write WAF rules, please refer to [https://github.com/p0pr0ck5/lua-resty-waf](https://github.com/p0pr0ck5/lua-resty-waf). [configmap]: ./configmap.md diff --git a/internal/ingress/annotations/luarestywaf/main.go b/internal/ingress/annotations/luarestywaf/main.go index ca7f4a8be..4873851ab 100644 --- a/internal/ingress/annotations/luarestywaf/main.go +++ b/internal/ingress/annotations/luarestywaf/main.go @@ -31,10 +31,13 @@ var luaRestyWAFModes = map[string]bool{"ACTIVE": true, "INACTIVE": true, "SIMULA // Config returns lua-resty-waf configuration for an Ingress rule type Config struct { - Mode string `json:"mode"` - Debug bool `json:"debug"` - IgnoredRuleSets []string `json:"ignored-rulesets"` - ExtraRulesetString string `json:"extra-ruleset-string"` + Mode string `json:"mode"` + Debug bool `json:"debug"` + IgnoredRuleSets []string `json:"ignored-rulesets"` + ExtraRulesetString string `json:"extra-ruleset-string"` + Score int `json:"score"` + AllowUnknownContent bool `json:"allow-unknown-content"` + DisableMultipartBody bool `json:"disable-multipart-body"` } // Equal tests for equality between two Config types @@ -57,6 +60,15 @@ func (e1 *Config) Equal(e2 *Config) bool { if e1.ExtraRulesetString != e2.ExtraRulesetString { return false } + if e1.Score != e2.Score { + return false + } + if e1.AllowUnknownContent != e2.AllowUnknownContent { + return false + } + if e1.DisableMultipartBody != e2.DisableMultipartBody { + return false + } return true } @@ -95,10 +107,19 @@ func (a luarestywaf) Parse(ing *extensions.Ingress) (interface{}, error) { // TODO(elvinefendi) maybe validate the ruleset string here extraRulesetString, _ := parser.GetStringAnnotation("lua-resty-waf-extra-rules", ing) + score, _ := parser.GetIntAnnotation("lua-resty-waf-score", ing) + + allowUnknownContent, _ := parser.GetBoolAnnotation("lua-resty-waf-allow-unknown-content", ing) + + disableMultipartBody, _ := parser.GetBoolAnnotation("lua-resty-waf-disable-multipart-body", ing) + return &Config{ - Mode: mode, - Debug: debug, - IgnoredRuleSets: ignoredRuleSets, - ExtraRulesetString: extraRulesetString, + Mode: mode, + Debug: debug, + IgnoredRuleSets: ignoredRuleSets, + ExtraRulesetString: extraRulesetString, + Score: score, + AllowUnknownContent: allowUnknownContent, + DisableMultipartBody: disableMultipartBody, }, nil } diff --git a/internal/ingress/annotations/luarestywaf/main_test.go b/internal/ingress/annotations/luarestywaf/main_test.go index d71191f12..e9b50c53f 100644 --- a/internal/ingress/annotations/luarestywaf/main_test.go +++ b/internal/ingress/annotations/luarestywaf/main_test.go @@ -30,6 +30,9 @@ func TestParse(t *testing.T) { luaRestyWAFAnnotation := parser.GetAnnotationWithPrefix("lua-resty-waf") luaRestyWAFDebugAnnotation := parser.GetAnnotationWithPrefix("lua-resty-waf-debug") luaRestyWAFIgnoredRuleSetsAnnotation := parser.GetAnnotationWithPrefix("lua-resty-waf-ignore-rulesets") + luaRestyWAFScoreAnnotation := parser.GetAnnotationWithPrefix("lua-resty-waf-score") + luaRestyWAFAllowUnknownAnnotation := parser.GetAnnotationWithPrefix("lua-resty-waf-allow-unknown-content") + luaRestyWAFDisableMultipartBody := parser.GetAnnotationWithPrefix("lua-resty-waf-disable-multipart-body") ap := NewParser(&resolver.Mock{}) if ap == nil { @@ -53,11 +56,15 @@ func TestParse(t *testing.T) { {map[string]string{ luaRestyWAFAnnotation: "active", luaRestyWAFDebugAnnotation: "true", - luaRestyWAFIgnoredRuleSetsAnnotation: "ruleset1, ruleset2 ruleset3, another.ruleset"}, - &Config{Mode: "ACTIVE", Debug: true, IgnoredRuleSets: []string{"ruleset1", "ruleset2", "ruleset3", "another.ruleset"}}}, + luaRestyWAFIgnoredRuleSetsAnnotation: "ruleset1, ruleset2 ruleset3, another.ruleset", + luaRestyWAFScoreAnnotation: "10", + luaRestyWAFAllowUnknownAnnotation: "true"}, + &Config{Mode: "ACTIVE", Debug: true, IgnoredRuleSets: []string{"ruleset1", "ruleset2", "ruleset3", "another.ruleset"}, Score: 10, AllowUnknownContent: true}}, {map[string]string{luaRestyWAFAnnotation: "siMulate", luaRestyWAFDebugAnnotation: "true"}, &Config{Mode: "SIMULATE", Debug: true, IgnoredRuleSets: []string{}}}, {map[string]string{luaRestyWAFAnnotation: "siMulateX", luaRestyWAFDebugAnnotation: "true"}, &Config{Debug: false}}, + + {map[string]string{luaRestyWAFAnnotation: "active", luaRestyWAFDisableMultipartBody: "false"}, &Config{Mode: "ACTIVE", DisableMultipartBody: false, IgnoredRuleSets: []string{}}}, } ing := &extensions.Ingress{ diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index 22db89b4d..16ebf9bb4 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -891,9 +891,23 @@ stream { waf:set_option("mode", "{{ $location.LuaRestyWAF.Mode }}") waf:set_option("storage_zone", "waf_storage") + + {{ if $location.LuaRestyWAF.AllowUnknownContent }} + waf:set_option("allow_unknown_content_types", true) + {{ else }} waf:set_option("allowed_content_types", { "text/html", "text/json", "application/json" }) + {{ end }} + waf:set_option("event_log_level", ngx.WARN) + {{ if gt $location.LuaRestyWAF.Score 0 }} + waf:set_option("score_threshold", {{ $location.LuaRestyWAF.Score }}) + {{ end }} + + {{ if $location.LuaRestyWAF.DisableMultipartBody }} + waf:set_option("process_multipart_body", false) + {{ end }} + {{ if $location.LuaRestyWAF.Debug }} waf:set_option("debug", true) waf:set_option("event_log_request_arguments", true) diff --git a/test/e2e/annotations/luarestywaf.go b/test/e2e/annotations/luarestywaf.go index ed332db04..e96727419 100644 --- a/test/e2e/annotations/luarestywaf.go +++ b/test/e2e/annotations/luarestywaf.go @@ -65,6 +65,54 @@ var _ = framework.IngressNginxDescribe("Annotations - lua-resty-waf", func() { Expect(len(errs)).Should(Equal(0)) Expect(resp.StatusCode).Should(Equal(http.StatusOK)) }) + It("should apply the score threshold", func() { + host := "foo" + createIngress(f, host, "http-svc", 80, map[string]string{ + "nginx.ingress.kubernetes.io/lua-resty-waf": "active", + "nginx.ingress.kubernetes.io/lua-resty-waf-score": "20"}) + + url := fmt.Sprintf("%s?msg=XSS", f.IngressController.HTTPURL) + resp, _, errs := gorequest.New(). + Get(url). + Set("Host", host). + End() + + Expect(len(errs)).Should(Equal(0)) + Expect(resp.StatusCode).Should(Equal(http.StatusOK)) + }) + It("should reject the invaild content", func() { + host := "foo" + contenttype := "application/octet-stream" + createIngress(f, host, "http-svc", 80, map[string]string{ + "nginx.ingress.kubernetes.io/lua-resty-waf": "active"}) + + url := fmt.Sprintf("%s?msg=my-message", f.IngressController.HTTPURL) + resp, _, errs := gorequest.New(). + Get(url). + Set("Host", host). + Set("Content-Type", contenttype). + End() + + Expect(len(errs)).Should(Equal(0)) + Expect(resp.StatusCode).Should(Equal(http.StatusForbidden)) + }) + It("should allow the multipart content type", func() { + host := "foo" + contenttype := "multipart/form-data; boundary=alamofire.boundary.3fc2e849279e18fc" + createIngress(f, host, "http-svc", 80, map[string]string{ + "nginx.ingress.kubernetes.io/lua-resty-waf-disable-multipart-body": "true", + "nginx.ingress.kubernetes.io/lua-resty-waf": "active"}) + + url := fmt.Sprintf("%s?msg=my-message", f.IngressController.HTTPURL) + resp, _, errs := gorequest.New(). + Get(url). + Set("Host", host). + Set("Content-Type", contenttype). + End() + + Expect(len(errs)).Should(Equal(0)) + Expect(resp.StatusCode).Should(Equal(http.StatusOK)) + }) It("should apply configured extra rules", func() { host := "foo" createIngress(f, host, "http-svc", 80, map[string]string{