test(messaging): widen the config-read gate past explicit config.Config types
The gate that keeps a PRD-forbidden [models]/[providers] read out of the
Messaging Service only blamed identifiers whose parameter or var type spelled
config.Config. A value reached through an inferred declaration
("cfg := doc.Config") or held in a struct field therefore left the read
intact while the rule saw no holder -- the same class of miss that let voice
transcription ship behind a green link gate.
Widen the holder detection to track the config type through inferred short
variable declarations and vars, struct fields and receivers, type-preserving
expressions, and range variables over a slice or map of config, then assert
each previously-inert shape red first.
This commit is contained in:
1 parent
04fa1b74fb
commit
dd939ed495
1 file changed
+253
-29
@@ -44,10 +44,13 @@ var bannedConfigFields = map[string]bool{
|
||||
}
|
||||
|
||||
// configReadOffenders returns the banned "<holder>.<Field>" config reads in
|
||||
// src, in source order. A holder is any identifier declared with type
|
||||
// config.Config (or *config.Config) in the same file, plus the .Config field
|
||||
// of a configuration.Document, because those are the shapes the Messaging
|
||||
// Service reads a decoded configuration through.
|
||||
// src, in source order. A holder is anything the file gives the
|
||||
// internal/config.Config type: an explicitly typed parameter, var, receiver or
|
||||
// struct field, a short variable declaration or inferred var whose right-hand
|
||||
// side resolves to one ("cfg := doc.Config"), or a range variable over a slice
|
||||
// of them. The type is tracked by name because a name is what a Go read
|
||||
// selects on. A config value returned by a call this file does not declare is
|
||||
// outside a syntactic rule and stays untracked.
|
||||
func configReadOffenders(src []byte) []string {
|
||||
fset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fset, "config.go", src, parser.SkipObjectResolution)
|
||||
@@ -57,7 +60,7 @@ func configReadOffenders(src []byte) []string {
|
||||
// when the tree is broken.
|
||||
return []string{"parse error: " + err.Error()}
|
||||
}
|
||||
holders := configHolders(file)
|
||||
kinds := configKinds(file)
|
||||
|
||||
var offenders []string
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
@@ -67,12 +70,16 @@ func configReadOffenders(src []byte) []string {
|
||||
}
|
||||
switch base := sel.X.(type) {
|
||||
case *ast.Ident:
|
||||
if holders[base.Name] {
|
||||
if kinds[base.Name] == configValue {
|
||||
offenders = append(offenders, base.Name+"."+sel.Sel.Name)
|
||||
}
|
||||
case *ast.SelectorExpr:
|
||||
if base.Sel.Name == "Config" {
|
||||
offenders = append(offenders, "Config."+sel.Sel.Name)
|
||||
// doc.Config, or the config-typed struct field h.cfg. Config is
|
||||
// named by configuration.Document in another package, so it
|
||||
// cannot come out of the kinds map; treat a selected Config as
|
||||
// a config holder unconditionally.
|
||||
if base.Sel.Name == "Config" || kinds[base.Sel.Name] == configValue {
|
||||
offenders = append(offenders, base.Sel.Name+"."+sel.Sel.Name)
|
||||
}
|
||||
}
|
||||
return true
|
||||
@@ -80,35 +87,159 @@ func configReadOffenders(src []byte) []string {
|
||||
return offenders
|
||||
}
|
||||
|
||||
// configHolders collects the identifiers in file declared with a
|
||||
// config.Config type, in a function parameter or a var.
|
||||
func configHolders(file *ast.File) map[string]bool {
|
||||
holders := map[string]bool{}
|
||||
// configKind is how an identifier or expression reaches a config.Config value.
|
||||
type configKind int
|
||||
|
||||
const (
|
||||
notConfig configKind = iota // not a config value
|
||||
configValue // config.Config
|
||||
configSlice // []config.Config, [N]config.Config or map[K]config.Config
|
||||
)
|
||||
|
||||
// configKinds resolves the identifiers in file that hold a config.Config, in
|
||||
// two passes because an inferred holder's source must be classified before the
|
||||
// holder's own name can be.
|
||||
func configKinds(file *ast.File) map[string]configKind {
|
||||
kinds := map[string]configKind{}
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
switch node := n.(type) {
|
||||
case *ast.FuncDecl:
|
||||
if node.Type.Params == nil {
|
||||
return true
|
||||
}
|
||||
for _, field := range node.Type.Params.List {
|
||||
if !isConfigType(field.Type) {
|
||||
continue
|
||||
}
|
||||
for _, name := range field.Names {
|
||||
holders[name.Name] = true
|
||||
}
|
||||
}
|
||||
classifyFields(node.Recv, kinds)
|
||||
classifyFields(node.Type.Params, kinds)
|
||||
case *ast.ValueSpec:
|
||||
if !isConfigType(node.Type) {
|
||||
return true
|
||||
}
|
||||
for _, name := range node.Names {
|
||||
holders[name.Name] = true
|
||||
classifyNames(node.Type, node.Names, kinds)
|
||||
case *ast.StructType:
|
||||
for _, field := range node.Fields.List {
|
||||
classifyNames(field.Type, field.Names, kinds)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return holders
|
||||
// Inferred holders: a single source-order pass suffices because Go
|
||||
// requires a name to be declared before it is referenced.
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
switch node := n.(type) {
|
||||
case *ast.AssignStmt:
|
||||
if node.Tok != token.DEFINE {
|
||||
return true
|
||||
}
|
||||
classifyValues(node.Lhs, node.Rhs, kinds)
|
||||
case *ast.ValueSpec:
|
||||
// An explicit type was handled above; here only "var cfg =
|
||||
// doc.Config", which infers the same way a := does.
|
||||
if node.Type != nil {
|
||||
return true
|
||||
}
|
||||
names := make([]ast.Expr, len(node.Names))
|
||||
for i, name := range node.Names {
|
||||
names[i] = name
|
||||
}
|
||||
classifyValues(names, node.Values, kinds)
|
||||
case *ast.RangeStmt:
|
||||
if exprKind(node.X, kinds) != configSlice {
|
||||
return true
|
||||
}
|
||||
if id, ok := node.Value.(*ast.Ident); ok {
|
||||
kinds[id.Name] = configValue
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return kinds
|
||||
}
|
||||
|
||||
// classifyValues records each lhs identifier that resolves to a config kind of
|
||||
// the rhs expression in the same position. A shorter lhs or rhs is fine;
|
||||
// position is what pairs a name with its value.
|
||||
func classifyValues(lhs, rhs []ast.Expr, kinds map[string]configKind) {
|
||||
for i, target := range lhs {
|
||||
if i >= len(rhs) {
|
||||
return
|
||||
}
|
||||
id, ok := target.(*ast.Ident)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if kind := exprKind(rhs[i], kinds); kind != notConfig {
|
||||
kinds[id.Name] = kind
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// classifyFields records every named field in fields whose type holds a
|
||||
// config.Config.
|
||||
func classifyFields(fields *ast.FieldList, kinds map[string]configKind) {
|
||||
if fields == nil {
|
||||
return
|
||||
}
|
||||
for _, field := range fields.List {
|
||||
classifyNames(field.Type, field.Names, kinds)
|
||||
}
|
||||
}
|
||||
|
||||
// classifyNames records names as holding the config kind of typ.
|
||||
func classifyNames(typ ast.Expr, names []*ast.Ident, kinds map[string]configKind) {
|
||||
kind := typeKind(typ)
|
||||
if kind == notConfig {
|
||||
return
|
||||
}
|
||||
for _, name := range names {
|
||||
kinds[name.Name] = kind
|
||||
}
|
||||
}
|
||||
|
||||
// exprKind resolves the config kind of an expression from the identifiers
|
||||
// already classified in kinds.
|
||||
func exprKind(expr ast.Expr, kinds map[string]configKind) configKind {
|
||||
switch e := expr.(type) {
|
||||
case *ast.Ident:
|
||||
return kinds[e.Name]
|
||||
case *ast.SelectorExpr:
|
||||
// doc.Config of a configuration.Document, or a struct field the file
|
||||
// declared as config.Config.
|
||||
if e.Sel.Name == "Config" {
|
||||
return configValue
|
||||
}
|
||||
return kinds[e.Sel.Name]
|
||||
case *ast.CompositeLit:
|
||||
return typeKind(e.Type)
|
||||
case *ast.StarExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.UnaryExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.ParenExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.IndexExpr:
|
||||
if exprKind(e.X, kinds) == configSlice {
|
||||
return configValue
|
||||
}
|
||||
}
|
||||
return notConfig
|
||||
}
|
||||
|
||||
// typeKind reports the config kind a type expression names: config.Config, or
|
||||
// a slice, array or map whose element is one.
|
||||
func typeKind(expr ast.Expr) configKind {
|
||||
switch t := expr.(type) {
|
||||
case *ast.ArrayType:
|
||||
if isConfigType(t.Elt) {
|
||||
return configSlice
|
||||
}
|
||||
case *ast.MapType:
|
||||
// A map's range value is its element; classify the map itself as a
|
||||
// slice-like source so a range over it yields config values.
|
||||
if isConfigType(t.Value) {
|
||||
return configSlice
|
||||
}
|
||||
case *ast.StarExpr:
|
||||
return typeKind(t.X)
|
||||
case *ast.ParenExpr:
|
||||
return typeKind(t.X)
|
||||
}
|
||||
if isConfigType(expr) {
|
||||
return configValue
|
||||
}
|
||||
return notConfig
|
||||
}
|
||||
|
||||
// isConfigType reports whether expr names internal/config's Config type,
|
||||
@@ -184,6 +315,99 @@ func setupTranscriber(cfg config.Config) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigReadRuleCatchesInferredHolders covers the holder shapes the first
|
||||
// cut of this rule missed. It only saw a config.Config written out as a
|
||||
// parameter or var type, so a value reached through an inferred short
|
||||
// variable declaration ("cfg := doc.Config") or held in a struct field
|
||||
// ("h.cfg.Models") left the forbidden [models]/[providers] read intact while
|
||||
// the rule saw no holder to blame. Each case here fails red against that rule:
|
||||
// the live gate would have reported a pass with the violation still shipping.
|
||||
func TestConfigReadRuleCatchesInferredHolders(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
src string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "short var decl from a Document's Config field",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
|
||||
func f(doc configuration.Document) {
|
||||
cfg := doc.Config
|
||||
_ = cfg.Models
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
{
|
||||
name: "inferred var without an explicit type",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
|
||||
func f(doc configuration.Document) {
|
||||
var cfg = doc.Config
|
||||
_ = cfg.Models
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
{
|
||||
name: "short var decl inherits config type from another holder",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
func f(cfg config.Config) {
|
||||
other := cfg
|
||||
_ = other.Providers
|
||||
}
|
||||
`,
|
||||
want: []string{"other.Providers"},
|
||||
},
|
||||
{
|
||||
name: "config held in a struct field",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
type holder struct {
|
||||
cfg config.Config
|
||||
}
|
||||
|
||||
func f(h holder) {
|
||||
_ = h.cfg.Providers
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Providers"},
|
||||
},
|
||||
{
|
||||
name: "range variable over a slice of config",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
func f(cfgs []config.Config) {
|
||||
for _, cfg := range cfgs {
|
||||
_ = cfg.Models
|
||||
}
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := configReadOffenders([]byte(tc.src))
|
||||
if strings.Join(got, ",") != strings.Join(tc.want, ",") {
|
||||
t.Fatalf("configReadOffenders() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigReadRuleAllowsThePermittedKeys proves the rule is not a blanket
|
||||
// ban on config access: the sections the PRD does permit keep passing, and a
|
||||
// nested chat field is not mistaken for a top-level one.
|
||||
|
||||
Reference in new issue
Block a user