@getstrata/core 2.2.39 → 2.2.41

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.
@@ -2,267 +2,8 @@
2
2
  // ../../src/core/auth/oauth/oidcProvider.ts
3
3
  import { createHash as createHash2, randomBytes } from "crypto";
4
4
 
5
- // ../../src/core/security/safeUrl.ts
6
- import { lookup as dnsLookupImpl } from "dns/promises";
7
- import { BadRequestError } from "@getstrata/core/errors/http";
8
-
9
- // ../../src/core/runtime/appEnv.ts
10
- var NON_PRODUCTION_APP_ENVS = new Set(["local", "development", "dev", "test", "testing", "ci"]);
11
- function normalizeEnvValue(value) {
12
- return (value ?? "").trim().toLowerCase();
13
- }
14
- function isProductionEnv(env = process.env) {
15
- const appEnv = normalizeEnvValue(env.APP_ENV);
16
- const nodeEnv = normalizeEnvValue(env.NODE_ENV);
17
- if (appEnv === "production" || nodeEnv === "production") {
18
- return true;
19
- }
20
- if (appEnv === "") {
21
- return false;
22
- }
23
- return !NON_PRODUCTION_APP_ENVS.has(appEnv);
24
- }
25
- function envFlagEnabled(value) {
26
- return value === "true";
27
- }
28
-
29
- // ../../src/core/security/safeUrl.ts
30
- var dnsLookup = dnsLookupImpl;
31
- var BLOCKED_HOSTNAMES = new Set([
32
- "localhost",
33
- "127.0.0.1",
34
- "0.0.0.0",
35
- "::1",
36
- "metadata.google.internal"
37
- ]);
38
- function parseCanonicalDottedDecimal(hostname) {
39
- const parts = hostname.split(".");
40
- if (parts.length !== 4) {
41
- return null;
42
- }
43
- const octets = [];
44
- for (const part of parts) {
45
- if (!/^(0|[1-9]\d{0,2})$/u.test(part)) {
46
- return null;
47
- }
48
- const value = Number.parseInt(part, 10);
49
- if (!Number.isInteger(value) || value < 0 || value > 255) {
50
- return null;
51
- }
52
- octets.push(value);
53
- }
54
- return octets;
55
- }
56
- function parseDottedIpv4(hostname) {
57
- return parseCanonicalDottedDecimal(hostname);
58
- }
59
- function isBlockedIpv4Octets(octets) {
60
- const [a = 0, b = 0] = octets;
61
- if (a === 10 || a === 127 || a === 0) {
62
- return true;
63
- }
64
- if (a === 169 && b === 254) {
65
- return true;
66
- }
67
- if (a === 172 && b >= 16 && b <= 31) {
68
- return true;
69
- }
70
- if (a === 192 && b === 168) {
71
- return true;
72
- }
73
- if (a === 100 && b >= 64 && b <= 127) {
74
- return true;
75
- }
76
- if (a === 198 && (b === 18 || b === 19)) {
77
- return true;
78
- }
79
- return false;
80
- }
81
- function expandIpv6(hostname) {
82
- const trimmed = hostname.trim().toLowerCase();
83
- const unbracketed = trimmed.startsWith("[") && trimmed.endsWith("]") ? trimmed.slice(1, -1) : trimmed;
84
- if (unbracketed.includes("%")) {
85
- return null;
86
- }
87
- const mapped = unbracketed.match(/^::ffff:([0-9a-fx.]+)$/iu);
88
- if (mapped?.[1]) {
89
- const octets = parseDottedIpv4(mapped[1]);
90
- if (!octets) {
91
- return null;
92
- }
93
- return [
94
- 0,
95
- 0,
96
- 0,
97
- 0,
98
- 0,
99
- 65535,
100
- octets[0] << 8 | octets[1],
101
- octets[2] << 8 | octets[3]
102
- ];
103
- }
104
- const halves = unbracketed.split("::");
105
- if (halves.length > 2) {
106
- return null;
107
- }
108
- const parseGroup = (part) => {
109
- if (!/^[0-9a-f]{1,4}$/u.test(part)) {
110
- return null;
111
- }
112
- return Number.parseInt(part, 16);
113
- };
114
- if (halves.length === 1) {
115
- const groups = unbracketed.split(":");
116
- if (groups.length !== 8) {
117
- return null;
118
- }
119
- const values = groups.map(parseGroup);
120
- return values.every((value) => value !== null) ? values : null;
121
- }
122
- const head = halves[0] ? halves[0].split(":").filter(Boolean) : [];
123
- const tail = halves[1] ? halves[1].split(":").filter(Boolean) : [];
124
- if (head.length + tail.length > 8) {
125
- return null;
126
- }
127
- const values = [];
128
- for (const part of head) {
129
- const value = parseGroup(part);
130
- if (value === null) {
131
- return null;
132
- }
133
- values.push(value);
134
- }
135
- while (values.length < 8 - tail.length) {
136
- values.push(0);
137
- }
138
- for (const part of tail) {
139
- const value = parseGroup(part);
140
- if (value === null) {
141
- return null;
142
- }
143
- values.push(value);
144
- }
145
- return values.length === 8 ? values : null;
146
- }
147
- function isBlockedIpv6(hostname) {
148
- const groups = expandIpv6(hostname);
149
- if (!groups) {
150
- return hostname.includes(":");
151
- }
152
- const first = groups[0] ?? 0;
153
- const second = groups[1] ?? 0;
154
- if (groups.every((group) => group === 0)) {
155
- return true;
156
- }
157
- if (groups.slice(0, 7).every((group) => group === 0) && groups[7] === 1) {
158
- return true;
159
- }
160
- if ((first & 65472) === 65152) {
161
- return true;
162
- }
163
- if ((first & 65024) === 64512) {
164
- return true;
165
- }
166
- if ((first & 65280) === 65280) {
167
- return true;
168
- }
169
- if (first === 0 && second === 0 && groups[2] === 0 && groups[3] === 0 && groups[4] === 0 && groups[5] === 65535) {
170
- const a = (groups[6] ?? 0) >> 8;
171
- const b = (groups[6] ?? 0) & 255;
172
- const c = (groups[7] ?? 0) >> 8;
173
- const d = (groups[7] ?? 0) & 255;
174
- return isBlockedIpv4Octets([a, b, c, d]);
175
- }
176
- return false;
177
- }
178
- function isBlockedHostname(hostname) {
179
- const normalized = hostname.trim().toLowerCase();
180
- if (normalized.length === 0) {
181
- return true;
182
- }
183
- if (BLOCKED_HOSTNAMES.has(normalized)) {
184
- return true;
185
- }
186
- if (normalized.endsWith(".local") || normalized.endsWith(".internal")) {
187
- return true;
188
- }
189
- if (normalized.includes(":")) {
190
- return isBlockedIpv6(normalized);
191
- }
192
- if (/^\d+$/u.test(normalized)) {
193
- return true;
194
- }
195
- if (normalized.includes(".")) {
196
- const canonical = parseCanonicalDottedDecimal(normalized);
197
- if (canonical) {
198
- return isBlockedIpv4Octets(canonical);
199
- }
200
- if (/^[0-9a-fx.]+$/iu.test(normalized)) {
201
- return true;
202
- }
203
- }
204
- return false;
205
- }
206
- function productionSafeOutboundOptions(options) {
207
- if (!isProductionEnv()) {
208
- return options;
209
- }
210
- return { ...options, allowPrivate: false, resolveDns: true };
211
- }
212
- function assertSafeOutboundUrl(rawUrl, options = {}) {
213
- const resolved = productionSafeOutboundOptions(options);
214
- let parsed;
215
- try {
216
- parsed = new URL(rawUrl);
217
- } catch {
218
- throw new BadRequestError("Webhook URL is invalid.");
219
- }
220
- if (parsed.protocol !== "https:" && !(resolved.allowHttp && parsed.protocol === "http:")) {
221
- throw new BadRequestError("Webhook URL must use HTTPS.");
222
- }
223
- if (parsed.username || parsed.password) {
224
- throw new BadRequestError("Webhook URL must not include credentials.");
225
- }
226
- if (!resolved.allowPrivate && isBlockedHostname(parsed.hostname)) {
227
- throw new BadRequestError("Webhook URL targets a blocked host.");
228
- }
229
- return parsed;
230
- }
231
- function isBlockedIpAddress(address) {
232
- return isBlockedHostname(address.trim().toLowerCase());
233
- }
234
- function isLiteralIpHostname(hostname) {
235
- return parseCanonicalDottedDecimal(hostname) !== null || expandIpv6(hostname) !== null;
236
- }
237
- function pinUrlToAddress(url, address) {
238
- const pinned = new URL(url.toString());
239
- const ipv6 = expandIpv6(address.replace(/^\[|\]$/gu, ""));
240
- pinned.hostname = ipv6 ? `[${address.replace(/^\[|\]$/gu, "")}]` : address;
241
- return pinned;
242
- }
243
- async function resolveSafeOutboundTarget(rawUrl, options = {}) {
244
- const resolvedOptions = productionSafeOutboundOptions(options);
245
- const parsed = assertSafeOutboundUrl(rawUrl, resolvedOptions);
246
- if (resolvedOptions.resolveDns === false || resolvedOptions.allowPrivate) {
247
- return { url: parsed, addresses: [] };
248
- }
249
- const hostname = parsed.hostname.trim().toLowerCase();
250
- if (isLiteralIpHostname(hostname)) {
251
- return { url: parsed, addresses: [hostname] };
252
- }
253
- let results;
254
- try {
255
- results = await dnsLookup(hostname, { all: true, verbatim: true });
256
- } catch {
257
- throw new BadRequestError("Webhook URL targets a blocked host.");
258
- }
259
- if (results.length === 0 || results.some((result) => isBlockedIpAddress(result.address))) {
260
- throw new BadRequestError("Webhook URL targets a blocked host.");
261
- }
262
- return { url: parsed, addresses: results.map((result) => result.address) };
263
- }
264
-
265
5
  // ../../src/core/security/safeFetch.ts
