@fabrika/auth-core 0.9.0-cloudflare.1 → 0.9.0-cloudflare.3

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.
@@ -0,0 +1,166 @@
1
+ // `egress: 'public'`: a cron job that calls the websites its operators name (a crawler, an auditor, a link checker)
2
+ // reaches any public host over HTTPS or HTTP, and never the installation or a private network (ADR-0071). These are the
3
+ // rules a URL alone decides, which the runtime guard and the checker share. The guard adds the installation's own hosts
4
+ // and, where the runtime exposes name resolution, the addresses a name resolves to.
5
+
6
+ /** The declared form of public egress: every public host, over HTTPS or HTTP, on its default port. */
7
+ export const PUBLIC_EGRESS = 'public'
8
+
9
+ export type PublicEgress = typeof PUBLIC_EGRESS
10
+
11
+ /** What a site declares in `egress`: its exact HTTPS origins, sorted, or every public host (a cron job only). */
12
+ export type DeclaredEgress = readonly string[] | PublicEgress
13
+
14
+ /**
15
+ * The name suffixes public egress refuses, each with every name under it: local and special-use names (RFC 6761,
16
+ * RFC 6762, RFC 8375, RFC 9476), the names private networks conventionally use (`.internal` holds Google's metadata
17
+ * host and Control's own `operations.internal`), and the platform subdomains an installation is served on beside its
18
+ * own domains: Cloudflare's `workers.dev`, where its Workers have their default routes, and Zerops' `zerops.app`, where
19
+ * its proxies, Control and IAM have their subdomains (a namespace proxy's `proxy-…-8080.<region>.zerops.app`), with the
20
+ * `zerops` suffix of a Zerops project's private network. Control knows no list of those subdomains, so every one is
21
+ * refused, another installation's too.
22
+ */
23
+ export const PUBLIC_EGRESS_REFUSED_SUFFIXES = [
24
+ 'localhost',
25
+ 'local',
26
+ 'internal',
27
+ 'intranet',
28
+ 'lan',
29
+ 'home',
30
+ 'corp',
31
+ 'private',
32
+ 'arpa',
33
+ 'test',
34
+ 'invalid',
35
+ 'example',
36
+ 'onion',
37
+ 'alt',
38
+ 'zerops',
39
+ 'zerops.app',
40
+ 'workers.dev',
41
+ ] as const
42
+
43
+ /** A host name without its trailing dots, lowercased: the form every comparison here uses. */
44
+ export function normalizedHost(hostname: string): string {
45
+ return hostname.toLowerCase().replace(/\.+$/, '')
46
+ }
47
+
48
+ const IPV4 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
49
+
50
+ /**
51
+ * Why public egress refuses `url`, or `null` when the URL alone admits it: a scheme other than `https:` and `http:`, a
52
+ * port other than the scheme's default, an IP literal, a name of one label, a name under a refused suffix, or one of
53
+ * `refusedHosts` (normalized names). A host it admits may still resolve to a private address, which only a runtime that
54
+ * resolves names refuses.
55
+ */
56
+ export function publicEgressRefusal(url: URL, refusedHosts: ReadonlySet<string> = new Set()): string | null {
57
+ if (url.protocol !== 'https:' && url.protocol !== 'http:') return `${url.protocol} is not https: or http:`
58
+ if (url.port !== '') return `port ${url.port} is not the default port of ${url.protocol}`
59
+ const host = normalizedHost(url.hostname)
60
+ if (host.startsWith('[') || IPV4.test(host)) return `${host} is an IP address, not a public host name`
61
+ if (!host.includes('.')) return `${host} is a name of one label, as an internal service or a local machine has`
62
+ if (host.split('.').some((label) => label === '')) return `${host} has an empty label`
63
+ const suffix = PUBLIC_EGRESS_REFUSED_SUFFIXES.find((refused) => host === refused || host.endsWith(`.${refused}`))
64
+ if (suffix !== undefined) return `${host} is under .${suffix}, which names no public website`
65
+ if (refusedHosts.has(host)) return `${host} is a host of this installation`
66
+ return null
67
+ }
68
+
69
+ /**
70
+ * Whether `address` (an IPv4 or IPv6 address, as a resolver answers it) is a public unicast address: not private,
71
+ * loopback, link-local (which holds the cloud metadata address 169.254.169.254), shared, multicast, reserved,
72
+ * documentation, or an IPv6 form that embeds or tunnels an IPv4 address it cannot vouch for. A text that is no address
73
+ * is not public.
74
+ */
75
+ export function isPublicAddress(address: string): boolean {
76
+ const v4 = ipv4Octets(address)
77
+ if (v4 !== null) return isPublicIpv4(v4)
78
+ const words = ipv6Words(address)
79
+ if (words === null) return false
80
+ const [w0 = 0, w1 = 0, w2 = 0, w3 = 0, w4 = 0, w5 = 0, w6 = 0, w7 = 0] = words
81
+ // ::ffff:a.b.c.d, an IPv4 address in IPv6 form, is that IPv4 address.
82
+ if (w0 === 0 && w1 === 0 && w2 === 0 && w3 === 0 && w4 === 0 && w5 === 0xffff) {
83
+ return isPublicIpv4([w6 >> 8, w6 & 0xff, w7 >> 8, w7 & 0xff])
84
+ }
85
+ // Only global unicast (2000::/3) is public; every other block is local, multicast, reserved or a translation prefix.
86
+ if ((w0 & 0xe000) !== 0x2000) return false
87
+ // 2001::/32 Teredo, 2001:10::/28 ORCHID, 2001:20::/28 ORCHIDv2, 2001:db8::/32 documentation, 2002::/16 6to4.
88
+ if (w0 === 0x2001 && (w1 === 0 || (w1 & 0xfff0) === 0x10 || (w1 & 0xfff0) === 0x20 || w1 === 0x0db8)) return false
89
+ if (w0 === 0x2002) return false
90
+ // 3fff::/20 documentation (RFC 9637).
91
+ if (w0 === 0x3fff && (w1 & 0xf000) === 0) return false
92
+ return true
93
+ }
94
+
95
+ /** The non-public IPv4 blocks, each as its network (a 32-bit number) and its prefix length. */
96
+ const IPV4_REFUSED: readonly (readonly [number, number])[] = [
97
+ [0x00000000, 8], // 0.0.0.0/8 this network
98
+ [0x0a000000, 8], // 10.0.0.0/8 private
99
+ [0x64400000, 10], // 100.64.0.0/10 shared address space
100
+ [0x7f000000, 8], // 127.0.0.0/8 loopback
101
+ [0xa9fe0000, 16], // 169.254.0.0/16 link-local, the cloud metadata address
102
+ [0xac100000, 12], // 172.16.0.0/12 private
103
+ [0xc0000000, 24], // 192.0.0.0/24 IETF protocol assignments
104
+ [0xc0000200, 24], // 192.0.2.0/24 documentation
105
+ [0xc0586300, 24], // 192.88.99.0/24 6to4 relay anycast
106
+ [0xc0a80000, 16], // 192.168.0.0/16 private
107
+ [0xc6120000, 15], // 198.18.0.0/15 benchmarking
108
+ [0xc6336400, 24], // 198.51.100.0/24 documentation
109
+ [0xcb007100, 24], // 203.0.113.0/24 documentation
110
+ [0xe0000000, 4], // 224.0.0.0/4 multicast
111
+ [0xf0000000, 4], // 240.0.0.0/4 reserved, broadcast
112
+ ]
113
+
114
+ function isPublicIpv4(octets: readonly number[]): boolean {
115
+ const [a = 0, b = 0, c = 0, d = 0] = octets
116
+ const value = ((a << 24) >>> 0) + (b << 16) + (c << 8) + d
117
+ return !IPV4_REFUSED.some(([network, prefix]) => {
118
+ const mask = prefix === 0 ? 0 : (0xffffffff << (32 - prefix)) >>> 0
119
+ return ((value & mask) >>> 0) === network
120
+ })
121
+ }
122
+
123
+ function ipv4Octets(text: string): number[] | null {
124
+ if (!IPV4.test(text)) return null
125
+ const octets = text.split('.').map((part) => Number.parseInt(part, 10))
126
+ return octets.every((octet) => octet >= 0 && octet <= 255) ? octets : null
127
+ }
128
+
129
+ const HEX_WORD = /^[0-9a-f]{1,4}$/i
130
+
131
+ /** The eight 16-bit words of an IPv6 address (brackets and a zone allowed, an embedded IPv4 tail too), or `null`. */
132
+ function ipv6Words(text: string): number[] | null {
133
+ let rest = text.startsWith('[') && text.endsWith(']') ? text.slice(1, -1) : text
134
+ const zone = rest.indexOf('%')
135
+ if (zone !== -1) rest = rest.slice(0, zone)
136
+ if (!rest.includes(':')) return null
137
+ const tailAt = rest.lastIndexOf(':')
138
+ const tail = ipv4Octets(rest.slice(tailAt + 1))
139
+ const embedded: number[] = []
140
+ if (tail !== null) {
141
+ const [a = 0, b = 0, c = 0, d = 0] = tail
142
+ embedded.push((a << 8) | b, (c << 8) | d)
143
+ rest = rest.slice(0, tailAt + 1) + '0'
144
+ }
145
+ const halves = rest.split('::')
146
+ if (halves.length > 2) return null
147
+ const parse = (half: string | undefined): number[] | null => {
148
+ if (half === undefined || half === '') return []
149
+ const parts = half.split(':')
150
+ if (!parts.every((part) => HEX_WORD.test(part))) return null
151
+ return parts.map((part) => Number.parseInt(part, 16))
152
+ }
153
+ const head = parse(halves[0])
154
+ const back = parse(halves[1])
155
+ if (head === null || back === null) return null
156
+ if (tail !== null) {
157
+ // The placeholder word stands for the two words of the IPv4 tail.
158
+ const last = halves.length === 2 ? back : head
159
+ last.pop()
160
+ last.push(...embedded)
161
+ }
162
+ const missing = 8 - head.length - back.length
163
+ if (halves.length === 1) return missing === 0 ? head : null
164
+ if (missing < 1) return null
165
+ return [...head, ...Array.from({ length: missing }, () => 0), ...back]
166
+ }
@@ -1,5 +1,6 @@
1
1
  import type { Locale } from './locale';
