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() {
|
|
|
|
err := f.NewEchoDeployment()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
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-04-09 12:19:13 +00:00
|
|
|
createIngress(f, host, map[string]string{"nginx.ingress.kubernetes.io/lua-resty-waf": "active"})
|
2018-04-08 20:37:13 +00:00
|
|
|
|
2018-04-18 19:15:08 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.IngressController.HTTPURL)
|
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"
|
|
|
|
createIngress(f, host, 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"})
|
|
|
|
|
2018-04-18 19:15:08 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.IngressController.HTTPURL)
|
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-04-09 10:14:30 +00:00
|
|
|
It("should apply configured extra rules", func() {
|
|
|
|
host := "foo"
|
|
|
|
createIngress(f, host, 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
|
|
|
})
|
|
|
|
|
2018-04-18 19:15:08 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=my-message", f.IngressController.HTTPURL)
|
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))
|
|
|
|
|
2018-04-18 19:15:08 +00:00
|
|
|
url = fmt.Sprintf("%s?msg=my-foo-message", f.IngressController.HTTPURL)
|
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"
|
|
|
|
createIngress(f, host, map[string]string{})
|
|
|
|
|
2018-04-18 19:15:08 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.IngressController.HTTPURL)
|
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"
|
|
|
|
createIngress(f, host, map[string]string{"nginx.ingress.kubernetes.io/lua-resty-waf": "simulate"})
|
|
|
|
|
2018-04-18 19:15:08 +00:00
|
|
|
url := fmt.Sprintf("%s?msg=<A href=\"http://mysite.com/\">XSS</A>", f.IngressController.HTTPURL)
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
func createIngress(f *framework.Framework, host string, annotations map[string]string) {
|
2018-04-18 19:15:08 +00:00
|
|
|
ing, err := f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.IngressController.Namespace, &annotations))
|
2018-04-08 20:37:13 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(ing).NotTo(BeNil())
|
|
|
|
|
|
|
|
err = f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return Expect(server).Should(ContainSubstring("server_name foo")) &&
|
|
|
|
Expect(server).ShouldNot(ContainSubstring("return 503"))
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
resp, body, errs := gorequest.New().
|
2018-04-18 19:15:08 +00:00
|
|
|
Get(f.IngressController.HTTPURL).
|
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)))
|
|
|
|
}
|