6
+ import { pinUrlToAddress, resolveSafeOutboundTarget } from "@getstrata/core/security/safeUrl";
266
7
  var DEFAULT_FETCH_TIMEOUT_MS = 1e4;
267
8
  function untilAborted(work, signal) {
268
9
  signal.throwIfAborted();
@@ -397,6 +138,26 @@ async function safeFetch(input, init = {}, options = {}) {
397
138
  // ../../src/core/auth/oauth/oidcIdToken.ts
398
139
  import { createHash, createPublicKey, createVerify } from "crypto";
399
140
 
141
+ // ../../src/core/runtime/appEnv.ts
142
+ var NON_PRODUCTION_APP_ENVS = new Set(["local", "development", "dev", "test", "testing", "ci"]);
143
+ function normalizeEnvValue(value) {
144
+ return (value ?? "").trim().toLowerCase();
145
+ }
146
+ function isProductionEnv(env = process.env) {
147
+ const appEnv = normalizeEnvValue(env.APP_ENV);
148
+ const nodeEnv = normalizeEnvValue(env.NODE_ENV);
149
+ if (appEnv === "production" || nodeEnv === "production") {
150
+ return true;
151
+ }
152
+ if (appEnv === "") {
153
+ return false;
154
+ }
155
+ return !NON_PRODUCTION_APP_ENVS.has(appEnv);
156
+ }
157
+ function envFlagEnabled(value) {
158
+ return value === "true";
159
+ }
160
+
400
161
  // ../../src/core/security/timingSafeCompare.ts
401
162
  import { timingSafeEqual } from "crypto";
402
163
  function timingSafeCompareString(left, right) {
@@ -85,245 +85,8 @@ function sdkClientClassName() {
85
85
  return fromName ? `${fromName}Client` : "AppClient";
86
86
  }
87
87
 
88
- // ../../src/core/security/safeUrl.ts
89
- import { lookup as dnsLookupImpl } from "dns/promises";
90
- import { BadRequestError } from "@getstrata/core/errors/http";
91
- var dnsLookup = dnsLookupImpl;
92
- var BLOCKED_HOSTNAMES = new Set([
93
- "localhost",
94
- "127.0.0.1",
95
- "0.0.0.0",
96
- "::1",
97
- "metadata.google.internal"
98
- ]);
99
- function parseCanonicalDottedDecimal(hostname) {
100
- const parts = hostname.split(".");
101
- if (parts.length !== 4) {
102
- return null;
103
- }
104
- const octets = [];
105
- for (const part of parts) {
106
- if (!/^(0|[1-9]\d{0,2})$/u.test(part)) {
107
- return null;
108
- }
109
- const value = Number.parseInt(part, 10);
110
- if (!Number.isInteger(value) || value < 0 || value > 255) {
111
- return null;
112
- }
113
- octets.push(value);
114
- }
115
- return octets;
116
- }
117
- function parseDottedIpv4(hostname) {
118
- return parseCanonicalDottedDecimal(hostname);
119
- }
120
- function isBlockedIpv4Octets(octets) {
121
- const [a = 0, b = 0] = octets;
122
- if (a === 10 || a === 127 || a === 0) {
123
- return true;
124
- }
125
- if (a === 169 && b === 254) {
126
- return true;
127
- }
128
- if (a === 172 && b >= 16 && b <= 31) {
129
- return true;
130
- }
131
- if (a === 192 && b === 168) {
132
- return true;
133
- }
134
- if (a === 100 && b >= 64 && b <= 127) {
135
- return true;
136
- }
137
- if (a === 198 && (b === 18 || b === 19)) {
138
- return true;
139
- }
140
- return false;
141
- }
142
- function expandIpv6(hostname) {
143
- const trimmed = hostname.trim().toLowerCase();
144
- const unbracketed = trimmed.startsWith("[") && trimmed.endsWith("]") ? trimmed.slice(1, -1) : trimmed;
145
- if (unbracketed.includes("%")) {
146
- return null;
147
- }
148
- const mapped = unbracketed.match(/^::ffff:([0-9a-fx.]+)$/iu);
149
- if (mapped?.[1]) {
150
- const octets = parseDottedIpv4(mapped[1]);
151
- if (!octets) {
152
- return null;
153
- }
154
- return [
155
- 0,
156
- 0,
157
- 0,
158
- 0,
159
- 0,
160
- 65535,
161
- octets[0] << 8 | octets[1],
162
- octets[2] << 8 | octets[3]
163
- ];
164
- }
165
- const halves = unbracketed.split("::");
166
- if (halves.length > 2) {
167
- return null;
168
- }
169
- const parseGroup = (part) => {
170
- if (!/^[0-9a-f]{1,4}$/u.test(part)) {
171
- return null;
172
- }
173
- return Number.parseInt(part, 16);
174
- };
175
- if (halves.length === 1) {
176
- const groups = unbracketed.split(":");
177
- if (groups.length !== 8) {
178
- return null;
179
- }
180
- const values = groups.map(parseGroup);
181
- return values.every((value) => value !== null) ? values : null;
182
- }
183
- const head = halves[0] ? halves[0].split(":").filter(Boolean) : [];
184
- const tail = halves[1] ? halves[1].split(":").filter(Boolean) : [];
185
- if (head.length + tail.length > 8) {
186
- return null;
187
- }
188
- const values = [];
189
- for (const part of head) {
190
- const value = parseGroup(part);
191
- if (value === null) {
192
- return null;
193
- }
194
- values.push(value);
195
- }
196
- while (values.length < 8 - tail.length) {
197
- values.push(0);
198
- }
199
- for (const part of tail) {
200
- const value = parseGroup(part);
201
- if (value === null) {
202
- return null;
203
- }
204
- values.push(value);
205
- }
206
- return values.length === 8 ? values : null;
207
- }
208
- function isBlockedIpv6(hostname) {
209
- const groups = expandIpv6(hostname);
210
- if (!groups) {
211
- return hostname.includes(":");
212
- }
213
- const first = groups[0] ?? 0;
214
- const second = groups[1] ?? 0;
215
- if (groups.every((group) => group === 0)) {
216
- return true;
217
- }
218
- if (groups.slice(0, 7).every((group) => group === 0) && groups[7] === 1) {
219
- return true;
220
- }
221
- if ((first & 65472) === 65152) {
222
- return true;
223
- }
224
- if ((first & 65024) === 64512) {
225
- return true;
226
- }
227
- if ((first & 65280) === 65280) {
228
- return true;
229
- }
230
- if (first === 0 && second === 0 && groups[2] === 0 && groups[3] === 0 && groups[4] === 0 && groups[5] === 65535) {
231
- const a = (groups[6] ?? 0) >> 8;
232
- const b = (groups[6] ?? 0) & 255;
233
- const c = (groups[7] ?? 0) >> 8;
234
- const d = (groups[7] ?? 0) & 255;
235
- return isBlockedIpv4Octets([a, b, c, d]);
236
- }
237
- return false;
238
- }
239
- function isBlockedHostname(hostname) {
240
- const normalized = hostname.trim().toLowerCase();
241
- if (normalized.length === 0) {
242
- return true;
243
- }
244
- if (BLOCKED_HOSTNAMES.has(normalized)) {
245
- return true;
246
- }
247
- if (normalized.endsWith(".local") || normalized.endsWith(".internal")) {
248
- return true;
249
- }
250
- if (normalized.includes(":")) {
251
- return isBlockedIpv6(normalized);
252
- }
253
- if (/^\d+$/u.test(normalized)) {
254
- return true;
255
- }
256
- if (normalized.includes(".")) {
257
- const canonical = parseCanonicalDottedDecimal(normalized);
258
- if (canonical) {
259
- return isBlockedIpv4Octets(canonical);
260
- }
261
- if (/^[0-9a-fx.]+$/iu.test(normalized)) {
262
- return true;
263
- }
264
- }
265
- return false;
266
- }
267
- function productionSafeOutboundOptions(options) {
268
- if (!isProductionEnv()) {
269
- return options;
270
- }
271
- return { ...options, allowPrivate: false, resolveDns: true };
272
- }
273
- function assertSafeOutboundUrl(rawUrl, options = {}) {
274
- const resolved = productionSafeOutboundOptions(options);
275
- let parsed;
276
- try {
277
- parsed = new URL(rawUrl);
278
- } catch {
279
- throw new BadRequestError("Webhook URL is invalid.");
280
- }
281
- if (parsed.protocol !== "https:" && !(resolved.allowHttp && parsed.protocol === "http:")) {
282
- throw new BadRequestError("Webhook URL must use HTTPS.");
283
- }
284
- if (parsed.username || parsed.password) {
285
- throw new BadRequestError("Webhook URL must not include credentials.");
286
- }
287
- if (!resolved.allowPrivate && isBlockedHostname(parsed.hostname)) {
288
- throw new BadRequestError("Webhook URL targets a blocked host.");
289
- }
290
- return parsed;
291
- }
292
- function isBlockedIpAddress(address) {
293
- return isBlockedHostname(address.trim().toLowerCase());
294
- }
295
- function isLiteralIpHostname(hostname) {
296
- return parseCanonicalDottedDecimal(hostname) !== null || expandIpv6(hostname) !== null;
297
- }
298
- function pinUrlToAddress(url, address) {
299
- const pinned = new URL(url.toString());
300
- const ipv6 = expandIpv6(address.replace(/^\[|\]$/gu, ""));
301
- pinned.hostname = ipv6 ? `[${address.replace(/^\[|\]$/gu, "")}]` : address;
302
- return pinned;
303
- }
304
- async function resolveSafeOutboundTarget(rawUrl, options = {}) {
305
- const resolvedOptions = productionSafeOutboundOptions(options);
306
- const parsed = assertSafeOutboundUrl(rawUrl, resolvedOptions);
307
- if (resolvedOptions.resolveDns === false || resolvedOptions.allowPrivate) {
308
- return { url: parsed, addresses: [] };
309
- }
310
- const hostname = parsed.hostname.trim().toLowerCase();
311
- if (isLiteralIpHostname(hostname)) {
312
- return { url: parsed, addresses: [hostname] };
313
- }
314
- let results;
315
- try {
316
- results = await dnsLookup(hostname, { all: true, verbatim: true });
317
- } catch {
318
- throw new BadRequestError("Webhook URL targets a blocked host.");
319
- }
320
- if (results.length === 0 || results.some((result) => isBlockedIpAddress(result.address))) {
321
- throw new BadRequestError("Webhook URL targets a blocked host.");
322
- }
323
- return { url: parsed, addresses: results.map((result) => result.address) };
324
- }
325
-
326
88
  // ../../src/core/security/safeFetch.ts
89
+ import { pinUrlToAddress, resolveSafeOutboundTarget } from "@getstrata/core/security/safeUrl";
327
90
  var DEFAULT_FETCH_TIMEOUT_MS = 1e4;
328
91
  function untilAborted(work, signal) {
329
92
  signal.throwIfAborted();
@@ -403,7 +403,9 @@ async function renderWebErrorHtml(input) {
403
403
  }
404
404
  }
405
405
  async function htmlErrorResponse(input) {
406
- return htmlResponse(await renderWebErrorHtml(input), { status: input.status });
406
+ const response = htmlResponse(await renderWebErrorHtml(input), { status: input.status });
407
+ response.headers.set("Cache-Control", "no-store");
408
+ return response;
407
409
  }
408
410
 
409
411
  // ../../src/core/http/contentNegotiation.ts