2
2
  import type { MailTemplates } from './mail';
3
+ import type { DeclaredEgress } from './public-egress';
3
4
  import type { TerminalArtifact } from './terminal';
4
5
  /**
5
6
  * The artifact format this release writes. A release that changes the artifact raises it, and Control upcasts every
@@ -694,10 +695,11 @@ export interface DeclaredReach {
694
695
  */
695
696
  readonly signs?: readonly string[];
696
697
  /**
697
- * The HTTPS origins this site's code calls (`fetch`), sorted: a credentialed procedure, a webhook or a job. Absent
698
- * unless declared, so the digest stays.
698
+ * The HTTPS origins this site's code calls (`fetch`), sorted: a credentialed procedure, a webhook or a job. Or
699
+ * `'public'` (a cron job only): every public host over HTTPS or HTTP, never the installation or a private network
700
+ * (ADR-0071). Absent unless declared, so the digest stays.
699
701
  */
700
- readonly egress?: readonly string[];
702
+ readonly egress?: DeclaredEgress;
701
703
  /**
702
704
  * A public procedure's workload actions, sorted: all its handler holds, through the key Control issues for the
703
705
  * endpoint (`contractWorkloads`). Present only on a public procedure's entry, so every other digest stays.
@@ -12,6 +12,11 @@ export declare const FABRIKA_TOKEN_KEYS = "FABRIKA_TOKEN_KEYS";
12
12
  export declare const FABRIKA_APP_ORIGIN = "FABRIKA_APP_ORIGIN";
13
13
  /** The digests of the environment's ops keys, which the object checks Control's key against (`parseOpsKeyDigests`, ADR-0069). */
