@runeditrun/rer 0.2.80 → 0.2.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/cli/deploy-needs.mjs +4 -2
- package/cli/export.mjs +1 -1
- package/cli/ledger-refusal.mjs +18 -0
- package/cli/portability-migrations.mjs +30 -11
- package/cli/portability-worker.mjs +3 -1
- package/cli/portability.mjs +2 -0
- package/cli/release.mjs +4 -4
- package/cli/site-hosting.mjs +90 -0
- package/cli/site-render.mjs +3 -2
- package/cli/site.mjs +5 -2
- package/edition-authoring/base-copies.mjs +12 -1
- package/edition-authoring/copy-checkout.mjs +152 -1
- package/edition-authoring/files.mjs +1 -1
- package/edition-authoring/runtime-dependencies.mjs +108 -0
- package/global/CHANGELOG.md +25 -0
- package/global/README.md +12 -0
- package/modules/demo/README.md +8 -0
- package/modules/demo/navigation.mjs +131 -15
- package/modules/dependencies/auth.mjs +2 -2
- package/modules/deploy-kit/owner.mjs +8 -1
- package/modules/import-kit/src/d1/apply-effects.ts +33 -0
- package/modules/import-kit/src/d1/apply.ts +38 -10
- package/modules/import-kit/src/door/ADOPTING.md +452 -0
- package/modules/import-kit/src/door/README.md +553 -0
- package/modules/import-kit/src/door/admission.ts +45 -0
- package/modules/import-kit/src/door/archive.ts +129 -0
- package/modules/import-kit/src/door/authority.ts +107 -0
- package/modules/import-kit/src/door/failures.ts +70 -0
- package/modules/import-kit/src/door/grants.ts +162 -0
- package/modules/import-kit/src/door/handler.ts +90 -0
- package/modules/import-kit/src/door/index.ts +20 -0
- package/modules/import-kit/src/door/intake/csv.ts +74 -0
- package/modules/import-kit/src/door/intake/json.ts +182 -0
- package/modules/import-kit/src/door/intake/projection.ts +61 -0
- package/modules/import-kit/src/door/intake/types.ts +28 -0
- package/modules/import-kit/src/door/intake-policy.ts +38 -0
- package/modules/import-kit/src/door/intake-upload.ts +91 -0
- package/modules/import-kit/src/door/inventory.ts +68 -0
- package/modules/import-kit/src/door/jobs.ts +134 -0
- package/modules/import-kit/src/door/limits.ts +16 -0
- package/modules/import-kit/src/door/operator.ts +41 -0
- package/modules/import-kit/src/door/planning.ts +227 -0
- package/modules/import-kit/src/door/protocol.ts +38 -0
- package/modules/import-kit/src/door/refusals.ts +51 -0
- package/modules/import-kit/src/door/schema.ts +101 -0
- package/modules/import-kit/src/door/store.ts +152 -0
- package/modules/import-kit/src/door/types.ts +100 -0
- package/modules/import-kit/src/door/uploads.ts +98 -0
- package/modules/import-kit/src/provenance/numeric-credentials.ts +40 -0
- package/modules/import-kit/src/readers/README.md +139 -0
- package/modules/import-kit/src/readers/api-policy.ts +67 -0
- package/modules/import-kit/src/readers/api.ts +86 -0
- package/modules/import-kit/src/readers/bytes.ts +73 -0
- package/modules/import-kit/src/readers/errors.ts +33 -0
- package/modules/import-kit/src/readers/file.ts +74 -0
- package/modules/import-kit/src/readers/index.ts +3 -0
- package/modules/import-kit/src/readers/json.ts +29 -0
- package/modules/import-kit/src/readers/numeric-lexemes.ts +104 -0
- package/modules/import-kit/src/readers/original.ts +31 -0
- package/modules/import-kit/src/readers/records.ts +105 -0
- package/modules/import-kit/src/types.ts +3 -1
- package/modules/operator-auth/README.md +92 -3
- package/modules/operator-auth/accounts/admin.mjs +52 -20
- package/modules/operator-auth/accounts/audit.mjs +24 -0
- package/modules/operator-auth/accounts/business-team.mjs +91 -0
- package/modules/operator-auth/accounts/domain-audit.mjs +24 -0
- package/modules/operator-auth/accounts/native.mjs +3 -1
- package/modules/operator-auth/accounts/settings-page.mjs +15 -2
- package/modules/operator-auth/accounts/status.mjs +7 -2
- package/modules/operator-auth/accounts/store.mjs +32 -6
- package/modules/operator-auth/business-settings.mjs +40 -0
- package/modules/operator-auth/identity/email-link/index.mjs +1 -1
- package/modules/operator-auth/identity/oidc/admin-audit.mjs +31 -0
- package/modules/operator-auth/identity/oidc/index.mjs +12 -5
- package/modules/operator-auth/identity/oidc/store.mjs +2 -0
- package/modules/operator-auth/index.d.mts +40 -1
- package/modules/operator-auth/routes.mjs +11 -6
- package/modules/operator-auth/service.mjs +8 -3
- package/modules/operator-auth/signin/index.mjs +1 -1
- package/modules/portability/README.md +82 -1
- package/modules/portability/export.mjs +6 -4
- package/modules/portability/format.mjs +5 -0
- package/modules/portability/index.d.mts +10 -2
- package/modules/portability/index.mjs +3 -2
- package/modules/portability/ledgers.mjs +27 -4
- package/modules/release/README.md +74 -2
- package/modules/release/sign.mjs +4 -2
- package/modules/release/signature.mjs +14 -7
- package/modules/release/signing-cli.mjs +2 -1
- package/modules/release/statement.mjs +2 -2
- package/modules/release/vectors/generate.mjs +1 -0
- package/modules/release/vectors/publisher-projects.mjs +133 -0
- package/modules/release/vectors/release-signature-vectors.json +5195 -0
- package/modules/release/verify.mjs +5 -1
- package/onboarding/schema/SETUP.schema.json +43 -1
- package/onboarding/schema/business-field-inputs.mjs +0 -1
- package/onboarding/schema/setup-validation.mjs +3 -0
- package/package.json +3 -2
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { canonicalJson, sha256 } from '../provenance/identity.ts';
|
|
4
|
+
import { readFileSourceRemovals } from '../readers/file.ts';
|
|
5
|
+
import { planD1Import } from '../d1/jobs.ts';
|
|
6
|
+
import type { ImportPlan, IntakeCustody } from '../types.ts';
|
|
7
|
+
import type { DoorRequest } from './types.ts';
|
|
8
|
+
import { kitContext } from './authority.ts';
|
|
9
|
+
import { DoorError } from './protocol.ts';
|
|
10
|
+
import { assertion, sql, transact } from './store.ts';
|
|
11
|
+
import { bundleBytes, text, uploadRow } from './uploads.ts';
|
|
12
|
+
import { refuseErased } from './planning.ts';
|
|
13
|
+
import { sourceRefusal } from './refusals.ts';
|
|
14
|
+
import { intakeIdentity, intakeState } from './intake-policy.ts';
|
|
15
|
+
|
|
16
|
+
interface ArchiveRow {
|
|
17
|
+
id: string; installation_id: string; namespace: string; upload_id: string; tool: string; taken_at: string;
|
|
18
|
+
name: string; bundle_sha256: string; total_bytes: number; total_chunks: number; counts_json: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const toolName = (value: unknown): string => {
|
|
22
|
+
if (typeof value !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(value)) throw new DoorError('invalid_tool', 400);
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
const instant = (value: unknown): string => {
|
|
26
|
+
if (typeof value !== 'string' || !Number.isFinite(new Date(value).getTime()) || new Date(value).toISOString() !== value) throw new DoorError('invalid_taken_at', 400);
|
|
27
|
+
return value;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** A door installed before archives answers these two actions 503 until its
|
|
31
|
+
* seed applies importDoorArchiveMigration; its other routes are unaffected. */
|
|
32
|
+
async function archivesInstalled(request: DoorRequest): Promise<void> {
|
|
33
|
+
const row = await request.options.database.prepare("SELECT count(*) AS n FROM sqlite_master WHERE type='table' AND name IN ('ik_door_archives','ik_door_archive_chunks')").first<{n: number}>();
|
|
34
|
+
if (row?.n !== 2) throw new DoorError('import_door_schema_outdated', 503, {reason: 'Apply importDoorArchiveMigration in a new numbered migration to use archive and restore-check.'});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Reads the bundle with the namespace's reader and plans it with the plain
|
|
38
|
+
* mapping, as a first plan of it would: counts and rejections, no write. An
|
|
39
|
+
* archive keeps exact bytes, so a bundle the reader had to remove a live
|
|
40
|
+
* credential value from is refused rather than kept; the markers rer's vendor
|
|
41
|
+
* exports leave (`[removed]`, `removed@`, `=removed`) hold none, so they archive. */
|
|
42
|
+
async function readArchive(request: DoorRequest, name: string, bytes: Uint8Array, id: string, bundleSha256: string, intake?: IntakeCustody): Promise<ImportPlan> {
|
|
43
|
+
const options = request.options, access = request.access;
|
|
44
|
+
if (!!intake !== !!options.intake || (intake && intake.allowlistSha256 !== intakeIdentity(options.intake!))) throw new DoorError('archive_intake_changed',409);
|
|
45
|
+
const kitNamespace = 'door-archive:' + sha256(canonicalJson([access.namespace, bundleSha256, id]));
|
|
46
|
+
try {
|
|
47
|
+
const {source, liveCredentialsRemoved} = await readFileSourceRemovals({namespace: kitNamespace, name, bytes, format: intake ? 'ndjson' : options.format, parse: options.parse, credentials: 'remove'},
|
|
48
|
+
{...options.limits, maxBytes: Math.min(options.limits?.maxBytes ?? access.maxBytes, access.maxBytes), maxRecords: Math.min(options.limits?.maxRecords ?? access.maxRecords, access.maxRecords)});
|
|
49
|
+
if (liveCredentialsRemoved) throw new DoorError('archive_holds_credentials', 422,
|
|
50
|
+
{reason: 'The bundle holds credential values, and an archive keeps its exact bytes, so it is not kept. Export again with credential values removed (rer\'s vendor exports remove them) and archive that.'});
|
|
51
|
+
if (intake) source.intake = intake;
|
|
52
|
+
return await planD1Import(source, options.mapping, kitContext(request, {id, kitNamespace, plan: {mappingVersion: options.mapping.version} as ImportPlan}));
|
|
53
|
+
} catch (error) { throw sourceRefusal(error) ?? error; }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const rejected = (plan: ImportPlan) => plan.entries.filter(e => e.rejection).map(e => ({kind: e.source.kind, sourceId: e.source.sourceId, reason: e.rejection}));
|
|
57
|
+
type ArchiveCounts = ImportPlan['counts'] & {intake?: IntakeCustody};
|
|
58
|
+
const answer = (row: Pick<ArchiveRow, 'id' | 'tool' | 'taken_at' | 'bundle_sha256' | 'total_bytes'>, {intake, ...counts}: ArchiveCounts) => ({
|
|
59
|
+
archiveId: row.id, key: `business-archive/${row.tool}/${row.taken_at}`, tool: row.tool, takenAt: row.taken_at,
|
|
60
|
+
bundleSha256: row.bundle_sha256, bytes: row.total_bytes, counts, ...(intake ? {intake} : {}),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
async function archiveRow(request: DoorRequest, id: string): Promise<ArchiveRow> {
|
|
64
|
+
const row = await request.options.database.prepare('SELECT * FROM ik_door_archives WHERE id=? AND installation_id=? AND namespace=?')
|
|
65
|
+
.bind(id, request.access.installationId, request.access.namespace).first<ArchiveRow>();
|
|
66
|
+
if (!row) throw new DoorError('archive_not_found', 404);
|
|
67
|
+
return row;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The archive's exact bytes. An erasure withdraws them whole and keeps the header, so a withdrawn archive says so. */
|
|
71
|
+
async function archiveBytes(request: DoorRequest, row: ArchiveRow): Promise<Uint8Array> {
|
|
72
|
+
const chunks = await request.options.database.prepare('SELECT ordinal,sha256,size,base64 FROM ik_door_archive_chunks WHERE archive_id=? ORDER BY ordinal').bind(row.id).all<{ordinal: number; sha256: string; size: number; base64: string}>();
|
|
73
|
+
if (!chunks.results.length) throw new DoorError('archive_withdrawn', 410, {archiveId: row.id,
|
|
74
|
+
reason: 'An erasure in this seed withdrew the archive\'s bytes with the rest of the import door\'s held content. Nothing remains to restore.'});
|
|
75
|
+
const parts = chunks.results.map((c, i) => {
|
|
76
|
+
const bytes = Buffer.from(c.base64, 'base64');
|
|
77
|
+
if (c.ordinal !== i || bytes.length !== c.size || sha256(bytes) !== c.sha256) throw new DoorError('archive_custody_mismatch', 409, {archiveId: row.id});
|
|
78
|
+
return bytes;
|
|
79
|
+
});
|
|
80
|
+
const bytes = Buffer.concat(parts);
|
|
81
|
+
if (parts.length !== row.total_chunks || bytes.length !== row.total_bytes || sha256(bytes) !== row.bundle_sha256) throw new DoorError('archive_custody_mismatch', 409, {archiveId: row.id});
|
|
82
|
+
return bytes;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** `{uploadId, tool, takenAt}`: keep a fully uploaded bundle as the tool's
|
|
86
|
+
* archive. Its bytes are copied out of the upload, so no plan, catch-up or
|
|
87
|
+
* cancelled preparation deletes them; only an erasure withdraws them. Repeating
|
|
88
|
+
* the same request answers the same archive. */
|
|
89
|
+
export async function archive(request: DoorRequest, input: Record<string, unknown>) {
|
|
90
|
+
if (Object.keys(input).some(k => !['uploadId', 'tool', 'takenAt'].includes(k))) throw new DoorError('invalid_archive_input', 400);
|
|
91
|
+
await archivesInstalled(request);
|
|
92
|
+
const upload = await uploadRow(request, text(input.uploadId, 'upload_id'));
|
|
93
|
+
const tool = toolName(input.tool), takenAt = instant(input.takenAt), db = request.options.database, g = request.access;
|
|
94
|
+
const existing = await db.prepare('SELECT * FROM ik_door_archives WHERE installation_id=? AND namespace=? AND upload_id=?').bind(g.installationId, g.namespace, upload.id).first<ArchiveRow>();
|
|
95
|
+
if (existing) {
|
|
96
|
+
if (existing.tool !== tool || existing.taken_at !== takenAt) throw new DoorError('archive_exists', 409, {archiveId: existing.id});
|
|
97
|
+
await archiveBytes(request, existing);
|
|
98
|
+
return answer(existing, JSON.parse(existing.counts_json));
|
|
99
|
+
}
|
|
100
|
+
const held = await db.prepare('SELECT count(*) AS n FROM ik_door_chunks WHERE upload_id=?').bind(upload.id).first<{n: number}>();
|
|
101
|
+
if (!held?.n) throw new DoorError('upload_bytes_withheld', 410, {reason: 'The upload\'s bytes are no longer held: they went when it was planned or refused. Upload the bundle again and archive it before planning.'});
|
|
102
|
+
const bytes = await bundleBytes(request, upload), id = randomUUID();
|
|
103
|
+
const intake = intakeState(upload.intake_json)?.custody;
|
|
104
|
+
const plan = await readArchive(request, upload.name, bytes, id, upload.bundle_sha256, intake);
|
|
105
|
+
// An upload holding an erased record is refused and goes, as plan does.
|
|
106
|
+
await refuseErased(request, upload, plan);
|
|
107
|
+
const row = {id, tool, taken_at: takenAt, bundle_sha256: upload.bundle_sha256, total_bytes: upload.total_bytes};
|
|
108
|
+
await transact(db, request.authority, [
|
|
109
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_uploads WHERE id=? AND bundle_sha256=? AND total_chunks=(SELECT count(*) FROM ik_door_chunks WHERE upload_id=?))', upload.id, upload.bundle_sha256, upload.id)),
|
|
110
|
+
sql(`INSERT INTO ik_door_archives(id,installation_id,namespace,upload_id,tool,taken_at,name,bundle_sha256,total_bytes,total_chunks,counts_json)
|
|
111
|
+
VALUES(?,?,?,?,?,?,?,?,?,?,?)`, id, g.installationId, g.namespace, upload.id, tool, takenAt, upload.name, upload.bundle_sha256, upload.total_bytes, upload.total_chunks, canonicalJson({...plan.counts,...(intake ? {intake} : {})})),
|
|
112
|
+
sql('INSERT INTO ik_door_archive_chunks(archive_id,ordinal,sha256,size,base64) SELECT ?,ordinal,sha256,size,base64 FROM ik_door_chunks WHERE upload_id=?', id, upload.id),
|
|
113
|
+
assertion(sql('(SELECT count(*) FROM ik_door_archive_chunks WHERE archive_id=?)=?', id, upload.total_chunks)),
|
|
114
|
+
]);
|
|
115
|
+
return answer(row, {...plan.counts,...(intake ? {intake} : {})});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** `{archiveId}`: parse the archive's exact bytes with the namespace's reader
|
|
119
|
+
* and mapping, as a restore would, writing nothing native. */
|
|
120
|
+
export async function restoreCheck(request: DoorRequest, input: Record<string, unknown>) {
|
|
121
|
+
if (Object.keys(input).some(k => k !== 'archiveId')) throw new DoorError('invalid_restore_check_input', 400);
|
|
122
|
+
await archivesInstalled(request);
|
|
123
|
+
const row = await archiveRow(request, text(input.archiveId, 'archive_id'));
|
|
124
|
+
if (row.total_bytes > request.access.maxBytes || row.total_chunks > request.access.maxChunks) throw new DoorError('archive_exceeds_grant_quota');
|
|
125
|
+
const bytes = await archiveBytes(request, row);
|
|
126
|
+
const intake = (JSON.parse(row.counts_json) as ArchiveCounts).intake;
|
|
127
|
+
const plan = await readArchive(request, row.name, bytes, row.id, row.bundle_sha256, intake);
|
|
128
|
+
return {...answer({...row, bundle_sha256: sha256(bytes), total_bytes: bytes.length}, {...plan.counts,...(intake ? {intake} : {})}), rejected: rejected(plan)};
|
|
129
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { D1Database, D1Sql } from '../d1/types.ts';
|
|
2
|
+
import type { ImportAction } from '../types.ts';
|
|
3
|
+
import type { DoorRequest, DoorSnapshot, DoorHistoryEntry } from './types.ts';
|
|
4
|
+
import { DoorError, type DoorAction } from './protocol.ts';
|
|
5
|
+
import { assertion, snapshotHeld, sql, statement, transact } from './store.ts';
|
|
6
|
+
import { approvalCaption } from './failures.ts';
|
|
7
|
+
import { reserveReturn } from '../d1/budget.ts';
|
|
8
|
+
|
|
9
|
+
const actions: Record<DoorAction, ImportAction[]> = {
|
|
10
|
+
uploads: [], plan: ['plan', 'read'], apply: ['create', 'apply', 'read'],
|
|
11
|
+
status: ['read'], reconcile: ['read'], undo: ['read', 'undo', 'rollback'],
|
|
12
|
+
// Parsing a bundle for its counts is a plan: no native write.
|
|
13
|
+
archive: ['plan'], 'restore-check': ['plan'],
|
|
14
|
+
};
|
|
15
|
+
/** Statements the door runs after a kit call returns (lineage recovery and the
|
|
16
|
+
* final authority check), kept out of the kit's self-sized batches. */
|
|
17
|
+
const DOOR_RETURN_STATEMENTS = 16;
|
|
18
|
+
export function kitContext(request: DoorRequest, snapshot: Pick<DoorSnapshot, 'kitNamespace'|'plan'|'id'>) {
|
|
19
|
+
const {options, access: grant} = request;
|
|
20
|
+
return reserveReturn({
|
|
21
|
+
destination: options.destination, namespace: snapshot.kitNamespace,
|
|
22
|
+
mappingVersion: snapshot.plan.mappingVersion,
|
|
23
|
+
operationKey: 'door:' + snapshot.id,
|
|
24
|
+
...(grant.action === 'apply' ? {reviewedFingerprint: grant.fingerprint} : {}),
|
|
25
|
+
batchSize: 20, maxBatches: 1,
|
|
26
|
+
storage: {database: options.database, targets: options.targets},
|
|
27
|
+
authorization: {
|
|
28
|
+
principalId: options.ownerPrincipalId,
|
|
29
|
+
predicate(action: ImportAction, destination: string): D1Sql {
|
|
30
|
+
if (destination !== options.destination || !actions[grant.action].includes(action)) return sql('0');
|
|
31
|
+
if (grant.action !== 'apply') return request.authority;
|
|
32
|
+
// A plan whose apply failed is terminal: no grant or approval applies it again.
|
|
33
|
+
const approved = sql(`EXISTS(SELECT 1 FROM ik_door_plans p JOIN ik_door_approvals a ON a.plan_id=p.id
|
|
34
|
+
WHERE p.id=? AND p.fingerprint=? AND p.complete=1 AND a.approval_id=? AND a.owner=? AND a.approved_at=? AND a.fingerprint=p.fingerprint
|
|
35
|
+
AND NOT EXISTS(SELECT 1 FROM ik_door_apply_failures f WHERE f.plan_id=p.id))`, snapshot.id, grant.fingerprint, grant.approvalRecordId, grant.email, grant.approvedAt);
|
|
36
|
+
// Writing from the reviewed snapshot needs it still held; reading the job does not.
|
|
37
|
+
const held = snapshotHeld(snapshot.id), fingerprint = action === 'read' ? approved : sql(`(${approved.sql}) AND (${held.sql})`, ...approved.params ?? [], ...held.params ?? []);
|
|
38
|
+
return sql(`(${request.authority.sql}) AND (${fingerprint.sql})`, ...request.authority.params ?? [], ...fingerprint.params ?? []);
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
}, DOOR_RETURN_STATEMENTS);
|
|
42
|
+
}
|
|
43
|
+
/** Server-only integration hook. The predicate must consult the seed's CURRENT
|
|
44
|
+
* owner session or the owner's provisioning approval in this same database. */
|
|
45
|
+
export async function writeImportDoorSettings(db: D1Database, installationId: string, changes: {enabled: boolean; revokedKids?: string[]}, ownerPredicate: D1Sql): Promise<void> {
|
|
46
|
+
if (!installationId || typeof changes.enabled !== 'boolean' || changes.revokedKids?.some(k => !/^[A-Za-z0-9_-]{1,64}$/.test(k))) throw new DoorError('invalid_settings', 400);
|
|
47
|
+
await transact(db, ownerPredicate, [
|
|
48
|
+
sql('INSERT INTO ik_door_settings(installation_id,enabled) VALUES(?,?) ON CONFLICT(installation_id) DO UPDATE SET enabled=excluded.enabled', installationId, changes.enabled ? 1 : 0),
|
|
49
|
+
...(changes.revokedKids ?? []).map(kid => sql('INSERT OR IGNORE INTO ik_door_revoked_keys(installation_id,kid) VALUES(?,?)', installationId, kid)),
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
/** Business-provisioned installs only: the seed calls this when it sees
|
|
53
|
+
* `RER_IMPORT_DOOR=on`, from the path that completes its first owner claim and
|
|
54
|
+
* again from every later owner path (the claim's already-initialized answer,
|
|
55
|
+
* owner sign-in or session start), with a predicate that holds once that owner
|
|
56
|
+
* exists. One batch checks the predicate before and after, and creates the
|
|
57
|
+
* installation's settings row enabled only when it has none, so it never
|
|
58
|
+
* overrides the owner's later choice: a door the owner turned off stays off, and
|
|
59
|
+
* revoked keys stay revoked. The claim and this batch are separate commits, and
|
|
60
|
+
* the claim cannot complete twice, so a failure between them is healed by the
|
|
61
|
+
* next owner path calling this again; a repeat is always safe. Without the
|
|
62
|
+
* variable, never call it: the door stays off. */
|
|
63
|
+
export async function enableDoorAtFirstClaim(db: D1Database, installationId: string, ownerPredicate: D1Sql): Promise<void> {
|
|
64
|
+
if (typeof installationId !== 'string' || !installationId.trim()) throw new DoorError('invalid_settings', 400);
|
|
65
|
+
try {
|
|
66
|
+
await transact(db, ownerPredicate, [
|
|
67
|
+
sql('INSERT INTO ik_door_settings(installation_id,enabled) VALUES(?,1) ON CONFLICT(installation_id) DO NOTHING', installationId),
|
|
68
|
+
]);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const owner = await statement(db, sql(`SELECT CASE WHEN (${ownerPredicate.sql}) THEN 1 ELSE 0 END AS ok`, ...ownerPredicate.params ?? [])).first();
|
|
71
|
+
if (owner?.ok !== 1) throw new DoorError('owner_required');
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Use alongside the seed's existing owner-bound import list and undo route. */
|
|
76
|
+
export async function readImportDoorHistory(db: D1Database, installationId: string, ownerPredicate: D1Sql) {
|
|
77
|
+
const query = sql(`SELECT p.id AS planId,p.job_id AS jobId,p.namespace,p.fingerprint,j.state,j.error,f.reason_code AS failureCode,
|
|
78
|
+
a.owner,a.approved_at AS approvedAt,a.approval_id AS approvalRecordId,a.reviewer
|
|
79
|
+
FROM ik_door_plans p JOIN ik_door_approvals a ON a.plan_id=p.id JOIN ik_jobs j ON j.id=p.job_id
|
|
80
|
+
LEFT JOIN ik_door_apply_failures f ON f.plan_id=p.id
|
|
81
|
+
WHERE p.installation_id=? ORDER BY a.approved_at DESC,p.id`, installationId);
|
|
82
|
+
const results = await db.batch([
|
|
83
|
+
statement(db, sql(`SELECT CASE WHEN (${ownerPredicate.sql}) THEN 1 ELSE json('IMPORT_DOOR_OWNER_REQUIRED') END`, ...ownerPredicate.params ?? [])),
|
|
84
|
+
statement(db, query),
|
|
85
|
+
statement(db, sql(`SELECT CASE WHEN (${ownerPredicate.sql}) THEN 1 ELSE json('IMPORT_DOOR_OWNER_REQUIRED') END`, ...ownerPredicate.params ?? [])),
|
|
86
|
+
]);
|
|
87
|
+
const rows = results[1] as {results: Omit<DoorHistoryEntry, 'caption'>[]} | Omit<DoorHistoryEntry, 'caption'>[];
|
|
88
|
+
return (Array.isArray(rows) ? rows : rows.results).map(row => ({...row,
|
|
89
|
+
caption: approvalCaption({owner: row.owner, approved_at: row.approvedAt, approval_id: row.approvalRecordId, reviewer: row.reviewer}, row.fingerprint, row.failureCode)}));
|
|
90
|
+
}
|
|
91
|
+
export async function reserveApply(request: DoorRequest, snapshot: DoorSnapshot): Promise<void> {
|
|
92
|
+
const g = request.access;
|
|
93
|
+
if (snapshot.standingApproval && (g.catchup !== 'bounded' || g.approvalRecordId !== snapshot.standingApproval.id || g.approvedAt !== snapshot.standingApproval.at)) throw new DoorError('standing_approval_mismatch');
|
|
94
|
+
if (!snapshot.standingApproval && g.catchup === 'bounded') throw new DoorError('standing_approval_mismatch');
|
|
95
|
+
const consumed = await request.options.database.prepare(`SELECT 1 FROM ik_door_approvals a JOIN ik_door_apply_failures f ON f.plan_id=a.plan_id
|
|
96
|
+
JOIN ik_door_plans p ON p.id=a.plan_id WHERE p.installation_id=? AND a.approval_id=?`).bind(g.installationId,g.approvalRecordId).first();
|
|
97
|
+
if (consumed) throw new DoorError('failed_approval_requires_new_approval',409);
|
|
98
|
+
await transact(request.options.database, request.authority, [
|
|
99
|
+
assertion(sql('NOT EXISTS(SELECT 1 FROM ik_door_apply_failures WHERE plan_id=?)', snapshot.id)),
|
|
100
|
+
assertion(sql(`NOT EXISTS(SELECT 1 FROM ik_door_approvals a JOIN ik_door_apply_failures f ON f.plan_id=a.plan_id
|
|
101
|
+
JOIN ik_door_plans p ON p.id=a.plan_id WHERE p.installation_id=? AND a.approval_id=?)`, g.installationId, g.approvalRecordId)),
|
|
102
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_namespaces WHERE installation_id=? AND namespace=? AND pending_id=? AND head_id IS ?)', g.installationId, g.namespace, snapshot.id, snapshot.previous)),
|
|
103
|
+
assertion(snapshotHeld(snapshot.id)),
|
|
104
|
+
sql('INSERT OR IGNORE INTO ik_door_approvals(plan_id,approval_id,owner,approved_at,fingerprint,reviewer) VALUES(?,?,?,?,?,?)', snapshot.id, g.approvalRecordId, g.email, g.approvedAt, g.fingerprint, snapshot.standingApproval?.id ?? (request.via === 'operator' ? 'operator' : g.approvalRecordId)),
|
|
105
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_approvals WHERE plan_id=? AND approval_id=? AND owner=? AND approved_at=? AND fingerprint=?)', snapshot.id, g.approvalRecordId, g.email, g.approvedAt, g.fingerprint)),
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ImportJob } from '../types.ts';
|
|
2
|
+
import { reversalState } from '../d1/undo-state.ts';
|
|
3
|
+
import type { DoorRequest, DoorSnapshot } from './types.ts';
|
|
4
|
+
import { DoorError } from './protocol.ts';
|
|
5
|
+
import { assertion, snapshotHeld, sql, transact } from './store.ts';
|
|
6
|
+
|
|
7
|
+
/** SQLite busy/locked wording, plus the retryable errors Cloudflare D1 reports
|
|
8
|
+
* (storage resets, code updates, lost connections, overload and timeouts). */
|
|
9
|
+
const transient = new RegExp([
|
|
10
|
+
String.raw`\bSQLITE_(?:BUSY|LOCKED|INTERRUPT)(?:_\w+)?\b`, String.raw`\bdatabase (?:table )?(?:is )?(?:busy|locked|unavailable)`,
|
|
11
|
+
'query timed out', 'database worker stopped', 'Network connection lost', 'reset because its code was updated',
|
|
12
|
+
'caused object to be reset', 'Replica disconnected', 'transient issue on remote node', 'client disconnected',
|
|
13
|
+
'D1 DB is overloaded', 'exceeded timeout',
|
|
14
|
+
].join('|'),'i');
|
|
15
|
+
/** Only persist allowlisted classifications, never adapter/SQL messages that
|
|
16
|
+
* may contain customer records. Providers can wrap SQLite errors in causes. */
|
|
17
|
+
function reasonCode(error: unknown): string {
|
|
18
|
+
const seen = new Set<unknown>();
|
|
19
|
+
for (let cause = error; cause && typeof cause === 'object' && !seen.has(cause);) {
|
|
20
|
+
seen.add(cause);
|
|
21
|
+
const value = cause as {code?: unknown; message?: unknown; cause?: unknown};
|
|
22
|
+
const text = `${typeof value.code === 'string' ? value.code : ''} ${typeof value.message === 'string' ? value.message : ''}`;
|
|
23
|
+
if (transient.test(text)) return 'import_door_apply_transient';
|
|
24
|
+
if (/\bSQLITE_CONSTRAINT(?:_\w+)?\b|\bconstraint failed\b/i.test(text)) return 'import_door_apply_constraint';
|
|
25
|
+
cause = value.cause;
|
|
26
|
+
}
|
|
27
|
+
return 'import_door_apply_failed';
|
|
28
|
+
}
|
|
29
|
+
export async function failedPlan(request: DoorRequest, planId: string): Promise<string | null> {
|
|
30
|
+
const failure = await request.options.database.prepare('SELECT reason_code FROM ik_door_apply_failures WHERE plan_id=?').bind(planId).first();
|
|
31
|
+
return failure?.reason_code ?? null;
|
|
32
|
+
}
|
|
33
|
+
export function requireNewPlan(planId: string, reason: string): never {
|
|
34
|
+
throw new DoorError('import_door_plan_failed', 409, {planId, reasonCode: reason, requiresNewPlan: true, requiresNewApproval: true});
|
|
35
|
+
}
|
|
36
|
+
export async function recordApplyFailure(request: DoorRequest, snapshot: DoorSnapshot, job: ImportJob, error: unknown): Promise<never> {
|
|
37
|
+
const g = request.access, reason = reasonCode(error);
|
|
38
|
+
// A preserving reversal journal starts at the last published apply fence.
|
|
39
|
+
// A refused batch published no progress: its terminal failed state/error
|
|
40
|
+
// fence stale applies without introducing an unjournaled publication fence.
|
|
41
|
+
const fence = job.fence + Number(reversalState(job).policy !== 'preserve-conflicts');
|
|
42
|
+
// This is server bookkeeping for an already accepted attempt, including when
|
|
43
|
+
// its key was revoked/expired during apply. It grants no further native writes.
|
|
44
|
+
// Fence on the pre-batch job: a concurrent publication must never be failed.
|
|
45
|
+
// An erasure that overtook the request, or withdrew the snapshot, is not an
|
|
46
|
+
// apply failure: this batch is then refused and the erasure's 410 answers.
|
|
47
|
+
const unerased = sql('(SELECT count(*) FROM ik_d1_erasure_journal WHERE destination=?)=?', request.options.destination, request.epoch);
|
|
48
|
+
// A grant request proves its accepted nonce; an owner-session request was
|
|
49
|
+
// authorised at admission and carries none.
|
|
50
|
+
const issued = request.nonce ? sql(`EXISTS(SELECT 1 FROM ik_door_grants WHERE installation_id=? AND jti=? AND action='apply' AND namespace=? AND kid=?)`,
|
|
51
|
+
g.installationId, request.nonce.jti, g.namespace, request.nonce.kid) : sql('1');
|
|
52
|
+
const accepted = sql(`EXISTS(SELECT 1 FROM ik_door_plans p JOIN ik_door_approvals a ON a.plan_id=p.id JOIN ik_jobs j ON j.id=p.job_id
|
|
53
|
+
WHERE p.id=? AND p.installation_id=? AND p.namespace=? AND p.fingerprint=? AND a.fingerprint=p.fingerprint
|
|
54
|
+
AND a.approval_id=? AND a.owner=? AND a.approved_at=? AND j.id=? AND j.fence=? AND j.checkpoint=? AND j.state IN ('queued','applying'))`,
|
|
55
|
+
snapshot.id, g.installationId, g.namespace, g.fingerprint,
|
|
56
|
+
g.approvalRecordId, g.email, g.approvedAt, job.id, job.fence, job.checkpoint);
|
|
57
|
+
await transact(request.options.database, sql('1'), [
|
|
58
|
+
assertion(unerased), assertion(snapshotHeld(snapshot.id)), assertion(issued), assertion(accepted),
|
|
59
|
+
sql('INSERT INTO ik_door_apply_failures(plan_id,reason_code) VALUES(?,?)', snapshot.id, reason),
|
|
60
|
+
sql("UPDATE ik_jobs SET state='failed',error=?,fence=? WHERE id=?", reason, fence, job.id),
|
|
61
|
+
assertion(sql("EXISTS(SELECT 1 FROM ik_jobs WHERE id=? AND state='failed' AND error=? AND fence=?)", job.id, reason, fence)),
|
|
62
|
+
]);
|
|
63
|
+
throw new DoorError(reason, reason === 'import_door_apply_transient' ? 503 : 409,
|
|
64
|
+
{planId:snapshot.id, jobId:job.id, requiresNewPlan:true, requiresNewApproval:true});
|
|
65
|
+
}
|
|
66
|
+
/** "via Business" only when Business's own approval record reviewed it; an
|
|
67
|
+
* owner-session apply is reviewed by `operator`. */
|
|
68
|
+
export function approvalCaption(approval: {owner: unknown; approved_at: unknown; approval_id: unknown; reviewer: unknown}, fingerprint: unknown, failure: unknown): string {
|
|
69
|
+
return `${failure ? `approved, then failed (${failure})` : 'approved'}${approval.reviewer !== approval.approval_id ? '' : ' via Business'} by ${approval.owner} at ${approval.approved_at}, fingerprint ${fingerprint}`;
|
|
70
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { configureClaim } from '../../../owner-claim/verify.mjs';
|
|
2
|
+
import type { OwnerClaimConfig } from '../../../owner-claim/verify.mjs';
|
|
3
|
+
|
|
4
|
+
import { DoorError } from './protocol.ts';
|
|
5
|
+
export { DoorError } from './protocol.ts';
|
|
6
|
+
export type { DoorAction, DoorGrant } from './protocol.ts';
|
|
7
|
+
import type { DoorGrant } from './protocol.ts';
|
|
8
|
+
|
|
9
|
+
function fail(): never { throw new DoorError('invalid_grant'); }
|
|
10
|
+
const object = (value: unknown): value is Record<string, unknown> => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
11
|
+
const requiredKeys = ['v', 'iss', 'aud', 'installationId', 'email', 'jti', 'iat', 'exp', 'action', 'namespace', 'maxBytes', 'maxChunks', 'maxRecords'];
|
|
12
|
+
const optionalKeys = ['fingerprint', 'approvalRecordId', 'approvedAt', 'catchup'];
|
|
13
|
+
const actions: readonly string[] = ['uploads', 'plan', 'apply', 'status', 'reconcile', 'undo', 'archive', 'restore-check'];
|
|
14
|
+
|
|
15
|
+
function decode(segment: string): Uint8Array<ArrayBuffer> {
|
|
16
|
+
if (!/^[A-Za-z0-9_-]+$/.test(segment)) fail();
|
|
17
|
+
try {
|
|
18
|
+
const raw = atob(segment.replaceAll('-', '+').replaceAll('_', '/'));
|
|
19
|
+
if (btoa(raw).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '') !== segment) fail();
|
|
20
|
+
return Uint8Array.from(raw, c => c.charCodeAt(0));
|
|
21
|
+
} catch { return fail(); }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function json(segment: string): Record<string, unknown> {
|
|
25
|
+
try {
|
|
26
|
+
const text = new TextDecoder('utf-8', { fatal: true }).decode(decode(segment));
|
|
27
|
+
const value: unknown = JSON.parse(text);
|
|
28
|
+
if (!object(value)) fail();
|
|
29
|
+
// Walk whole string tokens so colons or escaped quotes inside values cannot
|
|
30
|
+
// masquerade as members. Compare decoded keys, including escaped aliases.
|
|
31
|
+
const keys = new Set<string>();
|
|
32
|
+
for (let i = 0; i < text.length; i++) {
|
|
33
|
+
if (text[i] !== '"') continue;
|
|
34
|
+
const start = i++;
|
|
35
|
+
while (i < text.length && text[i] !== '"') {
|
|
36
|
+
if (text[i] === '\\') i++;
|
|
37
|
+
i++;
|
|
38
|
+
}
|
|
39
|
+
const end = i + 1;
|
|
40
|
+
let next = end;
|
|
41
|
+
while (next < text.length && /\s/.test(text[next])) next++;
|
|
42
|
+
if (text[next] !== ':') continue;
|
|
43
|
+
const key = JSON.parse(text.slice(start, end)) as string;
|
|
44
|
+
if (keys.has(key)) fail();
|
|
45
|
+
keys.add(key);
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
} catch { return fail(); }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function quota(value: unknown, max: number): boolean {
|
|
52
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0 && value <= max;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function approved(value: Record<string, unknown>): boolean {
|
|
56
|
+
if (typeof value.approvalRecordId !== 'string' || !value.approvalRecordId.trim() || value.approvalRecordId.length > 256
|
|
57
|
+
|| typeof value.approvedAt !== 'string') return false;
|
|
58
|
+
const date = new Date(value.approvedAt);
|
|
59
|
+
return Number.isFinite(date.getTime()) && date.toISOString() === value.approvedAt;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Payload members every RER grant carries, checked against the server-owned claim config. */
|
|
63
|
+
const baseKeys = ['v', 'iss', 'aud', 'installationId', 'email', 'jti', 'iat', 'exp', 'action'];
|
|
64
|
+
function bound(value: Record<string, unknown>, config: Readonly<OwnerClaimConfig>, now: number): boolean {
|
|
65
|
+
return value.v === 1 && value.iss === config.issuer && value.aud === config.appOrigin
|
|
66
|
+
&& value.installationId === config.installationId && value.email === config.expectedEmail
|
|
67
|
+
&& typeof value.jti === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value.jti)
|
|
68
|
+
&& typeof value.iat === 'number' && typeof value.exp === 'number'
|
|
69
|
+
&& Number.isSafeInteger(value.iat) && Number.isSafeInteger(value.exp) && value.iat >= 0
|
|
70
|
+
&& value.iat - now <= 30 && value.exp > now && value.exp > value.iat && value.exp - value.iat <= 300;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function validateGrant(value: Record<string, unknown>, config: Readonly<OwnerClaimConfig>, now: number): Readonly<DoorGrant> {
|
|
74
|
+
if (requiredKeys.some(key => !Object.hasOwn(value, key))
|
|
75
|
+
|| Object.keys(value).some(key => !requiredKeys.includes(key) && !optionalKeys.includes(key))
|
|
76
|
+
|| !bound(value, config, now)
|
|
77
|
+
|| typeof value.action !== 'string' || !actions.includes(value.action)
|
|
78
|
+
|| typeof value.namespace !== 'string' || !value.namespace || value.namespace !== value.namespace.trim()
|
|
79
|
+
|| value.namespace.length > 256 || /[\x00-\x1f\x7f-\x9f]/.test(value.namespace)
|
|
80
|
+
|| !quota(value.maxBytes, 300_000_000) || !quota(value.maxChunks, 1024) || !quota(value.maxRecords, 1_000_000)) fail();
|
|
81
|
+
|
|
82
|
+
const catchup = Object.hasOwn(value, 'catchup');
|
|
83
|
+
if (catchup && (value.catchup !== 'bounded' || (value.action !== 'plan' && value.action !== 'apply'))) fail();
|
|
84
|
+
if (catchup && !approved(value)) fail();
|
|
85
|
+
if (value.action === 'apply') {
|
|
86
|
+
if (typeof value.fingerprint !== 'string' || !/^[0-9a-f]{64}$/.test(value.fingerprint) || !approved(value)) fail();
|
|
87
|
+
} else {
|
|
88
|
+
if (Object.hasOwn(value, 'fingerprint')) fail();
|
|
89
|
+
const hasApproval = Object.hasOwn(value, 'approvalRecordId') || Object.hasOwn(value, 'approvedAt');
|
|
90
|
+
if (hasApproval && (value.action !== 'plan' || !catchup || !approved(value))) fail();
|
|
91
|
+
}
|
|
92
|
+
return Object.freeze(value) as unknown as Readonly<DoorGrant>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Signature and header check shared by every RER grant: ES256 under the
|
|
96
|
+
* owner-claim key ring, with the exact `rer-import-grant+jwt` header. */
|
|
97
|
+
async function signed(token: unknown, input: OwnerClaimConfig & { now?: number }): Promise<{ payload: Record<string, unknown>; kid: string; config: Readonly<OwnerClaimConfig>; now: number }> {
|
|
98
|
+
const configKeys = ['issuer', 'installationId', 'appOrigin', 'expectedEmail', 'publicKeys', 'now'];
|
|
99
|
+
if (!object(input) || Reflect.ownKeys(input).some(key => typeof key !== 'string' || !configKeys.includes(key))) throw new DoorError('invalid_configuration', 503);
|
|
100
|
+
let config: Readonly<OwnerClaimConfig>;
|
|
101
|
+
try { config = configureClaim(input); }
|
|
102
|
+
catch { throw new DoorError('invalid_configuration', 503); }
|
|
103
|
+
const now = input.now === undefined ? Math.floor(Date.now() / 1000) : input.now;
|
|
104
|
+
if (!Number.isSafeInteger(now) || now < 0) throw new DoorError('invalid_configuration', 503);
|
|
105
|
+
if (typeof token !== 'string' || token.length > 8192) fail();
|
|
106
|
+
const parts = token.split('.');
|
|
107
|
+
if (parts.length !== 3) fail();
|
|
108
|
+
const header = json(parts[0]);
|
|
109
|
+
if (Object.keys(header).length !== 3 || !['alg', 'typ', 'kid'].every(key => Object.hasOwn(header, key))
|
|
110
|
+
|| header.alg !== 'ES256' || header.typ !== 'rer-import-grant+jwt'
|
|
111
|
+
|| typeof header.kid !== 'string' || !Object.hasOwn(config.publicKeys, header.kid)) fail();
|
|
112
|
+
const kid = header.kid as string;
|
|
113
|
+
const signature = decode(parts[2]);
|
|
114
|
+
if (signature.length !== 64) fail();
|
|
115
|
+
try {
|
|
116
|
+
const key = await crypto.subtle.importKey('jwk', config.publicKeys[kid], { name: 'ECDSA', namedCurve: 'P-256' }, false, ['verify']);
|
|
117
|
+
const message = new TextEncoder().encode(`${parts[0]}.${parts[1]}`);
|
|
118
|
+
if (!await crypto.subtle.verify({ name: 'ECDSA', hash: 'SHA-256' }, key, signature, message)) fail();
|
|
119
|
+
} catch { fail(); }
|
|
120
|
+
return { payload: json(parts[1]), kid, config, now };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export async function verifyImportGrant(
|
|
124
|
+
token: string,
|
|
125
|
+
input: OwnerClaimConfig & { now?: number },
|
|
126
|
+
): Promise<{ grant: Readonly<DoorGrant>; kid: string }> {
|
|
127
|
+
const { payload, kid, config, now } = await signed(token, input);
|
|
128
|
+
return { grant: validateGrant(payload, config, now), kid };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** A verified RER-signed read outside the import door. */
|
|
132
|
+
export interface RerGrant {
|
|
133
|
+
v: 1; iss: string; aud: string; installationId: string; email: string;
|
|
134
|
+
jti: string; iat: number; exp: number; action: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Accept an RER-signed request on a seed route outside `/api/rer/import/`
|
|
138
|
+
* (for example Uptime's `POST /api/rer/runner-status`). The request carries
|
|
139
|
+
* `Authorization: Bearer <grant>`: the door's ES256 `rer-import-grant+jwt`
|
|
140
|
+
* under the same owner-claim key ring, whose payload is exactly `{v, iss, aud,
|
|
141
|
+
* installationId, email, jti, iat, exp, action}` with `action` equal to the
|
|
142
|
+
* route's own action. Door actions are reserved for the door, and a door grant
|
|
143
|
+
* (namespace and quotas) is refused here. The request must be addressed to the
|
|
144
|
+
* claim's app origin. Nonces are not recorded: use it for reads whose repeat
|
|
145
|
+
* within the grant's 300 seconds discloses nothing more. Revocation lives in
|
|
146
|
+
* the seed's database; check `kid` against it where the route needs that.
|
|
147
|
+
* Throws `DoorError`: 401 `grant_required`, 403 `invalid_grant` or
|
|
148
|
+
* `grant_binding_mismatch`, 503 `invalid_configuration`. */
|
|
149
|
+
export async function verifyRerGrant(
|
|
150
|
+
request: Request,
|
|
151
|
+
claimConfig: OwnerClaimConfig & { now?: number },
|
|
152
|
+
action: string,
|
|
153
|
+
): Promise<{ grant: Readonly<RerGrant>; kid: string }> {
|
|
154
|
+
if (typeof action !== 'string' || !/^[a-z][a-z0-9-]{0,62}$/.test(action) || actions.includes(action)) throw new DoorError('invalid_configuration', 503);
|
|
155
|
+
const authorization = request.headers.get('Authorization');
|
|
156
|
+
if (!authorization?.startsWith('Bearer ')) throw new DoorError('grant_required', 401);
|
|
157
|
+
const { payload, kid, config, now } = await signed(authorization.slice(7), claimConfig);
|
|
158
|
+
if (Object.keys(payload).length !== baseKeys.length || baseKeys.some(key => !Object.hasOwn(payload, key))
|
|
159
|
+
|| !bound(payload, config, now) || payload.action !== action) fail();
|
|
160
|
+
if (new URL(request.url).origin !== config.appOrigin) throw new DoorError('grant_binding_mismatch');
|
|
161
|
+
return { grant: Object.freeze(payload) as unknown as Readonly<RerGrant>, kid };
|
|
162
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { D1Database } from '../d1/types.ts';
|
|
2
|
+
import type { DoorRequest, ImportDoorRuntimeOptions } from './types.ts';
|
|
3
|
+
import { DoorError, GRANT_ONLY_ACTIONS, type DoorAction } from './protocol.ts';
|
|
4
|
+
import { authorized, erasedSince } from './store.ts';
|
|
5
|
+
import { sizeLimitError } from './limits.ts';
|
|
6
|
+
import { requestDatabase } from '../d1/budget.ts';
|
|
7
|
+
import { uploads } from './uploads.ts';
|
|
8
|
+
import { plan } from './planning.ts';
|
|
9
|
+
import { apply, status, reconcile, undo } from './jobs.ts';
|
|
10
|
+
import { archive, restoreCheck } from './archive.ts';
|
|
11
|
+
import { streamUpload } from './intake-upload.ts';
|
|
12
|
+
import { intakeIdentity } from './intake-policy.ts';
|
|
13
|
+
|
|
14
|
+
const routes = {uploads,plan,apply,status,reconcile,undo,archive,'restore-check':restoreCheck};
|
|
15
|
+
const headers = {'Cache-Control':'no-store','Content-Type':'application/json','X-Content-Type-Options':'nosniff'};
|
|
16
|
+
async function readInput(request: Request): Promise<Record<string,unknown>> {
|
|
17
|
+
if (request.headers.get('Content-Type')?.split(';')[0] !== 'application/json' || !request.body) throw new DoorError('json_body_required',400);
|
|
18
|
+
const reader = request.body.getReader(), chunks: Uint8Array[] = []; let size = 0;
|
|
19
|
+
try {
|
|
20
|
+
for (;;) {
|
|
21
|
+
const {done,value} = await reader.read(); if (done) break;
|
|
22
|
+
size += value.byteLength;
|
|
23
|
+
if (size > 100_000) throw new DoorError('request_too_large',413);
|
|
24
|
+
chunks.push(value);
|
|
25
|
+
}
|
|
26
|
+
} finally { await reader.cancel(); reader.releaseLock(); }
|
|
27
|
+
const bytes = new Uint8Array(size); let offset = 0;
|
|
28
|
+
for (const part of chunks) { bytes.set(part,offset); offset += part.byteLength; }
|
|
29
|
+
let value;
|
|
30
|
+
try { value = JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(bytes)); } catch { throw new DoorError('invalid_json',400); }
|
|
31
|
+
if (!value || typeof value!=='object' || Array.isArray(value)) throw new DoorError('invalid_input',400);
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
/** The settings every mount needs whatever its source: a preserving undo policy needs the adapter's preserved undo. */
|
|
35
|
+
export function mountValid(options: Partial<ImportDoorRuntimeOptions>): boolean {
|
|
36
|
+
return !!options.ownerPrincipalId?.trim() && !!options.destination?.trim() && [undefined,'strict','preserve-conflicts'].includes(options.reversalPolicy)
|
|
37
|
+
&& (options.reversalPolicy!=='preserve-conflicts' || typeof options.targets?.preparePreservedUndo==='function');
|
|
38
|
+
}
|
|
39
|
+
/** Refuses a mount, or a resolved source, missing what the pipeline needs. */
|
|
40
|
+
export function runtimeOptions<T extends Partial<ImportDoorRuntimeOptions>>(options: T): T & ImportDoorRuntimeOptions {
|
|
41
|
+
if (!options.mapping?.version || typeof options.parse!=='function' || !mountValid(options)) throw new DoorError('invalid_configuration',503);
|
|
42
|
+
if (options.intake !== undefined) intakeIdentity(options.intake);
|
|
43
|
+
return options as T & ImportDoorRuntimeOptions;
|
|
44
|
+
}
|
|
45
|
+
/** Server-side mount only. A null response belongs to another dispatcher. Every
|
|
46
|
+
* statement of one request, the door's own, the adapter's and each kit call's,
|
|
47
|
+
* runs on `database` wrapped in one D1 request budget; exhausting it is a named 413. */
|
|
48
|
+
export function createDoorHandler(prefix: string, database: D1Database,
|
|
49
|
+
admit: (request: Request, action: DoorAction, database: D1Database) => Promise<DoorRequest | null>,
|
|
50
|
+
review: (request: DoorRequest, input: Record<string, unknown>) => Promise<DoorRequest> = async request => request,
|
|
51
|
+
available: (database: D1Database) => Promise<boolean> = async () => true,
|
|
52
|
+
): (request: Request)=>Promise<Response|null> {
|
|
53
|
+
return async request => {
|
|
54
|
+
const url = new URL(request.url);
|
|
55
|
+
if (url.pathname.startsWith('/api/rer/import/') && prefix !== '/api/rer/import/') return Response.json({error:'not_found'},{status:404,headers});
|
|
56
|
+
if (!url.pathname.startsWith(prefix)) return null;
|
|
57
|
+
let admitted: DoorRequest | null = null;
|
|
58
|
+
try {
|
|
59
|
+
const budgeted = requestDatabase(database);
|
|
60
|
+
if (!await available(budgeted)) return Response.json({error:'not_found'},{status:404,headers});
|
|
61
|
+
const action = url.pathname.slice(prefix.length) as DoorAction;
|
|
62
|
+
if (!Object.hasOwn(routes,action) || (prefix !== '/api/rer/import/' && GRANT_ONLY_ACTIONS.includes(action))) return Response.json({error:'not_found'},{status:404,headers});
|
|
63
|
+
if (request.method!=='POST') return Response.json({error:'method_not_allowed'},{status:405,headers:{...headers,Allow:'POST'}});
|
|
64
|
+
if (url.search || url.hash) throw new DoorError('query_transport_forbidden',400);
|
|
65
|
+
admitted = await admit(request,action,budgeted);
|
|
66
|
+
if (!admitted) return Response.json({error:'not_found'},{status:404,headers});
|
|
67
|
+
if (action === 'uploads' && admitted.options.intake && request.headers.get('Content-Type')?.split(';')[0] === 'application/octet-stream') {
|
|
68
|
+
const result = await streamUpload(admitted,request);
|
|
69
|
+
await authorized(admitted);
|
|
70
|
+
return Response.json(result,{headers});
|
|
71
|
+
}
|
|
72
|
+
const input = await readInput(request);
|
|
73
|
+
admitted = await review(admitted,input);
|
|
74
|
+
if (['status','reconcile','undo'].includes(action) && Object.keys(input).some(k=>k!=='planId')) throw new DoorError('invalid_input',400);
|
|
75
|
+
const result = await routes[action](admitted,input);
|
|
76
|
+
await authorized(admitted);
|
|
77
|
+
return Response.json(result,{headers});
|
|
78
|
+
} catch (caught) {
|
|
79
|
+
// Work an erasure overtook answers one stable refusal, whichever batch it
|
|
80
|
+
// reached: an unknown error, or the final authority check, after an erasure.
|
|
81
|
+
const overtaken = admitted && (!(caught instanceof DoorError) || ['grant_revoked_or_expired','owner_required'].includes(caught.code))
|
|
82
|
+
&& await erasedSince(admitted).catch(() => false);
|
|
83
|
+
// A kit size or request limit is a named 413.
|
|
84
|
+
const error = overtaken ? new DoorError('erased_during_request', 410) : sizeLimitError(caught) ?? caught;
|
|
85
|
+
const known = error instanceof DoorError;
|
|
86
|
+
// Never reflect a parser/native adapter error containing customer records.
|
|
87
|
+
return Response.json({error:known?error.code:'import_door_conflict',...(known && error.details?{details:error.details}:{})},{status:known?error.status:409,headers});
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ImportDoorOptions } from './types.ts';
|
|
2
|
+
import { admit, enabled } from './admission.ts';
|
|
3
|
+
import { createDoorHandler, mountValid, runtimeOptions } from './handler.ts';
|
|
4
|
+
import { DoorError } from './grants.ts';
|
|
5
|
+
export type * from './types.ts';
|
|
6
|
+
export type { DoorAction, DoorGrant, RerGrant } from './grants.ts';
|
|
7
|
+
export { verifyImportGrant, verifyRerGrant, DoorError } from './grants.ts';
|
|
8
|
+
export { importDoorSchema, importDoorApplyFailureMigration, importDoorScopesMigration, importDoorArchiveMigration, importDoorIntakeMigration } from './schema.ts';
|
|
9
|
+
export { writeImportDoorSettings, readImportDoorHistory, enableDoorAtFirstClaim } from './authority.ts';
|
|
10
|
+
export { createImportOperatorHandler } from './operator.ts';
|
|
11
|
+
|
|
12
|
+
/** Grant mount: disabled until a trusted key ring and explicit opt-in exist. */
|
|
13
|
+
export function createImportDoorHandler(options: ImportDoorOptions): (request: Request)=>Promise<Response|null> {
|
|
14
|
+
if (options.installationId !== undefined && (typeof options.installationId !== 'string' || !options.installationId.trim())) throw new DoorError('invalid_configuration',503);
|
|
15
|
+
if (options.source === undefined) runtimeOptions(options);
|
|
16
|
+
else if (typeof options.source !== 'function' || options.parse !== undefined || options.mapping !== undefined
|
|
17
|
+
|| !mountValid(options)) throw new DoorError('invalid_configuration',503);
|
|
18
|
+
return createDoorHandler('/api/rer/import/', options.database, (request, action, database) => admit({...options, database}, request, action),
|
|
19
|
+
undefined, async database => !!options.claim?.publicKeys && Object.keys(options.claim.publicKeys).length > 0 && await enabled({...options, database}));
|
|
20
|
+
}
|