@coffre/core 0.10.2 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/workloads.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { shownMember } from "./schemas.js";
1
2
  //#region src/identity/workloads.ts
2
3
  /**
3
4
  * Trust bindings: which CI runs may sign in as a service, by the ID token
@@ -27,13 +28,53 @@
27
28
  const GITHUB_ISSUER = "https://token.actions.githubusercontent.com";
28
29
  const GITLAB_ISSUER = "https://gitlab.com";
29
30
  const WORKLOAD_PROFILES = [
31
+ "github",
32
+ "github-reusable",
33
+ "github-reusable-organization",
34
+ "gitlab",
35
+ "custom",
36
+ "kubernetes",
37
+ "gcp",
38
+ "azure",
39
+ "fly",
40
+ "vercel",
41
+ "aws",
42
+ "enrollment",
43
+ "cloudflare"
44
+ ];
45
+ /**
46
+ * The profiles `/api/auth/oidc` exchanges for a bearer credential, and only
47
+ * on a binding with no policy: a platform's token bought at that route reads
48
+ * values, so a binding made for a key-bound session, policy and all, is never
49
+ * one it matches. Every binding of another profile carries a policy.
50
+ */
51
+ const BEARER_PROFILES = [
30
52
  "github",
31
53
  "github-reusable",
32
54
  "github-reusable-organization",
33
55
  "gitlab",
34
56
  "custom"
35
57
  ];
36
- /** The claims each profile requires; a binding may add more, to narrow it. */
58
+ /** Whether a binding's profile is one `/api/auth/oidc` takes; it takes the binding only if it has no policy, too. */
59
+ function isBearerProfile(profile) {
60
+ return BEARER_PROFILES.includes(profile);
61
+ }
62
+ /** The profiles whose proof is no ID token: their binding's issuer is this URN, which no token names, and its keys none. */
63
+ const PROFILE_URNS = {
64
+ aws: "urn:coffre:aws",
65
+ enrollment: "urn:coffre:enrollment",
66
+ cloudflare: "urn:coffre:cloudflare"
67
+ };
68
+ /** The profiles whose proof is a platform's ID token. */
69
+ const OIDC_PROFILES = WORKLOAD_PROFILES.filter((profile) => !Object.hasOwn(PROFILE_URNS, profile));
70
+ /**
71
+ * Tokens a platform hands out again, the same one to every replica and
72
+ * restart for its lifetime: a managed identity's. Spending one would
73
+ * refuse them, so none is spent: it is a bearer until its own `exp`, which
74
+ * ends the session it makes (section 4.2).
75
+ */
76
+ const REPEATING_PROFILES = ["azure"];
77
+ /** The claims each profile requires; a binding may add more, to narrow it. An `aws` binding's are the role's account and unique ID. */
37
78
  const REQUIRED_CLAIMS = {
38
79
  github: [
39
80
  "repository_owner_id",
@@ -64,7 +105,19 @@ const REQUIRED_CLAIMS = {
64
105
  "ref",
65
106
  "pipeline_source"
66
107
  ],
67
- custom: ["sub"]
108
+ custom: ["sub"],
109
+ kubernetes: ["sub", "kubernetes.io.serviceaccount.uid"],
110
+ gcp: ["sub"],
111
+ azure: ["tid", "oid"],
112
+ fly: ["org_id", "app_id"],
113
+ vercel: [
114
+ "owner_id",
115
+ "project_id",
116
+ "environment"
117
+ ],
118
+ aws: ["account", "role_id"],
119
+ enrollment: [],
120
+ cloudflare: []
68
121
  };
69
122
  /**
70
123
  * The events a GitHub binding may trust. The first four are started by
@@ -121,7 +174,15 @@ const EVENT_CLAIM = {
121
174
  values: GITLAB_PIPELINE_SOURCES,
122
175
  why: "merge-request pipelines name another project"
123
176
  },
124
- custom: null
177
+ custom: null,
178
+ kubernetes: null,
179
+ gcp: null,
180
+ azure: null,
181
+ fly: null,
182
+ vercel: null,
183
+ aws: null,
184
+ enrollment: null,
185
+ cloudflare: null
125
186
  };
126
187
  /** At most this many live bindings per service, and claims per binding. */
127
188
  const MAX_BINDINGS = 16;
@@ -164,12 +225,25 @@ function forcedProfiles(issuer) {
164
225
  "github-reusable-organization"
165
226
  ];
166
227
  if (issuer === "https://gitlab.com") return ["gitlab"];
228
+ if (issuer === "https://accounts.google.com") return ["gcp"];
229
+ if (issuer.startsWith("https://oidc.fly.io/")) return ["fly"];
230
+ if (issuer === "https://oidc.vercel.com" || issuer.startsWith("https://oidc.vercel.com/")) return ["vercel"];
231
+ if (issuer.startsWith("https://sts.windows.net/") || issuer.startsWith("https://login.microsoftonline.com/")) return ["azure"];
167
232
  return null;
168
233
  }