14
14
  export declare const FABRIKA_OPS_KEY_DIGESTS = "FABRIKA_OPS_KEY_DIGESTS";
15
+ /**
16
+ * The installation's own hosts, which a cron job's public egress refuses (ADR-0071): Control's, IAM's, Operations' and
17
+ * every application environment's domain, normalized and joined by `,`. Written only for a contract with such a job.
18
+ */
19
+ export declare const FABRIKA_EGRESS_REFUSED_HOSTS = "FABRIKA_EGRESS_REFUSED_HOSTS";
15
20
  /** The name of the object that serves an environment's ordinary traffic on Cloudflare; `application` without it. */
16
21
  export declare const FABRIKA_APPLICATION_OBJECT = "FABRIKA_APPLICATION_OBJECT";
17
22
  /** The object names an ops request may target, all in the environment's own namespace (`parseApplicationObjects`). */
@@ -22,7 +27,7 @@ export declare const FABRIKA_APPLICATION_OBJECTS = "FABRIKA_APPLICATION_OBJECTS"
22
27
  * application variable or secret under one of them, and so does every provider, for every surface that writes one.
23
28
  * A work unit that gives an application a new credential adds its name here.
24
29
  */
25
- export declare const FRAMEWORK_VARIABLE_NAMES: readonly ["FABRIKA_RELEASE", "FABRIKA_CONTRACT_DIGEST", "FABRIKA_IAM_ISSUER", "FABRIKA_IAM_APP_ID", "FABRIKA_OPERATIONS_DSN", "FABRIKA_APP_ID", "FABRIKA_ENVIRONMENT", "FABRIKA_SERVICE_KEY", "FABRIKA_APP_DATABASE_URL", "FABRIKA_APP_RUNTIME_DATABASE_URL", "FABRIKA_APP_DATABASE_CONNECTIONS", "FABRIKA_APP_STORAGE_ENDPOINT", "FABRIKA_APP_STORAGE_BUCKET", "FABRIKA_APP_STORAGE_ACCESS_KEY_ID", "FABRIKA_APP_STORAGE_SECRET_ACCESS_KEY", "FABRIKA_STORAGE_PROVENANCE", "FABRIKA_PROXY_SECRET", "FABRIKA_IAM_RPC_URL", "FABRIKA_IAM_RPC_KEY", "FABRIKA_SOURCE_MAP_UPLOAD", "FABRIKA_SOURCE_MAP_UPLOAD_KEY", "FABRIKA_TOKEN_KEYS", "FABRIKA_APP_ORIGIN", "FABRIKA_OPS_KEY_DIGESTS", "FABRIKA_APPLICATION_OBJECT", "FABRIKA_APPLICATION_OBJECTS"];
30
+ export declare const FRAMEWORK_VARIABLE_NAMES: readonly ["FABRIKA_RELEASE", "FABRIKA_CONTRACT_DIGEST", "FABRIKA_IAM_ISSUER", "FABRIKA_IAM_APP_ID", "FABRIKA_OPERATIONS_DSN", "FABRIKA_APP_ID", "FABRIKA_ENVIRONMENT", "FABRIKA_SERVICE_KEY", "FABRIKA_APP_DATABASE_URL", "FABRIKA_APP_RUNTIME_DATABASE_URL", "FABRIKA_APP_DATABASE_CONNECTIONS", "FABRIKA_APP_STORAGE_ENDPOINT", "FABRIKA_APP_STORAGE_BUCKET", "FABRIKA_APP_STORAGE_ACCESS_KEY_ID", "FABRIKA_APP_STORAGE_SECRET_ACCESS_KEY", "FABRIKA_STORAGE_PROVENANCE", "FABRIKA_PROXY_SECRET", "FABRIKA_IAM_RPC_URL", "FABRIKA_IAM_RPC_KEY", "FABRIKA_SOURCE_MAP_UPLOAD", "FABRIKA_SOURCE_MAP_UPLOAD_KEY", "FABRIKA_TOKEN_KEYS", "FABRIKA_APP_ORIGIN", "FABRIKA_OPS_KEY_DIGESTS", "FABRIKA_EGRESS_REFUSED_HOSTS", "FABRIKA_APPLICATION_OBJECT", "FABRIKA_APPLICATION_OBJECTS"];
26
31
  export type FrameworkVariableName = (typeof FRAMEWORK_VARIABLE_NAMES)[number];
