@coffre/core 0.9.0 → 0.10.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.
@@ -156,7 +156,7 @@ function placeKey(place) {
156
156
  //#endregion
157
157
  //#region src/access.ts
158
158
  /**
159
- * Who may do what: seven permissions, the six project roles a grant names,
159
+ * Who may do what: eight permissions, the seven project roles a grant names,
160
160
  * and the five instance roles a person holds across every project.
161
161
  *
162
162
  * Deliberately constants and not tables or a policy language. A grant names
@@ -164,8 +164,16 @@ function placeKey(place) {
164
164
  * role and its scope, and a check is a lookup in this file, so authorisation
165
165
  * stays something you can read end to end.
166
166
  */
167
+ /**
168
+ * `secret.read` hands a value back: a reveal, `coffre get`, `coffre run`'s
169
+ * values. `secret.use` uses it without handing it back: a placeholder in a
170
+ * person's app, the credential proxy filling it in (docs/design/credential-proxy.md).
171
+ * Every role that reads uses too. What a person gets is the narrower of the
172
+ * two and the key's sensitivity: a sensitive key is only ever used.
173
+ */
167
174
  const PERMISSIONS = [
168
175
  "secret.read",
176
+ "secret.use",
169
177
  "secret.write",
170
178
  "secret.archive",
171
179
  "audit.read",
@@ -186,50 +194,84 @@ const PROJECT_ONLY_PERMISSIONS = [
186
194
  const ROLES = {
187
195
  viewer: {
188
196
  name: "Viewer",
189
- description: "Read secret values.",
190
- permissions: ["secret.read"]
197
+ description: "Read and use secret values.",
198
+ permissions: ["secret.read", "secret.use"],
199
+ protected: ["secret.use"]
200
+ },
201
+ contributor: {
202
+ name: "Contributor",
203
+ description: "Use and write secrets, never read them: apps get placeholders through the credential proxy.",
204
+ permissions: ["secret.use", "secret.write"],
205
+ protected: ["secret.use", "secret.write"]
191
206
  },
192
207
  developer: {
193
208
  name: "Developer",
194
- description: "Read and write secrets.",
195
- permissions: ["secret.read", "secret.write"]
209
+ description: "Read, use and write secrets.",
210
+ permissions: [
211
+ "secret.read",
212
+ "secret.use",
213
+ "secret.write"
214
+ ],
215
+ protected: ["secret.use", "secret.write"]
196
216
  },
197
217
  maintainer: {
198
218
  name: "Maintainer",
199
- description: "Read, write and retire secrets, and manage environments.",
219
+ description: "Read, use, write and retire secrets, and manage environments.",
200
220
  permissions: [
201
221
  "secret.read",
222
+ "secret.use",
202
223
  "secret.write",
203
224
  "secret.archive",
204
225
  "environment.manage"
226
+ ],
227
+ protected: [
228
+ "secret.use",
229
+ "secret.write",
230
+ "secret.archive"
205
231
  ]
206
232
  },
207
233
  "access-manager": {
208
234
  name: "Access manager",
209
235
  description: "Manage who has access. Cannot read secret values.",
210
- permissions: ["grant.manage"]
236
+ permissions: ["grant.manage"],
237
+ protected: []
211
238
  },
212
239
  auditor: {
213
240
  name: "Auditor",
214
241
  description: "Read the audit log. Cannot read secret values.",
215
- permissions: ["audit.read"]
242
+ permissions: ["audit.read"],
243
+ protected: ["audit.read"]
216
244
  },
217
245
  owner: {
218
246
  name: "Owner",
219
247
  description: "Everything, including reading secret values.",
220
- permissions: [...PERMISSIONS]
248
+ permissions: [...PERMISSIONS],
249
+ protected: [...PERMISSIONS].filter((permission) => !PROJECT_ONLY_PERMISSIONS.includes(permission))
221
250
  }
222
251
  };
223
252
  const ROLE_NAMES = Object.keys(ROLES);
224
- function isRole(value) {
253
+ /** A custom role's id: lowercase, as a slug, and no built-in one's. */
254
+ const ROLE_ID = /^[a-z][a-z0-9-]{1,31}$/;
255
+ /** Whether `value` names a role of `policy`'s: a built-in one, as it ships, unless told. */
256
+ function isRole(value, policy = DEFAULT_POLICY) {
257
+ return Object.hasOwn(policy.roles, value);
258
+ }
259
+ /** Whether `value` is one of the roles coffre ships. */
260
+ function isBuiltinRole(value) {
225
261
  return Object.hasOwn(ROLES, value);
226
262
  }
227
- function roleGrants(role, permission) {
228
- return ROLES[role].permissions.includes(permission);
263
+ /** Whether `role` holds `permission` where nothing is protected, as `policy` defines it: as it ships, unless told. */
264
+ function roleGrants(role, permission, policy = DEFAULT_POLICY) {
265
+ return definitionOf(policy, role)?.permissions.includes(permission) ?? false;
229
266
  }
230
267
  /** Roles that hold a project-only permission can only be granted on a project. */
231
- function assignableToEnvironment(role) {
232
- return !ROLES[role].permissions.some((permission) => PROJECT_ONLY_PERMISSIONS.includes(permission));
268
+ function assignableToEnvironment(role, policy = DEFAULT_POLICY) {
269
+ const definition = definitionOf(policy, role);
270
+ return definition !== void 0 && !definition.permissions.some((permission) => PROJECT_ONLY_PERMISSIONS.includes(permission));
271
+ }
272
+ /** A project role's definition in `policy`, or undefined for none. */
273
+ function definitionOf(policy, role) {
274
+ return Object.hasOwn(policy.roles, role) ? policy.roles[role] : void 0;
233
275
  }
234
276
  /**
235
277
  * A person's role on the whole instance: what they hold in every project
@@ -241,17 +283,24 @@ const INSTANCE_ROLES = {
241
283
  member: {
242
284
  name: "Member",
243
285
  description: "None.",
244
- permissions: []
286
+ permissions: [],
287
+ protected: []
245
288
  },
246
289
  auditor: {
247
290
  name: "Auditor",
248
291
  description: "Read the audit log. No secret values.",
249
- permissions: ["audit.read"]
292
+ permissions: ["audit.read"],
293
+ protected: ["audit.read"]
250
294
  },
251
295
  developer: {
252
296
  name: "Developer",
253
- description: "Read and write secrets.",
254
- permissions: ["secret.read", "secret.write"]
297
+ description: "Read, use and write secrets.",
298
+ permissions: [
299
+ "secret.read",
300
+ "secret.use",
301
+ "secret.write"
302
+ ],
303
+ protected: ["secret.use", "secret.write"]
255
304
  },
256
305
  admin: {
257
306
  name: "Admin",
@@ -261,20 +310,93 @@ const INSTANCE_ROLES = {
261
310
  "environment.manage",
262
311
  "grant.manage",
263
312
  "project.manage"
264
- ]
313
+ ],
314
+ protected: ["audit.read"]
265
315
  },
266
316
  owner: {
267
317
  name: "Owner",
268
318
  description: "Everything, every secret included.",
269
- permissions: [...PERMISSIONS]
319
+ permissions: [...PERMISSIONS],
320
+ protected: [...PERMISSIONS].filter((permission) => !PROJECT_ONLY_PERMISSIONS.includes(permission))
270
321
  }
271
322
  };
272
323
  const INSTANCE_ROLE_NAMES = Object.keys(INSTANCE_ROLES);
273
324
  function isInstanceRole(value) {
274
325
  return Object.hasOwn(INSTANCE_ROLES, value);
275
326
  }
276
- function instanceRoleGrants(role, permission) {
277
- return INSTANCE_ROLES[role].permissions.includes(permission);
327
+ /** Whether an instance role holds `permission` where nothing is protected, as `policy` defines it: as it ships, unless told. */
328
+ function instanceRoleGrants(role, permission, policy = DEFAULT_POLICY) {
329
+ return (policy.instanceRoles[role]?.permissions ?? []).includes(permission);
330
+ }
331
+ /** The policy before any Owner edits it: every role as it ships, and no environment protected. */
332
+ const DEFAULT_POLICY = {
333
+ roles: ROLES,
334
+ instanceRoles: INSTANCE_ROLES,
335
+ protected: [],
336
+ protectedIn: {}
337
+ };
338
+ /**
339
+ * How long the policy keeps an environment's id protected before its row
340
+ * is written, a fork's in flight, which writes it one call later; one with
341
+ * no row after that is a fork refused or abandoned. Until then it is a
342
+ * place a check on its project weighs (`protectedWithin`), which no scoped
343
+ * instance role's filters take in, so short. A fork writes its row only
344
+ * well within it, so no row ever brings back an id the policy dropped.
345
+ */
346
+ const UNWRITTEN_FORK_MS = 6e5;
347
+ /** The roles an Owner may not edit: Owner, which holds everything, and the instance's Member, which holds nothing. */
348
+ const FIXED_ROLES = {
349
+ roles: ["owner"],
350
+ instanceRoles: ["owner", "member"]
351
+ };
352
+ /**
353
+ * What is wrong with a role's definition, or null: a name and a
354
+ * description; known permissions, each once; reading implies using, in
355
+ * each column; and the protected column holds no more than the other, and
356
+ * nothing project-wide.
357
+ */
358
+ function definitionProblem(definition) {
359
+ const { name, description, permissions } = definition;
360
+ if (typeof name !== "string" || name.trim() === "" || name.length > 40 || /[\u0000-\u001f\u007f]/.test(name)) return "a role needs a name of 1 to 40 characters";
361
+ if (typeof description !== "string" || description.length > 200 || /[\u0000-\u001f\u007f]/.test(description)) return "a description is 200 characters at most";
362
+ for (const column of [permissions, definition.protected]) {
363
+ if (!Array.isArray(column) || column.some((permission) => !PERMISSIONS.includes(permission)) || new Set(column).size !== column.length) return `a role holds permissions from ${PERMISSIONS.join(", ")}, each once`;
364
+ if (column.includes("secret.read") && !column.includes("secret.use")) return "a role that reads values uses them too: add secret.use";
365
+ }
366
+ if (definition.protected.some((permission) => PROJECT_ONLY_PERMISSIONS.includes(permission))) return "a project-wide permission has one column: a project is never protected";
367
+ if (definition.protected.some((permission) => !permissions.includes(permission))) return "a role holds no more in a protected environment than in one that is not";
368
+ return null;
369
+ }
370
+ /** A definition as the vault stores and compares it: each column in catalogue order. */
371
+ function normalDefinition(definition) {
372
+ const ordered = (column) => PERMISSIONS.filter((permission) => column.includes(permission));
373
+ return {
374
+ name: definition.name.trim(),
375
+ description: definition.description,
376
+ permissions: ordered(definition.permissions),
377
+ protected: ordered(definition.protected)
378
+ };
379
+ }
380
+ /**
381
+ * Whether `definition` gives `holder` `permission` at `place`: its
382
+ * protected column for a person in a protected environment, its other
383
+ * column anywhere else, and project-wide permissions from that one always.
384
+ */
385
+ function holds(definition, permission, place, holder) {
386
+ if (definition === void 0) return false;
387
+ if (PROJECT_ONLY_PERMISSIONS.includes(permission)) return definition.permissions.includes(permission);
388
+ return (guarded(holder, place) ? definition.protected : definition.permissions).includes(permission);
389
+ }
390
+ /** In a policy's `protected`, every environment: the policy the vault falls back on when it cannot read its own (`CLOSED_POLICY`). */
391
+ const EVERY_ENVIRONMENT = "*";
392
+ /** Whether `holder` holds the protected column at `place`: a person, in an environment the policy protects. */
393
+ function guarded(holder, place) {
394
+ if (holder.service === true || place.environmentId == null) return false;
395
+ return protects(holder.policy ?? DEFAULT_POLICY, place.environmentId);
396
+ }
397
+ /** Whether `policy` protects the environment of this id: by name, or every one (`EVERY_ENVIRONMENT`). */
398
+ function protects(policy, environmentId) {
399
+ return policy.protected.includes(environmentId) || policy.protected.includes("*");
278
400
  }
279
401
  /** The roles that manage people and access: Admin, and Owner, which adds every secret. */
280
402
  function administers(role) {
@@ -367,6 +489,89 @@ function grantKind(grant) {
367
489
  if (grant.projectId === null) return null;
368
490
  return grant.environmentId === null ? "project" : "environment";
369
491
  }
492
+ /**
493
+ * A grant of a scope with what its role holds now in `policy`, kept: what a
494
+ * credential issued now carries. Given what its issuer holds at its place
495
+ * (`heldColumns`), no more than that either, column by column, so that
496
+ * neither the role nor the issuer, widened later, widens the credential:
497
+ *
498
+ * viewer edited to read in prod; Ada, a Developer, uses there
499
+ * Ada's token, viewer on market read, use / use
500
+ * Developer widened to read in prod the token still only uses prod
501
+ */
502
+ function issuedGrant(grant, policy = DEFAULT_POLICY, held) {
503
+ const definition = definitionOf(policy, grant.role);
504
+ const within = (column = [], cap) => cap === void 0 ? [...column] : column.filter((permission) => cap.includes(permission));
505
+ return {
506
+ projectId: grant.projectId,
507
+ environmentId: grant.environmentId,
508
+ role: grant.role,
509
+ permissions: {
510
+ normal: within(definition?.permissions, held?.normal),
511
+ protected: within(definition?.protected, held?.protected)
512
+ }
513
+ };
514
+ }
515
+ /**
516
+ * What `holder` holds at `place` in each column a credential's grant there
517
+ * keeps (`issuedGrant`): `normal`, as if nothing were protected; `protected`,
518
+ * as a person in a protected environment. At an environment, as if it were
519
+ * protected; at a project, in an environment of it that no grant names,
520
+ * the least they hold in any of its environments, those it protects later
521
+ * included: only what covers the whole project reaches one.
522
+ *
523
+ * Ada, a Developer of market, at market read, use, write / use, write
524
+ */
525
+ function heldColumns(holder, place) {
526
+ const policy = holder.policy ?? DEFAULT_POLICY;
527
+ const held = (protect, at) => PERMISSIONS.filter((permission) => allows({
528
+ ...holder,
529
+ policy: {
530
+ ...policy,
531
+ protected: protect
532
+ }
533
+ }, permission, at));
534
+ const anyOf = place.environmentId != null ? place : {
535
+ projectId: place.projectId,
536
+ environmentId: "",
537
+ environmentSlug: null
538
+ };
539
+ return {
540
+ normal: held([], place),
541
+ protected: held(["*"], anyOf)
542
+ };
543
+ }
544
+ /**
545
+ * The protected environments a grant at `place` reaches beyond it: for a
546
+ * grant on a project, each of its environments `policy` protects, where a
547
+ * person holds the protected column and a service account the other one;
548
+ * none for a grant on an environment. Whoever checks a grant on a project
549
+ * against what someone holds checks it there too (`givesService`,
550
+ * `personalScope`): it reaches them.
551
+ */
552
+ function protectedWithin(policy, place) {
553
+ if (place.environmentId != null) return [];
554
+ return (policy.protectedIn[place.projectId] ?? []).map(({ environmentId, slug }) => ({
555
+ projectId: place.projectId,
556
+ environmentId,
557
+ environmentSlug: slug
558
+ }));
559
+ }
560
+ /** What a scope's grant holds, as it was issued, as a role holds it: whatever the role is now, which no edit changes. */
561
+ function scopeDefinition(grant) {
562
+ if (grant.permissions !== void 0) return {
563
+ name: grant.role,
564
+ description: "",
565
+ permissions: grant.permissions.normal,
566
+ protected: grant.permissions.protected
567
+ };
568
+ return isBuiltinRole(grant.role) ? ROLES[grant.role] : {
569
+ name: grant.role,
570
+ description: "",
571
+ permissions: [],
572
+ protected: []
573
+ };
574
+ }
370
575
  /** The most places a scope asked for names: far more than any credential needs, and a bound on what a request makes the app check. */
371
576
  const MAX_SCOPE_PLACES = 1e3;
372
577
  /**
@@ -376,11 +581,15 @@ const MAX_SCOPE_PLACES = 1e3;
376
581
  * than it was given. A project's Owner who wants a token for all of it
377
582
  * names `maintainer`.
378
583
  */
379
- function delegable(role) {
380
- return !roleGrants(role, "grant.manage");
584
+ function delegable(role, policy = DEFAULT_POLICY) {
585
+ return isRole(role, policy) && !roleGrants(role, "grant.manage", policy);
381
586
  }
382
587
  /** The roles a personal token's scope may name, in catalogue order. */
383
- const DELEGABLE_ROLES = ROLE_NAMES.filter(delegable);
588
+ function delegableRoles(policy = DEFAULT_POLICY) {
589
+ return Object.keys(policy.roles).filter((role) => delegable(role, policy));
590
+ }
591
+ /** The roles a personal token's scope may name, as they ship. */
592
+ const DELEGABLE_ROLES = delegableRoles();
384
593
  /**
385
594
  * The longest a personal token lasts: a week. It is for an afternoon's
386
595
  * script or agent, and whoever needs it longer makes another, as a person,
@@ -400,11 +609,21 @@ function credentialScopeInWords(scope) {
400
609
  function isCredentialScope(value) {
401
610
  if (!Array.isArray(value)) return false;
402
611
  const places = /* @__PURE__ */ new Set();
612
+ const column = (list) => Array.isArray(list) && list.every((permission) => PERMISSIONS.includes(permission)) && new Set(list).size === list.length;
403
613
  return value.every((grant) => {
404
614
  if (typeof grant !== "object" || grant === null || Array.isArray(grant)) return false;
405
- const { projectId, environmentId, role, ...rest } = grant;
406
- if (Object.keys(rest).length > 0 || typeof projectId !== "string" || typeof role !== "string" || !isRole(role)) return false;
407
- if (environmentId !== null && (typeof environmentId !== "string" || !assignableToEnvironment(role))) return false;
615
+ const { projectId, environmentId, role, permissions, ...rest } = grant;
616
+ if (Object.keys(rest).length > 0 || typeof projectId !== "string" || typeof role !== "string") return false;
617
+ if (!ROLE_ID.test(role)) return false;
618
+ if (permissions === void 0) {
619
+ if (!isBuiltinRole(role)) return false;
620
+ } else {
621
+ if (typeof permissions !== "object" || permissions === null || Array.isArray(permissions)) return false;
622
+ const { normal, protected: guarded, ...more } = permissions;
623
+ if (Object.keys(more).length > 0 || !column(normal) || !column(guarded)) return false;
624
+ }
625
+ const definition = scopeDefinition(grant);
626
+ if (environmentId !== null && (typeof environmentId !== "string" || definition.permissions.some((permission) => PROJECT_ONLY_PERMISSIONS.includes(permission)))) return false;
408
627
  const place = `${projectId}/${environmentId ?? ""}`;
409
628
  if (places.has(place)) return false;
410
629
  places.add(place);
@@ -440,9 +659,10 @@ function covers(grant, place) {
440
659
  * `billing`.
441
660
  */
442
661
  function allows(holder, permission, place) {
443
- if (holder.credentialScope != null && !holdsAt(holder.credentialScope, permission, place)) return false;
444
- if (instanceRoleGrants(holder.role, permission) && inScope(holder.scope, place)) return true;
445
- return holdsAt(holder.grants, permission, place);
662
+ if (holder.credentialScope != null && !scopeHolds(holder, permission, place)) return false;
663
+ const policy = holder.policy ?? DEFAULT_POLICY;
664
+ if (inScope(holder.scope, place) && holds(policy.instanceRoles[holder.role], permission, place, holder)) return true;
665
+ return holder.grants.some((grant) => covers(grant, place) && holds(definitionOf(policy, grant.role), permission, place, holder));
446
666
  }
447
667
  /**
448
668
  * Whether the credential a request came in on, a scoped token or binding,
@@ -452,11 +672,11 @@ function allows(holder, permission, place) {
452
672
  * role its scope names there: a viewer's token reads it, and writes nothing.
453
673
  */
454
674
  function withinCredential(holder, permission, place) {
455
- return holder.credentialScope == null || holdsAt(holder.credentialScope, permission, place);
675
+ return holder.credentialScope == null || scopeHolds(holder, permission, place);
456
676
  }
457
- /** Whether a grant among `grants` covers `place` with a role that holds `permission`. */
458
- function holdsAt(grants, permission, place) {
459
- return grants.some((grant) => covers(grant, place) && roleGrants(grant.role, permission));
677
+ /** Whether the credential's scope covers `place` with a grant that held `permission` there when it was issued. */
678
+ function scopeHolds(holder, permission, place) {
679
+ return (holder.credentialScope ?? []).some((grant) => covers(grant, place) && holds(scopeDefinition(grant), permission, place, holder));
460
680
  }
461
681
  /**
462
682
  * Whether the actor may give or take grants at this place: with
@@ -484,16 +704,26 @@ function runsInstance(holder) {
484
704
  * developer there, never maintainer; a grant on `market` takes holding it
485
705
  * on all of `market`. `setting` is null for a service account, which sets
486
706
  * up none; a request on a credential with a scope sets none up either,
487
- * since what it set up would outlive it.
707
+ * since what it set up would outlive it. A service account holds a role's
708
+ * normal column even in a protected environment, so a person there, who
709
+ * holds its protected column, gives none what they do not hold themselves:
710
+ * a Developer of a protected prod, who only uses it, gives no viewer there.
488
711
  */
489
- function givesService(holder, setting, role, place) {
712
+ function givesService(holder, setting, role, place, kept) {
490
713
  if (mayManageAccess(holder, place)) return true;
491
- return setting !== null && holder.credentialScope == null && inScope(setting, place) && ROLES[role].permissions.every((permission) => allows(holder, permission, place));
714
+ const policy = holder.policy ?? DEFAULT_POLICY;
715
+ const definition = kept ?? definitionOf(policy, role);
716
+ if (definition === void 0 || definition.permissions.length === 0) return false;
717
+ if (setting === null || holder.credentialScope != null || !inScope(setting, place)) return false;
718
+ if (!definition.permissions.every((permission) => allows(holder, permission, place))) return false;
719
+ const inEnvironments = definition.permissions.filter((permission) => !PROJECT_ONLY_PERMISSIONS.includes(permission));
720
+ return protectedWithin(policy, place).every((there) => inEnvironments.every((permission) => allows(holder, permission, there)));
492
721
  }
493
722
  /** Whether `holder` gives a service account anything anywhere among `places`: whether they set any up. */
494
723
  function setsUpServices(holder, setting, places) {
495
724
  if (runsInstance(holder)) return true;
496
- return places.some((place) => ROLE_NAMES.some((role) => givesService(holder, setting, role, place)));
725
+ const roles = Object.keys((holder.policy ?? DEFAULT_POLICY).roles);
726
+ return places.some((place) => roles.some((role) => givesService(holder, setting, role, place)));
497
727
  }
498
728
  /**
499
729
  * Whether `holder`, the member `actor`, manages a service account: issues
@@ -514,33 +744,41 @@ function managesService(holder, setting, actor, account) {
514
744
  * Whether `holder`, the member `actor`, may issue a token or a trust
515
745
  * binding of `account` whose scope is `scope`: they manage the account
516
746
  * (`managesService`), and each grant of the scope is one they could give it
517
- * (`givesService`) and one it holds, every permission of its role there.
747
+ * (`givesService`) and one it holds, every permission of its role there;
748
+ * of a grant that keeps less than its role, every permission it `kept`, as
749
+ * a binding replacing others keeps what they held.
518
750
  * For an account that holds `developer` on `market/dev`, a Developer of
519
751
  * `dev` issues `developer` or `viewer` there; nobody issues `market/prod`
520
752
  * for it, or `maintainer` on `market/dev`, until the account holds them.
521
753
  */
522
754
  function issuesCredential(holder, setting, actor, account, scope) {
523
755
  if (!managesService(holder, setting, actor, account)) return false;
756
+ const policy = holder.policy ?? DEFAULT_POLICY;
524
757
  const held = {
525
758
  ...NOTHING,
759
+ policy,
760
+ service: true,
526
761
  grants: account.grants.map(({ role, place }) => ({
527
762
  projectId: place.projectId,
528
763
  environmentId: place.environmentId ?? null,
529
764
  role
530
765
  }))
531
766
  };
532
- return scope.every(({ role, place }) => givesService(holder, setting, role, place) && ROLES[role].permissions.every((permission) => allows(held, permission, place)));
767
+ return scope.every(({ role, place, kept }) => givesService(holder, setting, role, place, kept) && ((kept ?? definitionOf(policy, role))?.permissions.every((permission) => allows(held, permission, place)) ?? false));
533
768
  }
534
769
  /**
535
770
  * Whether the holder reads the whole log, the instance's own entries
536
771
  * (sign-ins, people, service accounts, settings) with every project's, and
537
772
  * verifies it: whoever holds `audit.read` by an instance role whose scope
538
- * narrows nothing, an Auditor's included. Reading, not
539
- * running: an Auditor of the whole instance changes nothing it reads about.
540
- * Anyone else reads the places they hold `audit.read` in.
773
+ * narrows nothing, in both its columns, an Auditor's included: an Auditor
774
+ * edited to read no protected environment's log reads every other place's,
775
+ * and not the whole. Reading, not running: an Auditor of the whole
776
+ * instance changes nothing it reads about. Anyone else reads the places
777
+ * they hold `audit.read` in.
541
778
  */
542
779
  function readsWholeLog(holder) {
543
- return holder.credentialScope == null && instanceRoleGrants(holder.role, "audit.read") && unscoped(holder.scope);
780
+ const role = (holder.policy ?? DEFAULT_POLICY).instanceRoles[holder.role];
781
+ return holder.credentialScope == null && unscoped(holder.scope) && role !== void 0 && role.permissions.includes("audit.read") && role.protected.includes("audit.read");
544
782
  }
545
783
  /**
546
784
  * Whether the holder may make a project: an Admin or Owner whose scope
@@ -553,4 +791,4 @@ function makesProjects(holder) {
553
791
  return holder.credentialScope == null && administers(holder.role) && environments === "all" && (projects === "all" || "except" in projects);
554
792
  }
555
793
  //#endregion
556
- export { mayManageAccess as A, convertEveryProjectGrants as B, isCredentialScope as C, issuesCredential as D, isScope as E, runsInstance as F, scopeInWords as I, setsUpServices as L, ownsInstance as M, readsWholeLog as N, makesProjects as O, roleGrants as P, unscoped as R, instanceRoleGrants as S, isRole as T, credentialScopeInWords as _, MAX_PERSONAL_TOKEN_MINUTES as a, grantKind as b, PERMISSIONS as c, ROLE_NAMES as d, administers as f, covers as g, assignableToEnvironment as h, INSTANCE_ROLE_NAMES as i, normalScope as j, managesService as k, PROJECT_ONLY_PERMISSIONS as l, allows as m, EVERYWHERE as n, MAX_SCOPE_PLACES as o, admits as p, INSTANCE_ROLES as r, NOTHING as s, DELEGABLE_ROLES as t, ROLES as u, delegable as v, isInstanceRole as w, inScope as x, givesService as y, withinCredential as z };
794
+ export { scopeInWords as $, heldColumns as A, issuesCredential as B, definitionOf as C, givesService as D, delegableRoles as E, isCredentialScope as F, normalScope as G, managesService as H, isInstanceRole as I, protects as J, ownsInstance as K, isRole as L, inScope as M, instanceRoleGrants as N, grantKind as O, isBuiltinRole as P, scopeDefinition as Q, isScope as R, credentialScopeInWords as S, delegable as T, mayManageAccess as U, makesProjects as V, normalDefinition as W, roleGrants as X, readsWholeLog as Y, runsInstance as Z, administers as _, FIXED_ROLES as a, assignableToEnvironment as b, MAX_PERSONAL_TOKEN_MINUTES as c, PERMISSIONS as d, setsUpServices as et, PROJECT_ONLY_PERMISSIONS as f, UNWRITTEN_FORK_MS as g, ROLE_NAMES as h, EVERY_ENVIRONMENT as i, holds as j, guarded as k, MAX_SCOPE_PLACES as l, ROLE_ID as m, DELEGABLE_ROLES as n, withinCredential as nt, INSTANCE_ROLES as o, ROLES as p, protectedWithin as q, EVERYWHERE as r, convertEveryProjectGrants as rt, INSTANCE_ROLE_NAMES as s, DEFAULT_POLICY as t, unscoped as tt, NOTHING as u, admits as v, definitionProblem as w, covers as x, allows as y, issuedGrant as z };
package/dist/access.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { $ as ConvertedGrant, A as givesService, B as managesService, C as administers, D as covers, E as assignableToEnvironment, F as isInstanceRole, G as roleGrants, H as normalScope, I as isRole, J as setsUpServices, K as runsInstance, L as isScope, M as inScope, N as instanceRoleGrants, O as credentialScopeInWords, P as isCredentialScope, Q as ConversionInput, R as issuesCredential, S as ServiceSetting, T as allows, U as ownsInstance, V as mayManageAccess, W as readsWholeLog, X as withinCredential, Y as unscoped, Z as Conversion, _ as ROLES, a as GrantPlace, b as Scope, c as INSTANCE_ROLE_NAMES, d as MAX_SCOPE_PLACES, et as EveryProjectGrant, f as NOTHING, g as Place, h as Permission, i as Filter, j as grantKind, k as delegable, l as InstanceRole, m as PROJECT_ONLY_PERMISSIONS, n as DELEGABLE_ROLES, nt as convertEveryProjectGrants, o as Holdings, p as PERMISSIONS, q as scopeInWords, r as EVERYWHERE, s as INSTANCE_ROLES, t as CredentialScope, tt as Narrowed, u as MAX_PERSONAL_TOKEN_MINUTES, v as ROLE_NAMES, w as admits, x as ServiceAccount, y as Role, z as makesProjects } from "./access-CONhKG2P.js";
2
- export { type Conversion, type ConversionInput, type ConvertedGrant, CredentialScope, DELEGABLE_ROLES, EVERYWHERE, type EveryProjectGrant, Filter, GrantPlace, Holdings, INSTANCE_ROLES, INSTANCE_ROLE_NAMES, InstanceRole, MAX_PERSONAL_TOKEN_MINUTES, MAX_SCOPE_PLACES, NOTHING, type Narrowed, PERMISSIONS, PROJECT_ONLY_PERMISSIONS, Permission, Place, ROLES, ROLE_NAMES, Role, Scope, ServiceAccount, ServiceSetting, administers, admits, allows, assignableToEnvironment, convertEveryProjectGrants, covers, credentialScopeInWords, delegable, givesService, grantKind, inScope, instanceRoleGrants, isCredentialScope, isInstanceRole, isRole, isScope, issuesCredential, makesProjects, managesService, mayManageAccess, normalScope, ownsInstance, readsWholeLog, roleGrants, runsInstance, scopeInWords, setsUpServices, unscoped, withinCredential };
1
+ import { $ as isRole, A as ServiceAccount, B as definitionProblem, C as ROLES, Ct as Narrowed, D as RoleDefinition, E as Role, F as allows, G as guarded, H as delegableRoles, I as assignableToEnvironment, J as inScope, K as heldColumns, L as covers, M as UNWRITTEN_FORK_MS, N as administers, O as Scope, P as admits, Q as isInstanceRole, R as credentialScopeInWords, S as Policy, St as EveryProjectGrant, T as ROLE_NAMES, U as givesService, V as delegable, W as grantKind, X as isBuiltinRole, Y as instanceRoleGrants, Z as isCredentialScope, _ as NOTHING, _t as unscoped, a as DELEGABLE_ROLES, at as mayManageAccess, b as Permission, bt as ConversionInput, c as FIXED_ROLES, ct as ownsInstance, d as Holdings, dt as readsWholeLog, et as isScope, f as INSTANCE_ROLES, ft as roleGrants, g as MAX_SCOPE_PLACES, gt as setsUpServices, h as MAX_PERSONAL_TOKEN_MINUTES, ht as scopeInWords, i as DEFAULT_POLICY, it as managesService, j as ServiceSetting, k as ScopeGrant, l as Filter, lt as protectedWithin, m as InstanceRole, mt as scopeDefinition, n as Columns, nt as issuesCredential, o as EVERYWHERE, ot as normalDefinition, p as INSTANCE_ROLE_NAMES, pt as runsInstance, q as holds, r as CredentialScope, rt as makesProjects, s as EVERY_ENVIRONMENT, st as normalScope, t as BuiltinRole, tt as issuedGrant, u as GrantPlace, ut as protects, v as PERMISSIONS, vt as withinCredential, w as ROLE_ID, wt as convertEveryProjectGrants, x as Place, xt as ConvertedGrant, y as PROJECT_ONLY_PERMISSIONS, yt as Conversion, z as definitionOf } from "./access-CF4noOjL.js";
2
+ export { BuiltinRole, Columns, type Conversion, type ConversionInput, type ConvertedGrant, CredentialScope, DEFAULT_POLICY, DELEGABLE_ROLES, EVERYWHERE, EVERY_ENVIRONMENT, type EveryProjectGrant, FIXED_ROLES, Filter, GrantPlace, Holdings, INSTANCE_ROLES, INSTANCE_ROLE_NAMES, InstanceRole, MAX_PERSONAL_TOKEN_MINUTES, MAX_SCOPE_PLACES, NOTHING, type Narrowed, PERMISSIONS, PROJECT_ONLY_PERMISSIONS, Permission, Place, Policy, ROLES, ROLE_ID, ROLE_NAMES, Role, RoleDefinition, Scope, ScopeGrant, ServiceAccount, ServiceSetting, UNWRITTEN_FORK_MS, administers, admits, allows, assignableToEnvironment, convertEveryProjectGrants, covers, credentialScopeInWords, definitionOf, definitionProblem, delegable, delegableRoles, givesService, grantKind, guarded, heldColumns, holds, inScope, instanceRoleGrants, isBuiltinRole, isCredentialScope, isInstanceRole, isRole, isScope, issuedGrant, issuesCredential, makesProjects, managesService, mayManageAccess, normalDefinition, normalScope, ownsInstance, protectedWithin, protects, readsWholeLog, roleGrants, runsInstance, scopeDefinition, scopeInWords, setsUpServices, unscoped, withinCredential };
package/dist/access.js CHANGED
@@ -1,2 +1,2 @@
1
- import { A as mayManageAccess, B as convertEveryProjectGrants, C as isCredentialScope, D as issuesCredential, E as isScope, F as runsInstance, I as scopeInWords, L as setsUpServices, M as ownsInstance, N as readsWholeLog, O as makesProjects, P as roleGrants, R as unscoped, S as instanceRoleGrants, T as isRole, _ as credentialScopeInWords, a as MAX_PERSONAL_TOKEN_MINUTES, b as grantKind, c as PERMISSIONS, d as ROLE_NAMES, f as administers, g as covers, h as assignableToEnvironment, i as INSTANCE_ROLE_NAMES, j as normalScope, k as managesService, l as PROJECT_ONLY_PERMISSIONS, m as allows, n as EVERYWHERE, o as MAX_SCOPE_PLACES, p as admits, r as INSTANCE_ROLES, s as NOTHING, t as DELEGABLE_ROLES, u as ROLES, v as delegable, w as isInstanceRole, x as inScope, y as givesService, z as withinCredential } from "./access-B1wpMzEB.js";
2
- export { DELEGABLE_ROLES, EVERYWHERE, INSTANCE_ROLES, INSTANCE_ROLE_NAMES, MAX_PERSONAL_TOKEN_MINUTES, MAX_SCOPE_PLACES, NOTHING, PERMISSIONS, PROJECT_ONLY_PERMISSIONS, ROLES, ROLE_NAMES, administers, admits, allows, assignableToEnvironment, convertEveryProjectGrants, covers, credentialScopeInWords, delegable, givesService, grantKind, inScope, instanceRoleGrants, isCredentialScope, isInstanceRole, isRole, isScope, issuesCredential, makesProjects, managesService, mayManageAccess, normalScope, ownsInstance, readsWholeLog, roleGrants, runsInstance, scopeInWords, setsUpServices, unscoped, withinCredential };
1
+ import { $ as scopeInWords, A as heldColumns, B as issuesCredential, C as definitionOf, D as givesService, E as delegableRoles, F as isCredentialScope, G as normalScope, H as managesService, I as isInstanceRole, J as protects, K as ownsInstance, L as isRole, M as inScope, N as instanceRoleGrants, O as grantKind, P as isBuiltinRole, Q as scopeDefinition, R as isScope, S as credentialScopeInWords, T as delegable, U as mayManageAccess, V as makesProjects, W as normalDefinition, X as roleGrants, Y as readsWholeLog, Z as runsInstance, _ as administers, a as FIXED_ROLES, b as assignableToEnvironment, c as MAX_PERSONAL_TOKEN_MINUTES, d as PERMISSIONS, et as setsUpServices, f as PROJECT_ONLY_PERMISSIONS, g as UNWRITTEN_FORK_MS, h as ROLE_NAMES, i as EVERY_ENVIRONMENT, j as holds, k as guarded, l as MAX_SCOPE_PLACES, m as ROLE_ID, n as DELEGABLE_ROLES, nt as withinCredential, o as INSTANCE_ROLES, p as ROLES, q as protectedWithin, r as EVERYWHERE, rt as convertEveryProjectGrants, s as INSTANCE_ROLE_NAMES, t as DEFAULT_POLICY, tt as unscoped, u as NOTHING, v as admits, w as definitionProblem, x as covers, y as allows, z as issuedGrant } from "./access-Dl13x6mB.js";
2
+ export { DEFAULT_POLICY, DELEGABLE_ROLES, EVERYWHERE, EVERY_ENVIRONMENT, FIXED_ROLES, INSTANCE_ROLES, INSTANCE_ROLE_NAMES, MAX_PERSONAL_TOKEN_MINUTES, MAX_SCOPE_PLACES, NOTHING, PERMISSIONS, PROJECT_ONLY_PERMISSIONS, ROLES, ROLE_ID, ROLE_NAMES, UNWRITTEN_FORK_MS, administers, admits, allows, assignableToEnvironment, convertEveryProjectGrants, covers, credentialScopeInWords, definitionOf, definitionProblem, delegable, delegableRoles, givesService, grantKind, guarded, heldColumns, holds, inScope, instanceRoleGrants, isBuiltinRole, isCredentialScope, isInstanceRole, isRole, isScope, issuedGrant, issuesCredential, makesProjects, managesService, mayManageAccess, normalDefinition, normalScope, ownsInstance, protectedWithin, protects, readsWholeLog, roleGrants, runsInstance, scopeDefinition, scopeInWords, setsUpServices, unscoped, withinCredential };
@@ -1,4 +1,4 @@
1
- import { d as checkContext, f as encodeAad, l as AAD_VERSION, p as ownerKey, t as KekRegistry, u as SecretContext } from "./registry-Bd7hPoY4.js";
1
+ import { d as checkContext, f as encodeAad, l as AAD_VERSION, p as ownerKey, t as KekRegistry, u as SecretContext } from "./registry-D7X-x202.js";
2
2
  //#region src/envelope.d.ts
3
3
  /**
4
4
  * Current envelope format version. Stored on every row so the format can change
@@ -27,17 +27,20 @@ export type Sealed = Pick<Envelope, 'envelopeVersion' | 'iv' | 'authTag' | 'ciph
27
27
  /** A fresh data key. One per version, so no key ever encrypts two values. */
28
28
  export declare function freshDek(): Buffer;
29
29
  /**
30
- * Encrypt a secret value under `dek`, bound to `ctx`. Whoever holds the key
31
- * encryption key wraps `dek` separately: the vault, in coffre.
30
+ * Encrypt a secret value under `dek`, bound to `ctx`'s ids. Whoever holds
31
+ * the key encryption key wraps `dek` separately: the vault, in coffre, whose
32
+ * wrap binds the owner and sensitivity too.
32
33
  */
33
- export declare function encrypt(plaintext: Buffer, ctx: SecretContext, dek: Buffer): Sealed;
34
+ export declare function encrypt(plaintext: Buffer, ctx: SecretIds, dek: Buffer): Sealed;
34
35
  /**
35
36
  * Decrypt with an unwrapped `dek`. Throws if the envelope was sealed for a
36
37
  * different project, environment or secret than `ctx` describes, because
37
38
  * the AAD will not match. This is the property that makes relocating a
38
39
  * ciphertext across environments fail closed.
39
40
  */
40
- export declare function decrypt(envelope: Sealed, ctx: SecretContext, dek: Buffer): Buffer;
41
+ export declare function decrypt(envelope: Sealed, ctx: SecretIds, dek: Buffer): Buffer;
42
+ /** What a value's own ciphertext binds: the secret's ids, never its owner or sensitivity, which only the wrap of its key does. */
43
+ export type SecretIds = Pick<SecretContext, 'projectId' | 'environmentId' | 'secretId'>;
41
44
  /**
42
45
  * Encrypt a secret value under a fresh DEK, and wrap that DEK under the
43
46
  * registry's primary KEK: `encrypt` and the wrap in one, where both halves
package/dist/envelope.js CHANGED
@@ -1,4 +1,4 @@
1
- import { a as AAD_VERSION, c as ownerKey, o as checkContext, s as encodeAad } from "./types-E1Sz50n6.js";
1
+ import { a as AAD_VERSION, c as ownerKey, o as checkContext, s as encodeAad } from "./types-Cp3lB_-W.js";
2
2
  import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
3
3
  //#region src/envelope.ts
4
4
  const IV_BYTES = 12;
@@ -13,13 +13,14 @@ function freshDek() {
13
13
  return randomBytes(32);
14
14
  }
15
15
  /**
16
- * Encrypt a secret value under `dek`, bound to `ctx`. Whoever holds the key
17
- * encryption key wraps `dek` separately: the vault, in coffre.
16
+ * Encrypt a secret value under `dek`, bound to `ctx`'s ids. Whoever holds
17
+ * the key encryption key wraps `dek` separately: the vault, in coffre, whose
18
+ * wrap binds the owner and sensitivity too.
18
19
  */
19
20
  function encrypt(plaintext, ctx, dek) {
20
21
  const iv = randomBytes(IV_BYTES);
21
22
  const cipher = createCipheriv("aes-256-gcm", dek, iv);
22
- cipher.setAAD(encodeAad(ctx));
23
+ cipher.setAAD(valueAad(ctx));
23
24
  const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
24
25
  return {
25
26
  envelopeVersion: 1,
@@ -39,10 +40,17 @@ function decrypt(envelope, ctx, dek) {
39
40
  if (envelope.iv.length !== IV_BYTES) throw new Error("envelope IV has the wrong length");
40
41
  if (envelope.authTag.length !== TAG_BYTES) throw new Error("envelope auth tag has the wrong length");
41
42
  const decipher = createDecipheriv("aes-256-gcm", dek, envelope.iv);
42
- decipher.setAAD(encodeAad(ctx));
43
+ decipher.setAAD(valueAad(ctx));
43
44
  decipher.setAuthTag(envelope.authTag);
44
45
  return Buffer.concat([decipher.update(envelope.ciphertext), decipher.final()]);
45
46
  }
47
+ function valueAad(ctx) {
48
+ return encodeAad({
49
+ projectId: ctx.projectId,
50
+ environmentId: ctx.environmentId,
51
+ secretId: ctx.secretId
52
+ });
53
+ }
46
54
  /**
47
55
  * Encrypt a secret value under a fresh DEK, and wrap that DEK under the
48
56
  * registry's primary KEK: `encrypt` and the wrap in one, where both halves
@@ -608,7 +608,7 @@ export declare function unseal<T>(key: Uint8Array, sealed: string | undefined |
608
608
  * service tokens, personal tokens, which act as the person who made
609
609
  * them within the scope they named, and handles, which reach nothing but
610
610
  * the credential proxy (`@coffre/server/plugins/credential-proxy`): the
611
- * stand-in a person's app holds in place of a secret's value.
611
+ * placeholder a person's app holds in place of a secret's value.
612
612
  *
613
613
  * A token is a kind prefix and 32 random bytes, e.g.
614
614
  * `coffre_cli_Qm9Y...`. The prefix tells a person (or a secret scanner) what a
package/dist/kek.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as KekProvider, c as WrappedDek, i as KekCancelledError, n as DEK_BYTES, o as KekUnavailableError, r as KekBadClaimError, s as KeyOperation, t as KekRegistry, u as SecretContext } from "./registry-Bd7hPoY4.js";
1
+ import { a as KekProvider, c as WrappedDek, i as KekCancelledError, n as DEK_BYTES, o as KekUnavailableError, r as KekBadClaimError, s as KeyOperation, t as KekRegistry, u as SecretContext } from "./registry-D7X-x202.js";
2
2
  //#region src/kek/local.d.ts
3
3
  /**
4
4
  * A KEK held in local process memory, loaded from configuration.
package/dist/kek.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { deriveLogKey } from "./audit.js";
2
- import { i as KekUnavailableError, n as KekBadClaimError, o as checkContext, r as KekCancelledError, s as encodeAad, t as DEK_BYTES } from "./types-E1Sz50n6.js";
2
+ import { i as KekUnavailableError, n as KekBadClaimError, o as checkContext, r as KekCancelledError, s as encodeAad, t as DEK_BYTES } from "./types-Cp3lB_-W.js";
3
3
  import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
4
4
  //#region src/kek/cancellation.ts
5
5
  function checkOperation(operation, uncertain = false) {
@@ -426,7 +426,8 @@ function encryptionContext(ctx) {
426
426
  "coffre:project": ctx.projectId,
427
427
  "coffre:environment": ctx.environmentId,
428
428
  "coffre:secret": ctx.secretId,
429
- ...ctx.owner === void 0 ? {} : { "coffre:owner": ctx.owner }
429
+ ...ctx.owner === void 0 ? {} : { "coffre:owner": ctx.owner },
430
+ ...ctx.sensitive === true ? { "coffre:sensitive": "true" } : {}
430
431
  };
431
432
  }
432
433
  /**