Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: canonicalize sheet hashes to lowercase at every boundary

Dannyc8e4841c950b
diff --git a/backend/common/resource_name.go b/backend/common/resource_name.go
index b6d611e2a3..69f149a3b4 100644
--- a/backend/common/resource_name.go
+++ b/backend/common/resource_name.go
@@ -521,7 +521,9 @@ func GetProjectResourceIDSheetSha256(name string) (string, string, error) {
if err != nil {
return "", "", err
}
- return tokens[0], tokens[1], nil
+ // The hash segment is hex: canonicalize to lowercase so it matches the
+ // form the store keys by and the payloads persist.
+ return tokens[0], strings.ToLower(tokens[1]), nil
}
// GetProjectIDWorksheetID returns the project ID and worksheet ID (resource_id) from a resource name.
diff --git a/backend/migrator/migration/3.22/0005##scope_sheet_blob.sql b/backend/migrator/migration/3.22/0005##scope_sheet_blob.sql
index 2e5a547444..49ffc6dba0 100644
--- a/backend/migrator/migration/3.22/0005##scope_sheet_blob.sql
+++ b/backend/migrator/migration/3.22/0005##scope_sheet_blob.sql
@@ -91,10 +91,13 @@ ON CONFLICT DO NOTHING;
-- hundreds of thousands of rows (measured: minutes-to-hours as CTEs, seconds
-- as temp tables). ON COMMIT DROP keeps them inside the migration
-- transaction, so atomicity and retry-on-failure are unchanged.
+-- Hex hashes are compared as text below, and legacy payloads may carry
+-- uppercase hex (old callers could name sheets in either case), so every
+-- extracted hash is lowered to the canonical encode() form first.
CREATE TEMP TABLE _sheet_scope_rev ON COMMIT DROP AS
SELECT r.resource_id,
r.created_at,
- r.payload->>'sheetSha256' AS sha,
+ lower(r.payload->>'sheetSha256') AS sha,
(regexp_match(r.payload->>'release', 'projects/([^/]+)/'))[1] AS rel_project,
(regexp_match(r.payload->>'release', 'releases/([^/]+)'))[1] AS rel_id,
-- The digit bound keeps the bigint casts total: a malformed name
@@ -117,7 +120,7 @@ ANALYZE _sheet_scope_rev;
-- single-row identity, release no newer than the revision, file carrying
-- the revision's hash.
CREATE TEMP TABLE _sheet_scope_srf ON COMMIT DROP AS
-SELECT rel.project, rel.release_id, rel.created_at, f->>'sheetSha256' AS sha
+SELECT rel.project, rel.release_id, rel.created_at, lower(f->>'sheetSha256') AS sha
FROM release rel
JOIN (
SELECT project, release_id
@@ -166,7 +169,7 @@ LEFT JOIN _sheet_scope_srf srf
AND srf.sha = rev.sha
WHERE rev.tr_project IS NOT NULL AND rev.tr_id IS NOT NULL AND rev.task_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM _sheet_scope_corroborated c WHERE c.resource_id = rev.resource_id)
- AND (t.payload->>'sheetSha256' = rev.sha OR srf.project IS NOT NULL);
+ AND (lower(t.payload->>'sheetSha256') = rev.sha OR srf.project IS NOT NULL);
ANALYZE _sheet_scope_corroborated;
@@ -183,7 +186,7 @@ WHERE r.resource_id = c.resource_id;
-- foreign key (corroborated stamps always pass it - they came from FK-backed
-- release/task_run rows).
WITH stamped AS MATERIALIZED (
- SELECT r.payload->>'project' AS project, r.payload->>'sheetSha256' AS sha
+ SELECT r.payload->>'project' AS project, lower(r.payload->>'sheetSha256') AS sha
FROM revision r
WHERE r.payload->>'project' IS NOT NULL
AND r.payload->>'sheetSha256' ~ '^[0-9a-fA-F]{64}$'
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index a26dd38998..953bb5161f 100644
--- a/backend/migrator/migrator_test.go
+++ b/backend/migrator/migrator_test.go
@@ -854,7 +854,7 @@ func TestMigration3_22_5_ScopeSheetBlob(t *testing.T) {
CREATE TABLE revision (resource_id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text, instance TEXT NOT NULL, db_name TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), payload JSONB NOT NULL DEFAULT '{}');
INSERT INTO project VALUES
('p1'), ('p2'), ('p3'), ('p4'),
- ('rel-ok'), ('tr-ok'), ('rel-old'), ('tr-late'), ('rel-mismatch'), ('tr-mismatch'),
+ ('rel-ok'), ('rel-upper'), ('tr-ok'), ('rel-old'), ('tr-late'), ('rel-mismatch'), ('tr-mismatch'),
('rel-dup'), ('tr-dup'), ('rel-task'), ('rel-taskid'), ('tr-taskid'), ('tr-direct'),
('rel-prefer'), ('rel-nest-prefer'), ('tr-prefer'),
('rel-newer'), ('rel-nest-fb'), ('tr-fb');
@@ -868,7 +868,7 @@ func TestMigration3_22_5_ScopeSheetBlob(t *testing.T) {
blobs := map[string]string{}
for _, name := range []string{
"plan", "task", "release", "pcr", "shared", "orphan",
- "rev-rel", "rev-nest", "rev-late", "rev-mismatch", "rev-other",
+ "rev-rel", "rev-upper", "rev-nest", "rev-late", "rev-mismatch", "rev-other",
"rev-dup", "rev-taskid", "rev-direct", "rev-prefer", "rev-fb", "rev-ghost", "rev-bare",
"rev-overflow", "rev-stale-stamp", "rev-empty-stamp",
} {
@@ -936,6 +936,14 @@ func TestMigration3_22_5_ScopeSheetBlob(t *testing.T) {
{"INSERT INTO revision (instance, db_name, created_at, payload) VALUES ('i', 'd', $1, $2::jsonb)",
[]any{rev, fmt.Sprintf(`{"sheetSha256":%q,"release":"projects/rel-ok/releases/r"}`, blobs["rev-rel"])}},
+ // Legacy uppercase hex: the revision stores the hash in uppercase
+ // while the release file carries lowercase. Text comparisons are
+ // lowered, so this corroborates → (rel-upper, rev-upper).
+ {"INSERT INTO release (project, release_id, created_at, payload) VALUES ('rel-upper', 'r', $1, $2::jsonb)",
+ []any{older, fmt.Sprintf(`{"files":[{"sheetSha256":%q}]}`, blobs["rev-upper"])}},
+ {"INSERT INTO revision (instance, db_name, created_at, payload) VALUES ('i', 'd', $1, $2::jsonb)",
+ []any{rev, fmt.Sprintf(`{"sheetSha256":%q,"release":"projects/rel-upper/releases/r"}`, strings.ToUpper(blobs["rev-upper"]))}},
+
// Observable probe, all constraints satisfied: task-run provenance in
// tr-ok whose task routes the hash through a release in rel-task.
// Expect the additive (tr-ok, rev-nest) alongside the source-derived
@@ -1083,6 +1091,7 @@ func TestMigration3_22_5_ScopeSheetBlob(t *testing.T) {
"p4|" + blobs["pcr"],
// Release rows seeded as corroboration targets are sources themselves.
"rel-ok|" + blobs["rev-rel"],
+ "rel-upper|" + blobs["rev-upper"],
"rel-task|" + blobs["rev-nest"],
"rel-old|" + blobs["rev-late"],
"rel-mismatch|" + blobs["rev-other"],
@@ -1109,6 +1118,7 @@ func TestMigration3_22_5_ScopeSheetBlob(t *testing.T) {
// directly: both branches corroborate and the release's project wins.
for sha, want := range map[string]string{
blobs["rev-rel"]: "rel-ok",
+ blobs["rev-upper"]: "rel-upper",
blobs["rev-nest"]: "tr-ok",
blobs["rev-late"]: "",
blobs["rev-mismatch"]: "",
@@ -1125,7 +1135,7 @@ func TestMigration3_22_5_ScopeSheetBlob(t *testing.T) {
} {
var got string
require.NoError(t, db.QueryRowContext(ctx,
- "SELECT COALESCE(payload->>'project', '') FROM revision WHERE payload->>'sheetSha256' = $1", sha).Scan(&got))
+ "SELECT COALESCE(payload->>'project', '') FROM revision WHERE lower(payload->>'sheetSha256') = $1", sha).Scan(&got))
require.Equal(t, want, got, "stamped project for hash %s", sha)
}
diff --git a/backend/store/sheet.go b/backend/store/sheet.go
index fa3d5a616e..4c8ea4a15e 100644
--- a/backend/store/sheet.go
+++ b/backend/store/sheet.go
@@ -7,6 +7,7 @@ import (
"fmt"
"maps"
"slices"
+ "strings"
"github.com/pkg/errors"
@@ -24,10 +25,12 @@ type SheetMessage struct {
Size int64
}
-// validSheetSha256Hexes filters to well-formed 32-byte hex hashes. Stored
-// payloads are not trusted to carry valid hex; a malformed value can never
-// match a blob, so callers treat it as absent rather than letting decode()
-// turn it into a SQL error.
+// validSheetSha256Hexes filters to well-formed 32-byte hex hashes and
+// canonicalizes them to lowercase — the form encode(sha256, 'hex') returns —
+// so result maps and the content cache key consistently no matter the case a
+// caller or a legacy stored payload used. Stored payloads are not trusted to
+// carry valid hex; a malformed value can never match a blob, so callers treat
+// it as absent rather than letting decode() turn it into a SQL error.
func validSheetSha256Hexes(sha256Hexes []string) []string {
valid := make([]string, 0, len(sha256Hexes))
for _, sha256Hex := range sha256Hexes {
@@ -37,7 +40,7 @@ func validSheetSha256Hexes(sha256Hexes []string) []string {
if _, err := hex.DecodeString(sha256Hex); err != nil {
continue
}
- valid = append(valid, sha256Hex)
+ valid = append(valid, strings.ToLower(sha256Hex))
}
return valid
}
@@ -51,7 +54,7 @@ func (s *Store) GetSheetFull(ctx context.Context, sha256Hex string) (*SheetMessa
if err != nil {
return nil, err
}
- return sheets[sha256Hex], nil
+ return sheets[strings.ToLower(sha256Hex)], nil
}
// GetSheetsForProject is the sheet gate: a project may access a sheet only
@@ -89,7 +92,7 @@ func (s *Store) GetSheetForProject(ctx context.Context, projectID, sha256Hex str
if err != nil {
return nil, err
}
- return sheets[sha256Hex], nil
+ return sheets[strings.ToLower(sha256Hex)], nil
}
// MissingSheetsForProject returns the hashes, in input order, for which the
@@ -104,9 +107,12 @@ func (s *Store) MissingSheetsForProject(ctx context.Context, projectID string, s
if err != nil {
return nil, err
}
+ // Look up by the canonical lowercase form, but do NOT pre-filter the
+ // inputs through validSheetSha256Hexes here: a malformed hash must be
+ // reported missing, not silently dropped from the result.
var missing []string
for _, sha256Hex := range sha256Hexes {
- if !allowed[sha256Hex] {
+ if !allowed[strings.ToLower(sha256Hex)] {
missing = append(missing, sha256Hex)
}
}
@@ -119,7 +125,9 @@ func (s *Store) MissingSheetsForProject(ctx context.Context, projectID string, s
// keyed by hex hash alone; scoping runs before content retrieval (a cache hit
// issues no query).
func (s *Store) getSheetsFull(ctx context.Context, sha256Hexes ...string) (map[string]*SheetMessage, error) {
- sha256Hexes = common.Uniq(sha256Hexes)
+ // Canonicalize before the cache loop so cache keys, result keys, and the
+ // keys encode() produces all agree, and mixed-case duplicates collapse.
+ sha256Hexes = common.Uniq(validSheetSha256Hexes(sha256Hexes))
result := make(map[string]*SheetMessage, len(sha256Hexes))
misses := sha256Hexes
if s.enableCache {
diff --git a/backend/tests/sheet_scope_test.go b/backend/tests/sheet_scope_test.go
index 987e402172..7818a73ba1 100644
--- a/backend/tests/sheet_scope_test.go
+++ b/backend/tests/sheet_scope_test.go
@@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
+ "strings"
"testing"
"time"
@@ -57,6 +58,17 @@ func TestSheetProjectScope(t *testing.T) {
a.Equal(content, resp.Msg.Content)
}
+ // Uppercase hex in the name resolves to the same sheet: hashes are
+ // canonicalized to lowercase at the parse and store boundaries, so case
+ // must affect neither the ref check nor the content-cache key.
+ upperName := fmt.Sprintf("%s/sheets/%s", projectA.Name, strings.ToUpper(sheetSha256))
+ upperResp, err := ctl.sheetServiceClient.GetSheet(ctx, connect.NewRequest(&v1pb.GetSheetRequest{
+ Name: upperName,
+ Raw: true,
+ }))
+ a.NoError(err, "GetSheet with uppercase hex under the owning project")
+ a.Equal(content, upperResp.Msg.Content)
+
// The same hash under project B is NotFound. The raw variant is the
// cache-ordering case: the content cache was warmed above, so this fails
// if enforcement ever runs behind the cache read instead of before it.