@foldrun/core 19.1.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.
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
  /**