@coffre/client 0.12.0 → 0.13.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.
@@ -89,6 +89,28 @@ declare function canonicalHtu(input: string | URL): string;
89
89
  declare function fetchMethod(method: string): string;
90
90
  /** The SHA-256 of a buffered body, in hex, as a canonical request's last line holds it. */
91
91
  declare function bodyDigest(body: Uint8Array | string): Promise<string>;
92
+ /**
93
+ * The most a body that declares its digest carries (`Content-Digest`),
94
+ * which coffre streams and `coffre serve` holds while it hashes it: 95 MiB,
95
+ * 99,614,720 bytes, under the 100 MB (decimal) Cloudflare takes in a
96
+ * request on Free and Pro zones, so a body near it gets coffre's answer and
97
+ * not the zone's.
98
+ */
99
+ declare const MAX_DIGESTED_BODY: number;
100
+ /** A body's `Content-Digest` (RFC 9530) from its SHA-256 in hex, as the SDK and `coffre serve` send it: `sha-256=:<base64>:`. */
101
+ declare function contentDigest(sha256: string): string;
102
+ /**
103
+ * The SHA-256 a `Content-Digest` declares, in hex, as a body's line takes
104
+ * it: its `sha-256` member's, among any others (RFC 9530 lets one header
105
+ * name several algorithms). Null for one with no `sha-256` member, an app's
106
+ * own `sha-512` say, which declares nothing to coffre. Throws on a
107
+ * `sha-256` member that is no SHA-256 (32 bytes, as base64 between colons),
108
+ * or one named twice: such a header binds no body, and stands for none.
109
+ *
110
+ * declaredDigest('sha-256=:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=:') 'e3b0c442…7852b855'
111
+ * declaredDigest('sha-512=:z4PhNX7v…:') null
112
+ */
113
+ declare function declaredDigest(header: string): string | null;
92
114
  /** `ath`: base64url SHA-256 of the session token the proof is sent with. */
93
115
  declare function tokenDigest(token: string): Promise<string>;
94
116
  /**
@@ -103,14 +125,17 @@ declare const UNSIGNED_BODY = "UNSIGNED-PAYLOAD";
103
125
  * each from what it knows of the body:
104
126
  *
105
127
  * - `declared`: its `x-amz-content-sha256`, given only through a credential
106
- * `sigv4` signs; null otherwise, or when it sets none.
128
+ * `sigv4` signs; else the SHA-256 its `Content-Digest` declares
129
+ * (`declaredDigest`); null when it sets neither.
107
130
  * - `length`: its bytes as known: those read, a stream's `content-length`,
108
131
  * or null for a stream of no stated length.
109
132
  *
110
133
  * A body of no bytes signs the empty body's digest, whatever it declares.
111
- * One that declares a hash signs it, which S3 checks against the bytes, so
112
- * it may stream: 64 lower-case hex digits, or `UNSIGNED_BODY` for any other
113
- * value. Every other body signs its bytes' digest (`bytes`, none for none).
134
+ * One that declares a hash signs it, which S3, or coffre for a
135
+ * `Content-Digest`, checks against the bytes, so it may stream: 64
136
+ * lower-case hex digits, or `UNSIGNED_BODY` for any other value, which only
137
+ * an `x-amz-content-sha256` can be. Every other body signs its bytes'
138
+ * digest (`bytes`, none for none).
114
139
  *
115
140
  * bodyLine({ declared: null, length: 6, bytes }) bodyDigest(bytes)
116
141
  * bodyLine({ declared: 'UNSIGNED-PAYLOAD', length: 0 }) bodyDigest('')
@@ -134,9 +159,11 @@ declare function bodyLine(body: {
134
159
  * behind one; this line signs it as it goes upstream, so `/use/s3/a/b` and
135
160
  * `/use/s3/a//b`, one object and another to S3, are two requests. A path a
136
161
  * zone changed fails closed. The body's line is its buffered bytes' digest
137
- * (`bodyDigest`), or, for a streamed upload through a credential `sigv4`
138
- * signs, the 64 hex digits its `x-amz-content-sha256` declares, which S3
139
- * checks against the bytes; any other declared value is `UNSIGNED_BODY`. A
162
+ * (`bodyDigest`), or, for a body that declares one (`bodyLine`), the 64 hex
163
+ * digits its `Content-Digest` declares, which coffre checks against the
164
+ * bytes as they stream, or, through a credential `sigv4` signs, those its
165
+ * `x-amz-content-sha256` declares, which S3 checks; any other value of the
166
+ * latter is `UNSIGNED_BODY`. A
140
167
  * header sent several times is one value, joined by `, `, as `Headers`
141
168
  * gives it. No line holds a line break, so no two requests read as one;
142
169
  * throws on one that does, or on a body line that is neither.
@@ -162,7 +189,7 @@ declare function signedHeaders(headers: HeadersInit): [string, string][];
162
189
  * - `token` adds `ath`, for a request made with a session; the exchange has none.
163
190
  * - `nonce` is the gate's newest, from the last answer's `DPoP-Nonce`.
164
191
  * - `content` adds `hdr` and `req`: the headers the app set, and the body's
165
- * line (`bodyDigest`, a streamed SigV4 upload's declared digest, or
192
+ * line (`bodyLine`: its bytes' digest, the one it declares, or
166
193
  * `UNSIGNED_BODY`).
167
194
  * Whatever it names is all coffre forwards.
168
195
  */
@@ -180,4 +207,4 @@ declare function createProof(key: ProofKey, request: {
180
207
  jti?: string;
181
208
  }): Promise<string>;
182
209
  //#endregion
