@foldrun/core 19.2.0 → 19.3.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.
@@ -155,7 +155,18 @@ export interface S3Config {
155
155
  /** `https://s3.ap-southeast-2.amazonaws.com` → `ap-southeast-2`. Null for
156
156
  * anything else (R2, MinIO), whose region is whatever the env says. */
157
157
  export declare function regionOfEndpoint(endpoint: string): string | null;
158
- export declare function s3Config(): S3Config | null;
158
+ export declare function s3Config(env?: NodeJS.ProcessEnv): S3Config | null;
159
+ /**
160
+ * The origin the browser reaches the object store at — where a presigned PUT
161
+ * (an upload) and a presigned GET (a video, audio or PDF preview) go — so the
162
+ * dashboard's Content-Security-Policy can name it and nothing wider. Null
163
+ * unless S3 is the driver in force (the same test driverFor makes), or when
164
+ * the address is not a plain http(s) host. It is built by the same function
165
+ * as the signed URLs (bucketHost), so the two cannot drift: path-style gives
166
+ * the endpoint's origin, virtual-hosted gives `https://<bucket>.<host>`.
167
+ * Reads only the environment; it fetches no credentials.
168
+ */
169
+ export declare function storeOrigin(env?: NodeJS.ProcessEnv): string | null;
159
170
  /** For tests: forget cached role credentials. */
160
171
  export declare function forgetS3Credentials(): void;
161
172
  /** A body that is not held in memory: read as it is sent. `length` and
@@ -434,31 +434,59 @@ export function regionOfEndpoint(endpoint) {
434
434
  /** True when this process runs under an AWS role handed out through the
435
435
  * container credential endpoint — EKS Pod Identity, ECS, or an IRSA-style
436
436
  * shim. The endpoint is the pod's, and the token file is mounted beside it. */
437
- function containerCredentialsAvailable() {
438
- return Boolean(process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI || process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI);
439
- }
440
- export function s3Config() {
441
- const endpoint = process.env.FOLDRUN_S3_ENDPOINT;
442
- const bucket = process.env.FOLDRUN_S3_BUCKET;
443
- const accessKeyId = process.env.FOLDRUN_S3_ACCESS_KEY_ID;
444
- const secretAccessKey = process.env.FOLDRUN_S3_SECRET_ACCESS_KEY;
437
+ function containerCredentialsAvailable(env = process.env) {
438
+ return Boolean(env.AWS_CONTAINER_CREDENTIALS_FULL_URI || env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI);
439
+ }
440
+ export function s3Config(env = process.env) {
441
+ const endpoint = env.FOLDRUN_S3_ENDPOINT;
442
+ const bucket = env.FOLDRUN_S3_BUCKET;
443
+ const accessKeyId = env.FOLDRUN_S3_ACCESS_KEY_ID;
444
+ const secretAccessKey = env.FOLDRUN_S3_SECRET_ACCESS_KEY;
445
445
  if (!endpoint || !bucket)
446
446
  return null;
447
447
  const staticKeys = Boolean(accessKeyId && secretAccessKey);
448
448
  // No key and no role is a half-set environment: fall back to fs rather
449
449
  // than fail at the first upload (driverFor says why).
450
- if (!staticKeys && !containerCredentialsAvailable())
450
+ if (!staticKeys && !containerCredentialsAvailable(env))
451
451
  return null;
452
452
  return {
453
453
  endpoint: endpoint.replace(/\/+$/, ""),
454
454
  bucket,
455
455
  // R2 has one region and calls it `auto`. An AWS endpoint names its own;
456
456
  // anything else says so in the env.
457
- region: process.env.FOLDRUN_S3_REGION ?? regionOfEndpoint(endpoint) ?? "auto",
457
+ region: env.FOLDRUN_S3_REGION ?? regionOfEndpoint(endpoint) ?? "auto",
458
458
  ...(staticKeys ? { accessKeyId, secretAccessKey } : {}),
459
- pathStyle: process.env.FOLDRUN_S3_PATH_STYLE !== "false",
459
+ pathStyle: env.FOLDRUN_S3_PATH_STYLE !== "false",
460
460
  };
461
461
  }
462
+ /**
463
+ * The origin the browser reaches the object store at — where a presigned PUT
464
+ * (an upload) and a presigned GET (a video, audio or PDF preview) go — so the
465
+ * dashboard's Content-Security-Policy can name it and nothing wider. Null
466
+ * unless S3 is the driver in force (the same test driverFor makes), or when
467
+ * the address is not a plain http(s) host. It is built by the same function
468
+ * as the signed URLs (bucketHost), so the two cannot drift: path-style gives
469
+ * the endpoint's origin, virtual-hosted gives `https://<bucket>.<host>`.
470
+ * Reads only the environment; it fetches no credentials.
471
+ */
472
+ export function storeOrigin(env = process.env) {
473
+ if (env.FOLDRUN_STORAGE_DRIVER !== "s3")
474
+ return null;
475
+ try {
476
+ const cfg = s3Config(env);
477
+ if (!cfg)
478
+ return null;
479
+ const u = new URL(bucketHost(cfg).origin);
480
+ if (u.protocol !== "https:" && u.protocol !== "http:")
481
+ return null;
482
+ if (!u.hostname || u.hostname.includes("*") || u.origin === "null")
483
+ return null;
484
+ return u.origin;
485
+ }
486
+ catch {
487
+ return null; // an endpoint that is not a URL: no source, never a broken one
488
+ }
489
+ }
462
490
  // ---------- credentials ----------
463
491
  //
464
492
  // Static keys are used as given. Without them, the process is running under
@@ -523,13 +551,19 @@ function signingKey(cfg, creds, dateStamp) {
523
551
  }
524
552
  /** Where a key lives, split into the host and the canonical path SigV4 signs. */
525
553
  function target(cfg, key) {
526
- const url = new URL(cfg.endpoint);
527
554
  const encoded = uriEncode(key, true);
528
- if (cfg.pathStyle) {
529
- return { host: url.host, origin: url.origin, canonicalUri: `/${cfg.bucket}/${encoded}` };
530
- }
555
+ const { host, origin } = bucketHost(cfg);
556
+ return { host, origin, canonicalUri: cfg.pathStyle ? `/${cfg.bucket}/${encoded}` : `/${encoded}` };
557
+ }
558
+ /** The host a request for this bucket goes to: the endpoint's own
559
+ * (path-style), or `<bucket>.<endpoint host>` (virtual-hosted). Shared by
560
+ * target() and storeOrigin(). */
561
+ function bucketHost(cfg) {
562
+ const url = new URL(cfg.endpoint);
563
+ if (cfg.pathStyle)
564
+ return { host: url.host, origin: url.origin };
531
565
  const host = `${cfg.bucket}.${url.host}`;
532
- return { host, origin: `${url.protocol}//${host}`, canonicalUri: `/${encoded}` };
566
+ return { host, origin: `${url.protocol}//${host}` };
533
567
  }
534
568
  /**
535
569
  * A presigned URL — SigV4 in the query string, so the browser (or a run pod)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foldrun/core",
3
- "version": "19.2.0",
3
+ "version": "19.3.0",
4
4
  "description": "Agents are just folders. The format, the runner, the checks.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "type": "module",
package/src/storage.ts CHANGED
@@ -529,31 +529,55 @@ export function regionOfEndpoint(endpoint: string): string | null {
529
529
  /** True when this process runs under an AWS role handed out through the
530
530
  * container credential endpoint — EKS Pod Identity, ECS, or an IRSA-style
531
531
  * shim. The endpoint is the pod's, and the token file is mounted beside it. */
532
- function containerCredentialsAvailable(): boolean {
533
- return Boolean(process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI || process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI);
532
+ function containerCredentialsAvailable(env: NodeJS.ProcessEnv = process.env): boolean {
533
+ return Boolean(env.AWS_CONTAINER_CREDENTIALS_FULL_URI || env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI);
534
534
  }
535
535
 
536
- export function s3Config(): S3Config | null {
537
- const endpoint = process.env.FOLDRUN_S3_ENDPOINT;
538
- const bucket = process.env.FOLDRUN_S3_BUCKET;
539
- const accessKeyId = process.env.FOLDRUN_S3_ACCESS_KEY_ID;
540
- const secretAccessKey = process.env.FOLDRUN_S3_SECRET_ACCESS_KEY;
536
+ export function s3Config(env: NodeJS.ProcessEnv = process.env): S3Config | null {
537
+ const endpoint = env.FOLDRUN_S3_ENDPOINT;
538
+ const bucket = env.FOLDRUN_S3_BUCKET;
539
+ const accessKeyId = env.FOLDRUN_S3_ACCESS_KEY_ID;
540
+ const secretAccessKey = env.FOLDRUN_S3_SECRET_ACCESS_KEY;
541
541
  if (!endpoint || !bucket) return null;
542
542
  const staticKeys = Boolean(accessKeyId && secretAccessKey);
543
543
  // No key and no role is a half-set environment: fall back to fs rather
544
544
  // than fail at the first upload (driverFor says why).
545
- if (!staticKeys && !containerCredentialsAvailable()) return null;
545
+ if (!staticKeys && !containerCredentialsAvailable(env)) return null;
546
546
  return {
547
547
  endpoint: endpoint.replace(/\/+$/, ""),
548
548
  bucket,
549
549
  // R2 has one region and calls it `auto`. An AWS endpoint names its own;
550
550
  // anything else says so in the env.
551
- region: process.env.FOLDRUN_S3_REGION ?? regionOfEndpoint(endpoint) ?? "auto",
551
+ region: env.FOLDRUN_S3_REGION ?? regionOfEndpoint(endpoint) ?? "auto",
552
552
  ...(staticKeys ? { accessKeyId, secretAccessKey } : {}),
553
- pathStyle: process.env.FOLDRUN_S3_PATH_STYLE !== "false",
553
+ pathStyle: env.FOLDRUN_S3_PATH_STYLE !== "false",
554
554
  };
555
555
  }
556
556
 
557
+ /**
558
+ * The origin the browser reaches the object store at — where a presigned PUT
559
+ * (an upload) and a presigned GET (a video, audio or PDF preview) go — so the
560
+ * dashboard's Content-Security-Policy can name it and nothing wider. Null
561
+ * unless S3 is the driver in force (the same test driverFor makes), or when
562
+ * the address is not a plain http(s) host. It is built by the same function
563
+ * as the signed URLs (bucketHost), so the two cannot drift: path-style gives
564
+ * the endpoint's origin, virtual-hosted gives `https://<bucket>.<host>`.
565
+ * Reads only the environment; it fetches no credentials.
566
+ */
567
+ export function storeOrigin(env: NodeJS.ProcessEnv = process.env): string | null {
568
+ if (env.FOLDRUN_STORAGE_DRIVER !== "s3") return null;
569
+ try {
570
+ const cfg = s3Config(env);
571
+ if (!cfg) return null;
572
+ const u = new URL(bucketHost(cfg).origin);
573
+ if (u.protocol !== "https:" && u.protocol !== "http:") return null;
574
+ if (!u.hostname || u.hostname.includes("*") || u.origin === "null") return null;
575
+ return u.origin;
576
+ } catch {
577
+ return null; // an endpoint that is not a URL: no source, never a broken one
578
+ }
579
+ }
580
+
557
581
  // ---------- credentials ----------
558
582
  //
559
583
  // Static keys are used as given. Without them, the process is running under
@@ -630,13 +654,19 @@ function signingKey(cfg: S3Config, creds: Credentials, dateStamp: string) {
630
654
 
631
655
  /** Where a key lives, split into the host and the canonical path SigV4 signs. */
632
656
  function target(cfg: S3Config, key: string) {
633
- const url = new URL(cfg.endpoint);
634
657
  const encoded = uriEncode(key, true);
635
- if (cfg.pathStyle) {
636
- return { host: url.host, origin: url.origin, canonicalUri: `/${cfg.bucket}/${encoded}` };
637
- }
658
+ const { host, origin } = bucketHost(cfg);
659
+ return { host, origin, canonicalUri: cfg.pathStyle ? `/${cfg.bucket}/${encoded}` : `/${encoded}` };
660
+ }
661
+
662
+ /** The host a request for this bucket goes to: the endpoint's own
663
+ * (path-style), or `<bucket>.<endpoint host>` (virtual-hosted). Shared by
664
+ * target() and storeOrigin(). */
665
+ function bucketHost(cfg: S3Config): { host: string; origin: string } {
666
+ const url = new URL(cfg.endpoint);
667
+ if (cfg.pathStyle) return { host: url.host, origin: url.origin };
638
668
  const host = `${cfg.bucket}.${url.host}`;
639
- return { host, origin: `${url.protocol}//${host}`, canonicalUri: `/${encoded}` };
669
+ return { host, origin: `${url.protocol}//${host}` };
640
670
  }
641
671
 
642
672
  /**