fix(server): refuse a host change while a stored sensitive value is kept

An update that keeps the stored value of a sensitive action can no longer
change the rule's host matcher (compared case-insensitively): the value
would otherwise be written to a new destination. Same invariant as the
custom HTTP tools. The url_regex matcher is not compared, an accepted and
documented limit.
This commit is contained in:
BaaRai
2026-11-17 00:12:09 +01:00
parent 38d66d83b2
commit 97f081ed63
3 changed files with 55 additions and 0 deletions
+6
View File
@@ -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.
+11
View File
@@ -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
}
+38
View File
@@ -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) {