Repository navigation
security: persist signed authority receipts and restore STEP_UP after restart #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
54f26d9
security: add verified HMAC-chained append-only receipts that survive…
blocksifrdev 9becf3b
security: persist signed authority receipts before returning permits …
blocksifrdev 986319c
test: recover durable signed permits after restart and reject tamperi…
blocksifrdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import fs from "node:fs" | ||
| import path from "node:path" | ||
| import crypto from "node:crypto" | ||
| import type { ExecutionReceipt } from "./schemas" | ||
| import { chainHash, signReceipt } from "./receiptSigner" | ||
|
|
||
| function secureMode(stat: fs.Stats): boolean { | ||
| return process.platform === "win32" || | ||
| ((stat.mode & 0o077) === 0 && | ||
| (typeof process.getuid !== "function" || stat.uid === process.getuid())) | ||
| } | ||
| function trustedDirectory(dir: string): void { | ||
| fs.mkdirSync(dir, { recursive: true, mode: 0o700 }) | ||
| const st = fs.lstatSync(dir) | ||
| if (!st.isDirectory() || st.isSymbolicLink() || !secureMode(st)) { | ||
| throw new Error("UNTRUSTED_RECEIPT_LEDGER_DIRECTORY") | ||
| } | ||
| } | ||
| function verifyRecord(record: ExecutionReceipt, previous: string | null): void { | ||
| if (!record || typeof record !== "object" || typeof record.receiptId !== "string" || | ||
| !/^er-[0-9a-f-]{36}$/i.test(record.receiptId) || | ||
| typeof record.signature !== "string" || typeof record.chainHash !== "string") { | ||
| throw new Error("INVALID_RECEIPT_LEDGER_RECORD") | ||
| } | ||
| const { signature, chainHash: claimedHash, ...partial } = record | ||
| const hash = chainHash(previous, partial) | ||
| if (hash !== claimedHash) throw new Error("RECEIPT_LEDGER_CHAIN_BROKEN") | ||
| const expected = Buffer.from(signReceipt({ ...partial, chainHash: claimedHash })) | ||
| const supplied = Buffer.from(signature) | ||
| if (supplied.length !== expected.length || | ||
| !crypto.timingSafeEqual(supplied, expected)) { | ||
| throw new Error("RECEIPT_LEDGER_SIGNATURE_INVALID") | ||
| } | ||
| } | ||
|
|
||
| export interface ReceiptLedger { | ||
| get(id: string): ExecutionReceipt | undefined | ||
| append(receipt: ExecutionReceipt): void | ||
| lastHash(): string | null | ||
| count(): number | ||
| close(): void | ||
| } | ||
|
|
||
| // Exact single-process, host-local, signed append-only receipt ledger. | ||
| // A live exclusive lock prevents independent processes from forking the chain. | ||
| // Unclean termination intentionally requires operator-reviewed lock recovery. | ||
| export function openReceiptLedger(file = process.env.RUNTIME_AUTH_RECEIPT_LEDGER_PATH): ReceiptLedger { | ||
| if (!file || !path.isAbsolute(file)) throw new Error("PERSISTENT_RECEIPT_LEDGER_UNCONFIGURED") | ||
| trustedDirectory(path.dirname(file)) | ||
| const lockFile = file + ".lock" | ||
| let lockFd: number | undefined | ||
| let fd: number | undefined | ||
| let released = false | ||
| const previousRecords = new Map<string, ExecutionReceipt>() | ||
| let last: string | null = null | ||
| let bytes = 0 | ||
| try { | ||
| lockFd = fs.openSync(lockFile, fs.constants.O_WRONLY | fs.constants.O_CREAT | | ||
| fs.constants.O_EXCL | (fs.constants.O_NOFOLLOW ?? 0), 0o600) | ||
| fs.writeSync(lockFd, String(process.pid) + "\n") | ||
| fs.fsyncSync(lockFd) | ||
| fd = fs.openSync(file, fs.constants.O_RDWR | fs.constants.O_APPEND | | ||
| fs.constants.O_CREAT | (fs.constants.O_NOFOLLOW ?? 0), 0o600) | ||
| const fileStat = fs.fstatSync(fd) | ||
| if (!fileStat.isFile() || !secureMode(fileStat)) throw new Error("UNTRUSTED_RECEIPT_LEDGER_FILE") | ||
| const raw = fs.readFileSync(fd, "utf8") | ||
| const size = Buffer.byteLength(raw) | ||
| if (size !== fileStat.size || (size > 0 && !raw.endsWith("\n"))) { | ||
| throw new Error("TRUNCATED_OR_CHANGED_RECEIPT_LEDGER") | ||
| } | ||
| for (const line of raw.split("\n").filter(Boolean)) { | ||
| let receipt: ExecutionReceipt | ||
| try { receipt = JSON.parse(line) as ExecutionReceipt } | ||
| catch { throw new Error("MALFORMED_RECEIPT_LEDGER") } | ||
| if (previousRecords.has(receipt.receiptId)) throw new Error("DUPLICATE_RECEIPT_ID") | ||
| verifyRecord(receipt, last) | ||
| previousRecords.set(receipt.receiptId, receipt) | ||
| last = receipt.chainHash | ||
| } | ||
| bytes = size | ||
| } catch (error) { | ||
| if (fd !== undefined) fs.closeSync(fd) | ||
| if (lockFd !== undefined) { | ||
| fs.closeSync(lockFd) | ||
| fs.unlinkSync(lockFile) | ||
| } | ||
| throw error | ||
| } | ||
|
|
||
| const fileFd = fd! | ||
| const ownedLockFd = lockFd! | ||
| function close(): void { | ||
| if (released) return | ||
| released = true | ||
| fs.closeSync(fileFd) | ||
| fs.closeSync(ownedLockFd) | ||
| fs.unlinkSync(lockFile) | ||
| } | ||
| return { | ||
| get: id => previousRecords.get(id), | ||
| lastHash: () => last, | ||
| count: () => previousRecords.size, | ||
| append(receipt: ExecutionReceipt): void { | ||
| if (released) throw new Error("RECEIPT_LEDGER_CLOSED") | ||
| if (previousRecords.has(receipt.receiptId)) throw new Error("DUPLICATE_RECEIPT_ID") | ||
| const stat = fs.fstatSync(fileFd) | ||
| if (!stat.isFile() || !secureMode(stat) || stat.size !== bytes) { | ||
| throw new Error("RECEIPT_LEDGER_CHANGED_EXTERNALLY") | ||
| } | ||
| verifyRecord(receipt, last) | ||
| const buffer = Buffer.from(JSON.stringify(receipt) + "\n") | ||
| const written = fs.writeSync(fileFd, buffer) | ||
| if (written !== buffer.length) throw new Error("SHORT_RECEIPT_LEDGER_WRITE") | ||
| fs.fsyncSync(fileFd) | ||
| // A newly opened file's directory entry must be durable as well. | ||
| if (process.platform !== "win32") { | ||
| const parentFd = fs.openSync(path.dirname(file), fs.constants.O_RDONLY) | ||
| try { fs.fsyncSync(parentFd) } finally { fs.closeSync(parentFd) } | ||
| } | ||
| bytes += buffer.length | ||
| last = receipt.chainHash | ||
| previousRecords.set(receipt.receiptId, receipt) | ||
| }, | ||
| close | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the ledger loses one or more complete trailing records, startup accepts the shortened file because its remaining signatures and chain links still verify. I reproduced this by appending two signed receipts, closing the ledger, removing the second JSONL line, and reopening successfully with a count of one. Truncating to zero bytes is also accepted. This silently loses previously acknowledged decisions and prior STEP_UP receipts instead of failing closed. Persist and verify an independent committed head or record count so recovery can distinguish an intact ledger from a valid prefix.
Useful? React with 👍 / 👎.