2018-04-08 20:37:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package annotations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2018-04-09 12:19:13 +00:00
|
|
|
"time"
|
2018-04-08 20:37:13 +00:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/parnurzeal/gorequest"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = framework.IngressNginxDescribe("Annotations - lua-resty-waf", func() {
|
|
|
|
f := framework.NewDefaultFramework("luarestywaf")
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-10-29 21:39:04 +00:00
|
|
|
f.NewEchoDeployment()
|
2018-04-08 20:37:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("when lua-resty-waf is enabled", func() {
|
|
|
|
It("should return 403 for a malicious request that matches a default WAF rule and 200 for other requests", func() {
|
|
|
|
host := "foo"
|
2018-05-17 12:35:11 +00:00
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{"nginx.ingress.kubernetes.io/lua-resty-waf": "active"})
|
2018-04-08 20:37:13 +00:00
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.GetURL(framework.HTTP))
|
2018-04-08 20:37:13 +00:00
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(url).
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(Equal(0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusForbidden))
|
|
|
|
})
|
2018-04-09 01:55:23 +00:00
|
|
|
It("should not apply ignored rulesets", func() {
|
|
|
|
host := "foo"
|
2018-05-17 12:35:11 +00:00
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{
|
2018-04-09 12:19:13 +00:00
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf": "active",
|
2018-04-09 01:55:23 +00:00
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf-ignore-rulesets": "41000_sqli, 42000_xss"})
|
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.GetURL(framework.HTTP))
|
2018-04-09 01:55:23 +00:00
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(url).
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(Equal(0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
})
|
2018-10-05 04:24:37 +00:00
|
|
|
It("should apply the score threshold", func() {
|
|
|
|
host := "foo"
|
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{
|
2018-10-20 04:06:05 +00:00
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf": "active",
|
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf-score-threshold": "20"})
|
2018-10-05 04:24:37 +00:00
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.GetURL(framework.HTTP))
|
2018-10-05 04:24:37 +00:00
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(url).
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(Equal(0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
})
|
2018-10-24 10:30:43 +00:00
|
|
|
It("should not reject request with an unknown content type", func() {
|
2018-10-05 04:24:37 +00:00
|
|
|
host := "foo"
|
|
|
|
contenttype := "application/octet-stream"
|
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{
|
2018-10-20 04:06:05 +00:00
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf-allow-unknown-content-types": "true",
|
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf": "active"})
|
2018-10-05 04:24:37 +00:00
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=my-message", f.GetURL(framework.HTTP))
|
2018-10-05 04:24:37 +00:00
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(url).
|
|
|
|
Set("Host", host).
|
|
|
|
Set("Content-Type", contenttype).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(Equal(0))
|
2018-10-20 04:06:05 +00:00
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
2018-10-05 04:24:37 +00:00
|
|
|
})
|
2018-10-25 02:43:26 +00:00
|
|
|
It("should not fail a request with multipart content type when multipart body processing disabled", func() {
|
2018-10-05 04:24:37 +00:00
|
|
|
contenttype := "multipart/form-data; boundary=alamofire.boundary.3fc2e849279e18fc"
|
2018-10-25 02:43:26 +00:00
|
|
|
host := "foo"
|
2018-10-05 04:24:37 +00:00
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{
|
2018-10-20 04:06:05 +00:00
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf-process-multipart-body": "false",
|
2018-10-05 04:24:37 +00:00
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf": "active"})
|
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=my-message", f.GetURL(framework.HTTP))
|
2018-10-05 04:24:37 +00:00
|
|
|
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))
|
|
|
|
})
|
2018-10-25 02:43:26 +00:00
|
|
|
It("should fail a request with multipart content type when multipart body processing enabled by default", func() {
|
|
|
|
contenttype := "multipart/form-data; boundary=alamofire.boundary.3fc2e849279e18fc"
|
|
|
|
host := "foo"
|
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf": "active"})
|
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=my-message", f.GetURL(framework.HTTP))
|
2018-10-25 02:43:26 +00:00
|
|
|
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.StatusBadRequest))
|
|
|
|
})
|
2018-04-09 10:14:30 +00:00
|
|
|
It("should apply configured extra rules", func() {
|
|
|
|
host := "foo"
|
2018-05-17 12:35:11 +00:00
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{
|
2018-04-09 12:19:13 +00:00
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf": "active",
|
2018-04-09 10:14:30 +00:00
|
|
|
"nginx.ingress.kubernetes.io/lua-resty-waf-extra-rules": `[=[
|
2018-04-09 12:19:13 +00:00
|
|
|
{ "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":[]
|
|
|
|
}
|
|
|
|
]=]`,
|
2018-04-09 10:14:30 +00:00
|
|
|
})
|
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=my-message", f.GetURL(framework.HTTP))
|
2018-04-09 10:14:30 +00:00
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(url).
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(Equal(0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url = fmt.Sprintf("%s?msg=my-foo-message", f.GetURL(framework.HTTP))
|
2018-04-09 10:14:30 +00:00
|
|
|
resp, _, errs = gorequest.New().
|
|
|
|
Get(url).
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
2018-04-08 20:37:13 +00:00
|
|
|
|
2018-04-09 10:14:30 +00:00
|
|
|
Expect(len(errs)).Should(Equal(0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusForbidden))
|
|
|
|
})
|
|
|
|
})
|
2018-04-08 20:37:13 +00:00
|
|
|
Context("when lua-resty-waf is not enabled", func() {
|
|
|
|
It("should return 200 even for a malicious request", func() {
|
|
|
|
host := "foo"
|
2018-05-17 12:35:11 +00:00
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{})
|
2018-04-08 20:37:13 +00:00
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.GetURL(framework.HTTP))
|
2018-04-08 20:37:13 +00:00
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(url).
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(Equal(0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
})
|
2018-04-09 12:19:13 +00:00
|
|
|
It("should run in simulate mode", func() {
|
|
|
|
host := "foo"
|
2018-05-17 12:35:11 +00:00
|
|
|
createIngress(f, host, "http-svc", 80, map[string]string{"nginx.ingress.kubernetes.io/lua-resty-waf": "simulate"})
|
2018-04-09 12:19:13 +00:00
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.GetURL(framework.HTTP))
|
2018-04-09 12:19:13 +00:00
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
Get(url).
|
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(Equal(0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
log, err := f.NginxLogs()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(log).To(ContainSubstring("Request score greater than score threshold"))
|
|
|
|
})
|
2018-04-08 20:37:13 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-05-17 12:35:11 +00:00
|
|
|
func createIngress(f *framework.Framework, host, service string, port int, annotations map[string]string) {
|
2019-02-22 14:03:42 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, service, port, &annotations)
|
2018-10-29 21:39:04 +00:00
|
|
|
f.EnsureIngress(ing)
|
2018-04-08 20:37:13 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-04-08 20:37:13 +00:00
|
|
|
func(server string) bool {
|
2018-11-30 12:37:48 +00:00
|
|
|
return Expect(server).Should(ContainSubstring(fmt.Sprintf("server_name %v", host)))
|
2018-04-08 20:37:13 +00:00
|
|
|
})
|
|
|
|
|
2018-08-23 01:20:57 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2018-04-08 20:37:13 +00:00
|
|
|
resp, body, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTP)).
|
2018-04-08 20:37:13 +00:00
|
|
|
Set("Host", host).
|
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(len(errs)).Should(Equal(0))
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(body).Should(ContainSubstring(fmt.Sprintf("host=%v", host)))
|
|
|
|
}
|