183
- export { tokenDigest as S, generateProofKey as _, ProofKey as a, signedHeaders as b, UNSIGNED_HEADERS as c, bodyLine as d, canonicalHtu as f, fromBase64url as g, fetchMethod as h, ProofClaims as i, base64url as l, createProof as m, MAX_SIGNED_HEADERS as n, PublicJwk as o, canonicalRequest as p, METHOD_TOKEN as r, UNSIGNED_BODY as s, MAX_PROOF_LENGTH as t, bodyDigest as u, requestDigest as v, thumbprint as x, sentTarget as y };
210
+ export { signedHeaders as C, sentTarget as S, tokenDigest as T, declaredDigest as _, ProofClaims as a, generateProofKey as b, UNSIGNED_BODY as c, bodyDigest as d, bodyLine as f, createProof as g, contentDigest as h, METHOD_TOKEN as i, UNSIGNED_HEADERS as l, canonicalRequest as m, MAX_PROOF_LENGTH as n, ProofKey as o, canonicalHtu as p, MAX_SIGNED_HEADERS as r, PublicJwk as s, MAX_DIGESTED_BODY as t, base64url as u, fetchMethod as v, thumbprint as w, requestDigest as x, fromBase64url as y };
package/dist/dpop.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { S as tokenDigest, _ as generateProofKey, a as ProofKey, b as signedHeaders, c as UNSIGNED_HEADERS, d as bodyLine, f as canonicalHtu, g as fromBase64url, h as fetchMethod, i as ProofClaims, l as base64url, m as createProof, n as MAX_SIGNED_HEADERS, o as PublicJwk, p as canonicalRequest, r as METHOD_TOKEN, s as UNSIGNED_BODY, t as MAX_PROOF_LENGTH, u as bodyDigest, v as requestDigest, x as thumbprint, y as sentTarget } from "./dpop-C5Zyaixy.js";
2
- export { MAX_PROOF_LENGTH, MAX_SIGNED_HEADERS, METHOD_TOKEN, ProofClaims, ProofKey, PublicJwk, UNSIGNED_BODY, UNSIGNED_HEADERS, base64url, bodyDigest, bodyLine, canonicalHtu, canonicalRequest, createProof, fetchMethod, fromBase64url, generateProofKey, requestDigest, sentTarget, signedHeaders, thumbprint, tokenDigest };
1
+ import { C as signedHeaders, S as sentTarget, T as tokenDigest, _ as declaredDigest, a as ProofClaims, b as generateProofKey, c as UNSIGNED_BODY, d as bodyDigest, f as bodyLine, g as createProof, h as contentDigest, i as METHOD_TOKEN, l as UNSIGNED_HEADERS, m as canonicalRequest, n as MAX_PROOF_LENGTH, o as ProofKey, p as canonicalHtu, r as MAX_SIGNED_HEADERS, s as PublicJwk, t as MAX_DIGESTED_BODY, u as base64url, v as fetchMethod, w as thumbprint, x as requestDigest, y as fromBase64url } from "./dpop-CYJtAnvD.js";
2
+ export { MAX_DIGESTED_BODY, MAX_PROOF_LENGTH, MAX_SIGNED_HEADERS, METHOD_TOKEN, ProofClaims, ProofKey, PublicJwk, UNSIGNED_BODY, UNSIGNED_HEADERS, base64url, bodyDigest, bodyLine, canonicalHtu, canonicalRequest, contentDigest, createProof, declaredDigest, fetchMethod, fromBase64url, generateProofKey, requestDigest, sentTarget, signedHeaders, thumbprint, tokenDigest };
package/dist/dpop.js CHANGED
@@ -159,6 +159,38 @@ function fetchMethod(method) {
159
159
  async function bodyDigest(body) {
160
160
  return hex(await sha256(body));
161
161
  }
162
+ /**
163
+ * The most a body that declares its digest carries (`Content-Digest`),
164
+ * which coffre streams and `coffre serve` holds while it hashes it: 95 MiB,
165
+ * 99,614,720 bytes, under the 100 MB (decimal) Cloudflare takes in a
166
+ * request on Free and Pro zones, so a body near it gets coffre's answer and
167
+ * not the zone's.
168
+ */
169
+ const MAX_DIGESTED_BODY = 99614720;
170
+ /** A body's `Content-Digest` (RFC 9530) from its SHA-256 in hex, as the SDK and `coffre serve` send it: `sha-256=:<base64>:`. */
171
+ function contentDigest(sha256) {
172
+ if (!SHA256_HEX.test(sha256)) throw new TypeError("a Content-Digest is made from a SHA-256 in hex");
173
+ return `sha-256=:${btoa(String.fromCharCode(...sha256.match(/../g).map((pair) => parseInt(pair, 16))))}:`;
174
+ }
175
+ /**
176
+ * The SHA-256 a `Content-Digest` declares, in hex, as a body's line takes
177
+ * it: its `sha-256` member's, among any others (RFC 9530 lets one header
178
+ * name several algorithms). Null for one with no `sha-256` member, an app's
179
+ * own `sha-512` say, which declares nothing to coffre. Throws on a
180
+ * `sha-256` member that is no SHA-256 (32 bytes, as base64 between colons),
181
+ * or one named twice: such a header binds no body, and stands for none.
182
+ *
183
+ * declaredDigest('sha-256=:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=:') 'e3b0c442…7852b855'
184
+ * declaredDigest('sha-512=:z4PhNX7v…:') null
185
+ */
186
+ function declaredDigest(header) {
187
+ const members = header.split(",").map((member) => member.trim()).filter((member) => /^sha-256(?:[=;]|$)/.test(member));
188
+ if (members.length === 0) return null;
189
+ const base64 = members.length === 1 ? /^sha-256=:([A-Za-z0-9+/]{43}=):$/.exec(members[0])?.[1] : void 0;
190
+ const bytes = base64 === void 0 ? null : Uint8Array.from(atob(base64), (char) => char.charCodeAt(0));
191
+ if (bytes === null || btoa(String.fromCharCode(...bytes)) !== base64) throw new TypeError("Content-Digest declares a body's SHA-256 once, as sha-256=:<44 characters of base64>:");
192
+ return hex(bytes);
193
+ }
162
194
  /** `ath`: base64url SHA-256 of the session token the proof is sent with. */
163
195
  async function tokenDigest(token) {
164
196
  return base64url(await sha256(token));
@@ -176,14 +208,17 @@ const SHA256_HEX = /^[0-9a-f]{64}$/;
176
208
  * each from what it knows of the body:
177
209
  *
178
210
  * - `declared`: its `x-amz-content-sha256`, given only through a credential
179
- * `sigv4` signs; null otherwise, or when it sets none.
211
+ * `sigv4` signs; else the SHA-256 its `Content-Digest` declares
212
+ * (`declaredDigest`); null when it sets neither.
180
213
  * - `length`: its bytes as known: those read, a stream's `content-length`,
181
214
  * or null for a stream of no stated length.
182
215
  *
183
216
  * A body of no bytes signs the empty body's digest, whatever it declares.
184
- * One that declares a hash signs it, which S3 checks against the bytes, so
185
- * it may stream: 64 lower-case hex digits, or `UNSIGNED_BODY` for any other
186
- * value. Every other body signs its bytes' digest (`bytes`, none for none).
217
+ * One that declares a hash signs it, which S3, or coffre for a
218
+ * `Content-Digest`, checks against the bytes, so it may stream: 64
219
+ * lower-case hex digits, or `UNSIGNED_BODY` for any other value, which only
220
+ * an `x-amz-content-sha256` can be. Every other body signs its bytes'
221
+ * digest (`bytes`, none for none).
187
222
  *
188
223
  * bodyLine({ declared: null, length: 6, bytes }) bodyDigest(bytes)
189
224
  * bodyLine({ declared: 'UNSIGNED-PAYLOAD', length: 0 }) bodyDigest('')
@@ -206,9 +241,11 @@ async function bodyLine(body) {
206
241
  * behind one; this line signs it as it goes upstream, so `/use/s3/a/b` and
207
242
  * `/use/s3/a//b`, one object and another to S3, are two requests. A path a
208
243
  * zone changed fails closed. The body's line is its buffered bytes' digest
209
- * (`bodyDigest`), or, for a streamed upload through a credential `sigv4`
210
- * signs, the 64 hex digits its `x-amz-content-sha256` declares, which S3
211
- * checks against the bytes; any other declared value is `UNSIGNED_BODY`. A
244
+ * (`bodyDigest`), or, for a body that declares one (`bodyLine`), the 64 hex
245
+ * digits its `Content-Digest` declares, which coffre checks against the
246
+ * bytes as they stream, or, through a credential `sigv4` signs, those its
247
+ * `x-amz-content-sha256` declares, which S3 checks; any other value of the
248
+ * latter is `UNSIGNED_BODY`. A
212
249
  * header sent several times is one value, joined by `, `, as `Headers`
213
250
  * gives it. No line holds a line break, so no two requests read as one;
214
251
  * throws on one that does, or on a body line that is neither.
@@ -245,7 +282,7 @@ function signedHeaders(headers) {
245
282
  * - `token` adds `ath`, for a request made with a session; the exchange has none.
246
283
  * - `nonce` is the gate's newest, from the last answer's `DPoP-Nonce`.
247
284
  * - `content` adds `hdr` and `req`: the headers the app set, and the body's
248
- * line (`bodyDigest`, a streamed SigV4 upload's declared digest, or
285
+ * line (`bodyLine`: its bytes' digest, the one it declares, or
249
286
  * `UNSIGNED_BODY`).
250
287
  * Whatever it names is all coffre forwards.
251
288
  */
@@ -287,4 +324,4 @@ async function createProof(key, request) {
287
324
  return `${input}.${base64url(new Uint8Array(signature))}`;
288
325
  }
289
326
  //#endregion
290
- export { MAX_PROOF_LENGTH, MAX_SIGNED_HEADERS, METHOD_TOKEN, UNSIGNED_BODY, UNSIGNED_HEADERS, base64url, bodyDigest, bodyLine, canonicalHtu, canonicalRequest, createProof, fetchMethod, fromBase64url, generateProofKey, requestDigest, sentTarget, signedHeaders, thumbprint, tokenDigest };
327
+ export { MAX_DIGESTED_BODY, MAX_PROOF_LENGTH, MAX_SIGNED_HEADERS, METHOD_TOKEN, UNSIGNED_BODY, UNSIGNED_HEADERS, base64url, bodyDigest, bodyLine, canonicalHtu, canonicalRequest, contentDigest, createProof, declaredDigest, fetchMethod, fromBase64url, generateProofKey, requestDigest, sentTarget, signedHeaders, thumbprint, tokenDigest };
@@ -1710,6 +1710,15 @@ type Api = {
1710
1710
  revoked: true;
1711
1711
  };
1712
1712
  };
1713
+ "POST /audit/loss": {
1714
+ input: {
1715
+ from: number;
1716
+ to: number;
1717
+ };
1718
+ output: {
1719
+ resumes: number | null;
1720
+ };
1721
+ };
1713
1722
  };
1714
1723
  type AccessValue = string | null | {
1715
1724
  role: string;
@@ -3786,6 +3795,14 @@ declare function createClient(options: ClientOptions): {
3786
3795
  }[];
3787
3796
  };
3788
3797
  }>;
3798
+ /**
3799
+ * Accept that entries `from` to `to` are lost for good, as an Owner of
3800
+ * the whole instance: the audit watch's finding, inside the range, stays
3801
+ * in the log, and its anchor goes on past them.
3802
+ */
3803
+ acceptLoss: (range: RouteInput<"POST /audit/loss">) => Promise<{
3804
+ resumes: number | null;
3805
+ }>;
3789
3806
  };
3790
3807
  };
