mesh/delivery merged: source.main moved to novox/mesh-catalog@3ded9e780043
mesh/check pass e7aaa8420eac on 3ded9e780043 | unit 2018 run, 2325 kept, 5 skipped | integration 2040 run, 2344 kept, 9 skipped | over bound: unit, in…
The runtime refuses a state field whose name ends in secret, so no bus-account hash could be written on novox. The test state now applies the same check. Issue: 1945
68 lines
2.1 KiB
Go
68 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// credentialEndings and credentialField mirror the runtime's refusal of a state or log value with a field whose name says
|
|
// it is a credential (mesh-tools node-tools/internal/bus/state.go, credentialField, ADR 0201): the runtime's check is
|
|
// unexported and in another repository, so this is a copy and must follow it. A fake state that skips it let a
|
|
// field named "secret" pass every test and be refused on novox (issue 1945).
|
|
var credentialEndings = []string{"password", "passwd", "secret", "token", "credential", "credentials",
|
|
"authorization", "apikey", "privatekey", "accesskey", "cookie"}
|
|
|
|
func credentialField(value []byte) string {
|
|
var v any
|
|
if json.Unmarshal(value, &v) != nil {
|
|
return ""
|
|
}
|
|
var walk func(any) string
|
|
walk = func(v any) string {
|
|
switch t := v.(type) {
|
|
case map[string]any:
|
|
for k, x := range t {
|
|
norm := strings.NewReplacer("-", "", "_", "", " ", "").Replace(strings.ToLower(k))
|
|
for _, end := range credentialEndings {
|
|
if strings.HasSuffix(norm, end) {
|
|
return k
|
|
}
|
|
}
|
|
if f := walk(x); f != "" {
|
|
return f
|
|
}
|
|
}
|
|
case []any:
|
|
for _, x := range t {
|
|
if f := walk(x); f != "" {
|
|
return f
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
return walk(v)
|
|
}
|
|
|
|
// The vault's bus-accounts records pass the runtime's credential-field check; a record with a "secret" field does not.
|
|
func TestStateRecordsPassTheRuntimesCredentialFieldCheck(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
value any
|
|
field string
|
|
}{
|
|
{"a hash record of the vault's own secret", AccountHash{User: "ace.mesh-build-go", Hash: "$2a$10$h", Of: "own/ace/mesh-build-go/broker", Generation: 1, Fingerprint: "ab12"}, ""},
|
|
{"an adopted hash record", AccountHash{User: "ace.x", Hash: "$2a$10$h", Adopted: true}, ""},
|
|
{"a record with a secret field is refused", map[string]any{"user": "u", "secret": "own/x"}, "secret"},
|
|
} {
|
|
raw, err := json.Marshal(c.value)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := credentialField(raw); got != c.field {
|
|
t.Errorf("%s: credential field %q, want %q (%s)", c.name, got, c.field, raw)
|
|
}
|
|
}
|
|
}
|