169
- /** The issuer a profile means when none is given: github.com's or gitlab.com's. A GitHub Enterprise Server or a self-managed GitLab names its own. */
234
+ /** Google's, for every `gcp` binding. */
235
+ const GOOGLE_ISSUER = "https://accounts.google.com";
236
+ /**
237
+ * The issuer a profile means when none is given: github.com's, gitlab.com's
238
+ * or Google's, or the URN of a profile that is no ID token. A GitHub
239
+ * Enterprise Server, a self-managed GitLab, a cluster, a tenant or a team
240
+ * names its own.
241
+ */
170
242
  function defaultIssuer(profile) {
243
+ if (Object.hasOwn(PROFILE_URNS, profile)) return PROFILE_URNS[profile];
244
+ if (profile.startsWith("github")) return GITHUB_ISSUER;
171
245
  if (profile === "gitlab") return GITLAB_ISSUER;
172
- return profile === "custom" ? null : GITHUB_ISSUER;
246
+ return profile === "gcp" ? GOOGLE_ISSUER : null;
173
247
  }
174
248
  /** The longest issuer a binding with uploaded keys names, in bytes. */
175
249
  const MAX_ISSUER_BYTES = 400;
@@ -183,13 +257,18 @@ function checkBinding(input, options = {}) {
183
257
  const profile = input.profile;
184
258
  if (!WORKLOAD_PROFILES.includes(profile)) throw new BindingInvalid(`unknown profile "${input.profile}": one of ${WORKLOAD_PROFILES.join(", ")}`);
185
259
  const issuer = input.issuer ?? defaultIssuer(profile);
186
- if (issuer === null) throw new BindingInvalid("a custom binding names its issuer");
187
- if (options.uploaded === true) checkIssuerName(issuer);
260
+ if (issuer === null) throw new BindingInvalid(`a ${profile} binding names its issuer`);
261
+ const urn = defaultIssuer(profile);
262
+ if (Object.hasOwn(PROFILE_URNS, profile)) {
263
+ if (issuer !== urn) throw new BindingInvalid(`a ${profile} binding's issuer is ${urn}`);
264
+ if (options.uploaded === true) throw new BindingInvalid(`a ${profile} binding carries no keys: its proof is no ID token`);
265
+ } else if (options.uploaded === true) checkIssuerName(issuer);
188
266
  else checkFetchUrl(issuer, "the issuer", {
189
267
  allowLoopback: options.allowLoopback,
190
268
  path: true,
191
269
  query: false
192
270
  });
271
+ if (profile === "gcp" && issuer !== "https://accounts.google.com") throw new BindingInvalid(`a gcp binding's issuer is ${GOOGLE_ISSUER}`);
193
272
  const forced = forcedProfiles(issuer);
194
273
  if (forced !== null && !forced.includes(profile)) throw new BindingInvalid(`${issuer} signs only for the ${forced[0]} profile${forced.length > 1 ? "s" : ""}`);
195
274
  const entries = Object.entries(input.claims);
@@ -197,7 +276,7 @@ function checkBinding(input, options = {}) {
197
276
  const event = EVENT_CLAIM[profile];
198
277
  const claims = {};
199
278
  for (const [name, value] of entries.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) {
200
- if (!CLAIM_NAME.test(name)) throw new BindingInvalid(`"${name}" is not a claim name a binding can match`);
279
+ if (!CLAIM_NAME.test(name) || name === "__proto__") throw new BindingInvalid(`"${name}" is not a claim name a binding can match`);
201
280
  if (RESERVED.has(name)) throw new BindingInvalid(`a binding never names "${name}": the exchange checks it`);
202
281
  if (name === event?.name) {
203
282
  const given = Array.isArray(value) ? value : [value];
@@ -210,6 +289,7 @@ function checkBinding(input, options = {}) {
210
289
  if (new TextEncoder().encode(value).length > 256) throw new BindingInvalid(`claim ${name} is longer than 256 bytes`);
211
290
  claims[name] = value;
212
291
  }
292
+ if (REQUIRED_CLAIMS[profile].length === 0 && entries.length > 0) throw new BindingInvalid(`a ${profile} binding names no claims: its proof carries none`);
213
293
  const missing = REQUIRED_CLAIMS[profile].filter((name) => claims[name] === void 0);
214
294
  if (missing.length > 0) throw new BindingInvalid(`the ${profile} profile requires ${missing.join(", ")}`);
215
295
  if (profile === "github-reusable-organization" && claims.repository_id !== void 0) throw new BindingInvalid("a binding for any repository of the organization names no repository_id: use github-reusable");
@@ -231,6 +311,7 @@ function checkIssuerName(issuer) {
231
311
  if (issuer === "" || /^\s|\s$/.test(issuer) || /\p{Cc}/u.test(issuer)) throw new BindingInvalid("the issuer is its tokens' iss, exactly: no control character, no space at either end");
232
312
  if (new TextEncoder().encode(issuer).length > 400) throw new BindingInvalid(`the issuer is longer than 400 bytes`);
233
313
  if (issuer === "https://token.actions.githubusercontent.com" || issuer === "https://gitlab.com") throw new BindingInvalid(`${issuer} publishes its keys and rotates them: coffre fetches them, so upload none`);
314
+ if (issuer.startsWith("urn:coffre:")) throw new BindingInvalid(`${issuer} is coffre's own, for a binding that is no ID token's`);
234
315
  }
235
316
  /** A claim's values, one or several. */
236
317
  function claimValues(value) {
@@ -342,7 +423,7 @@ function atBaseBranch(claims) {
342
423
  const { event_name: event, ref, base_ref: base, workflow_ref: workflow } = claims;
343
424
  if (event !== "pull_request" || typeof ref !== "string" || typeof base !== "string" || base === "") return claims;
344
425
  const at = `refs/heads/${base}`;
345
- const file = typeof workflow === "string" && workflow.endsWith(`@${ref}`) ? `${workflow.slice(0, -ref.length)}${at}` : workflow;
426
+ const file = typeof workflow === "string" && workflow.endsWith(`@${ref}`) ? `${workflow.slice(0, workflow.length - ref.length)}${at}` : workflow;
346
427
  return {
347
428
  ...claims,
348
429
  ref: at,
@@ -458,5 +539,309 @@ function gitlabProject(input) {
458
539
  }
459
540
  };
460
541
  }
542
+ /** What a field left out of a policy means. */
543
+ const POLICY_DEFAULTS = {
544
+ keyAudience: false,
545
+ sessionHours: 24,
546
+ sessionDays: 30,
547
+ overlapMinutes: 15,
548
+ approve: false,
549
+ onContention: "freeze",
550
+ unsignedUploads: false
551
+ };
552
+ /** The most environments, and keys in each, a binding's `reads` lists; the most `sources`. */
553
+ const MAX_READ_PLACES = 64;
554
+ const MAX_READ_KEYS = 256;
555
+ const MAX_SOURCES = 16;
556
+ const SESSION_FIELDS = [
557
+ "reads",
558
+ "sources",
559
+ "sessionHours",
560
+ "unsignedUploads"
561
+ ];
562
+ const KEY_AUDIENCE = [...SESSION_FIELDS, "keyAudience"];
563
+ /**
564
+ * The profiles whose caller picks the token's audience, so their binding's
565
+ * policy always says `keyAudience`: `checkPolicy` sets it, and refuses it off.
566
+ * A Vercel function picks one by exchanging its own token at
567
+ * `https://oidc.vercel.com/~token`, as `@vercel/oidc`'s
568
+ * `getVercelOidcToken({ audience })` does.
569
+ */
570
+ const KEY_NAMING_PROFILES = [
571
+ "github",
572
+ "github-reusable",
573
+ "github-reusable-organization",
574
+ "gcp",
575
+ "fly",
576
+ "vercel"
577
+ ];
578
+ /** The fields each profile's policy may hold. */
579
+ const POLICY_FIELDS = {
580
+ github: KEY_AUDIENCE,
581
+ "github-reusable": KEY_AUDIENCE,
582
+ "github-reusable-organization": KEY_AUDIENCE,
583
+ gitlab: SESSION_FIELDS,
584
+ custom: KEY_AUDIENCE,
585
+ kubernetes: SESSION_FIELDS,
586
+ gcp: KEY_AUDIENCE,
587
+ azure: [...SESSION_FIELDS, "audience"],
588
+ fly: KEY_AUDIENCE,
589
+ vercel: KEY_AUDIENCE,
590
+ aws: SESSION_FIELDS,
591
+ enrollment: [
592
+ "reads",
593
+ "sources",
594
+ "replicas",
595
+ "sessionDays",
596
+ "overlapMinutes",
597
+ "mintedBy",
598
+ "approve",
599
+ "onContention",
600
+ "unsignedUploads"
601
+ ],
602
+ cloudflare: ["reads"]
603
+ };
604
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
605
+ /** A credential's name in the proxy's config: a minter's, a signer's, an upstream's. */
606
+ const CREDENTIAL_NAME = /^[a-z][a-z0-9-]{0,31}$/;
607
+ /** A service's name after `token:`, as a member names it. */
608
+ const SERVICE_NAME = /^[^\s\p{Cc}]{1,320}$/u;
609
+ function integer(value, name, min, max) {
610
+ if (!Number.isSafeInteger(value) || value < min || value > max) throw new BindingInvalid(`${name} is a whole number from ${min} to ${max}`);
611
+ return value;
612
+ }
613
+ function sortedUnique(values) {
614
+ return [...new Set(values)].sort();
615
+ }
616
+ function uuidList(value, what, max) {
617
+ if (!Array.isArray(value) || value.length > max || value.some((id) => typeof id !== "string" || !UUID.test(id.toLowerCase()))) throw new BindingInvalid(`${what} lists at most ${max} IDs`);
618
+ return sortedUnique(value.map((id) => id.toLowerCase()));
619
+ }
620
+ function checkReads(value) {
621
+ if (typeof value !== "object" || value === null || Array.isArray(value)) throw new BindingInvalid("reads maps environment IDs to { keys, mints }");
622
+ const entries = Object.entries(value);
623
+ if (entries.length > 64) throw new BindingInvalid(`reads names at most 64 environments`);
624
+ const reads = {};
625
+ const seen = /* @__PURE__ */ new Set();
626
+ for (const [environment, entry] of entries) {
627
+ const id = environment.toLowerCase();
628
+ if (!UUID.test(id)) throw new BindingInvalid(`reads names environments by ID: ${environment} is none`);
629
+ if (seen.has(id)) throw new BindingInvalid(`reads names ${id} twice`);
630
+ seen.add(id);
631
+ if (typeof entry !== "object" || entry === null || Array.isArray(entry)) throw new BindingInvalid("each environment of reads is { keys, mints }");
632
+ const { keys, mints, ...rest } = entry;
633
+ if (Object.keys(rest).length > 0) throw new BindingInvalid(`each environment of reads holds keys and mints, not ${Object.keys(rest).join(", ")}`);
634
+ const read = {};
635
+ if (keys !== void 0) {
636
+ const listed = keys === "*" ? "*" : uuidList(keys, "reads.keys", 256);
637
+ if (listed === "*" || listed.length > 0) read.keys = listed;
638
+ }
639
+ if (mints !== void 0) {
640
+ if (!Array.isArray(mints) || mints.length > 256 || mints.some((name) => typeof name !== "string" || !CREDENTIAL_NAME.test(name))) throw new BindingInvalid("reads.mints lists minters by name");
641
+ if (mints.length > 0) read.mints = sortedUnique(mints);
642
+ }
643
+ if (read.keys !== void 0 || read.mints !== void 0) reads[id] = read;
644
+ }
645
+ return Object.keys(reads).length === 0 ? void 0 : reads;
646
+ }
647
+ /**
648
+ * Check a binding's policy against its profile and return it canonical:
649
+ * every field its profile allows, valid, those at their default left out,
650
+ * lists sorted and IDs in lower case, so that one policy has one spelling
651
+ * (`policyText`). `service` is the binding's own, `token:<name>`, which its
652
+ * `mintedBy` may not name: a session of the service would then mint codes
653
+ * for itself, and outlive every deploy. Throws `BindingInvalid`.
654
+ */
655
+ function checkPolicy(profile, input, options) {
656
+ if (input === void 0 || input === null) input = {};
657
+ if (typeof input !== "object" || Array.isArray(input)) throw new BindingInvalid("a policy is an object");
658
+ const given = input;
659
+ const allowed = POLICY_FIELDS[profile];
660
+ const extra = Object.keys(given).filter((name) => given[name] !== void 0 && !allowed.includes(name));
661
+ if (extra.length > 0) throw new BindingInvalid(`a ${profile} binding's policy holds ${allowed.join(", ") || "nothing"}, not ${extra.join(", ")}`);
662
+ const policy = {};
663
+ if (given.audience !== void 0) {
664
+ if (typeof given.audience !== "string" || given.audience === "" || given.audience.length > 400 || /\p{Cc}|^\s|\s$/u.test(given.audience)) throw new BindingInvalid("audience is the exact audience the platform puts in its tokens");
665
+ policy.audience = given.audience;
666
+ } else if (allowed.includes("audience")) throw new BindingInvalid(`a ${profile} binding names the audience its platform puts in its tokens: ${profile} lets no caller choose one`);
667
+ if (given.keyAudience !== void 0 && typeof given.keyAudience !== "boolean") throw new BindingInvalid("keyAudience is true or false");
668
+ if (KEY_NAMING_PROFILES.includes(profile)) {
669
+ if (given.keyAudience === false) throw new BindingInvalid(`a ${profile} binding's tokens name the session's key in their audience: ${profile} lets its caller choose one, so a copied token works for no other key`);
670
+ policy.keyAudience = true;
671
+ } else if (given.keyAudience === true) policy.keyAudience = true;
672
+ if (given.reads !== void 0) {
673
+ const reads = checkReads(given.reads);
674
+ if (reads !== void 0) policy.reads = reads;
675
+ }
676
+ if (given.sources !== void 0) {
677
+ if (!Array.isArray(given.sources) || given.sources.length > 16) throw new BindingInvalid(`sources lists at most 16 CIDRs`);
678
+ const sources = sortedUnique(given.sources.map((source) => {
679
+ const block = typeof source === "string" ? parseCidr(source) : null;
680
+ if (block === null) throw new BindingInvalid(`sources lists CIDRs, such as 162.220.232.0/24 or 2001:db8::/32: ${String(source)} is none`);
681
+ return block.text;
682
+ }));
683
+ if (sources.length > 0) policy.sources = sources;
684
+ }
685
+ const number = (name, min, max) => {
686
+ if (given[name] === void 0) return;
687
+ const value = integer(given[name], name, min, max);
688
+ if (name === "replicas" || value !== POLICY_DEFAULTS[name]) policy[name] = value;
689
+ };
690
+ number("sessionHours", 1, 168);
691
+ number("sessionDays", 1, 90);
692
+ number("overlapMinutes", 1, 60);
693
+ number("replicas", 1, 64);
694
+ if (profile === "enrollment" && policy.replicas === void 0) throw new BindingInvalid("an enrollment binding says how many replicas run, its replicas: a code admits twice that many sessions");
695
+ if (given.mintedBy !== void 0) {
696
+ const named = typeof given.mintedBy === "string" ? given.mintedBy.startsWith("token:") ? given.mintedBy : `token:${given.mintedBy}` : "";
697
+ if (!SERVICE_NAME.test(named.slice(6))) throw new BindingInvalid("mintedBy names a service account");
698
+ if (named === options.service) throw new BindingInvalid(`mintedBy names another service than ${shownMember(options.service)}: a session of its own would mint codes for itself`);
699
+ policy.mintedBy = named;
700
+ }
701
+ for (const flag of ["approve", "unsignedUploads"]) {
702
+ if (given[flag] === void 0) continue;
703
+ if (typeof given[flag] !== "boolean") throw new BindingInvalid(`${flag} is true or false`);
704
+ if (given[flag]) policy[flag] = true;
705
+ }
706
+ if (given.onContention !== void 0) {
707
+ if (given.onContention !== "freeze" && given.onContention !== "alert") throw new BindingInvalid("onContention is freeze or alert");
708
+ if (given.onContention === "alert") policy.onContention = "alert";
709
+ }
710
+ return policy;
711
+ }
712
+ /** A value as canonical JSON: every object's keys sorted, at every depth. */
713
+ function canonicalJson(value) {
714
+ if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
715
+ if (typeof value === "object" && value !== null) return `{${Object.entries(value).filter(([, item]) => item !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, item]) => `${JSON.stringify(key)}:${canonicalJson(item)}`).join(",")}}`;
716
+ return JSON.stringify(value);
717
+ }
718
+ /** A policy as the binding's `policy` column and its MAC hold it: `checkPolicy`'s result, as canonical JSON. */
719
+ function policyText(policy) {
720
+ return canonicalJson(policy);
721
+ }
722
+ function ipv4(text) {
723
+ const parts = text.split(".");
724
+ if (parts.length !== 4 || parts.some((part) => !/^(0|[1-9][0-9]{0,2})$/.test(part) || Number(part) > 255)) return null;
725
+ return Uint8Array.from(parts.map(Number));
726
+ }
727
+ function ipv6(text) {
728
+ const halves = text.toLowerCase().split("::");
729
+ if (halves.length > 2) return null;
730
+ const groups = halves.map((half) => half === "" ? [] : half.split(":"));
731
+ if (groups.flat().some((group) => !/^[0-9a-f]{1,4}$/.test(group))) return null;
732
+ const count = groups.flat().length;
733
+ if (halves.length === 1 ? count !== 8 : count > 7) return null;
734
+ const words = halves.length === 1 ? groups[0] : [
735
+ ...groups[0],
736
+ ...Array(8 - count).fill("0"),
737
+ ...groups[1]
738
+ ];
739
+ const bytes = /* @__PURE__ */ new Uint8Array(16);
740
+ words.forEach((word, i) => {
741
+ const value = parseInt(word, 16);
742
+ bytes[2 * i] = value >> 8;
743
+ bytes[2 * i + 1] = value & 255;
744
+ });
745
+ return bytes;
746
+ }
747
+ /** An address, IPv4 dotted or IPv6 in hex groups; null for anything else. */
748
+ function parseAddress(text) {
749
+ const mapped = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i.exec(text);
750
+ if (mapped !== null) return parseAddress(mapped[1]);
751
+ const v4 = ipv4(text);
752
+ if (v4 !== null) return {
753
+ version: 4,
754
+ bytes: v4
755
+ };
756
+ const v6 = text.includes(":") ? ipv6(text) : null;
757
+ return v6 === null ? null : {
758
+ version: 6,
759
+ bytes: v6
760
+ };
761
+ }
762
+ /** IPv6 as RFC 5952 writes it: lower case, no leading zeros, the longest run of two or more zero groups as `::`, the first of equals. */
763
+ function ipv6Text(bytes) {
764
+ const words = Array.from({ length: 8 }, (_, i) => bytes[2 * i] << 8 | bytes[2 * i + 1]);
765
+ let best = {
766
+ at: -1,
767
+ length: 0
768
+ };
769
+ for (let i = 0; i < 8;) {
770
+ if (words[i] !== 0) {
771
+ i++;
772
+ continue;
773
+ }
774
+ let j = i;
775
+ while (j < 8 && words[j] === 0) j++;
776
+ if (j - i > best.length) best = {
777
+ at: i,
778
+ length: j - i
779
+ };
780
+ i = j;
781
+ }
782
+ const hexes = words.map((word) => word.toString(16));
783
+ if (best.length < 2) return hexes.join(":");
784
+ return `${hexes.slice(0, best.at).join(":")}::${hexes.slice(best.at + best.length).join(":")}`;
785
+ }
786
+ /**
787
+ * A CIDR, `162.220.232.0/24` or `2001:db8::/32`, as its one spelling; null
788
+ * for anything else, a block with host bits set included, since
789
+ * `162.220.232.5/24` most likely means another block than it says.
790
+ */
791
+ function parseCidr(text) {
792
+ const slash = text.indexOf("/");
793
+ if (slash < 0 || !/^(0|[1-9][0-9]{0,2})$/.test(text.slice(slash + 1))) return null;
794
+ const address = parseAddress(text.slice(0, slash));
795
+ const bits = Number(text.slice(slash + 1));
796
+ if (address === null || bits > address.bytes.length * 8) return null;
797
+ for (let bit = bits; bit < address.bytes.length * 8; bit++) if (address.bytes[bit >> 3] >> 7 - (bit & 7) & 1) return null;
798
+ const shown = address.version === 4 ? Array.from(address.bytes).join(".") : ipv6Text(address.bytes);
799
+ return {
800
+ ...address,
801
+ bits,
802
+ text: `${shown}/${bits}`
803
+ };
804
+ }
805
+ /** Whether an address is within any of these blocks, each `parseCidr`'s: never for an address that does not parse, or of the other version. */
806
+ function addressWithin(address, sources) {
807
+ const parsed = parseAddress(address);
808
+ if (parsed === null) return false;
809
+ return sources.some((source) => {
810
+ const block = parseCidr(source);
811
+ if (block === null || block.version !== parsed.version) return false;
812
+ for (let bit = 0; bit < block.bits; bit++) {
813
+ const mask = 1 << 7 - (bit & 7);
814
+ if ((block.bytes[bit >> 3] & mask) !== (parsed.bytes[bit >> 3] & mask)) return false;
815
+ }
816
+ return true;
817
+ });
818
+ }
819
+ /**
820
+ * What a freeze names, in its one spelling (section 8.2): every workload's
821
+ * use, one service, one binding, one session, or one upstream credential of
822
+ * an environment, by the environment's ID so a rename moves nothing.
823
+ *
824
+ * freezeTarget('Binding:0F0E…') → null freezeTarget('binding:0f0e…') → 'binding:0f0e…'
825
+ * freezeTarget('credential:6B1C…-…/stripe') → 'credential:6b1c…-…/stripe'
826
+ */
827
+ function freezeTarget(text) {
828
+ if (text === "instance") return text;
829
+ const colon = text.indexOf(":");
830
+ if (colon < 0) return null;
831
+ const kind = text.slice(0, colon);
832
+ const rest = text.slice(colon + 1);
833
+ switch (kind) {
834
+ case "token":
835
+ case "service": return SERVICE_NAME.test(rest) ? `token:${rest}` : null;
836
+ case "binding":
837
+ case "session": return UUID.test(rest.toLowerCase()) ? `${kind}:${rest.toLowerCase()}` : null;
838
+ case "credential": {
839
+ const [environment, name, ...more] = rest.split("/");
840
+ if (more.length > 0 || environment === void 0 || name === void 0) return null;
841
+ return UUID.test(environment.toLowerCase()) && CREDENTIAL_NAME.test(name) ? `credential:${environment.toLowerCase()}/${name}` : null;
842
+ }
843
+ default: return null;
844
+ }
845
+ }
461
846
  //#endregion
462
- export { BindingInvalid, EVENT_EXPOSURE, GITHUB_EVENTS, GITHUB_ISSUER, GITLAB_ISSUER, GITLAB_PIPELINE_SOURCES, MAX_BINDINGS, MAX_CLAIMS, MAX_CLAIM_BYTES, MAX_ISSUER_BYTES, REQUIRED_CLAIMS, WORKLOAD_PROFILES, canonicalClaims, checkBinding, checkFetchUrl, claimAt, claimValues, defaultIssuer, differingClaims, fullRef, githubEventsAt, githubReusable, githubWorkflow, gitlabProject, workflowRef };
847
+ export { BEARER_PROFILES, BindingInvalid, CREDENTIAL_NAME, EVENT_EXPOSURE, GITHUB_EVENTS, GITHUB_ISSUER, GITLAB_ISSUER, GITLAB_PIPELINE_SOURCES, GOOGLE_ISSUER, KEY_NAMING_PROFILES, MAX_BINDINGS, MAX_CLAIMS, MAX_CLAIM_BYTES, MAX_ISSUER_BYTES, MAX_READ_KEYS, MAX_READ_PLACES, MAX_SOURCES, OIDC_PROFILES, POLICY_DEFAULTS, POLICY_FIELDS, PROFILE_URNS, REPEATING_PROFILES, REQUIRED_CLAIMS, WORKLOAD_PROFILES, addressWithin, canonicalClaims, checkBinding, checkFetchUrl, checkPolicy, claimAt, claimValues, defaultIssuer, differingClaims, freezeTarget, fullRef, githubEventsAt, githubReusable, githubWorkflow, gitlabProject, isBearerProfile, parseAddress, parseCidr, policyText, workflowRef };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/core",
3
- "version": "0.10.2",
3
+ "version": "0.11.0",
4
4
  "description": "What coffre's packages share: roles, envelopes, identity, the audit chain and the vault's contract.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -82,14 +82,15 @@
82
82
  "dependencies": {
83
83
  "jose": "6.2.3",
84
84
  "oauth4webapi": "3.8.8",
85
- "zod": "4.4.3"
85
+ "zod": "4.4.3",
86
+ "@coffre/client": "0.11.0"
86
87
  },
87
88
  "devDependencies": {
88
89
  "@hegeldev/hegel": "0.4.7",
89
90
  "@types/node": "26.1.1",
90
91
  "tsdown": "0.23.0",
91
92
  "typescript": "5.9.3",
92
- "@coffre/conformance": "0.10.2"
93
+ "@coffre/conformance": "0.11.0"
93
94
  },
94
95
  "scripts": {
95
96
  "build": "tsdown",