fix(server): match a kept sensitive value on type, header name and regex

A sensitive action sent without its value took the stored value at the same
position, so two sensitive actions of the same type and header name swapped
their values when reordered. The stored action is now found by type,
canonical header name and regex, wherever it stands, and exactly one must
match: none or several is refused (400, send the value).

The CRUD test no longer expects a refusal for a reordered action.
This commit is contained in:
BaaRai
2026-11-17 00:12:09 +01:00
parent 97f081ed63
commit 8474131ba2
3 changed files with 70 additions and 10 deletions
+4 -2
View File
@@ -76,8 +76,10 @@ An action marked `"sensitive": true` keeps its `value` out of every API response
the action without `value` and with `"value_set": true`.
On update, a sensitive action sent without `value` keeps the stored value, provided the stored
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.
rule has exactly one sensitive action with the same type, header name (case-insensitive) and
`regex`, whatever its position: actions can be reordered without sending their values. When the
stored rule has no such action, or several (same type, same name, same `regex`), the update is
refused with HTTP 400 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
+12 -7
View File
@@ -23,8 +23,8 @@ import (
// rewriteActionDTO is an action on the wire. The value of a sensitive action is
// never returned: a read leaves Value out and sets ValueSet. On update, a
// sensitive action sent without a value keeps the stored one, provided the
// stored rule has a sensitive action of the same type and header name at the
// same position.
// stored rule has exactly one sensitive action of the same type, canonical
// header name and regex, wherever it stands; none or several is refused.
type rewriteActionDTO struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
@@ -97,14 +97,19 @@ func (req rewriteRuleReq) rule(stored *rewrite.Rule) (rewrite.Rule, error) {
case a.Value != nil:
out.Value = *a.Value
case a.Sensitive && a.Type != rewrite.ActionDeleteHeader:
if stored == nil || i >= len(stored.Actions) {
if stored == nil {
return r, fmt.Errorf("action %d (%s): a sensitive action needs a value", i+1, a.Type)
}
old := stored.Actions[i]
if !old.Sensitive || old.Type != a.Type || http.CanonicalHeaderKey(old.Name) != http.CanonicalHeaderKey(a.Name) {
return r, fmt.Errorf("action %d (%s): no stored value to keep, send the value", i+1, a.Type)
var match []rewrite.Action
for _, old := range stored.Actions {
if old.Sensitive && old.Type == a.Type && http.CanonicalHeaderKey(old.Name) == http.CanonicalHeaderKey(a.Name) && old.Regex == a.Regex {
match = append(match, old)
}
}
out.Value = old.Value
if len(match) != 1 {
return r, fmt.Errorf("action %d (%s): %d stored sensitive actions match its type, header name and regex, exactly one is needed to keep a value: send the value", i+1, a.Type, len(match))
}
out.Value = match[0].Value
kept = true
}
r.Actions = append(r.Actions, out)
+54 -1
View File
@@ -212,7 +212,6 @@ func TestRewriteRulesCRUD(t *testing.T) {
for name, bad := range map[string]string{
"other type": `[{"type":"set_body","sensitive":true}]`,
"other header": `[{"type":"set_header","name":"X-Other","sensitive":true}]`,
"other index": `[{"type":"set_header","name":"X-Plain","value":"v"},{"type":"set_header","name":"Authorization","sensitive":true}]`,
} {
resp := request(true, "PUT", path, `{"name":"t-rwapi-crud2","phase":"request","actions":`+bad+`}`)
if resp.Code != 400 || strings.Contains(resp.Body.String(), "rwapi-plain") {
@@ -285,6 +284,60 @@ func TestRewriteRulesKeptValueHostChange400(t *testing.T) {
}
}
// A kept value is matched on the action's type, header name and regex, not on
// its position: reordering two sensitive actions keeps each value on its own
// action.
func TestRewriteRulesKeptValuesFollowReorder(t *testing.T) {
s, request := rewriteServer(t)
const auth, key = "Bearer rwapi-plain-auth", "rwapi-plain-key"
r := decodeRewriteRuleJSON(t, request(true, "POST", rewriteAPI, `{"name":"t-rwapi-order","phase":"request","matchers":{"host":"api.example"},
"actions":[{"type":"set_header","name":"Authorization","value":"`+auth+`","sensitive":true},{"type":"set_header","name":"X-Api-Key","value":"`+key+`","sensitive":true}]}`))
path := fmt.Sprintf("%s/%d", rewriteAPI, r.ID)
reordered := request(true, "PUT", path, `{"name":"t-rwapi-order","enabled":true,"phase":"request","matchers":{"host":"api.example"},
"actions":[{"type":"set_header","name":"x-api-key","sensitive":true},{"type":"set_header","name":"Authorization","sensitive":true}]}`)
r2 := decodeRewriteRuleJSON(t, reordered)
if len(r2.Actions) != 2 || r2.Actions[0].Name != "x-api-key" || r2.Actions[1].Name != "Authorization" || !r2.Actions[0].ValueSet || !r2.Actions[1].ValueSet {
t.Fatalf("reordered rule = %+v", r2)
}
if strings.Contains(reordered.Body.String(), "rwapi-plain") {
t.Fatal("the sensitive values were returned by the update")
}
if h := sentToTarget(s).Header; h.Get("Authorization") != auth || h.Get("X-Api-Key") != key {
t.Fatalf("after the reorder: Authorization=%q X-Api-Key=%q, want each value on its own action", h.Get("Authorization"), h.Get("X-Api-Key"))
}
}
// Two stored sensitive actions of the same type, header name and regex: an
// action sent without its value cannot be told apart, the update is refused.
func TestRewriteRulesKeptValueAmbiguous400(t *testing.T) {
s, request := rewriteServer(t)
action := func(value string) string {
return `{"type":"replace_header","name":"Authorization","regex":"^Bearer (.*)$",` + value + `"sensitive":true}`
}
r := decodeRewriteRuleJSON(t, request(true, "POST", rewriteAPI, `{"name":"t-rwapi-ambiguous","phase":"request","matchers":{"host":"api.example"},
"actions":[`+action(`"value":"rwapi-plain-a",`)+`,`+action(`"value":"rwapi-plain-b",`)+`]}`))
path := fmt.Sprintf("%s/%d", rewriteAPI, r.ID)
for name, actions := range map[string]string{
"both omitted": action("") + `,` + action(""),
"one omitted": action(`"value":"rwapi-new",`) + `,` + action(""),
"single, omitted": action(""),
} {
resp := request(true, "PUT", path, `{"name":"t-rwapi-ambiguous","phase":"request","matchers":{"host":"api.example"},"actions":[`+actions+`]}`)
if resp.Code != 400 || !strings.Contains(resp.Body.String(), "send the value") || strings.Contains(resp.Body.String(), "rwapi-plain") {
t.Fatalf("%s: HTTP %d %s, want 400", name, resp.Code, resp.Body.String())
}
}
stored, err := s.m.pg.GetTrafficRewriteRule(r.ID)
if err != nil {
t.Fatal(err)
}
if a := string(stored.Actions); !strings.Contains(a, "rwapi-plain-a") || !strings.Contains(a, "rwapi-plain-b") || strings.Contains(a, "rwapi-new") {
t.Fatalf("a refused update changed the stored actions: %s", a)
}
}
// The validation refuses a rule with HTTP 400 on create and on update, and
// stores nothing.
func TestRewriteRulesValidation400(t *testing.T) {