config: SWW_RPROXY_CONTACT_EMAIL that is not an email address stops the start
It was taken as given, so a typo or a name with the address became a broken mailto link on every refusal page. Every other setting that is set but invalid stops the start; this one now must be a bare address net/mail reads, such as ops@example.com. Model: opus-5-5
This commit is contained in:
1 parent
a7f25f0558
commit
84c1b88722
4 files changed
+47
-14
No files matched your search
@@ -308,7 +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_RPROXY_CONTACT_EMAIL` | unset | The email address the refusal pages link to, a bare one such as `ops@example.com`. |
|
||||
| `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. |
|
||||
|
||||
+1
-1
@@ -484,7 +484,7 @@ start, and an invalid value stops the start. Any can be given as a file, as a
|
||||
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.
|
||||
pages link to. A bare address, such as `ops@example.com`.
|
||||
- `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
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/mail"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -47,7 +48,8 @@ type RProxy struct {
|
||||
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.
|
||||
// or "" for no link (SWW_RPROXY_CONTACT_EMAIL). It is a bare address,
|
||||
// such as ops@example.com.
|
||||
ContactEmail string
|
||||
|
||||
// settings are the values read, as given or by default, for the log line
|
||||
@@ -72,6 +74,8 @@ var (
|
||||
errNotMillisecondsOrOff = errors.New(
|
||||
"is not a whole number of milliseconds above zero, such as 60000, or off",
|
||||
)
|
||||
errNotEmail = errors.New(
|
||||
"is not a bare email address, such as ops@example.com")
|
||||
)
|
||||
|
||||
// RProxyFromEnvironment reads rproxy's settings through v, normally the viper
|
||||
@@ -111,7 +115,7 @@ func RProxyFromEnvironment(v *viper.Viper) (*RProxy, error) {
|
||||
"SWW_RPROXY_MAX_RESPONSE_TIME_MS",
|
||||
"1800000",
|
||||
),
|
||||
ContactEmail: env.value("SWW_RPROXY_CONTACT_EMAIL", ""),
|
||||
ContactEmail: env.email("SWW_RPROXY_CONTACT_EMAIL"),
|
||||
}
|
||||
|
||||
if env.err != nil {
|
||||
@@ -184,6 +188,22 @@ func (e *environment) disposition(
|
||||
return value
|
||||
}
|
||||
|
||||
// email reads a setting that is a bare email address, such as
|
||||
// ops@example.com, or "" when it is not set.
|
||||
func (e *environment) email(name string) string {
|
||||
value := e.value(name, "")
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
address, err := mail.ParseAddress(value)
|
||||
if err != nil || address.Address != value {
|
||||
e.check(name, fmt.Errorf("%q %w", value, errNotEmail))
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// millisecondsOrOff reads a setting that is a whole number of milliseconds
|
||||
// above zero, or off, which is 0.
|
||||
func (e *environment) millisecondsOrOff(
|
||||
|
||||
@@ -26,6 +26,7 @@ const (
|
||||
rproxyRequestTime = "SWW_RPROXY_MAX_REQUEST_TIME_MS"
|
||||
rproxyAnswerTime = "SWW_RPROXY_MAX_RESPONSE_TIME_MS"
|
||||
rproxyContactEmail = "SWW_RPROXY_CONTACT_EMAIL"
|
||||
contactAddress = "ops@example.com"
|
||||
backendURL = "http://waf:8080"
|
||||
rproxyNoAnswerAddr = ":8080"
|
||||
rproxyNoAnswerApp = "http://127.0.0.1:8081"
|
||||
@@ -88,21 +89,30 @@ func TestRProxySettingsAreRead(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cfg, err := rproxyConfig(map[string]string{
|
||||
wafBackend: "https://waf.example:8443",
|
||||
rproxyListen: ":9000",
|
||||
rproxyUpstream: "http://app:3000",
|
||||
rproxyTrusted: "192.0.2.0/24",
|
||||
wafBackendTimeout: "150",
|
||||
rproxyNoAnswer: "deny",
|
||||
rproxyMaxRequest: "1K",
|
||||
rproxyMaxResponse: off,
|
||||
rproxyRequestTime: off,
|
||||
rproxyAnswerTime: "250",
|
||||
wafBackend: "https://waf.example:8443",
|
||||
rproxyListen: ":9000",
|
||||
rproxyUpstream: "http://app:3000",
|
||||
rproxyTrusted: "192.0.2.0/24",
|
||||
wafBackendTimeout: "150",
|
||||
rproxyNoAnswer: "deny",
|
||||
rproxyMaxRequest: "1K",
|
||||
rproxyMaxResponse: off,
|
||||
rproxyRequestTime: off,
|
||||
rproxyAnswerTime: "250",
|
||||
rproxyContactEmail: contactAddress,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("settings: %v", err)
|
||||
}
|
||||
|
||||
if cfg.ContactEmail != contactAddress {
|
||||
t.Errorf(
|
||||
"contact email %q, want %q",
|
||||
cfg.ContactEmail,
|
||||
contactAddress,
|
||||
)
|
||||
}
|
||||
|
||||
if cfg.ListenAddr != ":9000" || cfg.UpstreamURL.Host != "app:3000" ||
|
||||
cfg.WAFBackend.Host != "waf.example:8443" ||
|
||||
cfg.TrustedProxies[0] != netip.MustParsePrefix("192.0.2.0/24") {
|
||||
@@ -155,6 +165,9 @@ func TestRProxyInvalidSettingsAreRefused(t *testing.T) {
|
||||
{rproxyRequestTime, "0"},
|
||||
{rproxyRequestTime, "1m"},
|
||||
{rproxyAnswerTime, "0"},
|
||||
{rproxyContactEmail, "ops"},
|
||||
{rproxyContactEmail, "Ops <" + contactAddress + ">"},
|
||||
{rproxyContactEmail, contactAddress + " "},
|
||||
} {
|
||||
t.Run(tc.name+"="+tc.value, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
Reference in new issue
Block a user