3791
3808
  type CoffreClient = ReturnType<typeof createClient>;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { $ as RolesView, A as Decision, B as KeysPlan, C as ApprovalView, D as BindingPlan, E as AuthorizationView, F as DryRunResult, G as MissingKey, H as Me, I as EnvironmentView, J as ProjectSummary, K as OffboardingReport, L as IdentityRow, M as DeletionResult, N as DismissedKey, O as BindingView, P as DryRunOutcome, Q as RoleView, R as ImpactLine, S as Api, T as AuthInfo, U as Member, V as ListedReference, W as MemberAccess, X as RemovedMember, Y as ReferenceView, Z as RoleImpact, _ as byFolder, a as Params, at as TokenRow, b as olderThan, c as RouteOutput, d as planImport, et as SecretKey, f as Unreachable, g as shownMember, h as serviceName, i as ImportAction, it as TokenPlan, j as Deletion, k as ConnectedApp, l as Transport, m as apiMember, n as CoffreClient, nt as SessionRow, o as RouteInput, ot as WorkloadIds, p as unreachable, q as PersonalConfig, r as CoffreError, rt as SetResult, s as RouteKey, t as ClientOptions, tt as SecretVersion, u as createClient, v as foldersOf, w as AuditEntryView, x as AccessValue, y as ageInDays, z as InheritResult } from "./index-DxSvWq3r.js";