27
32
  /**
28
33
  * The name families only the platform writes: one variable per job, webhook or public procedure, named after it
@@ -18,6 +18,7 @@ export * from './membership';
18
18
  export * from './oauth';
19
19
  export * from './ops';
20
20
  export * from './permissions';
21
+ export * from './public-egress';
21
22
  export * from './requirement';
22
23
  export * from './rpc';
23
24
  export { sha256Hex } from './sha256';
@@ -2,7 +2,7 @@
2
2
  * The framework names Control hands every deploy, each with a value or `null` (the deploy removes it). Beside them, a
3
3
  * deploy carries the families of `FRAMEWORK_VARIABLE_PREFIXES`, one variable per workload, import or receiver.
4
4
  */
5
- export declare const MANAGED_VARIABLE_NAMES: readonly ["FABRIKA_RELEASE", "FABRIKA_OPERATIONS_DSN", "FABRIKA_IAM_ISSUER", "FABRIKA_IAM_APP_ID", "FABRIKA_APP_ID", "FABRIKA_ENVIRONMENT", "FABRIKA_SERVICE_KEY", "FABRIKA_PROXY_SECRET", "FABRIKA_IAM_RPC_URL", "FABRIKA_IAM_RPC_KEY", "FABRIKA_APP_ORIGIN", "FABRIKA_TOKEN_KEYS", "FABRIKA_OPS_KEY_DIGESTS"];
5
+ export declare const MANAGED_VARIABLE_NAMES: readonly ["FABRIKA_RELEASE", "FABRIKA_OPERATIONS_DSN", "FABRIKA_IAM_ISSUER", "FABRIKA_IAM_APP_ID", "FABRIKA_APP_ID", "FABRIKA_ENVIRONMENT", "FABRIKA_SERVICE_KEY", "FABRIKA_PROXY_SECRET", "FABRIKA_IAM_RPC_URL", "FABRIKA_IAM_RPC_KEY", "FABRIKA_APP_ORIGIN", "FABRIKA_TOKEN_KEYS", "FABRIKA_OPS_KEY_DIGESTS", "FABRIKA_EGRESS_REFUSED_HOSTS"];
6
6
  export type ManagedVariableName = (typeof MANAGED_VARIABLE_NAMES)[number];
7
7
  /**
8
8
  * Whether a managed variable carries a credential, so a provider writes it as a secret and never reads it back. Fail