@getstrata/core 2.2.56 → 2.2.57

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # @getstrata/core changelog
2
2
 
3
+ ## 2.2.57
4
+
5
+ Add opt-in CsrfOptions.targetOrigin with strict request Host/URL authority and source-origin validation while retaining signed cookie/token checks and existing defaults. Preserve native Request identity and ignore forwarding claims. Fall back safely to JSON error formatting for malformed native URL metadata.
6
+
3
7
  ## 2.2.56
4
8
 
5
9
  - Stop adding schema CREATE to PostgreSQL runtime roles; keep USAGE, existing object/default grants and both catalog/ACL concurrency protocols. Existing grants, PUBLIC privileges, memberships and ownership are not revoked automatically; restricted deployments can use the documented public schema-only migration runner.
@@ -1,3 +1,8 @@
1
1
  import type { Middleware } from "./middleware";
2
- declare function createCsrfMiddleware(): Middleware;
2
+ interface CsrfOptions {
3
+ /** Explicit public origin for TLS offload; requires matching URL authority and Host on every request. */
4
+ targetOrigin?: string;
5
+ }
6
+ declare function createCsrfMiddleware(options?: CsrfOptions): Middleware;
7
+ export type { CsrfOptions };
3
8
  export { createCsrfMiddleware };
@@ -0,0 +1,6 @@
1
+ /** Explicit target policy only. Forwarding headers never select an origin or approve a host. */
2
+ declare function createCsrfOriginPolicy(value: unknown): {
3
+ acceptsSourceOrigin(source: string | null): boolean;
4
+ assertHost(request: Request): void;
5
+ };
6
+ export { createCsrfOriginPolicy };
@@ -4,10 +4,15 @@ function requestPrefersJson(request) {
4
4
  if (!request) {
5
5
  return true;
6
6
  }
7
+ let pathname;
8
+ try {
9
+ pathname = new URL(request.url).pathname;
10
+ } catch {
11
+ return true;
12
+ }
7
13
  if (request.headers.get("HX-Request") === "true") {
8
14
  return false;
9
15
  }
10
- const pathname = new URL(request.url).pathname;
11
16
  if (pathname.startsWith("/api/")) {
12
17
  return true;
13
18
  }
@@ -28,9 +28,58 @@ var init_asyncContextStore = () => {};
28
28
 
29
29
  // ../../src/core/http/csrfMiddleware.ts
30
30
  import { currentAuthUser, currentCredentialSource } from "@getstrata/core/auth/authContext";
31
- import { ForbiddenError } from "@getstrata/core/errors/http";
31
+ import { ForbiddenError as ForbiddenError2 } from "@getstrata/core/errors/http";
32
32
  import { envFlagEnabled as envFlagEnabled2, isProductionEnv as isProductionEnv2 } from "@getstrata/core/runtime/appEnv";
33
33
 
34
+ // ../../src/core/http/csrfOriginPolicy.ts
35
+ import { isIP } from "net";
36
+ import { ForbiddenError } from "@getstrata/core/errors/http";
37
+ function parseAuthority(authority, protocol) {
38
+ if (!authority || authority.endsWith(":") || /[\s\\/@?#%,*]/.test(authority))
39
+ return null;
40
+ try {
41
+ const url = new URL(`${protocol}//${authority}`);
42
+ const hostname = url.hostname.replace(/\.$/, "");
43
+ const ip = hostname.startsWith("[") ? hostname.slice(1, -1) : hostname;
44
+ if (!isIP(ip) && (hostname.length > 253 || !hostname.split(".").every((label) => label.length > 0 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(label))))
45
+ return null;
46
+ return url.host;
47
+ } catch {
48
+ return null;
49
+ }
50
+ }
51
+ function parseOrigin(value, allowRootPath) {
52
+ if (typeof value !== "string" || value !== value.trim())
53
+ return null;
54
+ const match = /^(https?):\/\/([^/?#]+)(\/?)$/i.exec(value);
55
+ if (!match || !allowRootPath && match[3])
56
+ return null;
57
+ const protocol = `${match[1]?.toLowerCase()}:`;
58
+ const host = parseAuthority(match[2] ?? null, protocol);
59
+ return host ? `${protocol}//${host}` : null;
60
+ }
61
+ function createCsrfOriginPolicy(value) {
62
+ const origin = parseOrigin(value, true);
63
+ if (!origin)
64
+ throw new TypeError("CSRF targetOrigin must be a single HTTP(S) origin without credentials, path, query, fragment or wildcard.");
65
+ const target = new URL(origin);
66
+ return {
67
+ acceptsSourceOrigin(source) {
68
+ return parseOrigin(source, false) === origin;
69
+ },
70
+ assertHost(request) {
71
+ let url;
72
+ try {
73
+ url = new URL(request.url);
74
+ } catch {
75
+ throw new ForbiddenError("Unrecognized request host.");
76
+ }
77
+ if (!/^https?:$/.test(url.protocol) || url.username || url.password || parseAuthority(url.host, target.protocol) !== target.host || parseAuthority(request.headers.get("host"), target.protocol) !== target.host)
78
+ throw new ForbiddenError("Unrecognized request host.");
79
+ }
80
+ };
81
+ }
82
+
34
83
  // ../../src/core/http/csrfToken.ts
35
84
  import { timingSafeEqual } from "crypto";
36
85
 
@@ -272,8 +321,11 @@ function appendSetCookie(response, cookie) {
272
321
  headers
273
322
  });
274
323
  }
275
- function createCsrfMiddleware() {
324
+ function createCsrfMiddleware(options = {}) {
325
+ const targetOrigin = options.targetOrigin;
326
+ const policy = targetOrigin === undefined ? undefined : createCsrfOriginPolicy(targetOrigin);
276
327
  return async (request, next) => {
328
+ policy?.assertHost(request);
277
329
  const credentialSource = currentCredentialSource();
278
330
  const developmentHeaderProof = credentialSource === "guest" && currentAuthUser() !== null && envFlagEnabled2(process.env.AUTH_DEV_HEADERS) && !isProductionEnv2();
279
331
  if (credentialSource === "bearer" || credentialSource === "basic" || developmentHeaderProof) {
@@ -296,19 +348,19 @@ function createCsrfMiddleware() {
296
348
  }
297
349
  const submitted = await readSubmittedCsrfTokenFromBody(request);
298
350
  if (!verifyCsrfToken(request, submitted)) {
299
- throw new ForbiddenError("Invalid or missing CSRF token.");
351
+ throw new ForbiddenError2("Invalid or missing CSRF token.");
300
352
  }
301
353
  const originHeader = request.headers.get("origin");
302
354
  let headerOrigin = null;
303
- if (originHeader) {
355
+ if (!policy && originHeader) {
304
356
  try {
305
357
  headerOrigin = new URL(originHeader).origin;
306
358
  } catch {
307
359
  headerOrigin = null;
308
360
  }
309
361
  }
310
- if (!headerOrigin || headerOrigin !== new URL(request.url).origin) {
311
- throw new ForbiddenError("Cross-site cookie requests are not allowed.");
362
+ if (policy ? !policy.acceptsSourceOrigin(originHeader) : !headerOrigin || headerOrigin !== new URL(request.url).origin) {
363
+ throw new ForbiddenError2("Cross-site cookie requests are not allowed.");
312
364
  }
313
365
  return await next();
314
366
  };
@@ -419,10 +419,15 @@ function requestPrefersJson(request) {
419
419
  if (!request) {
420
420
  return true;
421
421
  }
422
+ let pathname;
423
+ try {
424
+ pathname = new URL(request.url).pathname;
425
+ } catch {
426
+ return true;
427
+ }
422
428
  if (request.headers.get("HX-Request") === "true") {
423
429
  return false;
424
430
  }
425
- const pathname = new URL(request.url).pathname;
426
431
  if (pathname.startsWith("/api/")) {
427
432
  return true;
428
433
  }
@@ -54,10 +54,15 @@ function requestPrefersJson(request) {
54
54
  if (!request) {
55
55
  return true;
56
56
  }
57
+ let pathname;
58
+ try {
59
+ pathname = new URL(request.url).pathname;
60
+ } catch {
61
+ return true;
62
+ }
57
63
  if (request.headers.get("HX-Request") === "true") {
58
64
  return false;
59
65
  }
60
- const pathname = new URL(request.url).pathname;
61
66
  if (pathname.startsWith("/api/")) {
62
67
  return true;
63
68
  }
@@ -38,10 +38,15 @@ function requestPrefersJson(request) {
38
38
  if (!request) {
39
39
  return true;
40
40
  }
41
+ let pathname;
42
+ try {
43
+ pathname = new URL(request.url).pathname;
44
+ } catch {
45
+ return true;
46
+ }
41
47
  if (request.headers.get("HX-Request") === "true") {
42
48
  return false;
43
49
  }
44
- const pathname = new URL(request.url).pathname;
45
50
  if (pathname.startsWith("/api/")) {
46
51
  return true;
47
52
  }
@@ -14,10 +14,15 @@ function requestPrefersJson(request) {
14
14
  if (!request) {
15
15
  return true;
16
16
  }
17
+ let pathname;
18
+ try {
19
+ pathname = new URL(request.url).pathname;
20
+ } catch {
21
+ return true;
22
+ }
17
23
  if (request.headers.get("HX-Request") === "true") {
18
24
  return false;
19
25
  }
20
- const pathname = new URL(request.url).pathname;
21
26
  if (pathname.startsWith("/api/")) {
22
27
  return true;
23
28
  }
@@ -9,10 +9,15 @@ function requestPrefersJson(request) {
9
9
  if (!request) {
10
10
  return true;
11
11
  }
12
+ let pathname;
13
+ try {
14
+ pathname = new URL(request.url).pathname;
15
+ } catch {
16
+ return true;
17
+ }
12
18
  if (request.headers.get("HX-Request") === "true") {
13
19
  return false;
14
20
  }
15
- const pathname = new URL(request.url).pathname;
16
21
  if (pathname.startsWith("/api/")) {
17
22
  return true;
18
23
  }
@@ -473,10 +473,15 @@ function requestPrefersJson(request) {
473
473
  if (!request) {
474
474
  return true;
475
475
  }
476
+ let pathname;
477
+ try {
478
+ pathname = new URL(request.url).pathname;
479
+ } catch {
480
+ return true;
481
+ }
476
482
  if (request.headers.get("HX-Request") === "true") {
477
483
  return false;
478
484
  }
479
- const pathname = new URL(request.url).pathname;
480
485
  if (pathname.startsWith("/api/")) {
481
486
  return true;
482
487
  }
@@ -358,10 +358,15 @@ function requestPrefersJson(request) {
358
358
  if (!request) {
359
359
  return true;
360
360
  }
361
+ let pathname;
362
+ try {
363
+ pathname = new URL(request.url).pathname;
364
+ } catch {
365
+ return true;
366
+ }
361
367
  if (request.headers.get("HX-Request") === "true") {
362
368
  return false;
363
369
  }
364
- const pathname = new URL(request.url).pathname;
365
370
  if (pathname.startsWith("/api/")) {
366
371
  return true;
367
372
  }
@@ -7,10 +7,15 @@ function requestPrefersJson(request) {
7
7
  if (!request) {
8
8
  return true;
9
9
  }
10
+ let pathname;
11
+ try {
12
+ pathname = new URL(request.url).pathname;
13
+ } catch {
14
+ return true;
15
+ }
10
16
  if (request.headers.get("HX-Request") === "true") {
11
17
  return false;
12
18
  }
13
- const pathname = new URL(request.url).pathname;
14
19
  if (pathname.startsWith("/api/")) {
15
20
  return true;
16
21
  }
@@ -87,7 +87,7 @@ export type { ContentSecurityPolicyDirectives, ContentSecurityPolicyOptions, } f
87
87
  export { configureContentSecurityPolicy, generateCspNonce, HTMX_2_0_4_INDICATOR_STYLE_HASH, resetContentSecurityPolicyForTests, resolveContentSecurityPolicy, resolveHtmlContentSecurityPolicy, serverHtmxContentSecurityPolicy, spaContentSecurityPolicy, strictApiContentSecurityPolicy, } from "../core/http/contentSecurityPolicy.ts";
88
88
  export { readBunRequestCookie, readRequestCookie } from "../core/http/cookies.ts";
89
89
  export { type CorsConfig, type CorsOptions, createCorsMiddleware, } from "../core/http/corsMiddleware.ts";
90
- export { createCsrfMiddleware } from "../core/http/csrfMiddleware.ts";
90
+ export { type CsrfOptions, createCsrfMiddleware } from "../core/http/csrfMiddleware.ts";
91
91
  export { createCsrfProtection } from "../core/http/csrfProtection.ts";
92
92
  export { createCsrfTokenCookie, readSubmittedCsrfToken, readSubmittedCsrfTokenFromBody, resolveCsrfToken, resolveCsrfTokenForRequest, verifyCsrfToken, } from "../core/http/csrfToken.ts";
93
93
  export { applyConditionalGet, assertIfMatch, computeEtagFromJson, type EtagVersioned, etagFromResource, etagValuesMatch, ifMatchSatisfied, ifNoneMatchSatisfied, isEtagEnabled, notModifiedResponse, } from "../core/http/etag.ts";
package/dist/index.js CHANGED
@@ -12046,10 +12046,15 @@ function requestPrefersJson(request) {
12046
12046
  if (!request) {
12047
12047
  return true;
12048
12048
  }
12049
+ let pathname;
12050
+ try {
12051
+ pathname = new URL(request.url).pathname;
12052
+ } catch {
12053
+ return true;
12054
+ }
12049
12055
  if (request.headers.get("HX-Request") === "true") {
12050
12056
  return false;
12051
12057
  }
12052
- const pathname = new URL(request.url).pathname;
12053
12058
  if (pathname.startsWith("/api/")) {
12054
12059
  return true;
12055
12060
  }
@@ -12640,6 +12645,54 @@ function buildCorsHeaders(request, options) {
12640
12645
  }
12641
12646
  return headers;
12642
12647
  }
12648
+ // ../../src/core/http/csrfOriginPolicy.ts
12649
+ import { isIP } from "net";
12650
+ function parseAuthority(authority, protocol) {
12651
+ if (!authority || authority.endsWith(":") || /[\s\\/@?#%,*]/.test(authority))
12652
+ return null;
12653
+ try {
12654
+ const url = new URL(`${protocol}//${authority}`);
12655
+ const hostname = url.hostname.replace(/\.$/, "");
12656
+ const ip = hostname.startsWith("[") ? hostname.slice(1, -1) : hostname;
12657
+ if (!isIP(ip) && (hostname.length > 253 || !hostname.split(".").every((label) => label.length > 0 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(label))))
12658
+ return null;
12659
+ return url.host;
12660
+ } catch {
12661
+ return null;
12662
+ }
12663
+ }
12664
+ function parseOrigin(value, allowRootPath) {
12665
+ if (typeof value !== "string" || value !== value.trim())
12666
+ return null;
12667
+ const match = /^(https?):\/\/([^/?#]+)(\/?)$/i.exec(value);
12668
+ if (!match || !allowRootPath && match[3])
12669
+ return null;
12670
+ const protocol = `${match[1]?.toLowerCase()}:`;
12671
+ const host = parseAuthority(match[2] ?? null, protocol);
12672
+ return host ? `${protocol}//${host}` : null;
12673
+ }
12674
+ function createCsrfOriginPolicy(value) {
12675
+ const origin = parseOrigin(value, true);
12676
+ if (!origin)
12677
+ throw new TypeError("CSRF targetOrigin must be a single HTTP(S) origin without credentials, path, query, fragment or wildcard.");
12678
+ const target = new URL(origin);
12679
+ return {
12680
+ acceptsSourceOrigin(source) {
12681
+ return parseOrigin(source, false) === origin;
12682
+ },
12683
+ assertHost(request) {
12684
+ let url;
12685
+ try {
12686
+ url = new URL(request.url);
12687
+ } catch {
12688
+ throw new ForbiddenError("Unrecognized request host.");
12689
+ }
12690
+ if (!/^https?:$/.test(url.protocol) || url.username || url.password || parseAuthority(url.host, target.protocol) !== target.host || parseAuthority(request.headers.get("host"), target.protocol) !== target.host)
12691
+ throw new ForbiddenError("Unrecognized request host.");
12692
+ }
12693
+ };
12694
+ }
12695
+
12643
12696
  // ../../src/core/http/csrfToken.ts
12644
12697
  import { timingSafeEqual as timingSafeEqual3 } from "crypto";
12645
12698
  var CSRF_COOKIE = appCookieName("csrf");
@@ -12748,8 +12801,11 @@ function appendSetCookie(response, cookie) {
12748
12801
  headers
12749
12802
  });
12750
12803
  }
12751
- function createCsrfMiddleware() {
12804
+ function createCsrfMiddleware(options = {}) {
12805
+ const targetOrigin = options.targetOrigin;
12806
+ const policy = targetOrigin === undefined ? undefined : createCsrfOriginPolicy(targetOrigin);
12752
12807
  return async (request, next) => {
12808
+ policy?.assertHost(request);
12753
12809
  const credentialSource = currentCredentialSource();
12754
12810
  const developmentHeaderProof = credentialSource === "guest" && currentAuthUser() !== null && envFlagEnabled(process.env.AUTH_DEV_HEADERS) && !isProductionEnv();
12755
12811
  if (credentialSource === "bearer" || credentialSource === "basic" || developmentHeaderProof) {
@@ -12776,14 +12832,14 @@ function createCsrfMiddleware() {
12776
12832
  }
12777
12833
  const originHeader = request.headers.get("origin");
12778
12834
  let headerOrigin = null;
12779
- if (originHeader) {
12835
+ if (!policy && originHeader) {
12780
12836
  try {
12781
12837
  headerOrigin = new URL(originHeader).origin;
12782
12838
  } catch {
12783
12839
  headerOrigin = null;
12784
12840
  }
12785
12841
  }
12786
- if (!headerOrigin || headerOrigin !== new URL(request.url).origin) {
12842
+ if (policy ? !policy.acceptsSourceOrigin(originHeader) : !headerOrigin || headerOrigin !== new URL(request.url).origin) {
12787
12843
  throw new ForbiddenError("Cross-site cookie requests are not allowed.");
12788
12844
  }
12789
12845
  return await next();
@@ -16417,7 +16473,7 @@ import { createHash as createHash8 } from "crypto";
16417
16473
  init_databaseTenantContext();
16418
16474
 
16419
16475
  // ../../src/core/tenant/publicTenantHost.ts
16420
- import { BlockList, isIP } from "net";
16476
+ import { BlockList, isIP as isIP2 } from "net";
16421
16477
  function normalizeTenantHostname(authority) {
16422
16478
  if (!authority || authority !== authority.trim() || /[\s\\/@?#%,]/.test(authority)) {
16423
16479
  throw new ForbiddenError("Invalid tenant hostname.");
@@ -16430,7 +16486,7 @@ function normalizeTenantHostname(authority) {
16430
16486
  }
16431
16487
  const hostname = url.hostname.toLowerCase().replace(/\.$/, "");
16432
16488
  const ip = hostname.startsWith("[") ? hostname.slice(1, -1) : hostname;
16433
- if (!isIP(ip) && (hostname.length > 253 || !hostname.split(".").every((label) => label.length > 0 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label)))) {
16489
+ if (!isIP2(ip) && (hostname.length > 253 || !hostname.split(".").every((label) => label.length > 0 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label)))) {
16434
16490
  throw new ForbiddenError("Invalid tenant hostname.");
16435
16491
  }
16436
16492
  return hostname;
@@ -16441,14 +16497,14 @@ function createPublicTenantSelector(options) {
16441
16497
  throw new Error("Public tenancy supports at most 128 trusted proxy addresses.");
16442
16498
  const proxies = new BlockList;
16443
16499
  for (const address of addresses) {
16444
- const version = isIP(address);
16500
+ const version = isIP2(address);
16445
16501
  if (!version)
16446
16502
  throw new Error("Trusted proxy addresses must be literal IP addresses.");
16447
16503
  proxies.addAddress(address, version === 6 ? "ipv6" : "ipv4");
16448
16504
  }
16449
16505
  return async (request) => {
16450
16506
  const peer = currentRequestMeta().peerAddress;
16451
- const version = peer ? isIP(peer) : 0;
16507
+ const version = peer ? isIP2(peer) : 0;
16452
16508
  const trusted = peer && version && proxies.check(peer, version === 6 ? "ipv6" : "ipv4");
16453
16509
  const forwarded = trusted ? request.headers.get("x-forwarded-host") : null;
16454
16510
  const authority = forwarded ?? request.headers.get("host") ?? new URL(request.url).host;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getstrata/core",
3
- "version": "2.2.56",
3
+ "version": "2.2.57",
4
4
  "description": "Strata Bun framework public API",
5
5
  "type": "module",
6
6
  "license": "MIT",