1
+ import { $ as RolesView, A as Decision, B as KeysPlan, C as ApprovalView, D as BindingPlan, E as AuthorizationView, F as DryRunResult, G as MissingKey, H as Me, I as EnvironmentView, J as ProjectSummary, K as OffboardingReport, L as IdentityRow, M as DeletionResult, N as DismissedKey, O as BindingView, P as DryRunOutcome, Q as RoleView, R as ImpactLine, S as Api, T as AuthInfo, U as Member, V as ListedReference, W as MemberAccess, X as RemovedMember, Y as ReferenceView, Z as RoleImpact, _ as byFolder, a as Params, at as TokenRow, b as olderThan, c as RouteOutput, d as planImport, et as SecretKey, f as Unreachable, g as shownMember, h as serviceName, i as ImportAction, it as TokenPlan, j as Deletion, k as ConnectedApp, l as Transport, m as apiMember, n as CoffreClient, nt as SessionRow, o as RouteInput, ot as WorkloadIds, p as unreachable, q as PersonalConfig, r as CoffreError, rt as SetResult, s as RouteKey, t as ClientOptions, tt as SecretVersion, u as createClient, v as foldersOf, w as AuditEntryView, x as AccessValue, y as ageInDays, z as InheritResult } from "./index-eVeI8AOV.js";
2
2
  export { type AccessValue, type Api, type ApprovalView, type AuditEntryView, type AuthInfo, type AuthorizationView, type BindingPlan, type BindingView, ClientOptions, CoffreClient, CoffreError, type ConnectedApp, type Decision, type Deletion, type DeletionResult, type DismissedKey, type DryRunOutcome, type DryRunResult, type EnvironmentView, type IdentityRow, type ImpactLine, ImportAction, type InheritResult, type KeysPlan, type ListedReference, type Me, type Member, type MemberAccess, type MissingKey, type OffboardingReport, Params, type PersonalConfig, type ProjectSummary, type ReferenceView, type RemovedMember, type RoleImpact, type RoleView, type RolesView, RouteInput, RouteKey, RouteOutput, type SecretKey, type SecretVersion, type SessionRow, type SetResult, type TokenPlan, type TokenRow, Transport, Unreachable, type WorkloadIds, ageInDays, apiMember, byFolder, createClient, foldersOf, olderThan, planImport, serviceName, shownMember, unreachable };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { a as serviceName, c as foldersOf, d as Unreachable, f as unreachable, i as apiMember, l as ageInDays, n as createClient, o as shownMember, r as planImport, s as byFolder, t as CoffreError, u as olderThan } from "./src-CjUBSxZs.js";
1
+ import { a as serviceName, c as foldersOf, d as Unreachable, f as unreachable, i as apiMember, l as ageInDays, n as createClient, o as shownMember, r as planImport, s as byFolder, t as CoffreError, u as olderThan } from "./src-BCKSeCER.js";
2
2
  export { CoffreError, Unreachable, ageInDays, apiMember, byFolder, createClient, foldersOf, olderThan, planImport, serviceName, shownMember, unreachable };
package/dist/routes.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as Params, o as RouteInput, s as RouteKey } from "./index-DxSvWq3r.js";
1
+ import { a as Params, o as RouteInput, s as RouteKey } from "./index-eVeI8AOV.js";
2
2
  //#region src/routes.d.ts
3
3
  export type GetKey = Extract<RouteKey, `GET ${string}`>;
4
4
  /** Each GET route, with what to ask it. */
package/dist/routes.js CHANGED
@@ -142,7 +142,8 @@ const EVERY_ROUTE = {
142
142
  "DELETE /apps/:id": true,
143
143
  "GET /audit": true,
144
144
  "GET /audit/verification": true,
145
- "GET /audit/keys": true
145
+ "GET /audit/keys": true,
146
+ "POST /audit/loss": true
146
147
  };
147
148
  /**
148
149
  * Every route as a method and a URL, each parameter a made-up value: what
@@ -454,7 +454,13 @@ set: (member, access) => call("PATCH /access/:member", { member }, access) },
454
454
  }
455
455
  },
456
456
  /** What an escrowed key is checked against: public material, for admins and owners of the whole instance. */
457
- keys: () => call("GET /audit/keys", {})
457
+ keys: () => call("GET /audit/keys", {}),
458
+ /**
459
+ * Accept that entries `from` to `to` are lost for good, as an Owner of
460
+ * the whole instance: the audit watch's finding, inside the range, stays
461
+ * in the log, and its anchor goes on past them.
462
+ */
463
+ acceptLoss: (range) => call("POST /audit/loss", {}, range)
458
464
  }
