diff --git a/docs/traffic-rewrite-rules.md b/docs/traffic-rewrite-rules.md index e78b246..9c73af0 100644 --- a/docs/traffic-rewrite-rules.md +++ b/docs/traffic-rewrite-rules.md @@ -79,6 +79,12 @@ On update, a sensitive action sent without `value` keeps the stored value, provi rule has a sensitive action of the same type and header name at the same position. Otherwise the update is refused and the value must be sent again. An explicit `"value": ""` is a value. +A stored sensitive value is not written to a new destination. When an update keeps at least one +stored value, the `host` matcher must stay the same (compared case-insensitively); otherwise the +update is refused with HTTP 400 and the value must be sent again or cleared. The `url_regex` +matcher is not part of this comparison, an accepted limit: a kept value follows a rule whose +`url_regex` changes on the same host. + The flag covers the action's value only. Matchers are not masked. A value an action places in the URL, marked sensitive or not, is sent to the target as part of the URL. diff --git a/server/traffic_rewrite.go b/server/traffic_rewrite.go index 9ad8e8c..da95ba2 100644 --- a/server/traffic_rewrite.go +++ b/server/traffic_rewrite.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "net/http" + "strings" "time" "github.com/Autumn-27/artex/db" @@ -82,8 +83,14 @@ func encodeRewriteRule(r rewrite.Rule) db.TrafficRewriteRule { // rule builds the rule a request describes. stored is the rule being updated, // nil on create: it supplies the sensitive values the request left out. +// +// A stored sensitive value is not written to a new destination: when one is +// kept, the host matcher must not change (compared case-insensitively). The +// url_regex matcher is not compared, an accepted limit: a kept value can follow +// a rule whose url_regex changes on the same host. func (req rewriteRuleReq) rule(stored *rewrite.Rule) (rewrite.Rule, error) { r := rewrite.Rule{Name: req.Name, Enabled: req.Enabled, Phase: req.Phase, Priority: req.Priority, Matchers: req.Matchers, Note: req.Note} + kept := false for i, a := range req.Actions { out := rewrite.Action{Type: a.Type, Name: a.Name, Regex: a.Regex, Sensitive: a.Sensitive} switch { @@ -98,9 +105,13 @@ func (req rewriteRuleReq) rule(stored *rewrite.Rule) (rewrite.Rule, error) { return r, fmt.Errorf("action %d (%s): no stored value to keep, send the value", i+1, a.Type) } out.Value = old.Value + kept = true } r.Actions = append(r.Actions, out) } + if kept && !strings.EqualFold(stored.Matchers.Host, req.Matchers.Host) { + return r, errors.New("destination changed (host matcher) while a stored sensitive value was kept: send the value again or clear it; stored secrets are not written to a new destination") + } return r, nil } diff --git a/server/traffic_rewrite_test.go b/server/traffic_rewrite_test.go index 4b473ea..d58de0c 100644 --- a/server/traffic_rewrite_test.go +++ b/server/traffic_rewrite_test.go @@ -247,6 +247,44 @@ func TestRewriteRulesCRUD(t *testing.T) { } } +// An update that keeps a stored sensitive value cannot change the host matcher: +// the value is not written to a new destination. +func TestRewriteRulesKeptValueHostChange400(t *testing.T) { + s, request := rewriteServer(t) + const secret = "Bearer rwapi-plain-host" + body := func(host, value string) string { + return `{"name":"t-rwapi-host","enabled":true,"phase":"request","matchers":{"host":"` + host + `"}, +"actions":[{"type":"set_header","name":"Authorization",` + value + `"sensitive":true}]}` + } + r := decodeRewriteRuleJSON(t, request(true, "POST", rewriteAPI, body("api.example", `"value":"`+secret+`",`))) + path := fmt.Sprintf("%s/%d", rewriteAPI, r.ID) + if resp := request(true, "POST", path+"/toggle", `{"enabled":true}`); resp.Code != 200 { + t.Fatalf("toggle: HTTP %d %s", resp.Code, resp.Body.String()) + } + + for _, host := range []string{"evil.example", "*.example", ""} { + resp := request(true, "PUT", path, body(host, "")) + if resp.Code != 400 || !strings.Contains(resp.Body.String(), "destination changed (host matcher)") || strings.Contains(resp.Body.String(), "rwapi-plain") { + t.Fatalf("host %q with the stored value kept: HTTP %d %s, want 400", host, resp.Code, resp.Body.String()) + } + } + if stored, _ := listRewriteRules(t, request); len(stored) != 1 || stored[0].Matchers.Host != "api.example" { + t.Fatalf("a refused update changed the rule: %+v", stored) + } + if got := sentToTarget(s).Header.Get("Authorization"); got != secret { + t.Fatalf("after the refused updates: Authorization=%q, want the stored value", got) + } + + // Same host in another case: kept. A new host with the value sent again: accepted. + decodeRewriteRuleJSON(t, request(true, "PUT", path, body("API.Example", ""))) + if got := sentToTarget(s).Header.Get("Authorization"); got != secret { + t.Fatalf("host in another case: Authorization=%q, want the stored value kept", got) + } + if r2 := decodeRewriteRuleJSON(t, request(true, "PUT", path, body("evil.example", `"value":"other",`))); r2.Matchers.Host != "evil.example" { + t.Fatalf("new host with the value sent again: %+v", r2) + } +} + // The validation refuses a rule with HTTP 400 on create and on update, and // stores nothing. func TestRewriteRulesValidation400(t *testing.T) {