ReviewOS

also looking at this

stacks/drone

feat: own runtime firmware candidates

#49
Merged chris wants to merge codex/runtime-candidate-ownership into main
24 files +1245 -92
resources/functions/drone/updates/candidate-files.tsadded+36-0
Changes to resources/functions/drone/updates/candidate-files.ts
@@ -0,0 +1,36 @@
1import { mkdirSync, realpathSync } from 'node:fs'
2import { basename, dirname, join, relative, resolve, sep } from 'node:path'
3import { Readable } from 'node:stream'
4import type { TargetFile } from 'tuf-js'
5import { directory, imageLimit, regularFile, safePath, syncDirectory, syncTree, writeExclusive } from './files'
6
7/** Internal candidate file-format helpers. They do not grant firmware selection authority. */
8export function runtimeCandidateOutput(output: string, excluded: readonly string[]) {
9 const requested = resolve(output), parent = realpathSync(dirname(requested))
10 directory(parent)
11 const destination = join(parent, basename(requested))
12 for (const source of excluded) {
13 const root = realpathSync(source)
14 const child = relative(root, destination), ancestor = relative(destination, root)
15 if (!child || (!child.startsWith(`..${sep}`) && child !== '..') || (!ancestor.startsWith(`..${sep}`) && ancestor !== '..')) throw new Error('Candidate output must be separate from repository and protected trust state')
16 }
17 return destination
18}
19
20/** Caller owns SDK verification. Recheck actual image bytes before writing the completion marker. */
21export async function finishRuntimeCandidate(output: string, info: TargetFile, receipt: unknown, files: ReadonlyMap<string, Buffer>) {
22 await info.verify(Readable.from(regularFile(join(output, 'image.bin'), imageLimit)))
23 let total = 0
24 for (const [path, bytes] of files) {
25 safePath(path)
26 if (!path.startsWith('metadata/') && !path.startsWith('targets/')) throw new Error('Invalid candidate repository entry')
27 total += bytes.length
28 if (total > 32 * 1024 * 1024 || files.size > 64) throw new Error('Candidate archive budget exceeded')
29 const file = join(output, 'repository', path)
30 mkdirSync(dirname(file), { recursive: true, mode: 0o700 })
31 writeExclusive(file, bytes)
32 }
33 syncTree(output)
34 writeExclusive(join(output, 'stage.json'), Buffer.from(`${JSON.stringify(receipt, null, 2)}\n`))
35 syncDirectory(output); syncDirectory(dirname(output))
36}