A deny of 403 from waf is answered with refused.html, a deny of 429 with ratelimited.html, each as text/html with waf's other headers kept. Both pages are made once at start and held in memory. With SWW_RPROXY_CONTACT_EMAIL set, their "contact the site's operator" is a mailto link to it; the value is taken as given.
This commit is contained in:
1 parent
5dfa47c97b
commit
b157359910
8 files changed
+111
-3
No files matched your search
@@ -308,6 +308,7 @@ Every setting the code reads, with its default:
|
||||
| `SWW_RPROXY_MAX_RESPONSE_SIZE` | `1G` | The largest response body, a size or `off`. The visitor gets `502` in place of a larger one, or it is cut off if it has already started. |
|
||||
| `SWW_RPROXY_MAX_REQUEST_TIME_MS` | `60000` | Two time limits, each this long, in milliseconds or `off`: one for the visitor to send its request line and headers, then one for the whole request to reach the app. |
|
||||
| `SWW_RPROXY_MAX_RESPONSE_TIME_MS` | `1800000` | How long the answer may take, from the end of the request to its last byte reaching the visitor, in milliseconds or `off`. |
|
||||
| `SWW_RPROXY_CONTACT_EMAIL` | unset | The email address the refusal pages link to. |
|
||||
| `SWW_WAF_LISTEN_ADDR` | `:8080` | Where `waf` listens; leave the host part empty. |
|
||||
| `SWW_WAF_INSTANCE_NAME` | the container's host name | The name in every line `waf` logs, and in every alert and metric, such as `host1/waf`. |
|
||||
| `SWW_WAF_MODE` | `enforce` | `observe` refuses nothing and makes no ban; each log line says what `enforce` would have done. |
|
||||
|
||||
@@ -2431,8 +2431,9 @@ but the last below carries `X-Sww-Disposition`.
|
||||
carries `X-Client-ASN` and `X-Client-Country`, which `rproxy` adds to the
|
||||
request it passes on. A header whose value is unknown is left out.
|
||||
- `X-Sww-Disposition: deny`, with the status, headers and body the visitor is to
|
||||
get. `rproxy` gives them to the visitor, less this header. The status is one
|
||||
of:
|
||||
get. `rproxy` gives them to the visitor, less this header; a `403` or `429`
|
||||
gets `rproxy`'s own page as its body, linking `SWW_RPROXY_CONTACT_EMAIL` when
|
||||
set. The status is one of:
|
||||
- `SWW_WAF_BAN_RESPONSE`, for a refusal;
|
||||
- `403`, for a `block` rule or the Core Rule Set;
|
||||
- `414` or `431`, for a request over the header limits;
|
||||
|
||||
@@ -10,6 +10,9 @@ services:
|
||||
SWW_RPROXY_WAF_BACKEND: http://waf:8080
|
||||
SWW_RPROXY_UPSTREAM_URL: http://exampleapp:8081
|
||||
|
||||
# The address the refusal pages link to.
|
||||
# SWW_RPROXY_CONTACT_EMAIL: ops@example.com
|
||||
|
||||
# The netblocks whose X-Forwarded-For rproxy believes. By default
|
||||
# the private ranges, which cover a TLS-terminating proxy on the
|
||||
# Docker network. A proxy elsewhere needs its address added; a
|
||||
|
||||
@@ -483,6 +483,8 @@ start, and an invalid value stops the start. Any can be given as a file, as a
|
||||
answer `rproxy` gives itself always has at least 10 seconds to be sent, and so
|
||||
do the status and headers of the app's answer. A time in milliseconds or
|
||||
`off`.
|
||||
- `SWW_RPROXY_CONTACT_EMAIL` (default unset): the email address the refusal
|
||||
pages link to.
|
||||
- `SWW_RPROXY_LISTEN_ADDR` (default `:8080`): where `rproxy` listens, such as
|
||||
`:9000`.
|
||||
- `SWW_RPROXY_UPSTREAM_URL` (default `http://127.0.0.1:8081`): the app. An
|
||||
|
||||
@@ -46,6 +46,9 @@ type RProxy struct {
|
||||
// (SWW_RPROXY_MAX_RESPONSE_TIME_MS).
|
||||
MaxRequestTime time.Duration
|
||||
MaxResponseTime time.Duration
|
||||
// ContactEmail is the address the pages a refused visitor gets link to,
|
||||
// or "" for no link (SWW_RPROXY_CONTACT_EMAIL). It is taken as given.
|
||||
ContactEmail string
|
||||
|
||||
// settings are the values read, as given or by default, for the log line
|
||||
// at start.
|
||||
@@ -108,6 +111,7 @@ func RProxyFromEnvironment(v *viper.Viper) (*RProxy, error) {
|
||||
"SWW_RPROXY_MAX_RESPONSE_TIME_MS",
|
||||
"1800000",
|
||||
),
|
||||
ContactEmail: env.value("SWW_RPROXY_CONTACT_EMAIL", ""),
|
||||
}
|
||||
|
||||
if env.err != nil {
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package rproxy
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// pageFiles are the pages a refused visitor gets, as HTML with nothing
|
||||
// external: refused.html for a 403, ratelimited.html for a 429.
|
||||
//
|
||||
//go:embed refused.html ratelimited.html
|
||||
var pageFiles embed.FS
|
||||
|
||||
// contactSentence is the sentence of each page that becomes a link to
|
||||
// SWW_RPROXY_CONTACT_EMAIL when that is set.
|
||||
const contactSentence = "contact the site's operator"
|
||||
|
||||
// htmlContentType is the Content-Type of the pages.
|
||||
const htmlContentType = "text/html; charset=utf-8"
|
||||
|
||||
// blockPages are the pages a refused visitor gets, ready to send, made once
|
||||
// at start.
|
||||
type blockPages struct {
|
||||
refused []byte
|
||||
rateLimited []byte
|
||||
}
|
||||
|
||||
// newBlockPages makes the pages. With contactEmail set, each page's
|
||||
// contactSentence is a mailto link to it.
|
||||
func newBlockPages(contactEmail string) blockPages {
|
||||
return blockPages{
|
||||
refused: readPage("refused.html", contactEmail),
|
||||
rateLimited: readPage("ratelimited.html", contactEmail),
|
||||
}
|
||||
}
|
||||
|
||||
// readPage reads the page in name from pageFiles, linking its
|
||||
// contactSentence to contactEmail unless that is "". The files are part of
|
||||
// the binary, so a missing one is a bug, and a panic.
|
||||
func readPage(name, contactEmail string) []byte {
|
||||
contents, err := pageFiles.ReadFile(name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if contactEmail == "" {
|
||||
return contents
|
||||
}
|
||||
|
||||
link := `<a href="mailto:` + html.EscapeString(contactEmail) + `">` +
|
||||
contactSentence + `</a>`
|
||||
|
||||
return []byte(
|
||||
strings.Replace(string(contents), contactSentence, link, 1),
|
||||
)
|
||||
}
|
||||
|
||||
// forStatus returns the page for a refusal with status, and whether there is
|
||||
// one: the rate-limited page for a 429, the refused page for a 403.
|
||||
func (p blockPages) forStatus(status int) ([]byte, bool) {
|
||||
switch status {
|
||||
case http.StatusForbidden:
|
||||
return p.refused, true
|
||||
case http.StatusTooManyRequests:
|
||||
return p.rateLimited, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,7 @@ func New(params Params) *Server {
|
||||
Path: wafprotocol.ReportPath,
|
||||
}).String(),
|
||||
breakerClock: time.Now,
|
||||
pages: newBlockPages(params.Config.ContactEmail),
|
||||
}
|
||||
|
||||
s := &Server{h: h}
|
||||
@@ -156,6 +157,8 @@ type handler struct {
|
||||
breakerClock func() time.Time
|
||||
// reports are the goroutines sending reports to waf.
|
||||
reports reportGoroutines
|
||||
// pages are the pages a refused visitor gets.
|
||||
pages blockPages
|
||||
}
|
||||
|
||||
// newTransport returns what carries requests to the app. It never goes through
|
||||
|
||||
+24
-1
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"maps"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -187,7 +188,9 @@ func canBeAsked(r *http.Request) bool {
|
||||
len(r.Header.Values("User-Agent")) <= 1
|
||||
}
|
||||
|
||||
// answerWith gives the visitor waf's answer, less its disposition.
|
||||
// answerWith gives the visitor waf's answer, less its disposition. A deny of
|
||||
// 403 or 429 gets rproxy's page for that status as its body, in place of
|
||||
// waf's.
|
||||
func (rq *request) answerWith(answer *wafAnswer) {
|
||||
rq.prepareOwnAnswer(rq.limits.requestDeadline())
|
||||
|
||||
@@ -195,6 +198,15 @@ func (rq *request) answerWith(answer *wafAnswer) {
|
||||
maps.Copy(header, answer.header)
|
||||
header.Del(wafprotocol.DispositionHeader)
|
||||
|
||||
if rq.disposition == wafprotocol.DispositionDeny {
|
||||
page, found := rq.h.pages.forStatus(answer.status)
|
||||
if found {
|
||||
rq.sendPage(answer.status, page)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Go's server adds a Content-Type it guesses from the body to a response whose
|
||||
// headers have no Content-Type key. The key with no value sends none.
|
||||
if header.Get("Content-Type") == "" {
|
||||
@@ -204,6 +216,17 @@ func (rq *request) answerWith(answer *wafAnswer) {
|
||||
rq.send(answer.status, answer.body)
|
||||
}
|
||||
|
||||
// sendPage sends page as the body of a refusal with status. The length is set
|
||||
// here too, since send leaves a HEAD's as waf gave it, which is of waf's body.
|
||||
func (rq *request) sendPage(status int, page []byte) {
|
||||
header := rq.out.Header()
|
||||
header.Set("Content-Type", htmlContentType)
|
||||
header.Set("X-Content-Type-Options", "nosniff")
|
||||
header.Set("Cache-Control", "no-store")
|
||||
header.Set("Content-Length", strconv.Itoa(len(page)))
|
||||
rq.send(status, page)
|
||||
}
|
||||
|
||||
// sendReport sends waf report on a goroutine of its own, so that the
|
||||
// visitor does not wait for it. The report goes on after ctx, the request's own
|
||||
// context, has ended.
|
||||
|
||||
Reference in new issue
Block a user