diff --git a/internal/infrastructure/postgres/resources.go b/internal/infrastructure/postgres/resources.go index ee15f4504..2f6f4ad3f 100644 --- a/internal/infrastructure/postgres/resources.go +++ b/internal/infrastructure/postgres/resources.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math" + "strings" "time" "github.com/jackc/pgx/v5" @@ -25,6 +26,9 @@ func NewResources(pool *pgxpool.Pool) *Resources { } func (s *Resources) Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) { + if err := checkResourceKey(orgID, kind); err != nil { + return storecontract.Resource{}, err + } resource, err := postgresdb.New(s.pool).ResourceByKind(ctx, postgresdb.ResourceByKindParams{OrgID: orgID, Kind: kind}) if errors.Is(err, pgx.ErrNoRows) { return storecontract.Resource{}, storecontract.ErrResourceNotFound @@ -36,6 +40,9 @@ func (s *Resources) Resource(ctx context.Context, orgID, kind string) (storecont } func (s *Resources) ResourceHistory(ctx context.Context, orgID, kind string, limit int) ([]storecontract.Resource, error) { + if err := checkResourceKey(orgID, kind); err != nil { + return nil, err + } if limit <= 0 { limit = 0 } @@ -55,6 +62,17 @@ func (s *Resources) ResourceHistory(ctx context.Context, orgID, kind string, lim return resources, nil } +// checkResourceKey refuses a key the audit record key could not tell apart from +// another: an empty org or kind, or either containing the "/" that joins them +// in storecontract.ResourceAuditKey. A forgotten org fails here rather than +// reading as an absent resource. +func checkResourceKey(orgID, kind string) error { + if orgID == "" || kind == "" || strings.Contains(orgID, "/") || strings.Contains(kind, "/") { + return fmt.Errorf("invalid resource key: org %q, kind %q", orgID, kind) + } + return nil +} + // Audit returns the field-level audit of the named records of one table, // newest first. A limit of zero or less returns every entry. func (s *Resources) Audit(ctx context.Context, table string, keys []string, limit int) ([]storecontract.AuditEntry, error) { @@ -90,8 +108,8 @@ func (s *Resources) Audit(ctx context.Context, table string, keys []string, limi // caller cannot tell the difference, and a kind can stay gone while every // process believes it holds a value. func (s *Resources) PutResource(ctx context.Context, write storecontract.ResourceWrite) (_ storecontract.Resource, retErr error) { - if write.OrgID == "" { - return storecontract.Resource{}, errors.New("resource write names no org") + if err := checkResourceKey(write.OrgID, write.Kind); err != nil { + return storecontract.Resource{}, err } tx, err := s.pool.Begin(ctx) if err != nil { diff --git a/internal/infrastructure/postgres/statestore_test.go b/internal/infrastructure/postgres/statestore_test.go index 77c1c86d7..16fa0fd9e 100644 --- a/internal/infrastructure/postgres/statestore_test.go +++ b/internal/infrastructure/postgres/statestore_test.go @@ -523,10 +523,18 @@ func TestResourcesAreIndependentPerOrg(t *testing.T) { } } -func TestPutResourceRefusesNoOrg(t *testing.T) { - if _, err := resourcesFor(t).PutResource(t.Context(), storecontract.ResourceWrite{ - Kind: "settings", Value: []byte(`{}`), Actor: "operator", Source: "test", RequestID: "r", - }); err == nil { - t.Fatal("a write naming no org was accepted") +// A key the audit record key could not tell apart from another, or one naming +// no org, is refused on both paths rather than reading as an absent resource. +func TestResourceKeysAreValidated(t *testing.T) { + resources := resourcesFor(t) + for _, key := range [][2]string{{"", "settings"}, {"acme", ""}, {"a/b", "settings"}, {"a", "b/settings"}} { + if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{ + OrgID: key[0], Kind: key[1], Value: []byte(`{}`), Actor: "operator", Source: "test", RequestID: "r", + }); err == nil { + t.Errorf("write %q/%q accepted", key[0], key[1]) + } + if _, err := resources.Resource(t.Context(), key[0], key[1]); errors.Is(err, storecontract.ErrResourceNotFound) || err == nil { + t.Errorf("read %q/%q = %v, want a key error, not not-found", key[0], key[1], err) + } } }