@wtfalch/keys 0.4.1 → 0.6.0
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 +143 -5
- package/dist/bin/copy.d.ts +8 -4
- package/dist/bin/copy.js +8 -4
- package/dist/bin/migrations.js +21 -7
- package/dist/held/schema.js +1 -1
- package/dist/held/seal.d.ts +22 -0
- package/dist/held/seal.js +48 -0
- package/dist/held/shred.d.ts +1 -1
- package/dist/held/shred.js +1 -1
- package/dist/host/audit-endpoint.d.ts +15 -0
- package/dist/host/audit-endpoint.js +117 -0
- package/dist/host/events.d.ts +26 -0
- package/dist/host/events.js +31 -0
- package/dist/host/index.d.ts +11 -0
- package/dist/host/index.js +11 -0
- package/dist/host/schema.d.ts +153 -0
- package/dist/host/schema.js +20 -0
- package/dist/issued/index.js +2 -2
- package/dist/issued/ratelimit.d.ts +1 -1
- package/dist/issued/ratelimit.js +1 -1
- package/dist/issued/resign.js +1 -1
- package/dist/issued/tables.d.ts +4 -4
- package/dist/issued/tables.js +4 -4
- package/dist/migrations/{0003_keys_shred.sql → held/0002_keys_shred.sql} +26 -31
- package/dist/migrations/{0004_keys_security.sql → held/0003_keys_security.sql} +36 -70
- package/dist/migrations/host/0001_keys_host_events.sql +73 -0
- package/dist/migrations/issued/0002_keys_environment.sql +29 -0
- package/dist/migrations/{0006_keys_usage.sql → issued/0003_keys_usage.sql} +3 -2
- package/dist/migrations/{0008_keys_environment_default.sql → issued/0005_keys_environment_default.sql} +1 -1
- package/dist/migrations/issued/0006_keys_issued_guard.sql +47 -0
- package/dist/migrations-dir.d.ts +19 -0
- package/dist/migrations-dir.js +20 -0
- package/dist/testing/index.d.ts +9 -0
- package/dist/testing/index.js +9 -0
- package/dist/testing/worker-stub.d.ts +55 -0
- package/dist/testing/worker-stub.js +180 -0
- package/package.json +17 -4
- package/dist/migrations/0005_keys_environment.sql +0 -52
- /package/dist/migrations/{0002_keys_held.sql → held/0001_keys_held.sql} +0 -0
- /package/dist/migrations/{0001_keys_issued.sql → issued/0001_keys_issued.sql} +0 -0
- /package/dist/migrations/{0007_keys_rate_limit.sql → issued/0004_keys_rate_limit.sql} +0 -0
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
-- not edit copied migrations.
|
|
4
4
|
--
|
|
5
5
|
-- Neither column is part of SignableRow (worker-contract.ts) -- unsigned,
|
|
6
|
-
-- same footing as environment/idempotency_key/created_at (
|
|
7
|
-
-- keys_issued_guard()
|
|
6
|
+
-- same footing as environment/idempotency_key/created_at (0002) -- and, per
|
|
7
|
+
-- keys_issued_guard() (0006_keys_issued_guard.sql), is NOT frozen the way those are: check() writes
|
|
8
8
|
-- both on every successful verification, so they must stay ordinarily
|
|
9
9
|
-- updatable rather than joining the identity/lineage/environment immutable
|
|
10
10
|
-- set.
|
|
@@ -17,6 +17,7 @@ begin
|
|
|
17
17
|
if not exists (
|
|
18
18
|
select 1 from pg_constraint
|
|
19
19
|
where conname = 'keys_issued_credentials_use_count_check'
|
|
20
|
+
and conrelid = 'keys_issued_credentials'::regclass
|
|
20
21
|
) then
|
|
21
22
|
alter table keys_issued_credentials
|
|
22
23
|
add constraint keys_issued_credentials_use_count_check
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
-- Restores the `environment` default that
|
|
1
|
+
-- Restores the `environment` default that 0002_keys_environment.sql dropped.
|
|
2
2
|
-- Additive; do not edit copied migrations.
|
|
3
3
|
--
|
|
4
4
|
-- Without it, a host that runs its migrations before redeploying breaks every
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
-- The issued-credential guard (security review K03): revocation stays
|
|
2
|
+
-- permanent, identity, lineage and environment are immutable, and a row can
|
|
3
|
+
-- be neither deleted nor truncated. Additive; do not edit copied migrations.
|
|
4
|
+
--
|
|
5
|
+
-- Part of the issued chain, so a host that uses only the issued module (no
|
|
6
|
+
-- held tables) gets the guard: without it a revoked credential could be
|
|
7
|
+
-- un-revoked, or deleted and inserted again. The function body freezes
|
|
8
|
+
-- `environment` too (0002_keys_environment.sql).
|
|
9
|
+
--
|
|
10
|
+
-- The function pins its search_path to the schema this file runs in,
|
|
11
|
+
-- followed by pg_catalog, with pg_temp last, so a caller-owned temporary
|
|
12
|
+
-- relation cannot shadow one of this schema's. The first statement builds that path
|
|
13
|
+
-- for this transaction; `set search_path from current` then records it on
|
|
14
|
+
-- the function.
|
|
15
|
+
--
|
|
16
|
+
-- The runtime role must be able to update but never delete or truncate this
|
|
17
|
+
-- table: the host lists it under `noDelete` in `ensureRuntimeRole` (README).
|
|
18
|
+
-- The triggers below refuse both whatever the role holds.
|
|
19
|
+
select set_config('search_path', quote_ident(current_schema()) || ', pg_catalog, pg_temp', true);
|
|
20
|
+
|
|
21
|
+
create or replace function keys_issued_guard() returns trigger
|
|
22
|
+
language plpgsql
|
|
23
|
+
set search_path from current
|
|
24
|
+
as $$
|
|
25
|
+
begin
|
|
26
|
+
if tg_op in ('DELETE', 'TRUNCATE') then
|
|
27
|
+
raise exception 'keys_issued_credentials: delete and truncate refused';
|
|
28
|
+
end if;
|
|
29
|
+
if tg_op = 'UPDATE' then
|
|
30
|
+
if new.id is distinct from old.id or new.issued_by_id is distinct from old.issued_by_id
|
|
31
|
+
or new.key_prefix is distinct from old.key_prefix
|
|
32
|
+
or new.environment is distinct from old.environment then
|
|
33
|
+
raise exception 'keys_issued_credentials: identity, lineage and environment are immutable';
|
|
34
|
+
end if;
|
|
35
|
+
if old.revoked_at is not null and new.revoked_at is distinct from old.revoked_at then
|
|
36
|
+
raise exception 'keys_issued_credentials: revocation is permanent';
|
|
37
|
+
end if;
|
|
38
|
+
end if;
|
|
39
|
+
return new;
|
|
40
|
+
end
|
|
41
|
+
$$;
|
|
42
|
+
drop trigger if exists keys_issued_guard_update on keys_issued_credentials;
|
|
43
|
+
create trigger keys_issued_guard_update before update or delete on keys_issued_credentials
|
|
44
|
+
for each row execute function keys_issued_guard();
|
|
45
|
+
drop trigger if exists keys_issued_guard_truncate on keys_issued_credentials;
|
|
46
|
+
create trigger keys_issued_guard_truncate before truncate on keys_issued_credentials
|
|
47
|
+
for each statement execute function keys_issued_guard();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Absolute paths of the numbered `.sql` migrations this package ships, one
|
|
3
|
+
* directory per chain. Each is a complete, ordered chain for its own part, so
|
|
4
|
+
* a host applies only the parts it uses, as `runMigrationSources` sources:
|
|
5
|
+
*
|
|
6
|
+
* { name: 'keys-issued', dir: issuedMigrationsDir } // ./issued
|
|
7
|
+
* { name: 'keys-held', dir: heldMigrationsDir } // ./held
|
|
8
|
+
* { name: 'keys-host', dir: hostMigrationsDir } // ./host
|
|
9
|
+
*
|
|
10
|
+
* An issued-only host passes the first and gets no held tables. The files
|
|
11
|
+
* name no schema: the host's `schema` decides where they land.
|
|
12
|
+
*
|
|
13
|
+
* Its own subpath (`@wtfalch/keys/migrations-dir`) and no other imports: a
|
|
14
|
+
* migration image imports this without the Drizzle the entries pull in, and a
|
|
15
|
+
* bundler never meets this file's `import.meta.url` through them.
|
|
16
|
+
*/
|
|
17
|
+
export declare const issuedMigrationsDir: string;
|
|
18
|
+
export declare const heldMigrationsDir: string;
|
|
19
|
+
export declare const hostMigrationsDir: string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
/**
|
|
3
|
+
* Absolute paths of the numbered `.sql` migrations this package ships, one
|
|
4
|
+
* directory per chain. Each is a complete, ordered chain for its own part, so
|
|
5
|
+
* a host applies only the parts it uses, as `runMigrationSources` sources:
|
|
6
|
+
*
|
|
7
|
+
* { name: 'keys-issued', dir: issuedMigrationsDir } // ./issued
|
|
8
|
+
* { name: 'keys-held', dir: heldMigrationsDir } // ./held
|
|
9
|
+
* { name: 'keys-host', dir: hostMigrationsDir } // ./host
|
|
10
|
+
*
|
|
11
|
+
* An issued-only host passes the first and gets no held tables. The files
|
|
12
|
+
* name no schema: the host's `schema` decides where they land.
|
|
13
|
+
*
|
|
14
|
+
* Its own subpath (`@wtfalch/keys/migrations-dir`) and no other imports: a
|
|
15
|
+
* migration image imports this without the Drizzle the entries pull in, and a
|
|
16
|
+
* bundler never meets this file's `import.meta.url` through them.
|
|
17
|
+
*/
|
|
18
|
+
export const issuedMigrationsDir = fileURLToPath(new URL('./migrations/issued/', import.meta.url));
|
|
19
|
+
export const heldMigrationsDir = fileURLToPath(new URL('./migrations/held/', import.meta.url));
|
|
20
|
+
export const hostMigrationsDir = fileURLToPath(new URL('./migrations/host/', import.meta.url));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `./testing`: an in-process Worker stub built on the package's own signing
|
|
3
|
+
* and AAD code, for a host's tests that need a `WorkerClient` without a
|
|
4
|
+
* live Worker (issue #83). `canonicalJson`, `encodeSigned` and
|
|
5
|
+
* `pickSignable` are re-exported here -- not from `./issued` or the
|
|
6
|
+
* public root -- because they exist for building test fixtures, not for a
|
|
7
|
+
* host's runtime code, which never needs to construct signed bytes itself.
|
|
8
|
+
*/
|
|
9
|
+
export { createKeysWorkerStub, TEST_CREDENTIAL, canonicalJson, encodeSigned, pickSignable, type CreateKeysWorkerStubOptions, type KeysWorkerStub, } from './worker-stub.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `./testing`: an in-process Worker stub built on the package's own signing
|
|
3
|
+
* and AAD code, for a host's tests that need a `WorkerClient` without a
|
|
4
|
+
* live Worker (issue #83). `canonicalJson`, `encodeSigned` and
|
|
5
|
+
* `pickSignable` are re-exported here -- not from `./issued` or the
|
|
6
|
+
* public root -- because they exist for building test fixtures, not for a
|
|
7
|
+
* host's runtime code, which never needs to construct signed bytes itself.
|
|
8
|
+
*/
|
|
9
|
+
export { createKeysWorkerStub, TEST_CREDENTIAL, canonicalJson, encodeSigned, pickSignable, } from './worker-stub.js';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { canonicalJson, encodeSigned, pickSignable } from '../issued/encoding.js';
|
|
2
|
+
import type { VerifyKeys, WorkerClient, WorkerErrorCode } from '../worker-contract.js';
|
|
3
|
+
/**
|
|
4
|
+
* An in-process stand-in for the unwrap Worker (`packages/worker`), for
|
|
5
|
+
* every host test that needs a `WorkerClient<TGrant>` without a live
|
|
6
|
+
* Worker. Implements the full contract -- `wrap`/`unwrap`/`rewrap` for
|
|
7
|
+
* `./held`, `signRow`/`resign`/`recordEviction` for `./issued` -- with the
|
|
8
|
+
* package's own real signing, AAD and sealing code, not a reimplementation:
|
|
9
|
+
* it imports `encodeSigned`/`canonicalJson`/`pickSignable` from `../issued/
|
|
10
|
+
* encoding.js`, `encodeAad` from `../held/aad.js`, and `aesGcmSeal`/
|
|
11
|
+
* `aesGcmOpen` from `../held/seal.js` -- the same module
|
|
12
|
+
* `packages/worker/src/crypto.ts` imports for its own AES-256-GCM layout
|
|
13
|
+
* (#86: before this, the two copies could drift silently) -- rather than
|
|
14
|
+
* duplicating any of them, so a signature or a sealed key this stub
|
|
15
|
+
* produces is verified the same way a real Worker's would be -- proven by
|
|
16
|
+
* `worker-stub.test.ts`, which runs `check()` (`../issued`) and `open()`
|
|
17
|
+
* (`../held`) against rows this stub signed and sealed, and by
|
|
18
|
+
* `../../../worker/src/crypto.test.ts`, which opens a value sealed through
|
|
19
|
+
* this same module with the Worker's own re-exported `aesGcmOpen`, and the
|
|
20
|
+
* reverse.
|
|
21
|
+
*
|
|
22
|
+
* This is what every host's own `tests/keys-worker-stub.ts` re-implemented
|
|
23
|
+
* by hand (issue #83, "checked by hand against dist/issued/index.d.ts") --
|
|
24
|
+
* see the package README's "Adopting ./testing".
|
|
25
|
+
*
|
|
26
|
+
* Signing uses `node:crypto`'s classic, synchronous `generateKeyPairSync`/
|
|
27
|
+
* `sign`/`verify` rather than `webcrypto.subtle`: both produce and check
|
|
28
|
+
* the same RFC 8032 Ed25519 signatures -- `worker-stub.test.ts` proves it
|
|
29
|
+
* by running `../issued/verify.js`'s own `verifyIssuedRowSignature`
|
|
30
|
+
* (which uses `webcrypto.subtle.verify`) against a signature this stub
|
|
31
|
+
* produced -- but being synchronous, it lets `verifyKeys`/`verifyKeysEnv`
|
|
32
|
+
* be plain properties instead of a promise a caller would have to await
|
|
33
|
+
* before building `KEYS_VERIFY_KEYS`.
|
|
34
|
+
*/
|
|
35
|
+
/** Every credential this stub accepts. Anything else is `credential_invalid`, exactly as a real bad bearer token would be. */
|
|
36
|
+
export declare const TEST_CREDENTIAL = "stub-test-credential";
|
|
37
|
+
type Method = keyof WorkerClient<unknown>;
|
|
38
|
+
export interface CreateKeysWorkerStubOptions {
|
|
39
|
+
/** Unix milliseconds. Defaults to `Date.now`; a test passes a fake clock to check `minter_expired`. */
|
|
40
|
+
readonly now?: () => number;
|
|
41
|
+
}
|
|
42
|
+
export interface KeysWorkerStub<TGrant> extends WorkerClient<TGrant> {
|
|
43
|
+
/** This stub's one Ed25519 public key, for `createCredentialIssuer`'s `verifyKeys` option. */
|
|
44
|
+
readonly verifyKeys: VerifyKeys;
|
|
45
|
+
/** The same key, base64url, in the comma-separated form `KEYS_VERIFY_KEYS` takes. */
|
|
46
|
+
readonly verifyKeysEnv: string;
|
|
47
|
+
/** Calls received so far, per method -- for asserting a code path called the Worker exactly once, or not at all. */
|
|
48
|
+
readonly callCounts: Record<Method, number>;
|
|
49
|
+
/** Every call to `method` (or `'all'`) fails with `code` until `clearFailure()`. */
|
|
50
|
+
failAs(method: Method | 'all', code: WorkerErrorCode, retryAfterMs?: number): void;
|
|
51
|
+
clearFailure(): void;
|
|
52
|
+
}
|
|
53
|
+
/** `canonicalJson`, `encodeSigned` and `pickSignable` are the package's own -- re-exported by `./testing/index.js`, not reimplemented here. */
|
|
54
|
+
export declare function createKeysWorkerStub<TGrant = unknown>(options?: CreateKeysWorkerStubOptions): KeysWorkerStub<TGrant>;
|
|
55
|
+
export { canonicalJson, encodeSigned, pickSignable };
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { generateKeyPairSync, sign as signWithKey, verify as verifyWithKey, webcrypto, } from 'node:crypto';
|
|
2
|
+
import { encodeAad } from '../held/aad.js';
|
|
3
|
+
import { aesGcmOpen, aesGcmSeal } from '../held/seal.js';
|
|
4
|
+
import { canonicalJson, encodeSigned, pickSignable } from '../issued/encoding.js';
|
|
5
|
+
/**
|
|
6
|
+
* An in-process stand-in for the unwrap Worker (`packages/worker`), for
|
|
7
|
+
* every host test that needs a `WorkerClient<TGrant>` without a live
|
|
8
|
+
* Worker. Implements the full contract -- `wrap`/`unwrap`/`rewrap` for
|
|
9
|
+
* `./held`, `signRow`/`resign`/`recordEviction` for `./issued` -- with the
|
|
10
|
+
* package's own real signing, AAD and sealing code, not a reimplementation:
|
|
11
|
+
* it imports `encodeSigned`/`canonicalJson`/`pickSignable` from `../issued/
|
|
12
|
+
* encoding.js`, `encodeAad` from `../held/aad.js`, and `aesGcmSeal`/
|
|
13
|
+
* `aesGcmOpen` from `../held/seal.js` -- the same module
|
|
14
|
+
* `packages/worker/src/crypto.ts` imports for its own AES-256-GCM layout
|
|
15
|
+
* (#86: before this, the two copies could drift silently) -- rather than
|
|
16
|
+
* duplicating any of them, so a signature or a sealed key this stub
|
|
17
|
+
* produces is verified the same way a real Worker's would be -- proven by
|
|
18
|
+
* `worker-stub.test.ts`, which runs `check()` (`../issued`) and `open()`
|
|
19
|
+
* (`../held`) against rows this stub signed and sealed, and by
|
|
20
|
+
* `../../../worker/src/crypto.test.ts`, which opens a value sealed through
|
|
21
|
+
* this same module with the Worker's own re-exported `aesGcmOpen`, and the
|
|
22
|
+
* reverse.
|
|
23
|
+
*
|
|
24
|
+
* This is what every host's own `tests/keys-worker-stub.ts` re-implemented
|
|
25
|
+
* by hand (issue #83, "checked by hand against dist/issued/index.d.ts") --
|
|
26
|
+
* see the package README's "Adopting ./testing".
|
|
27
|
+
*
|
|
28
|
+
* Signing uses `node:crypto`'s classic, synchronous `generateKeyPairSync`/
|
|
29
|
+
* `sign`/`verify` rather than `webcrypto.subtle`: both produce and check
|
|
30
|
+
* the same RFC 8032 Ed25519 signatures -- `worker-stub.test.ts` proves it
|
|
31
|
+
* by running `../issued/verify.js`'s own `verifyIssuedRowSignature`
|
|
32
|
+
* (which uses `webcrypto.subtle.verify`) against a signature this stub
|
|
33
|
+
* produced -- but being synchronous, it lets `verifyKeys`/`verifyKeysEnv`
|
|
34
|
+
* be plain properties instead of a promise a caller would have to await
|
|
35
|
+
* before building `KEYS_VERIFY_KEYS`.
|
|
36
|
+
*/
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// The stub
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
/** Every credential this stub accepts. Anything else is `credential_invalid`, exactly as a real bad bearer token would be. */
|
|
41
|
+
export const TEST_CREDENTIAL = 'stub-test-credential';
|
|
42
|
+
const KEK_ID = 'kek-test';
|
|
43
|
+
const SIGN_GENERATION_ID = 'sign-test';
|
|
44
|
+
function failure(code, retryAfterMs) {
|
|
45
|
+
return {
|
|
46
|
+
ok: false,
|
|
47
|
+
error: retryAfterMs !== undefined ? { code, retryAfterMs } : { code },
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function publicKeyRawOf(key) {
|
|
51
|
+
const jwk = key.export({ format: 'jwk' });
|
|
52
|
+
if (!jwk.x) {
|
|
53
|
+
throw new Error('createKeysWorkerStub: Ed25519 public key export had no JWK x coordinate');
|
|
54
|
+
}
|
|
55
|
+
return new Uint8Array(Buffer.from(jwk.x, 'base64url'));
|
|
56
|
+
}
|
|
57
|
+
/** `canonicalJson`, `encodeSigned` and `pickSignable` are the package's own -- re-exported by `./testing/index.js`, not reimplemented here. */
|
|
58
|
+
export function createKeysWorkerStub(options = {}) {
|
|
59
|
+
const now = options.now ?? (() => Date.now());
|
|
60
|
+
const { publicKey, privateKey } = generateKeyPairSync('ed25519');
|
|
61
|
+
const publicKeyRaw = publicKeyRawOf(publicKey);
|
|
62
|
+
const kek = webcrypto.getRandomValues(new Uint8Array(32));
|
|
63
|
+
const callCounts = {
|
|
64
|
+
wrap: 0,
|
|
65
|
+
unwrap: 0,
|
|
66
|
+
rewrap: 0,
|
|
67
|
+
signRow: 0,
|
|
68
|
+
resign: 0,
|
|
69
|
+
recordEviction: 0,
|
|
70
|
+
};
|
|
71
|
+
let forced = null;
|
|
72
|
+
function record(method) {
|
|
73
|
+
callCounts[method] += 1;
|
|
74
|
+
if (forced && (forced.method === method || forced.method === 'all')) {
|
|
75
|
+
return failure(forced.code, forced.retryAfterMs);
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
function signRowBytes(row) {
|
|
80
|
+
return new Uint8Array(signWithKey(null, encodeSigned('issued-row', pickSignable(row)), privateKey));
|
|
81
|
+
}
|
|
82
|
+
function verifyRowSignature(row, signature) {
|
|
83
|
+
return verifyWithKey(null, encodeSigned('issued-row', pickSignable(row)), publicKey, signature);
|
|
84
|
+
}
|
|
85
|
+
const client = {
|
|
86
|
+
async wrap(req) {
|
|
87
|
+
const forcedFailure = record('wrap');
|
|
88
|
+
if (forcedFailure)
|
|
89
|
+
return forcedFailure;
|
|
90
|
+
if (req.credential !== TEST_CREDENTIAL)
|
|
91
|
+
return failure('credential_invalid');
|
|
92
|
+
const dataKey = webcrypto.getRandomValues(new Uint8Array(32));
|
|
93
|
+
const wrappedKey = await aesGcmSeal(kek, dataKey, encodeAad(req.binding));
|
|
94
|
+
return { ok: true, dataKey, wrappedKey, kekId: KEK_ID };
|
|
95
|
+
},
|
|
96
|
+
async unwrap(req) {
|
|
97
|
+
const forcedFailure = record('unwrap');
|
|
98
|
+
if (forcedFailure)
|
|
99
|
+
return forcedFailure;
|
|
100
|
+
if (req.credential !== TEST_CREDENTIAL)
|
|
101
|
+
return failure('credential_invalid');
|
|
102
|
+
if (req.kekId !== KEK_ID)
|
|
103
|
+
return failure('unknown_generation');
|
|
104
|
+
const dataKey = await aesGcmOpen(kek, req.wrappedKey, encodeAad(req.binding));
|
|
105
|
+
if (!dataKey)
|
|
106
|
+
return failure('aad_mismatch');
|
|
107
|
+
return { ok: true, dataKey };
|
|
108
|
+
},
|
|
109
|
+
async rewrap(req) {
|
|
110
|
+
const forcedFailure = record('rewrap');
|
|
111
|
+
if (forcedFailure)
|
|
112
|
+
return forcedFailure;
|
|
113
|
+
if (req.credential !== TEST_CREDENTIAL)
|
|
114
|
+
return failure('credential_invalid');
|
|
115
|
+
if (req.kekIdOld !== KEK_ID || req.kekIdNew !== KEK_ID)
|
|
116
|
+
return failure('unknown_generation');
|
|
117
|
+
const aad = encodeAad(req.binding);
|
|
118
|
+
const dataKey = await aesGcmOpen(kek, req.wrappedKey, aad);
|
|
119
|
+
if (!dataKey)
|
|
120
|
+
return failure('aad_mismatch');
|
|
121
|
+
const wrappedKey = await aesGcmSeal(kek, dataKey, aad);
|
|
122
|
+
return { ok: true, wrappedKey };
|
|
123
|
+
},
|
|
124
|
+
async signRow(req) {
|
|
125
|
+
const forcedFailure = record('signRow');
|
|
126
|
+
if (forcedFailure)
|
|
127
|
+
return forcedFailure;
|
|
128
|
+
if (req.credential !== TEST_CREDENTIAL)
|
|
129
|
+
return failure('credential_invalid');
|
|
130
|
+
const { candidate, minter } = req;
|
|
131
|
+
if (minter) {
|
|
132
|
+
if (!verifyRowSignature(minter.row, minter.signature)) {
|
|
133
|
+
return failure('bad_minter_signature');
|
|
134
|
+
}
|
|
135
|
+
if (minter.row.expiresAt <= now())
|
|
136
|
+
return failure('minter_expired');
|
|
137
|
+
}
|
|
138
|
+
// Minimal lineage check -- the mint log and resign/recordEviction
|
|
139
|
+
// sweeps a real Worker keeps are out of this stub's scope.
|
|
140
|
+
const expectedIssuedById = minter ? minter.row.id : null;
|
|
141
|
+
if (candidate.issuedById !== expectedIssuedById)
|
|
142
|
+
return failure('lineage_mismatch');
|
|
143
|
+
return { ok: true, signature: signRowBytes(candidate), generationId: SIGN_GENERATION_ID };
|
|
144
|
+
},
|
|
145
|
+
async resign(req) {
|
|
146
|
+
const forcedFailure = record('resign');
|
|
147
|
+
if (forcedFailure)
|
|
148
|
+
return forcedFailure;
|
|
149
|
+
if (req.credential !== TEST_CREDENTIAL)
|
|
150
|
+
return failure('credential_invalid');
|
|
151
|
+
if (req.oldGenerationId !== SIGN_GENERATION_ID)
|
|
152
|
+
return failure('unknown_generation');
|
|
153
|
+
if (!verifyRowSignature(req.row, req.oldSignature))
|
|
154
|
+
return failure('bad_old_signature');
|
|
155
|
+
return { ok: true, signature: signRowBytes(req.row), generationId: SIGN_GENERATION_ID };
|
|
156
|
+
},
|
|
157
|
+
async recordEviction(req) {
|
|
158
|
+
const forcedFailure = record('recordEviction');
|
|
159
|
+
if (forcedFailure)
|
|
160
|
+
return forcedFailure;
|
|
161
|
+
if (req.credential !== TEST_CREDENTIAL)
|
|
162
|
+
return failure('credential_invalid');
|
|
163
|
+
return { ok: true, receiptId: `receipt-${req.credentialId}-${req.revokedAt}` };
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
return Object.assign(client, {
|
|
167
|
+
verifyKeys: [publicKeyRaw],
|
|
168
|
+
verifyKeysEnv: Buffer.from(publicKeyRaw).toString('base64url'),
|
|
169
|
+
callCounts,
|
|
170
|
+
failAs(method, code, retryAfterMs) {
|
|
171
|
+
forced = { method, code, retryAfterMs };
|
|
172
|
+
},
|
|
173
|
+
clearFailure() {
|
|
174
|
+
forced = null;
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
// Re-exported by ./testing/index.js so a host's own fixture code can build
|
|
179
|
+
// custom signable bytes without depending on this file's internals.
|
|
180
|
+
export { canonicalJson, encodeSigned, pickSignable };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wtfalch/keys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "The estate's own bearer keys, issued to callers and held on their behalf: two entries, issued and held, nothing stored in common.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
"types": "./dist/webhooks.d.ts",
|
|
29
29
|
"default": "./dist/webhooks.js"
|
|
30
30
|
},
|
|
31
|
+
"./migrations-dir": {
|
|
32
|
+
"types": "./dist/migrations-dir.d.ts",
|
|
33
|
+
"default": "./dist/migrations-dir.js"
|
|
34
|
+
},
|
|
31
35
|
"./migrations/*.sql": "./dist/migrations/*.sql",
|
|
32
36
|
"./package.json": "./package.json",
|
|
33
37
|
"./worker": {
|
|
@@ -37,6 +41,14 @@
|
|
|
37
41
|
"./build": {
|
|
38
42
|
"types": "./dist/build/index.d.ts",
|
|
39
43
|
"default": "./dist/build/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./host": {
|
|
46
|
+
"types": "./dist/host/index.d.ts",
|
|
47
|
+
"default": "./dist/host/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./testing": {
|
|
50
|
+
"types": "./dist/testing/index.d.ts",
|
|
51
|
+
"default": "./dist/testing/index.js"
|
|
40
52
|
}
|
|
41
53
|
},
|
|
42
54
|
"sideEffects": false,
|
|
@@ -47,18 +59,19 @@
|
|
|
47
59
|
"node": ">=22.0.0"
|
|
48
60
|
},
|
|
49
61
|
"scripts": {
|
|
50
|
-
"build": "rm -rf dist && tsc -p tsconfig.build.json && mkdir -p dist/migrations &&
|
|
62
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && mkdir -p dist/migrations && cp -R src/migrations/issued src/migrations/held src/migrations/host dist/migrations/",
|
|
51
63
|
"prepack": "pnpm build",
|
|
52
64
|
"typecheck": "tsc --noEmit",
|
|
53
65
|
"test": "vitest run"
|
|
54
66
|
},
|
|
55
67
|
"peerDependencies": {
|
|
56
|
-
"drizzle-orm": ">=0.45.2"
|
|
68
|
+
"drizzle-orm": ">=0.45.2 <1.0.0"
|
|
57
69
|
},
|
|
58
70
|
"devDependencies": {
|
|
59
71
|
"@electric-sql/pglite": "^0.5.8",
|
|
60
72
|
"@types/node": "^22",
|
|
61
|
-
"
|
|
73
|
+
"@wtfalch/db": "0.5.2",
|
|
74
|
+
"drizzle-orm": "0.45.2",
|
|
62
75
|
"postgres": "^3.4.5",
|
|
63
76
|
"typescript": "^5.9.0",
|
|
64
77
|
"vitest": "^4.1.6"
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
-- #47: a built-in `environment` on every issued credential, so a host's
|
|
2
|
-
-- sandbox/live split is a structured field rather than a convention the host
|
|
3
|
-
-- invents on top of `key_prefix`. Additive; do not edit copied migrations.
|
|
4
|
-
--
|
|
5
|
-
-- Not part of SignableRow (worker-contract.ts): the Worker's signature never
|
|
6
|
-
-- covers it, same footing as idempotency_key and created_at. What keeps it
|
|
7
|
-
-- trustworthy despite that is keys_issued_guard() below, extended to refuse
|
|
8
|
-
-- any UPDATE that changes it -- set once at issue(), for that row's life.
|
|
9
|
-
--
|
|
10
|
-
-- Backfilled 'live' on existing rows: every row minted before this migration
|
|
11
|
-
-- was minted under a single, unqualified prefix, which today's default
|
|
12
|
-
-- environment ('live') matches.
|
|
13
|
-
alter table keys_issued_credentials
|
|
14
|
-
add column if not exists environment text not null default 'live';
|
|
15
|
-
alter table keys_issued_credentials alter column environment drop default;
|
|
16
|
-
|
|
17
|
-
do $$
|
|
18
|
-
begin
|
|
19
|
-
if not exists (
|
|
20
|
-
select 1 from pg_constraint
|
|
21
|
-
where conname = 'keys_issued_credentials_environment_check'
|
|
22
|
-
) then
|
|
23
|
-
alter table keys_issued_credentials
|
|
24
|
-
add constraint keys_issued_credentials_environment_check
|
|
25
|
-
check (environment in ('live', 'test'));
|
|
26
|
-
end if;
|
|
27
|
-
end
|
|
28
|
-
$$;
|
|
29
|
-
|
|
30
|
-
-- Extends 0004's keys_issued_guard(): identity, lineage AND environment are
|
|
31
|
-
-- immutable once a row is inserted.
|
|
32
|
-
create or replace function public.keys_issued_guard() returns trigger
|
|
33
|
-
language plpgsql
|
|
34
|
-
set search_path = pg_catalog, public, pg_temp
|
|
35
|
-
as $$
|
|
36
|
-
begin
|
|
37
|
-
if tg_op in ('DELETE', 'TRUNCATE') then
|
|
38
|
-
raise exception 'keys_issued_credentials: delete and truncate refused';
|
|
39
|
-
end if;
|
|
40
|
-
if tg_op = 'UPDATE' then
|
|
41
|
-
if new.id is distinct from old.id or new.issued_by_id is distinct from old.issued_by_id
|
|
42
|
-
or new.key_prefix is distinct from old.key_prefix
|
|
43
|
-
or new.environment is distinct from old.environment then
|
|
44
|
-
raise exception 'keys_issued_credentials: identity, lineage and environment are immutable';
|
|
45
|
-
end if;
|
|
46
|
-
if old.revoked_at is not null and new.revoked_at is distinct from old.revoked_at then
|
|
47
|
-
raise exception 'keys_issued_credentials: revocation is permanent';
|
|
48
|
-
end if;
|
|
49
|
-
end if;
|
|
50
|
-
return new;
|
|
51
|
-
end
|
|
52
|
-
$$;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|