@fabrika/auth-core 0.9.0-dev.2 → 0.9.0-dev.20

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.
Files changed (47) hide show
  1. package/package.json +2 -2
  2. package/src/canonical-path.ts +18 -0
  3. package/src/client-address.ts +74 -0
  4. package/src/contract-canonical.ts +152 -1
  5. package/src/contract-encoding.ts +181 -0
  6. package/src/contract.ts +201 -9
  7. package/src/dev-environment.ts +6 -0
  8. package/src/egress.ts +97 -0
  9. package/src/endpoint-digest.ts +22 -2
  10. package/src/framework-release.ts +2 -0
  11. package/src/framework-variables.ts +15 -0
  12. package/src/index.ts +8 -0
  13. package/src/locale.ts +1 -1
  14. package/src/managed-environment.ts +140 -0
  15. package/src/membership.ts +12 -4
  16. package/src/ops.ts +1933 -0
  17. package/src/public-egress.ts +168 -0
  18. package/src/requirement.ts +5 -2
  19. package/src/rpc.ts +8 -8
  20. package/src/sha256.ts +52 -14
  21. package/src/terminal.ts +105 -0
  22. package/src/token.ts +73 -13
  23. package/src/types.ts +63 -2
  24. package/src/wire.ts +7 -0
  25. package/types/src/canonical-path.d.ts +6 -0
  26. package/types/src/client-address.d.ts +7 -0
  27. package/types/src/contract-canonical.d.ts +11 -1
  28. package/types/src/contract-encoding.d.ts +18 -0
  29. package/types/src/contract.d.ts +186 -8
  30. package/types/src/dev-environment.d.ts +5 -0
  31. package/types/src/egress.d.ts +27 -0
  32. package/types/src/endpoint-digest.d.ts +5 -0
  33. package/types/src/framework-release.d.ts +2 -0
  34. package/types/src/framework-variables.d.ts +12 -1
  35. package/types/src/index.d.ts +8 -0
  36. package/types/src/locale.d.ts +1 -1
  37. package/types/src/managed-environment.d.ts +32 -0
  38. package/types/src/membership.d.ts +8 -3
  39. package/types/src/ops.d.ts +657 -0
  40. package/types/src/public-egress.d.ts +31 -0
  41. package/types/src/requirement.d.ts +2 -2
  42. package/types/src/rpc.d.ts +5 -5
  43. package/types/src/sha256.d.ts +10 -0
  44. package/types/src/terminal.d.ts +72 -0
  45. package/types/src/token.d.ts +36 -11
  46. package/types/src/types.d.ts +42 -2
  47. package/types/src/wire.d.ts +6 -0
package/src/contract.ts CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  import type { Locale } from './locale'
5
5
  import type { MailTemplates } from './mail'
6
+ import type { DeclaredEgress } from './public-egress'
7
+ import type { TerminalArtifact } from './terminal'
6
8
 
7
9
  /**
8
10
  * The artifact format this release writes. A release that changes the artifact raises it, and Control upcasts every
@@ -23,10 +25,15 @@ export interface ContractArtifact extends EndpointTable {
23
25
  /** Action → description. Exact actions only; the artifact holds no wildcard. */
24
26
  readonly actions: Readonly<Record<string, string>>
25
27
  readonly roles: Readonly<Record<string, RoleArtifact>>
28
+ /**
29
+ * The relations a table's rows confer (`relations: { owner: { dimension: 'project' } }`): a role's `via` holds its
30
+ * actions where a row names the caller. Absent unless declared, so the digest stays.
31
+ */
32
+ readonly relations?: Readonly<Record<string, RelationArtifact>>
26
33
  readonly mounts: Readonly<Record<string, MountArtifact>>
27
34
  readonly files: Readonly<Record<string, FileCollectionArtifact>>
28
35
  /**
29
- * The file kinds (`c.files({ types, maxBytes, scan? })`): classes of stored files a table's file column holds. A kind
36
+ * The file kinds (`c.files({ types, maxBytes, scan?, inline? })`): classes of stored files a table's file column holds. A kind
30
37
  * has no owner and no endpoint; the table that holds a file is its authority. Absent unless declared, so the digest stays.
31
38
  */
32
39
  readonly fileKinds?: Readonly<Record<string, FileKindArtifact>>
