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`,
)
}
}