459
465
  };
460
466
  }
@@ -1,4 +1,4 @@
1
- import { o as PublicJwk } from "./dpop-C5Zyaixy.js";
1
+ import { s as PublicJwk } from "./dpop-CYJtAnvD.js";
2
2
  //#region src/workload-proofs.d.ts
3
3
  /**
4
4
  * How a workload proves who it is, once per session (docs/design/workload-access.md,
@@ -246,5 +246,10 @@ declare function fromCoffre(coffre: Workload, minter: string): () => Promise<{
246
246
  sessionToken?: string;
247
247
  expiration: Date;
248
248
  }>;
249
+ /**
250
+ * Whether coffre refused a proof for its nonce alone (RFC 9449, section 8): sent again once, with the nonce it gave. A
251
+ * workload's `fetch` does so itself, but for a stream, which it cannot read twice: whoever holds its bytes may.
252
+ */
253
+ declare function askedNonce(response: Response): boolean;
249
254
  //#endregion
250
- export { vercel as C, oidc as S, fly as _, fromCoffre as a, keyAudience as b, workload as c, PlatformProof as d, Proof as f, enrollment as g, azure as h, WorkloadOptions as i, AwsCredentials as l, aws as m, UsedCredential as n, upgradeHeaders as o, ProofContext as p, Workload as r, upgradeRefused as s, NarrowOptions as t, KUBERNETES_TOKEN as u, gcp as v, kubernetes as x, github as y };
255
+ export { oidc as C, kubernetes as S, enrollment as _, askedNonce as a, github as b, upgradeRefused as c, KUBERNETES_TOKEN as d, PlatformProof as f, azure as g, aws as h, WorkloadOptions as i, workload as l, ProofContext as m, UsedCredential as n, fromCoffre as o, Proof as p, Workload as r, upgradeHeaders as s, NarrowOptions as t, AwsCredentials as u, fly as v, vercel as w, keyAudience as x, gcp as y };
@@ -1,5 +1,5 @@
1
- import { t as CoffreError } from "./src-CjUBSxZs.js";
2
- import { base64url, bodyDigest, bodyLine, createProof, generateProofKey, thumbprint } from "./dpop.js";
1
+ import { t as CoffreError } from "./src-BCKSeCER.js";
2
+ import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, generateProofKey, thumbprint } from "./dpop.js";
3
3
  //#region src/workload-proofs.ts
4
4
  /** `<audience>#jkt=<thumbprint>`: an audience only this key can use (section 4.2). */