@@ -61,6 +68,11 @@ export interface ContractArtifact extends EndpointTable {
61
68
  readonly members?: { readonly manage: string; readonly list: string }
62
69
  /** `labels`: the application's directory shows labels to `members.list` holders. Absent unless declared, so digests stay. */
63
70
  readonly directory?: 'labels'
71
+ /**
72
+ * The page on the application's own origin that identifies a person at an enrolled terminal (ADR-0067): IAM sends a
73
+ * terminal's browser there, and only an application that declares it may assert a person. Absent unless declared.
74
+ */
75
+ readonly terminal?: TerminalArtifact
64
76
  /**
65
77
  * What each procedure of the staged kit admits and how long it runs, by endpoint id, with every default applied: the
66
78
  * rules the application applies itself, beside the row the proxy applies. Absent for a contract with no such
@@ -183,7 +195,8 @@ interface EndpointRowBase {
183
195
  readonly crossOrigin?: CrossOriginForm
184
196
  /**
185
197
  * `file`: the procedure answers a stored file (an output `file('<kind>')`), so the proxy and the application give every
186
- * answer of it, a denial included, ADR-0022's file treatment. Absent otherwise, so every other row keeps its digest.
198
+ * answer of it, a denial included, ADR-0022's file treatment. `inline-file`: the same, for a kind declared `inline:
199
+ * true`, whose XML documents are shown too. Absent otherwise, so every other row keeps its digest.
187
200
  */
188
201
  readonly response?: EndpointResponse
189
202
  /**
@@ -203,8 +216,8 @@ interface EndpointRowBase {
203
216
  /** What the proxy does with an anonymous request's `Authorization` on a row that names it: forward it to the application. */
204
217
  export type EndpointAuthorization = 'forward'
205
218
 
206
- /** The answer profile of a row that answers a stored file. */
207
- export type EndpointResponse = 'file'
219
+ /** The answer profile of a row that answers a stored file: `inline-file` also shows the XML documents a machine reads. */
220
+ export type EndpointResponse = 'file' | 'inline-file'
208
221
 
209
222
  /** A cacheable row's lifetime: `maxAgeSeconds`, or `immutable`, a year, for an input that names an immutable value. */
210
223
  export type EndpointCache = { readonly maxAgeSeconds: number } | 'immutable'
@@ -217,7 +230,7 @@ export const FILE_RESPONSE_KINDS: readonly EndpointKind[] = ['query', 'mutation'
217
230
 
218
231
  /** Whether every answer of a row gets the file treatment: a collection's download, or a procedure that answers a file. */
219
232
  export function answersFile(row: { readonly kind: string; readonly response?: EndpointResponse }): boolean {
220
- return row.kind === 'download' || row.response === 'file'
233
+ return row.kind === 'download' || row.response !== undefined
221
234
  }
222
235
 
223
236
  /** How an endpoint admits another origin: from the environment's list, or (a form or a public procedure) from any origin. */
@@ -251,7 +264,7 @@ export interface BodyAdmission {
251
264
  * `action` is that action in some scope, global included, and only globally under the `app` form. The access form is
252
265
  * what the framework then resolves.
253
266
  */
254
- export type EndpointRequirement = MemberRequirement | ActionRequirement | CapabilitiesRequirement
267
+ export type EndpointRequirement = MemberRequirement | ActionRequirement | AnyOfRequirement | CapabilitiesRequirement
255
268
 
256
269
  export interface MemberRequirement {
257
270
  readonly kind: 'member'
@@ -263,6 +276,24 @@ export interface ActionRequirement {
263
276
  readonly access: AccessForm
264
277
  }
265
278
 
279
+ /**
280
+ * Any one of several actions (`access: { anyOf, app: true }`), each held as a global grant, the `app` form's rule: two to
281
+ * `MAX_ANY_OF` distinct actions, sorted, so the order in the source does not change the digest.
282
+ */
283
+ export interface AnyOfRequirement {
284
+ readonly kind: 'anyOf'
285
+ readonly actions: readonly [string, string, ...string[]]
286
+ readonly access: { readonly form: 'app' }
287
+ }
288
+
289
+ /** The most actions an `anyOf` requirement lists. */
290
+ export const MAX_ANY_OF = 16
291
+
292
+ /** Whether `actions` is an `anyOf` list as a row carries it: two to `MAX_ANY_OF` actions, strictly ascending. */
293
+ export function isAnyOfList(actions: readonly string[]): boolean {
294
+ return actions.length >= 2 && actions.length <= MAX_ANY_OF && actions.every((action, index) => index === 0 || (actions[index - 1] ?? '') < action)
295
+ }
296
+
266
297
  /**
267
298
  * Several capabilities (`access: { capabilities }`), by the name the handler reads each under (`ctx.capabilities.<name>`):
268
299
  * the token must carry every action, and the access form of each narrows it as it narrows a single one. At least two.
@@ -337,6 +368,29 @@ export function isFileShape(shape: OutputShape): shape is FileShape {
337
368
  return shape.type === 'file'
338
369
  }
339
370
 
371
+ /** The kinds of the files a schema holds, anywhere in it, each once per place it appears. */
372
+ export function fileKindsIn(shape: SchemaShape): readonly string[] {
373
+ switch (shape.type) {
374
+ case 'file':
375
+ return [shape.kind]
376
+ case 'array':
377
+ return fileKindsIn(shape.items)
378
+ case 'tuple':
379
+ return [...shape.items.flatMap(fileKindsIn), ...(shape.rest === null ? [] : fileKindsIn(shape.rest))]
380
+ case 'record':
381
+ return fileKindsIn(shape.value)
382
+ case 'object':
383
+ return Object.values(shape.fields).flatMap(fileKindsIn)
384
+ case 'optional':
385
+ case 'nullable':
386
+ return fileKindsIn(shape.inner)
387
+ case 'union':
388
+ return shape.options.flatMap(fileKindsIn)
389
+ default:
390
+ return []
391
+ }
392
+ }
393
+
340
394
  /**
341
395
  * What a procedure answers: a JSON value of a schema, a file of rows (`attachment()` of `@fabrika/app/contract`), or a
342
396
  * redirect (`redirect()`).
@@ -454,6 +508,16 @@ export interface RoleArtifact {
454
508
  readonly member: { readonly scope: string; readonly assignableBy: readonly string[] } | null
455
509
  /** An external principal may hold this membership role (ADR-0051). Absent unless declared, so digests stay. */
456
510
  readonly external?: true
511
+ /**
512
+ * Relation → actions the holder of a global grant of this role holds at each node a relation row names them at.
513
+ * Absent unless declared, so digests stay.
514
+ */
515
+ readonly via?: Readonly<Record<string, readonly string[]>>
516
+ }
517
+
518
+ /** A relation of the vocabulary: the dimension whose nodes its rows name. */
519
+ export interface RelationArtifact {
520
+ readonly dimension: string
457
521
  }
458
522
 
459
523
  // ── Mounts and companions ───────────────────────────────────────────────────────────────────────
@@ -668,6 +732,11 @@ export interface FileKindArtifact {
668
732
  readonly maxBytes: number
669
733
  /** Present when every file of the kind waits `quarantined` for a clean malware verdict before it is served. */
670
734
  readonly scan?: true
735
+ /**
736
+ * Present when its files are shown rather than downloaded: every type is an inert image or an XML document a machine
737
+ * reads, and a row answering it is `response: 'inline-file'`. Absent otherwise, so every other kind keeps its digest.
738
+ */
739
+ readonly inline?: true
671
740
  }
672
741
 
673
742
  export interface JobArtifact {
@@ -748,10 +817,12 @@ export interface DeclaredReach {
748
817
  */
749
818
  readonly signs?: readonly string[]
750
819
  /**
751
- * The HTTPS origins this site's code calls (`fetch`), sorted: a credentialed procedure, a webhook or a job. Absent
752
- * unless declared, so the digest stays.
820
+ * The HTTPS origins this site's code calls (`fetch`), sorted, each an exact origin or a one-label pattern
821
+ * (`parseEgressEntry`): a credentialed procedure, a webhook or a job. Or `'public'` (a cron job only): every public
822
+ * host over HTTPS or HTTP, never the installation or a private network (ADR-0075). Absent unless declared, so the
823
+ * digest stays.
753
824
  */
754
- readonly egress?: readonly string[]
825
+ readonly egress?: DeclaredEgress
755
826
  /**
756
827
  * A public procedure's workload actions, sorted: all its handler holds, through the key Control issues for the
757
828
  * endpoint (`contractWorkloads`). Present only on a public procedure's entry, so every other digest stays.
@@ -796,6 +867,12 @@ export interface CspSource {
796
867
  */
797
868
  export type TableActionArtifact = string | readonly string[]
798
869
 
870
+ /**
871
+ * An application table's `actions.read` that every `app` endpoint, job and webhook reads with, whatever its action; never
872
+ * a public procedure, a scoped site, a link or an export. No vocabulary may declare an action of this name.
873
+ */
874
+ export const READ_ANY = 'any'
875
+
799
876
  export interface TablePolicyArtifact {
800
877
  readonly scope: TableScope
801
878
  /** An inbox's read action is `read`; it has no write action, because only its form appends. */
@@ -826,6 +903,121 @@ export interface TablePolicyArtifact {
826
903
  * actions are the file's authority. Absent unless declared, so the digest stays.
827
904
  */
828
905
  readonly files?: Readonly<Record<string, string>>
906
+ /**
907
+ * A view (ADR-0072): read like its scope's tables, written by nothing, its containment stated by its SQL alone.
908
+ * Absent for a table, so the digest stays.
909
+ */
910
+ readonly view?: true
911
+ /** Relation → how a row of this table confers it. Absent unless declared, so the digest stays. */
912
+ readonly confers?: Readonly<Record<string, ConfersArtifact>>
913
+ /** Action → the condition a row must meet for it, narrowing what any grant of it reaches. Absent unless declared. */
914
+ readonly when?: Readonly<Record<string, ConditionArtifact>>
915
+ /**
916
+ * Relation → action → the condition a row the relation reaches must meet for it; it narrows that relation's reach only.
917
+ * Absent unless declared, so the digest stays. A reader that ignores it reaches more: Control refuses to publish it to
918
+ * an application pinned below the release that enforces it.
919
+ */
920
+ readonly relationWhen?: Readonly<Record<string, Readonly<Record<string, ConditionArtifact>>>>
921
+ /**
922
+ * Relation of the table's own dimension → the ancestor its rows reveal. Absent unless declared, so the digest stays.
923
+ * A reader that ignores it reaches less, never more.
924
+ */
925
+ readonly reveals?: Readonly<Record<string, RevealsArtifact>>
926
+ /**
927
+ * Dimension outside the chain → the column a `Within`'s read reaches a row through. Absent unless declared, so the
928
+ * digest stays. A reader that ignores it reaches less, never more.
929
+ */
930
+ readonly readThrough?: Readonly<Record<string, string>>
931
+ /**
932
+ * Dimension of `readThrough` → the actions a `Ref` held through that path holds, writes included; whoever inserts the
933
+ * path's column must hold each at the node it names. Absent unless declared, so the digest stays. A reader that
934
+ * ignores it reaches less, never more.
935
+ */
936
+ readonly writeThrough?: Readonly<Record<string, readonly string[]>>
937
+ /**
938
+ * The table's own dimension → the ancestor its rows reveal to a grant at their node, for a `Within`'s reads. Absent
939
+ * unless declared, so the digest stays. A reader that ignores it reaches less, never more.
940
+ */
941
+ readonly memberReveals?: Readonly<Record<string, MemberRevealsArtifact>>
942
+ /**
943
+ * Relation → the access view that lists the rows it reaches (ADR-0076). Absent unless declared, so the digest stays.
944
+ * A reader that ignores it reaches less, never more.
945
+ */
946
+ readonly access?: Readonly<Record<string, AccessArtifact>>
947
+ }
948
+
949
+ /** An access view: its `row` column names the table's key; `principal` is compared to the caller, `member` to its nodes. */
950
+ export interface AccessArtifact {
951
+ readonly view: string
952
+ readonly row: string
953
+ readonly principal: string | null
954
+ readonly member: string | null
955
+ }
956
+
957
+ /**
958
+ * A row reveals its node in `to` for `actions` to a grant of them at the row's own node, or at the node its path to the
959
+ * key's dimension names, while `when` admits the row.
960
+ */
961
+ export interface MemberRevealsArtifact {
962
+ readonly to: string
963
+ readonly actions: readonly string[]
964
+ readonly when?: ConditionArtifact
965
+ /**
966
+ * Through a path, on a table with an insert: the writer picks the node, and `self` lets it pick one where it holds a
967
+ * grant the reveal counts. Absent otherwise; only a reader that refuses a path's key reads it.
968
+ */
969
+ readonly assign?: { readonly self: boolean }
970
+ }
971
+
972
+ /** A row the relation reaches reveals its node in `to` for `actions`, while `when` admits the row. */
973
+ export interface RevealsArtifact {
974
+ readonly to: string
975
+ readonly actions: readonly string[]
976
+ readonly when?: ConditionArtifact
977
+ /** On a reveal matched through `readThrough`: an insert delegates, and `self` lets it name a node its writer holds. */
978
+ readonly assign?: { readonly self: boolean }
979
+ }
980
+
981
+ /** A row names `principal` at the node in `node`, until `expires`, while `when` admits it; `assign` bounds its writer. */
982
+ export interface ConfersArtifact {
983
+ /** The column that holds the principal, or `<column>.<principal column>` through one foreign key of the table. */
984
+ readonly principal: string
985
+ readonly node: string
986
+ readonly expires: string | null
987
+ readonly when?: ConditionArtifact
988
+ readonly assign: { readonly maxDays: number | null; readonly self: boolean } | null
989
+ }
990
+
991
+ /**
992
+ * A structured row condition: connectives, `{ '<column or fk_column.column>': test }` with every test holding, or
993
+ * `some` / `none` of the rows of one table whose foreign key names the row, also at the end of a path
994
+ * (`{ '<fk_column>': { some: … } }`). Each form is absent from a condition that does not use it, so its digest stays.
995
+ */
996
+ export type ConditionArtifact =
997
+ | { readonly allOf: readonly ConditionArtifact[] }
998
+ | { readonly anyOf: readonly ConditionArtifact[] }
999
+ | { readonly not: ConditionArtifact }
1000
+ | RowsTestArtifact
1001
+ | { readonly [path: string]: ColumnTestArtifact | RowsTestArtifact }
1002
+
1003
+ /** `some` or `none` of the rows of one table whose foreign key names the row. */
1004
+ export type RowsTestArtifact =
1005
+ | { readonly some: Readonly<Record<string, ConditionArtifact>> }
1006
+ | { readonly none: Readonly<Record<string, ConditionArtifact>> }
1007
+
1008
+ export type ConditionLiteralArtifact = string | number | boolean
1009
+
1010
+ /** One operator of a column test; `{ now: true }` compares a timestamp with the statement's clock. */
1011
+ export interface ColumnTestArtifact {
1012
+ readonly eq?: ConditionLiteralArtifact
1013
+ readonly notEq?: ConditionLiteralArtifact
1014
+ readonly in?: readonly ConditionLiteralArtifact[]
1015
+ readonly notIn?: readonly ConditionLiteralArtifact[]
1016
+ readonly isNull?: boolean
1017
+ readonly lt?: ConditionLiteralArtifact | { readonly now: true }
1018
+ readonly lte?: ConditionLiteralArtifact | { readonly now: true }
1019
+ readonly gt?: ConditionLiteralArtifact | { readonly now: true }
1020
+ readonly gte?: ConditionLiteralArtifact | { readonly now: true }
829
1021
  }
830
1022
 
831
1023
  /** A column's own read action, its own write action, or both; an absent one is left out. */
@@ -13,6 +13,12 @@ export const DEV_LAUNCHER_MODULE = '@fabrika/app/dev'
13
13
  /** Where the launcher records what it holds, so a restart of the same container resumes it. A new container has none. */
14
14
  export const DEV_LAUNCHER_STATE_FILE = 'fabrika/dev-state.json'
15
15
 
16
+ /**
17
+ * The launcher writes it once its boot reconcile reported, served or failed. A dev container's readiness check passes on
18
+ * it too, so a boot that failed still activates, and `reset_dev` can reach the launcher that reports the failure.
19
+ */
20
+ export const DEV_LAUNCHER_BOOTED_FILE = 'fabrika/dev-booted'
21
+
16
22
  /** One command of the build's check chain, run by the shell in the application root. */
17
23
  export interface DevLauncherCheck {
18
24
  readonly name: string
package/src/egress.ts ADDED
@@ -0,0 +1,97 @@
1
+ // What a site's `egress` entry is, and which request URL it admits: the one rule the contract build, the artifact parser,
2
+ // the runtime guard on `fetch`, the checker and the approval diff share (ADR-0012 §15). An entry is an exact `https`
3
+ // origin or a pattern `https://*.<domain>[:port]` whose `*` stands for exactly one DNS label.
4
+
5
+ /** One parsed `egress` entry. `port` is `''` for the default port, as `URL.port` spells it. */
6
+ export type EgressEntry =
7
+ | { readonly kind: 'origin'; readonly origin: string; readonly host: string; readonly port: string }
8
+ | { readonly kind: 'pattern'; readonly pattern: string; readonly domain: string; readonly port: string }
9
+
10
+ export type EgressEntryParse = { readonly ok: true; readonly entry: EgressEntry } | { readonly ok: false; readonly problem: string }
11
+
12
+ /** What every pattern starts with: the scheme and the one wildcard label. */
13
+ const EGRESS_WILDCARD_PREFIX = 'https://*.'
14
+
15
+ const HOSTNAME = /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/
16
+ const NUMERIC_LABEL = /^[0-9]+$/
17
+
18
+ /**
19
+ * `value` as an `egress` entry, or why it is none. Each entry is written canonically (lower case, punycode, no default
20
+ * port, no trailing dot, no path), so an artifact names one entry one way; a problem names the canonical spelling when
21
+ * there is one.
22
+ */
23
+ export function parseEgressEntry(value: string): EgressEntryParse {
24
+ return value.startsWith(EGRESS_WILDCARD_PREFIX) ? parsePattern(value) : parseOrigin(value)
25
+ }
26
+
27
+ function parseOrigin(value: string): EgressEntryParse {
28
+ const url = urlOf(value)
29
+ if (url === null || url.protocol !== 'https:') return refused('is not an https origin')
30
+ if (url.hostname.endsWith('.')) return refused(canonicalProblem(url, `https://${url.host.replace(/\.(?=:|$)/, '')}`))
31
+ if (!HOSTNAME.test(url.hostname)) {
32
+ return refused(url.hostname.includes('*') ? 'puts a wildcard elsewhere than one leading label' : 'is not an https origin with a literal host')
33
+ }
34
+ if (url.origin !== value) return refused(canonicalProblem(url, url.origin))
35
+ return { ok: true, entry: { kind: 'origin', origin: value, host: url.hostname, port: url.port } }
36
+ }
37
+
38
+ function parsePattern(value: string): EgressEntryParse {
39
+ const rest = value.slice(EGRESS_WILDCARD_PREFIX.length)
40
+ const url = urlOf(`https://${rest}`)
41
+ if (url === null) return refused('is not an https origin pattern')
42
+ if (url.hostname.includes('*')) return refused('has more than one wildcard label (allowed: one leading *.)')
43
+ if (url.hostname.endsWith('.')) return refused(canonicalProblem(url, `${EGRESS_WILDCARD_PREFIX}${url.host.replace(/\.(?=:|$)/, '')}`))
44
+ if (!HOSTNAME.test(url.hostname)) return refused('is not an https origin pattern with a literal domain')
45
+ const labels = url.hostname.split('.')
46
+ if (NUMERIC_LABEL.test(labels.at(-1) ?? '')) return refused('puts its wildcard on an IP address (allowed: a domain name)')
47
+ if (isPublicSuffix(labels)) {
48
+ return refused(`puts its wildcard on the public suffix '${url.hostname}' (allowed: a domain of your own, as in 'https://*.example.com')`)
49
+ }
50
+ if (url.origin !== `https://${rest}`) return refused(canonicalProblem(url, `${EGRESS_WILDCARD_PREFIX}${url.host}`))
51
+ return { ok: true, entry: { kind: 'pattern', pattern: value, domain: url.hostname, port: url.port } }
52
+ }
53
+
54
+ /**
55
+ * Whether `labels` name a public suffix by this rule, which stands in for the public suffix list (the platform ships no
56
+ * copy of it): one label (`com`), or two labels under a two-letter country code whose first has at
57
+ * most three characters (`co.uk`, `com.au`, `ne.jp`). A suffix this rule cannot tell, such as a longer second-level
58
+ * label or a shared hosting domain, is the approver's to judge: the diff shows every pattern as a wider grant.
59
+ */
60
+ function isPublicSuffix(labels: readonly string[]): boolean {
61
+ if (labels.length < 2) return true
62
+ const top = labels.at(-1) ?? ''
63
+ const second = labels.at(-2) ?? ''
64
+ return labels.length === 2 && /^[a-z]{2}$/.test(top) && second.length <= 3
65
+ }
66
+
67
+ /** Whether `url` is a request an entry of `entries` admits: its host and port, a trailing dot of the host aside. */
68
+ export function egressAdmits(entries: readonly EgressEntry[], url: URL): boolean {
69
+ if (url.protocol !== 'https:') return false
70
+ const host = url.hostname.endsWith('.') ? url.hostname.slice(0, -1) : url.hostname
71
+ return entries.some((entry) => entry.port === url.port && (entry.kind === 'origin' ? entry.host === host : isOneLabelUnder(host, entry.domain)))
72
+ }
73
+
74
+ /** `host` is exactly one non-empty label followed by `.<domain>`, so `evil-example.com` is not under `example.com`. */
75
+ function isOneLabelUnder(host: string, domain: string): boolean {
76
+ const suffix = `.${domain}`
77
+ if (!host.endsWith(suffix)) return false
78
+ const label = host.slice(0, -suffix.length)
79
+ return label.length > 0 && !label.includes('.')
80
+ }
81
+
82
+ function canonicalProblem(url: URL, canonical: string): string {
83
+ const bare = url.pathname === '/' && url.search === '' && url.hash === '' && url.username === '' && url.password === ''
84
+ return bare ? `is not written canonically (write it as '${canonical}')` : 'carries a path, query or credentials (allowed: an origin only)'
85
+ }
86
+
87
+ function refused(problem: string): EgressEntryParse {
88
+ return { ok: false, problem }
89
+ }
90
+
91
+ function urlOf(value: string): URL | null {
92
+ try {
93
+ return new URL(value)
94
+ } catch {
95
+ return null
96
+ }
97
+ }
@@ -2,9 +2,9 @@
2
2
  // with every allowed request, and one for the whole contract, checked once at startup.
3
3
 
4
4
  import type { ContractArtifact, EndpointRow, ExportDescription } from './contract'
5
- import { canonicalAdmission, canonicalArtifactJson, canonicalJson, canonicalShape } from './contract-canonical'
5
+ import { canonicalAdmission, canonicalArtifactJson, canonicalJson, canonicalShape, writeCanonicalJson } from './contract-canonical'
6
6
  import type { InstallationEndpointRow, InstallationEndpointTable } from './installation-endpoint'
7
- import { sha256Hex } from './sha256'
7
+ import { Sha256, sha256Hex } from './sha256'
8
8
 
9
9
  /** The variable that carries the digest of the contract a deploy was approved with; the application compares its own. */
10
10
  export const FABRIKA_CONTRACT_DIGEST = 'FABRIKA_CONTRACT_DIGEST'
@@ -109,6 +109,26 @@ export function contractDigest(artifact: ContractArtifact): string {
109
109
  return digestOf(canonicalArtifactJson(artifact))
110
110
  }
111
111
 
112
+ /**
113
+ * `contractDigest` of an artifact that already is canonical (`canonicalizeArtifact`'s, as a decoded artifact is),
114
+ * hashed as it is serialized: neither a canonical copy nor its text is built. A shared shape is hashed at each place.
115
+ */
116
+ export function canonicalArtifactDigest(canonical: ContractArtifact): string {
117
+ const hash = new Sha256()
118
+ let pending = ''
119
+ writeCanonicalJson(canonical, (piece) => {
120
+ pending += piece
121
+ if (pending.length >= HASH_CHUNK) {
122
+ hash.update(pending)
123
+ pending = ''
124
+ }
125
+ })
126
+ return `sha256:${hash.update(pending).hex()}`
127
+ }
128
+
129
+ /** Pieces are hashed in chunks of about this many code units: one encode per piece would dominate the digest. */
130
+ const HASH_CHUNK = 16_384
131
+
112
132
  /**
113
133
  * The digest of an installation service's table: each row's id and `requirementDigest`, and the reserved prefixes. A
114
134
  * composition writes it beside the service from the table its proxy publishes, so the service refuses to start under
@@ -0,0 +1,2 @@
1
+ /** A framework release as npm publishes it; the only form one may take before it reaches a generated build command. */
2
+ export const FRAMEWORK_RELEASE_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/
@@ -16,6 +16,17 @@ export const FABRIKA_SOURCE_MAP_UPLOAD_KEY = 'FABRIKA_SOURCE_MAP_UPLOAD_KEY'
16
16
  export const FABRIKA_TOKEN_KEYS = 'FABRIKA_TOKEN_KEYS'
17
17
  /** The environment's canonical origin (`https://crm.example`), which `ctx.url` builds absolute links on. */
18
18
  export const FABRIKA_APP_ORIGIN = 'FABRIKA_APP_ORIGIN'
19
+ /** The digests of the environment's ops keys, which the object checks Control's key against (`parseOpsKeyDigests`, ADR-0069). */
20
+ export const FABRIKA_OPS_KEY_DIGESTS = 'FABRIKA_OPS_KEY_DIGESTS'
21
+ /**
22
+ * The installation's own hosts, which a cron job's public egress refuses (ADR-0075): Control's, IAM's, Operations' and
23
+ * every application environment's domain, normalized and joined by `,`. Written only for a contract with such a job.
24
+ */
25
+ export const FABRIKA_EGRESS_REFUSED_HOSTS = 'FABRIKA_EGRESS_REFUSED_HOSTS'
26
+ /** The name of the object that serves an environment's ordinary traffic on Cloudflare; `application` without it. */
27
+ export const FABRIKA_APPLICATION_OBJECT = 'FABRIKA_APPLICATION_OBJECT'
28
+ /** The object names an ops request may target, all in the environment's own namespace (`parseApplicationObjects`). */
29
+ export const FABRIKA_APPLICATION_OBJECTS = 'FABRIKA_APPLICATION_OBJECTS'
19
30
 
20
31
  /**
21
32
  * The environment names only the platform writes into an application
@@ -47,6 +58,10 @@ export const FRAMEWORK_VARIABLE_NAMES = [
47
58
  FABRIKA_SOURCE_MAP_UPLOAD_KEY,
48
59
  FABRIKA_TOKEN_KEYS,
49
60
  FABRIKA_APP_ORIGIN,
61
+ FABRIKA_OPS_KEY_DIGESTS,
62
+ FABRIKA_EGRESS_REFUSED_HOSTS,
63
+ FABRIKA_APPLICATION_OBJECT,
64
+ FABRIKA_APPLICATION_OBJECTS,
50
65
  ] as const
51
66
 
52
67
  export type FrameworkVariableName = (typeof FRAMEWORK_VARIABLE_NAMES)[number]
package/src/index.ts CHANGED
@@ -1,24 +1,32 @@
1
1
  // @fabrika/auth-core — pure logic, types, and wire names shared across auth components and apps.
2
2
  export * from './app-environment'
3
3
  export * from './canonical-path'
4
+ export * from './client-address'
4
5
  export * from './contract'
5
6
  export * from './contract-canonical'
7
+ export * from './contract-encoding'
6
8
  export * from './dev-environment'
7
9
  export * from './device'
10
+ export * from './egress'
8
11
  export * from './endpoint-digest'
9
12
  export * from './endpoints'
10
13
  export * from './environment'
14
+ export * from './framework-release'
11
15
  export * from './framework-variables'
12
16
  export * from './installation-endpoint'
13
17
  export { parseJsonStringArray } from './json'
14
18
  export * from './locale'
15
19
  export * from './mail'
20
+ export * from './managed-environment'
16
21
  export * from './membership'
17
22
  export * from './oauth'
23
+ export * from './ops'
18
24
  export * from './permissions'
25
+ export * from './public-egress'
19
26
  export * from './requirement'
20
27
  export * from './rpc'
21
28
  export { sha256Hex } from './sha256'
29
+ export * from './terminal'
22
30
  export * from './token'
23
31
  export * from './types'
24
32
  export * from './wire'
package/src/locale.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // it shows. The contract declares both; IAM's catalog covers every locale here, so adding one fails its typecheck.
3
3
 
4
4
  /** The languages IAM renders its pages and its own e-mails in. */
5
- export const LOCALES = ['en', 'cs'] as const
5
+ export const LOCALES = ['en', 'cs', 'uk'] as const
6
6
 
7
7
  export type Locale = (typeof LOCALES)[number]
8
8