fix(supabase): confirmed uploads gate asset reads (hash claim), seeds-listed test, Dockerfile workspace manifests; importer declares every authored label, real in=out control

This commit is contained in:
bchanot committed 2026-10-09 00:50:36 +02:00
1 parent bbfb8d6203
commit dee4ce43d9
28 files changed
+511 -87

No files matched your search

@@ -5,13 +5,15 @@
* Operator tool: needs the service key, which never ships to the browser.
* Reads SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY from the environment
* (local stack: `pnpm supabase status` prints them). Idempotent: an object
* whose sha256 already exists is skipped, a mismatching upload is refused.
* whose sha256 already exists is kept, a local file whose bytes differ from
* the manifest is refused, and each asset row is confirmed server-side
* (confirm_sheet_asset_upload) so the read policy can trust it.
*
* pnpm assets:upload supabase/seed/anatomical-sheets/equine/v1
*/
import { createHash } from 'node:crypto'
import { readFile } from 'node:fs/promises'
import { join, resolve } from 'node:path'
import { basename, join, resolve } from 'node:path'
import { createClient } from '@supabase/supabase-js'
const BUCKET = 'anatomical-sheet-assets'
@@ -20,6 +22,21 @@ interface AssetEntry {
file: string
sha256: string
byte_size: number
svg_asset_id: string
}
const SHA256_PATTERN = /^[0-9a-f]{64}$/
const UUID_PATTERN = /^[0-9a-f-]{36}$/
/** The manifest is committed data, still: no path separators, hex hash, uuid. */
function assertEntry(entry: AssetEntry): void {
if (basename(entry.file) !== entry.file || entry.file.startsWith('.')) {
throw new Error(`${entry.file}: file must be a plain file name`)
}
if (!SHA256_PATTERN.test(entry.sha256))
throw new Error(`${entry.file}: bad sha256`)
if (!UUID_PATTERN.test(entry.svg_asset_id))
throw new Error(`${entry.file}: bad asset id`)
}
function requireEnv(name: string): string {
@@ -30,7 +47,9 @@ function requireEnv(name: string): string {
async function readManifest(dir: string): Promise<AssetEntry[]> {
const raw = await readFile(join(dir, 'assets.json'), 'utf8')
return JSON.parse(raw) as AssetEntry[]
const entries = JSON.parse(raw) as AssetEntry[]
entries.forEach(assertEntry)
return entries
}
/** The bytes on disk must still match what the seed recorded. */
@@ -63,7 +82,14 @@ async function main(): Promise<void> {
if (error && !/already exists/i.test(error.message)) {
throw new Error(`${key}: ${error.message}`)
}
console.log(`${error ? 'kept' : 'uploaded'} ${key} (${entry.file})`)
// The asset row becomes readable only once the server records the upload.
const confirmed = await client.rpc('confirm_sheet_asset_upload', {
asset_id: entry.svg_asset_id,
})
if (confirmed.error) throw new Error(`${key}: ${confirmed.error.message}`)
console.log(
`${error ? 'kept' : 'uploaded'} ${key} (${entry.file}), confirmed`,
)
}
}
+7
View File
@@ -19,6 +19,7 @@ export type Database = {
id: string
sha256: string
sheet_id: string
uploaded_at: string | null
}
ComputedFields: never
Insert: {
@@ -30,6 +31,7 @@ export type Database = {
id?: string
sha256: string
sheet_id: string
uploaded_at?: string | null
}
Update: {
byte_size?: number
@@ -40,6 +42,7 @@ export type Database = {
id?: string
sha256?: string
sheet_id?: string
uploaded_at?: string | null
}
Relationships: [
{
@@ -369,6 +372,10 @@ export type Database = {
}
Functions: {
archive_anatomical_sheet: { Args: { sheet_id: string }; Returns: string }
confirm_sheet_asset_upload: {
Args: { asset_id: string }
Returns: string
}
create_anatomical_sheet: {
Args: {
description?: string
+1 -1
View File
@@ -17,7 +17,7 @@ export const repoRoot = resolve(here, '../../../..')
export const supabaseDir = join(repoRoot, 'supabase')
export const migrationsDir = join(supabaseDir, 'migrations')
export const pgTapTestsDir = join(supabaseDir, 'tests')
/** Versioned seeds (reference data such as the official sheets); seed.sql itself needs the real auth schema and stays out. */
/** Versioned seeds (reference data such as the official sheets); seed.sql itself needs the real auth schema and stays out. The CLI reads literal paths from config.toml: the seeds test checks every file here is listed. */
export const seedDir = join(supabaseDir, 'seed')
async function listSqlFiles(dir: string): Promise<string[]> {
@@ -29,6 +29,7 @@ const expectedColumns = {
'id',
'sha256',
'sheet_id',
'uploaded_at',
],
anatomical_sheet_versions: [
'change_note',
@@ -106,6 +107,7 @@ type FunctionArgs = {
}
const expectedFunctionArgs = {
archive_anatomical_sheet: ['sheet_id'],
confirm_sheet_asset_upload: ['asset_id'],
create_anatomical_sheet: [
'description',
'document',
+20
View File
@@ -0,0 +1,20 @@
import { readFile } from 'node:fs/promises'
import { join, relative } from 'node:path'
import { listSeeds, seedDir, supabaseDir } from './harness'
/**
* The CLI applies only the literal files of config.toml sql_paths, while the
* harness applies every seed on disk: a seed forgotten in the list would pass
* here and never reach `supabase db reset`.
*/
describe('versioned seeds', () => {
it('are all listed in config.toml [db.seed] sql_paths', async () => {
const config = await readFile(join(supabaseDir, 'config.toml'), 'utf8')
const line = config.match(/^sql_paths\s*=\s*\[(.*)\]/m)?.[1] ?? ''
const listed = [...line.matchAll(/"([^"]+)"/g)].map((m) => m[1])
for (const file of await listSeeds()) {
const path = './' + relative(supabaseDir, join(seedDir, file))
expect(listed, `${path} missing from sql_paths`).toContain(path)
}
})
})