5
5
  function keyAudience(context) {
@@ -648,7 +648,8 @@ function surface(core) {
648
648
  const url = input instanceof Request ? input.url : input instanceof URL ? input.href : String(input);
649
649
  if (!under(url)) return core.base(input, init);
650
650
  const request = new Request(input, init);
651
- const declared = request.headers.get("x-amz-content-sha256");
651
+ const own = request.headers.get("content-digest");
652
+ const declared = request.headers.get("x-amz-content-sha256") ?? (own === null ? null : declaredDigest(own));
652
653
  const streamed = init?.body instanceof ReadableStream || input instanceof Request && init?.body === void 0;
653
654
  const stream = declared !== null && request.body !== null && streamed ? request.body : null;
654
655
  const bytes = stream === null && request.body !== null ? new Uint8Array(await request.arrayBuffer()) : null;
@@ -659,9 +660,12 @@ function surface(core) {
659
660
  length,
660
661
  bytes
661
662
  });
663
+ const content = new Headers(request.headers);
664
+ if (declared === null && own === null && length !== 0) content.set("content-digest", contentDigest(line));
662
665
  const send = async () => {
663
666
  const { token } = await core.current();
664
- const headers = new Headers(request.headers);
667
+ const headers = new Headers(content);
668
+ if (bytes !== null) headers.set("content-length", String(bytes.length));
665
669
  headers.set("authorization", `DPoP ${token}`);
666
670
  headers.set("dpop", await createProof(key, {
667
671
  method: request.method,
@@ -669,7 +673,7 @@ function surface(core) {
669
673
  token,
670
674
  nonce: nonces.value,
671
675
  content: {
672
- headers: request.headers,
676
+ headers: content,
673
677
  body: line
674
678
  }
675
679
  }));
@@ -710,7 +714,8 @@ function surface(core) {
710
714
  "content-length",
711
715
  "content-encoding",
712
716
  "content-language",
713
- "content-location"
717
+ "content-location",
718
+ "content-digest"
714
719
  ]) headers.delete(name);
715
720
  if (next.origin !== new URL(request.url).origin) for (const name of [
716
721
  "authorization",
@@ -980,7 +985,10 @@ async function logout(api, token) {
980
985
  const { response, body } = await api("/api/auth/logout", token, void 0, AbortSignal.timeout(LOGOUT_MS));
981
986
  if (!response.ok && (response.status !== 401 || askedNonce(response))) throw refusal(response.status, body, "coffre did not end the session");
982
987
  }
983
- /** Whether coffre refused a proof for its nonce alone (RFC 9449, section 8): sent again once, with the nonce it gave. */
988
+ /**
989
+ * Whether coffre refused a proof for its nonce alone (RFC 9449, section 8): sent again once, with the nonce it gave. A
990
+ * workload's `fetch` does so itself, but for a stream, which it cannot read twice: whoever holds its bytes may.
991
+ */
984
992
  function askedNonce(response) {
985
993
  return response.status === 401 && /error="?use_dpop_nonce/.test(response.headers.get("www-authenticate") ?? "") && response.headers.has("dpop-nonce");
986
994
  }
@@ -1015,4 +1023,4 @@ const sleep = (ms, signal) => new Promise((resolve, reject) => {
1015
1023
  signal.addEventListener("abort", stop, { once: true });
1016
1024
  });
1017
1025
  //#endregion
1018
- export { KUBERNETES_TOKEN as a, enrollment as c, github as d, keyAudience as f, vercel as h, workload as i, fly as l, oidc as m, upgradeHeaders as n, aws as o, kubernetes as p, upgradeRefused as r, azure as s, fromCoffre as t, gcp as u };
1026
+ export { workload as a, azure as c, gcp as d, github as f, vercel as g, oidc as h, upgradeRefused as i, enrollment as l, kubernetes as m, fromCoffre as n, KUBERNETES_TOKEN as o, keyAudience as p, upgradeHeaders as r, aws as s, askedNonce as t, fly as u };
@@ -1,4 +1,4 @@
1
- import { r as Workload } from "./workload-9LPBYMmN.js";
1
+ import { r as Workload } from "./workload-CdLHVAtr.js";
2
2
  import { Buffer } from "node:buffer";
3
3
  import { Duplex } from "node:stream";
4
4
  //#region src/workload-postgres.d.ts
@@ -1,4 +1,4 @@
1
- import { n as upgradeHeaders, r as upgradeRefused } from "./workload-rRw-uFfr.js";
1
+ import { i as upgradeRefused, r as upgradeHeaders } from "./workload-DDSVKy-p.js";
2
2
  import { Buffer } from "node:buffer";
3
3
  import { Duplex } from "node:stream";
4
4
  //#region src/workload-postgres.ts
@@ -1,3 +1,3 @@
1
- import { r as CoffreError } from "./index-DxSvWq3r.js";
2
- import { C as vercel, S as oidc, _ as fly, a as fromCoffre, b as keyAudience, c as workload, d as PlatformProof, f as Proof, g as enrollment, h as azure, i as WorkloadOptions, l as AwsCredentials, m as aws, n as UsedCredential, o as upgradeHeaders, p as ProofContext, r as Workload, s as upgradeRefused, t as NarrowOptions, u as KUBERNETES_TOKEN, v as gcp, x as kubernetes, y as github } from "./workload-9LPBYMmN.js";
3
- export { type AwsCredentials, CoffreError, KUBERNETES_TOKEN, NarrowOptions, type PlatformProof, type Proof, type ProofContext, UsedCredential, Workload, WorkloadOptions, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
1
+ import { r as CoffreError } from "./index-eVeI8AOV.js";
2
+ import { C as oidc, S as kubernetes, _ as enrollment, a as askedNonce, b as github, c as upgradeRefused, d as KUBERNETES_TOKEN, f as PlatformProof, g as azure, h as aws, i as WorkloadOptions, l as workload, m as ProofContext, n as UsedCredential, o as fromCoffre, p as Proof, r as Workload, s as upgradeHeaders, t as NarrowOptions, u as AwsCredentials, v as fly, w as vercel, x as keyAudience, y as gcp } from "./workload-CdLHVAtr.js";
3
+ export { type AwsCredentials, CoffreError, KUBERNETES_TOKEN, NarrowOptions, type PlatformProof, type Proof, type ProofContext, UsedCredential, Workload, WorkloadOptions, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
package/dist/workload.js CHANGED
@@ -1,3 +1,3 @@
1
- import { t as CoffreError } from "./src-CjUBSxZs.js";
2
- import { a as KUBERNETES_TOKEN, c as enrollment, d as github, f as keyAudience, h as vercel, i as workload, l as fly, m as oidc, n as upgradeHeaders, o as aws, p as kubernetes, r as upgradeRefused, s as azure, t as fromCoffre, u as gcp } from "./workload-rRw-uFfr.js";
3
- export { CoffreError, KUBERNETES_TOKEN, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
1
+ import { t as CoffreError } from "./src-BCKSeCER.js";
2
+ import { a as workload, c as azure, d as gcp, f as github, g as vercel, h as oidc, i as upgradeRefused, l as enrollment, m as kubernetes, n as fromCoffre, o as KUBERNETES_TOKEN, p as keyAudience, r as upgradeHeaders, s as aws, t as askedNonce, u as fly } from "./workload-DDSVKy-p.js";
3
+ export { CoffreError, KUBERNETES_TOKEN, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/client",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "The coffre API as typed function calls.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/api.ts CHANGED
@@ -1713,6 +1713,15 @@ export type Api = {
1713
1713
  revoked: true;
1714
1714
  };
1715
1715
  };
1716
+ "POST /audit/loss": {
1717
+ input: {
1718
+ from: number;
1719
+ to: number;
1720
+ };
1721
+ output: {
1722
+ resumes: number | null;
1723
+ };
1724
+ };
1716
1725
  };
1717
1726
 
1718
1727
  export type AccessValue = string | null | {
package/src/dpop.ts CHANGED
@@ -174,6 +174,42 @@ export async function bodyDigest(body: Uint8Array | string): Promise<string> {
174
174
  return hex(await sha256(body));
175
175
  }
176
176
 
177
+ /**
178
+ * The most a body that declares its digest carries (`Content-Digest`),
179
+ * which coffre streams and `coffre serve` holds while it hashes it: 95 MiB,
180
+ * 99,614,720 bytes, under the 100 MB (decimal) Cloudflare takes in a
181
+ * request on Free and Pro zones, so a body near it gets coffre's answer and
182
+ * not the zone's.
183
+ */
184
+ export const MAX_DIGESTED_BODY = 95 * 1024 * 1024;
185
+
186
+ /** A body's `Content-Digest` (RFC 9530) from its SHA-256 in hex, as the SDK and `coffre serve` send it: `sha-256=:<base64>:`. */
187
+ export function contentDigest(sha256: string): string {
188
+ if (!SHA256_HEX.test(sha256)) throw new TypeError('a Content-Digest is made from a SHA-256 in hex');
189
+ return `sha-256=:${btoa(String.fromCharCode(...sha256.match(/../g)!.map((pair) => parseInt(pair, 16))))}:`;
190
+ }
191
+
192
+ /**
193
+ * The SHA-256 a `Content-Digest` declares, in hex, as a body's line takes
194
+ * it: its `sha-256` member's, among any others (RFC 9530 lets one header
195
+ * name several algorithms). Null for one with no `sha-256` member, an app's
196
+ * own `sha-512` say, which declares nothing to coffre. Throws on a
197
+ * `sha-256` member that is no SHA-256 (32 bytes, as base64 between colons),
198
+ * or one named twice: such a header binds no body, and stands for none.
199
+ *
200
+ * declaredDigest('sha-256=:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=:') 'e3b0c442…7852b855'
201
+ * declaredDigest('sha-512=:z4PhNX7v…:') null
202
+ */
203
+ export function declaredDigest(header: string): string | null {
204
+ const members = header.split(',').map((member) => member.trim()).filter((member) => /^sha-256(?:[=;]|$)/.test(member));
205
+ if (members.length === 0) return null;
206
+ const base64 = members.length === 1 ? /^sha-256=:([A-Za-z0-9+/]{43}=):$/.exec(members[0]!)?.[1] : undefined;
207
+ const bytes = base64 === undefined ? null : Uint8Array.from(atob(base64), (char) => char.charCodeAt(0));
208
+ // One spelling for one digest: stray bits in the last character would let two headers declare it.
209
+ if (bytes === null || btoa(String.fromCharCode(...bytes)) !== base64) throw new TypeError('Content-Digest declares a body\'s SHA-256 once, as sha-256=:<44 characters of base64>:');
210
+ return hex(bytes);
211
+ }
212
+
177
213
  /** `ath`: base64url SHA-256 of the session token the proof is sent with. */
178
214
  export async function tokenDigest(token: string): Promise<string> {
179
215
  return base64url(await sha256(token));
@@ -194,14 +230,17 @@ const SHA256_HEX = /^[0-9a-f]{64}$/;
194
230
  * each from what it knows of the body:
195
231
  *
196
232
  * - `declared`: its `x-amz-content-sha256`, given only through a credential
197
- * `sigv4` signs; null otherwise, or when it sets none.
233
+ * `sigv4` signs; else the SHA-256 its `Content-Digest` declares
234
+ * (`declaredDigest`); null when it sets neither.
198
235
  * - `length`: its bytes as known: those read, a stream's `content-length`,
199
236
  * or null for a stream of no stated length.
200
237
  *
201
238
  * A body of no bytes signs the empty body's digest, whatever it declares.
202
- * One that declares a hash signs it, which S3 checks against the bytes, so
203
- * it may stream: 64 lower-case hex digits, or `UNSIGNED_BODY` for any other
204
- * value. Every other body signs its bytes' digest (`bytes`, none for none).
239
+ * One that declares a hash signs it, which S3, or coffre for a
240
+ * `Content-Digest`, checks against the bytes, so it may stream: 64
241
+ * lower-case hex digits, or `UNSIGNED_BODY` for any other value, which only
242
+ * an `x-amz-content-sha256` can be. Every other body signs its bytes'
243
+ * digest (`bytes`, none for none).
205
244
  *
206
245
  * bodyLine({ declared: null, length: 6, bytes }) bodyDigest(bytes)
207
246
  * bodyLine({ declared: 'UNSIGNED-PAYLOAD', length: 0 }) bodyDigest('')
@@ -225,9 +264,11 @@ export async function bodyLine(body: { declared: string | null; length: number |
225
264
  * behind one; this line signs it as it goes upstream, so `/use/s3/a/b` and
226
265
  * `/use/s3/a//b`, one object and another to S3, are two requests. A path a
227
266
  * zone changed fails closed. The body's line is its buffered bytes' digest
228
- * (`bodyDigest`), or, for a streamed upload through a credential `sigv4`
229
- * signs, the 64 hex digits its `x-amz-content-sha256` declares, which S3
230
- * checks against the bytes; any other declared value is `UNSIGNED_BODY`. A
267
+ * (`bodyDigest`), or, for a body that declares one (`bodyLine`), the 64 hex
268
+ * digits its `Content-Digest` declares, which coffre checks against the
269
+ * bytes as they stream, or, through a credential `sigv4` signs, those its
270
+ * `x-amz-content-sha256` declares, which S3 checks; any other value of the
271
+ * latter is `UNSIGNED_BODY`. A
231
272
  * header sent several times is one value, joined by `, `, as `Headers`
232
273
  * gives it. No line holds a line break, so no two requests read as one;
233
274
  * throws on one that does, or on a body line that is neither.
@@ -261,7 +302,7 @@ export function signedHeaders(headers: HeadersInit): [string, string][] {
261
302
  * - `token` adds `ath`, for a request made with a session; the exchange has none.
262
303
  * - `nonce` is the gate's newest, from the last answer's `DPoP-Nonce`.
263
304
  * - `content` adds `hdr` and `req`: the headers the app set, and the body's
264
- * line (`bodyDigest`, a streamed SigV4 upload's declared digest, or
305
+ * line (`bodyLine`: its bytes' digest, the one it declares, or
265
306
  * `UNSIGNED_BODY`).
266
307
  * Whatever it names is all coffre forwards.
267
308
  */
package/src/index.ts CHANGED
@@ -479,6 +479,12 @@ export function createClient(options: ClientOptions) {
479
479
  },
480
480
  /** What an escrowed key is checked against: public material, for admins and owners of the whole instance. */
481
481
  keys: () => call('GET /audit/keys', {}),
482
+ /**
483
+ * Accept that entries `from` to `to` are lost for good, as an Owner of
484
+ * the whole instance: the audit watch's finding, inside the range, stays
485
+ * in the log, and its anchor goes on past them.
486
+ */
487
+ acceptLoss: (range: RouteInput<'POST /audit/loss'>) => call('POST /audit/loss', {}, range),
482
488
  },
483
489
  };
484
490
  }
package/src/routes.ts CHANGED
@@ -141,6 +141,7 @@ const EVERY_ROUTE: { [K in RouteKey]: true } = {
141
141
  'GET /audit': true,
142
142
  'GET /audit/verification': true,
143
143
  'GET /audit/keys': true,
144
+ 'POST /audit/loss': true,
144
145
  };
145
146
 
146
147
  /**
package/src/workload.ts CHANGED
@@ -27,7 +27,7 @@
27
27
  * once if the gate asks for a fresh one;
28
28
  * - logout on `close()`, and on SIGTERM.
29
29
  */
30
- import { base64url, bodyDigest, bodyLine, createProof, generateProofKey, thumbprint, type ProofKey, type PublicJwk } from './dpop.ts';
30
+ import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, generateProofKey, thumbprint, type ProofKey, type PublicJwk } from './dpop.ts';
31
31
  import { CoffreError } from './index.ts';
32
32
  import type { Proof, ProofContext } from './workload-proofs.ts';
33
33
 
@@ -410,9 +410,11 @@ function surface(core: Core): Workload {
410
410
  if (!under(url)) return core.base(input, init);
411
411
  // As fetch would send it: the headers fetch adds for its body (a form's content-type) are the app's, and signed.
412
412
  const request = new Request(input, init);
413
- // A request to an AWS-signed credential declares its body's digest, which coffre takes as the body's line, and which S3 checks
414
- // against the bytes; any other declared value leaves the body unsigned. Only such a request streams; any other body is read whole.
415
- const declared = request.headers.get('x-amz-content-sha256');
413
+ // A body that declares its digest has it for its line: an AWS-signed credential's `x-amz-content-sha256`, which S3 checks
414
+ // against the bytes (any other value of it leaves the body unsigned), or a `Content-Digest`'s SHA-256, which coffre checks
415
+ // as they pass. Only such a body streams; any other is read whole, and declares its own digest, so that coffre streams it on.
416
+ const own = request.headers.get('content-digest');
417
+ const declared = request.headers.get('x-amz-content-sha256') ?? (own === null ? null : declaredDigest(own));
416
418
  const streamed = init?.body instanceof ReadableStream || (input instanceof Request && init?.body === undefined);
417
419
  const stream = declared !== null && request.body !== null && streamed ? request.body : null;
418
420
  const bytes = stream === null && request.body !== null ? new Uint8Array(await request.arrayBuffer()) : null;
@@ -420,11 +422,16 @@ function surface(core: Core): Workload {
420
422
  const stated = request.headers.get('content-length');
421
423
  const length = stream === null ? (bytes?.length ?? 0) : stated !== null && /^\d+$/.test(stated) ? Number(stated) : null;
422
424
  const line = await bodyLine({ declared, length, bytes });
425
+ // What the proof signs and coffre forwards: the app's headers, and the digest of a body read whole, unless the app set one.
426
+ const content = new Headers(request.headers);
427
+ if (declared === null && own === null && length !== 0) content.set('content-digest', contentDigest(line));
423
428
  const send = async () => {
424
429
  const { token } = await core.current();
425
- const headers = new Headers(request.headers);
430
+ const headers = new Headers(content);
431
+ // The length of a body read whole, which coffre streams a declared one by.
432
+ if (bytes !== null) headers.set('content-length', String(bytes.length));
426
433
  headers.set('authorization', `DPoP ${token}`);
427
- headers.set('dpop', await createProof(key, { method: request.method, url: request.url, token, nonce: nonces.value, content: { headers: request.headers, body: line } }));
434
+ headers.set('dpop', await createProof(key, { method: request.method, url: request.url, token, nonce: nonces.value, content: { headers: content, body: line } }));
428
435
  const sent = dispatch(nonces);
429
436
  const response = await core.base(request.url, {
430
437
  method: request.method,
@@ -460,7 +467,7 @@ function surface(core: Core): Workload {
460
467
  if (!asGet && stream !== null) throw new TypeError(`fetch failed: ${request.url} redirects a streamed body, which cannot be sent again`);
461
468
  const next = new URL(location, request.url);
462
469
  const headers = new Headers(request.headers);
463
- if (asGet) for (const name of ['content-type', 'content-length', 'content-encoding', 'content-language', 'content-location']) headers.delete(name);
470
+ if (asGet) for (const name of ['content-type', 'content-length', 'content-encoding', 'content-language', 'content-location', 'content-digest']) headers.delete(name);
464
471
  // As fetch does: credentials the app set go to no other origin.
465
472
  if (next.origin !== new URL(request.url).origin) for (const name of ['authorization', 'cookie', 'proxy-authorization']) headers.delete(name);
466
473
  // The next hop, signed afresh if it is coffre's, and sent with the app's own headers alone if it is not.
@@ -666,8 +673,11 @@ async function logout(api: Api, token: string): Promise<void> {
666
673
  if (!response.ok && (response.status !== 401 || askedNonce(response))) throw refusal(response.status, body, 'coffre did not end the session');
667
674
  }
668
675
 
669
- /** Whether coffre refused a proof for its nonce alone (RFC 9449, section 8): sent again once, with the nonce it gave. */
670
- function askedNonce(response: Response): boolean {
676
+ /**
677
+ * Whether coffre refused a proof for its nonce alone (RFC 9449, section 8): sent again once, with the nonce it gave. A
678
+ * workload's `fetch` does so itself, but for a stream, which it cannot read twice: whoever holds its bytes may.
679
+ */
680
+ export function askedNonce(response: Response): boolean {
671
681
  return response.status === 401 && /error="?use_dpop_nonce/.test(response.headers.get('www-authenticate') ?? '') && response.headers.has('dpop-nonce');
672
682
  }
673
683