Tests: rproxy gives a refused visitor a page of its own
A deny of 403 from waf gets rproxy's refused page, a deny of 429 its rate-limited page, each as text/html with waf's other headers kept. With SWW_RPROXY_CONTACT_EMAIL set, the pages' sentence about the operator is a mailto link. Every other answer from waf still goes to the visitor as waf gave it. The two pages are beside the code, as the tests read them.
This commit is contained in:
1 parent
8219664ad1
commit
5dfa47c97b
4 files changed
+237
-30
No files matched your search
+181
-30
@@ -11,6 +11,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -52,8 +53,56 @@ const (
|
||||
maxResponseSize = "SWW_RPROXY_MAX_RESPONSE_SIZE"
|
||||
maxRequestTime = "SWW_RPROXY_MAX_REQUEST_TIME_MS"
|
||||
maxResponseTime = "SWW_RPROXY_MAX_RESPONSE_TIME_MS"
|
||||
contactEmail = "SWW_RPROXY_CONTACT_EMAIL"
|
||||
)
|
||||
|
||||
// The pages rproxy gives a refused visitor in place of waf's body.
|
||||
const (
|
||||
// refusedPageFile and rateLimitedPageFile are the pages' files, beside
|
||||
// the code.
|
||||
refusedPageFile = "refused.html"
|
||||
rateLimitedPageFile = "ratelimited.html"
|
||||
// contactSentence is the sentence of each page that becomes a link to
|
||||
// SWW_RPROXY_CONTACT_EMAIL.
|
||||
contactSentence = "contact the site's operator"
|
||||
htmlContentType = "text/html; charset=utf-8"
|
||||
)
|
||||
|
||||
// page is the page in file, as rproxy serves it without a contact email.
|
||||
func page(t *testing.T, file string) string {
|
||||
t.Helper()
|
||||
|
||||
//nolint:gosec // a page beside the code
|
||||
contents, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
t.Fatalf("read the page: %v", err)
|
||||
}
|
||||
|
||||
return string(contents)
|
||||
}
|
||||
|
||||
// wantPage checks that got is status with the page in file as its body, and
|
||||
// the headers of a page.
|
||||
func wantPage(t *testing.T, got answered, status int, file string) {
|
||||
t.Helper()
|
||||
|
||||
if got.status != status || got.body != page(t, file) {
|
||||
t.Errorf(
|
||||
"answered %d %q, want %d and %s",
|
||||
got.status,
|
||||
got.body,
|
||||
status,
|
||||
file,
|
||||
)
|
||||
}
|
||||
|
||||
if got.header.Get("Content-Type") != htmlContentType ||
|
||||
got.header.Get("X-Content-Type-Options") != "nosniff" ||
|
||||
got.header.Get("Cache-Control") != "no-store" {
|
||||
t.Errorf("the page's headers are %v", got.header)
|
||||
}
|
||||
}
|
||||
|
||||
// The headers the tests set and look at.
|
||||
const (
|
||||
dispositionHeader = wafprotocol.DispositionHeader
|
||||
@@ -306,6 +355,20 @@ func deny(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = io.WriteString(w, deniedBody)
|
||||
}
|
||||
|
||||
// answerAs answers a question with status and body, with disposition unless
|
||||
// it is "", and a header of its own.
|
||||
func answerAs(disposition string, status int, body string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
if disposition != "" {
|
||||
w.Header().Set(dispositionHeader, disposition)
|
||||
}
|
||||
|
||||
w.Header().Set(fromWAFHeader, addedHeaderValue)
|
||||
w.WriteHeader(status)
|
||||
_, _ = io.WriteString(w, body)
|
||||
}
|
||||
}
|
||||
|
||||
// startApp starts app as the app rproxy passes requests to.
|
||||
func startApp(t *testing.T, app http.HandlerFunc) *httptest.Server {
|
||||
t.Helper()
|
||||
@@ -767,7 +830,7 @@ func TestRemovesTheVisitorsLookupHeadersWhenWAFGivesNone(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDenyGivesTheVisitorWAFsAnswer(t *testing.T) {
|
||||
func TestDenyGivesTheVisitorTheRefusedPageWithWAFsHeaders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
standIn := startWAF(t, deny)
|
||||
@@ -777,9 +840,10 @@ func TestDenyGivesTheVisitorWAFsAnswer(t *testing.T) {
|
||||
addr, out := startRProxy(t, standIn.URL, app.URL, nil)
|
||||
|
||||
got := get(t, addr, "/")
|
||||
if got.status != http.StatusForbidden || got.body != deniedBody ||
|
||||
got.header.Get(fromWAFHeader) != addedHeaderValue {
|
||||
t.Errorf("answered %+v, want waf's answer", got)
|
||||
wantPage(t, got, http.StatusForbidden, refusedPageFile)
|
||||
|
||||
if got.header.Get(fromWAFHeader) != addedHeaderValue {
|
||||
t.Errorf("answered %+v, want waf's headers", got)
|
||||
}
|
||||
|
||||
if len(got.header.Values(dispositionHeader)) != 0 {
|
||||
@@ -788,7 +852,7 @@ func TestDenyGivesTheVisitorWAFsAnswer(t *testing.T) {
|
||||
|
||||
report := standIn.report(t)
|
||||
if report.Status != http.StatusForbidden ||
|
||||
report.ResponseBytes != int64(len(deniedBody)) ||
|
||||
report.ResponseBytes != int64(len(page(t, refusedPageFile))) ||
|
||||
report.DurationUpstreamTotalMS != 0 {
|
||||
t.Errorf("report %+v, of a request denied", report)
|
||||
}
|
||||
@@ -800,7 +864,7 @@ func TestDenyGivesTheVisitorWAFsAnswer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestADeniedHeadKeepsWAFsContentLength(t *testing.T) {
|
||||
func TestADeniedHeadGivesThePagesContentLength(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
standIn := startWAF(t, deny)
|
||||
@@ -808,13 +872,13 @@ func TestADeniedHeadKeepsWAFsContentLength(t *testing.T) {
|
||||
addr, _ := startRProxy(t, standIn.URL, app.URL, nil)
|
||||
|
||||
got := visit(t, newRequest(t, http.MethodHead, addr, "/", http.NoBody))
|
||||
if got.status != http.StatusForbidden ||
|
||||
if got.status != http.StatusForbidden || got.body != "" ||
|
||||
got.header.Get(
|
||||
contentLengthHeader,
|
||||
) != strconv.Itoa(
|
||||
len(deniedBody),
|
||||
len(page(t, refusedPageFile)),
|
||||
) {
|
||||
t.Errorf("answered %+v, want waf's 403 and its length", got)
|
||||
t.Errorf("answered %+v, want waf's 403 and the page's length", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -823,11 +887,13 @@ func TestAnAnswerRProxyHoldsWholeIsSentBeforeItsLogLine(t *testing.T) {
|
||||
|
||||
const host = " HTTP/1.1\r\nHost: site.example\r\n\r\n"
|
||||
|
||||
refusedPage := page(t, refusedPageFile)
|
||||
|
||||
for _, tc := range []struct {
|
||||
name, request, body string
|
||||
status int
|
||||
}{
|
||||
{"a deny", "GET /" + host, deniedBody, http.StatusForbidden},
|
||||
{"a deny", "GET /" + host, refusedPage, http.StatusForbidden},
|
||||
{"rproxy's own answer", "GET x:admin" + host, "Bad Request\n", http.StatusBadRequest},
|
||||
{"the health check", "GET " + wafprotocol.HealthPath + host, "ok\n", http.StatusOK},
|
||||
} {
|
||||
@@ -899,6 +965,83 @@ func TestRProxysOwnAnswerIsPlainText(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestADenyOf429GivesTheVisitorTheRateLimitedPage(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
standIn := startWAF(t, func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set(dispositionHeader, dispositionDeny)
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
_, _ = io.WriteString(w, deniedBody)
|
||||
})
|
||||
app := startApp(t, greet)
|
||||
addr, _ := startRProxy(t, standIn.URL, app.URL, nil)
|
||||
|
||||
wantPage(
|
||||
t,
|
||||
get(t, addr, "/"),
|
||||
http.StatusTooManyRequests,
|
||||
rateLimitedPageFile,
|
||||
)
|
||||
}
|
||||
|
||||
func TestThePagesLinkTheContactEmail(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const email = "ops@example.com"
|
||||
|
||||
link := `<a href="mailto:` + email + `">` + contactSentence + `</a>`
|
||||
|
||||
for _, tc := range []struct {
|
||||
file string
|
||||
status int
|
||||
}{
|
||||
{refusedPageFile, http.StatusForbidden},
|
||||
{rateLimitedPageFile, http.StatusTooManyRequests},
|
||||
} {
|
||||
t.Run(tc.file, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
standIn := startWAF(
|
||||
t,
|
||||
func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set(dispositionHeader, dispositionDeny)
|
||||
w.WriteHeader(tc.status)
|
||||
},
|
||||
)
|
||||
app := startApp(t, greet)
|
||||
addr, _ := startRProxy(
|
||||
t,
|
||||
standIn.URL,
|
||||
app.URL,
|
||||
map[string]string{contactEmail: email},
|
||||
)
|
||||
|
||||
got := get(t, addr, "/")
|
||||
want := strings.Replace(
|
||||
page(t, tc.file),
|
||||
contactSentence,
|
||||
link,
|
||||
1,
|
||||
)
|
||||
|
||||
if got.status != tc.status || got.body != want {
|
||||
t.Errorf(
|
||||
"answered %d %q, want %d %q",
|
||||
got.status,
|
||||
got.body,
|
||||
tc.status,
|
||||
want,
|
||||
)
|
||||
}
|
||||
|
||||
if !strings.Contains(page(t, tc.file), contactSentence) {
|
||||
t.Errorf("%s has no %q to link", tc.file, contactSentence)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseClosesTheConnectionWithoutAnAnswer(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -1017,17 +1160,28 @@ func noAnswers() map[string]func(t *testing.T) string {
|
||||
func TestAnAnswerOtherThanAllowGoesToTheVisitorAsWAFGaveIt(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const unknownBody = "who knows\n"
|
||||
|
||||
refusedPage := page(t, refusedPageFile)
|
||||
rateLimitedPage := page(t, rateLimitedPageFile)
|
||||
|
||||
// want is the body the visitor gets: waf's, but for a refusal, which gets
|
||||
// rproxy's page for its status.
|
||||
for _, tc := range []struct {
|
||||
name, disposition, body string
|
||||
status int
|
||||
name, disposition, body, want string
|
||||
status int
|
||||
}{
|
||||
{"a refusal", dispositionDeny, deniedBody, http.StatusForbidden},
|
||||
{"a refusal, 429", dispositionDeny, deniedBody, http.StatusTooManyRequests},
|
||||
{"waf's 400", "", "refused\n", http.StatusBadRequest},
|
||||
{"a 500", "", "broken\n", http.StatusInternalServerError},
|
||||
{"a 503 denied", dispositionDeny, "busy\n", http.StatusServiceUnavailable},
|
||||
{"no header", "", "who knows\n", http.StatusOK},
|
||||
{"an unknown disposition", "maybe", "who knows\n", http.StatusOK},
|
||||
{"a refusal", dispositionDeny, deniedBody, refusedPage, http.StatusForbidden},
|
||||
{
|
||||
"a refusal, 429", dispositionDeny, deniedBody, rateLimitedPage,
|
||||
http.StatusTooManyRequests,
|
||||
},
|
||||
{"waf's 400", "", "refused\n", "refused\n", http.StatusBadRequest},
|
||||
{"a 500", "", "broken\n", "broken\n", http.StatusInternalServerError},
|
||||
{"a 503 denied", dispositionDeny, "busy\n", "busy\n", http.StatusServiceUnavailable},
|
||||
{"a 403 without a disposition", "", "nope\n", "nope\n", http.StatusForbidden},
|
||||
{"no header", "", unknownBody, unknownBody, http.StatusOK},
|
||||
{"an unknown disposition", "maybe", unknownBody, unknownBody, http.StatusOK},
|
||||
} {
|
||||
for _, setting := range []string{dispositionAllow, dispositionDeny} {
|
||||
subtest := tc.name + " with " + noAnswerSetting + "=" + setting
|
||||
@@ -1036,15 +1190,7 @@ func TestAnAnswerOtherThanAllowGoesToTheVisitorAsWAFGaveIt(t *testing.T) {
|
||||
|
||||
standIn := startWAF(
|
||||
t,
|
||||
func(w http.ResponseWriter, _ *http.Request) {
|
||||
if tc.disposition != "" {
|
||||
w.Header().Set(dispositionHeader, tc.disposition)
|
||||
}
|
||||
|
||||
w.Header().Set(fromWAFHeader, addedHeaderValue)
|
||||
w.WriteHeader(tc.status)
|
||||
_, _ = io.WriteString(w, tc.body)
|
||||
},
|
||||
answerAs(tc.disposition, tc.status, tc.body),
|
||||
)
|
||||
app := startApp(t, func(http.ResponseWriter, *http.Request) {
|
||||
t.Error("the app was asked")
|
||||
@@ -1059,9 +1205,14 @@ func TestAnAnswerOtherThanAllowGoesToTheVisitorAsWAFGaveIt(t *testing.T) {
|
||||
)
|
||||
|
||||
got := get(t, addr, "/")
|
||||
if got.status != tc.status || got.body != tc.body ||
|
||||
if got.status != tc.status || got.body != tc.want ||
|
||||
got.header.Get(fromWAFHeader) != addedHeaderValue {
|
||||
t.Errorf("answered %+v, want waf's answer", got)
|
||||
t.Errorf(
|
||||
"answered %+v, want %d %q",
|
||||
got,
|
||||
tc.status,
|
||||
tc.want,
|
||||
)
|
||||
}
|
||||
|
||||
if len(got.header.Values(dispositionHeader)) != 0 {
|
||||
|
||||
Reference in new issue
Block a user