fix(store): refuse resource keys the audit key cannot disambiguate
An empty org or kind, or either containing the '/' that joins them in the audit record key, fails on read and write instead of colliding or reading as an absent resource.
This commit is contained in:
1 parent
301ece6f55
commit
7fb91173da
2 files changed
+33
-7
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user