@ultimat3/core 22.5.0 → 22.5.1
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/CLAUDE.md +5 -2
- package/README.md +1 -0
- package/package.json +2 -2
- package/src/canonical-json.ts +4 -0
- package/src/cursor.ts +8 -0
- package/src/index.ts +6 -0
- package/src/keyed-fingerprint.ts +69 -0
package/CLAUDE.md
CHANGED
|
@@ -95,9 +95,12 @@ top-level `UltimateError` use in `error-codes.ts`.
|
|
|
95
95
|
derives their retry classification from the same set. It is a `SIDE_EFFECTS_ANCHORS` entry.
|
|
96
96
|
- `timing-safe-equal.ts` is the one constant-time comparison (`@ultimat3/auth`, `@ultimat3/storage`).
|
|
97
97
|
- **`canonical-json.ts`: `canonicalJson` is INJECTIVE and `fingerprint` is SHA-256/16 of it** — the
|
|
98
|
-
hash every sharing key is taken over (`
|
|
99
|
-
`
|
|
98
|
+
hash every in-memory sharing key is taken over (`query`'s `queryHash`, `realtime`'s `qid`).
|
|
99
|
+
`NaN`, `±Infinity` and `-0` are bare tokens; `Date`, `Map` and `Set` are TAGGED. Never a
|
|
100
100
|
fourth copy, never parseable (`@ultimat3/action`'s `stableStringify` is the document form).
|
|
101
|
+
- **A PERSISTED fingerprint is `keyedFingerprint`** (`keyed-fingerprint.ts`: HMAC under a per-purpose
|
|
102
|
+
key derived from the cursor secret, `h1:` versioned) — `action`'s idempotency `requestHash`. An
|
|
103
|
+
unkeyed prefix in a table is an offline oracle for a short account number in the input.
|
|
101
104
|
- **`decimal-order.ts`'s `compareDecimalText` answers `undefined` for a non-decimal**, and only a
|
|
102
105
|
caller that knows the column's kind may ask (`@ultimat3/entity`'s `compareByKind`) — never
|
|
103
106
|
`@ultimat3/query`, whose `OrderKey` has no kind.
|
package/README.md
CHANGED
|
@@ -498,6 +498,7 @@ never a silently wrong page.
|
|
|
498
498
|
|---|---|
|
|
499
499
|
| Signature | truncated HMAC-SHA256, compared in constant time |
|
|
500
500
|
| Secret | `configureCursorSigning()` at boot, else `ULTIMATE_CURSOR_SECRET`. **Read when a cursor is signed, never at import** — an app whose `openSecrets()` sets the variable during boot would otherwise sign every cursor with the dev key. Rotating it invalidates every open cursor |
|
|
501
|
+
| Also keys | `keyedFingerprint(value, purpose)` — `h1:<key id>:<HMAC>` over `canonicalJson`, under a per-purpose key derived from this secret; the fingerprint to PERSIST (`@ultimat3/action`'s idempotency `requestHash`). `compareFingerprint` answers `match` / `mismatch` / `unverifiable` (other key), and still checks a legacy bare `fingerprint()` exactly. Rotating the secret makes in-window stored fingerprints `unverifiable` |
|
|
501
502
|
| Signed, not encrypted | the client already has these rows; what it must not do is *invent* a position |
|
|
502
503
|
| `usesDevCursorSecret()` | true while the shipped dev key is in use |
|
|
503
504
|
| `resetCursorSigning()` | test seam: forget `configureCursorSigning` and fall back to the environment |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/core",
|
|
3
|
-
"version": "22.5.
|
|
3
|
+
"version": "22.5.1",
|
|
4
4
|
"description": "Ultimate's foundation: errors, context, env, config, clock, ids, logging, telemetry, lifecycle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -40,6 +40,6 @@
|
|
|
40
40
|
"test": "bun test"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@ultimat3/schema": "22.5.
|
|
43
|
+
"@ultimat3/schema": "22.5.1"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/canonical-json.ts
CHANGED
|
@@ -119,6 +119,10 @@ export function canonicalJson(value: unknown): string {
|
|
|
119
119
|
* hands one caller another's rows. FNV-1a/32, which two of the three copies started as, is
|
|
120
120
|
* 4x10^9 values and brute-forceable offline in seconds: an input landing on another read's key was
|
|
121
121
|
* something an attacker could mint rather than something they had to wait for.
|
|
122
|
+
*
|
|
123
|
+
* UNKEYED, so never the thing to PERSIST beside input that may hold a low-entropy secret — a
|
|
124
|
+
* 64-bit prefix of a known hash is an offline oracle for a short account number. A stored
|
|
125
|
+
* fingerprint is `keyedFingerprint` (`keyed-fingerprint.ts`).
|
|
122
126
|
*/
|
|
123
127
|
export function fingerprint(value: unknown): string {
|
|
124
128
|
return new Bun.CryptoHasher('sha256').update(canonicalJson(value)).digest('hex').slice(0, 16);
|
package/src/cursor.ts
CHANGED
|
@@ -52,6 +52,14 @@ function currentSecret(): string {
|
|
|
52
52
|
return configured ?? Bun.env['ULTIMATE_CURSOR_SECRET'] ?? DEV_SECRET;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The app's signing secret, exactly as cursor signing reads it. Internal to `@ultimat3/core`:
|
|
57
|
+
* `keyed-fingerprint.ts` derives its own key from it, so an app has one secret to set, not two.
|
|
58
|
+
*/
|
|
59
|
+
export function currentSigningSecret(): string {
|
|
60
|
+
return currentSecret();
|
|
61
|
+
}
|
|
62
|
+
|
|
55
63
|
/** Set once at boot from the app secret. Rotating it invalidates every open cursor. */
|
|
56
64
|
export function configureCursorSigning(next: string): void {
|
|
57
65
|
configured = next;
|
package/src/index.ts
CHANGED
|
@@ -509,6 +509,12 @@ export {
|
|
|
509
509
|
} from './intl-cache';
|
|
510
510
|
export { isIsoDateTime } from './iso-date';
|
|
511
511
|
export { isJsonObject } from './json-object';
|
|
512
|
+
export type { FingerprintMatch } from './keyed-fingerprint';
|
|
513
|
+
export {
|
|
514
|
+
compareFingerprint,
|
|
515
|
+
KEYED_FINGERPRINT_VERSION,
|
|
516
|
+
keyedFingerprint,
|
|
517
|
+
} from './keyed-fingerprint';
|
|
512
518
|
export type {
|
|
513
519
|
HealthPayload,
|
|
514
520
|
HealthReport,
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Single responsibility: a fingerprint of caller input that is safe to PERSIST. `fingerprint()` is
|
|
2
|
+
// an unkeyed SHA-256 prefix — fine as an in-memory sharing key, but stored beside a row it is an
|
|
3
|
+
// offline oracle: a 6–20-digit account number or an ID number in an input, with the other fields
|
|
4
|
+
// known, is brute-forced from a database read. Keyed with HMAC-SHA-256, the stored value proves
|
|
5
|
+
// nothing to anyone without the app's signing secret.
|
|
6
|
+
|
|
7
|
+
import { canonicalJson, fingerprint } from './canonical-json';
|
|
8
|
+
import { currentSigningSecret } from './cursor';
|
|
9
|
+
import { timingSafeEqual } from './timing-safe-equal';
|
|
10
|
+
|
|
11
|
+
/** The format tag. A stored value without it predates keying (see `compareFingerprint`). */
|
|
12
|
+
export const KEYED_FINGERPRINT_VERSION = 'h1';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What a stored fingerprint says about an input:
|
|
16
|
+
* - `match` / `mismatch` — computed under the key in force now (or the legacy unkeyed form);
|
|
17
|
+
* - `unverifiable` — keyed under a secret this process does not hold (a rotation), or a format this
|
|
18
|
+
* build does not know. The input can be neither confirmed nor refused.
|
|
19
|
+
*/
|
|
20
|
+
export type FingerprintMatch = 'match' | 'mismatch' | 'unverifiable';
|
|
21
|
+
|
|
22
|
+
const LEGACY = /^[0-9a-f]{16}$/;
|
|
23
|
+
|
|
24
|
+
/** One key per purpose, derived from the signing secret, so no two uses share a MAC key. */
|
|
25
|
+
function derivedKey(purpose: string): string {
|
|
26
|
+
return new Bun.CryptoHasher('sha256', currentSigningSecret())
|
|
27
|
+
.update(`ultimate:keyed-fingerprint:${purpose}`)
|
|
28
|
+
.digest('hex');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Names the key a fingerprint was made under, so a rotation reads as `unverifiable` rather than as
|
|
33
|
+
* a mismatch. 32 bits of a hash of a 256-bit derived key: a label, useless for recovering it.
|
|
34
|
+
*/
|
|
35
|
+
function keyId(key: string): string {
|
|
36
|
+
return new Bun.CryptoHasher('sha256').update(key).digest('hex').slice(0, 8);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* `h1:<key id>:<HMAC-SHA-256 over canonical JSON, 128 bits>`. `purpose` separates uses: the same
|
|
41
|
+
* input fingerprints differently for two purposes. Under the shipped development secret this is as
|
|
42
|
+
* public as `fingerprint()` — production boot refuses that secret (`X_CURSOR_SECRET_DEV`).
|
|
43
|
+
*/
|
|
44
|
+
export function keyedFingerprint(value: unknown, purpose: string): string {
|
|
45
|
+
const key = derivedKey(purpose);
|
|
46
|
+
const mac = new Bun.CryptoHasher('sha256', key)
|
|
47
|
+
.update(canonicalJson(value))
|
|
48
|
+
.digest('hex')
|
|
49
|
+
.slice(0, 32);
|
|
50
|
+
return `${KEYED_FINGERPRINT_VERSION}:${keyId(key)}:${mac}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Compare a STORED fingerprint with an input. A legacy unkeyed value (16 hex characters, written by
|
|
55
|
+
* a build before keying) is checked against `fingerprint()` computed here and never stored, so rows
|
|
56
|
+
* written across an upgrade keep their exact semantics until they age out.
|
|
57
|
+
*/
|
|
58
|
+
export function compareFingerprint(
|
|
59
|
+
stored: string,
|
|
60
|
+
value: unknown,
|
|
61
|
+
purpose: string,
|
|
62
|
+
): FingerprintMatch {
|
|
63
|
+
if (LEGACY.test(stored))
|
|
64
|
+
return timingSafeEqual(stored, fingerprint(value)) ? 'match' : 'mismatch';
|
|
65
|
+
const current = keyedFingerprint(value, purpose);
|
|
66
|
+
const prefix = current.slice(0, current.lastIndexOf(':') + 1);
|
|
67
|
+
if (!stored.startsWith(prefix)) return 'unverifiable';
|
|
68
|
+
return timingSafeEqual(stored, current) ? 'match' : 'mismatch';
|
|
69
|
+
}
|