@cappa/core 0.13.0 → 0.13.1

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/dist/index.cjs CHANGED
@@ -504,6 +504,57 @@ async function mapWithConcurrency(concurrency, items, fn) {
504
504
  return results;
505
505
  }
506
506
  //#endregion
507
+ //#region src/paths.ts
508
+ /**
509
+ * Screenshot filenames are not a trusted input.
510
+ *
511
+ * A name is whatever a plugin chose, and a plugin may derive it from something
512
+ * it does not control either: `@cappa/plugin-storybook` reads
513
+ * `parameters.cappa.variants[].filename` out of the browser, so the string
514
+ * originates in story-author code. Every name eventually lands in
515
+ * `path.resolve(<bucket dir>, name)`, which honours both absolute paths and
516
+ * `../`, so without a guard a single story could write a PNG anywhere the
517
+ * process can write.
518
+ *
519
+ * Two layers live here: `sanitizeScreenshotFilename` turns a name into a
520
+ * relative path that cannot escape (used where a bad name should degrade rather
521
+ * than fail a run), and `resolveInside` is the hard backstop at the write sink.
522
+ */
523
+ /**
524
+ * Whether `target` resolves to something strictly inside `base`.
525
+ *
526
+ * Note this is deliberately stricter than a `startsWith("..")` check on the
527
+ * relative path, which also rejects legitimate names such as `..foo.png`.
528
+ */
529
+ function isPathInside(base, target) {
530
+ const relative = node_path.default.relative(base, target);
531
+ return relative !== "" && relative !== ".." && !relative.startsWith(`..${node_path.default.sep}`) && !node_path.default.isAbsolute(relative);
532
+ }
533
+ /**
534
+ * Resolve `filename` inside `base`, rejecting absolute paths and any name that
535
+ * escapes `base`.
536
+ *
537
+ * @param label - The bucket name used in the error message (`actual`, `diff`, …)
538
+ */
539
+ function resolveInside(base, filename, label) {
540
+ const resolved = node_path.default.resolve(base, filename);
541
+ if (node_path.default.isAbsolute(filename) || !isPathInside(base, resolved)) throw new Error(`Refusing to resolve screenshot path outside of the ${label} directory: ${filename}`);
542
+ return resolved;
543
+ }
544
+ /**
545
+ * Turn a screenshot filename into a relative path that stays inside its bucket.
546
+ *
547
+ * Path separators are kept — they are how plugins express folders — but
548
+ * traversal (`..`), no-op (`.`) and empty segments are dropped, backslashes are
549
+ * normalised to `/`, and a Windows drive prefix is removed. Returns an empty
550
+ * string when nothing usable is left, which callers treat as "no name given".
551
+ */
552
+ function sanitizeScreenshotFilename(filename) {
553
+ const segments = filename.replace(/\\/g, "/").split("/").map((segment) => segment.replace(/\0/g, "").trim()).filter((segment) => segment !== "" && segment !== "." && segment !== "..");
554
+ if (segments[0] && /^[a-zA-Z]:$/.test(segments[0])) segments.shift();
555
+ return segments.join("/");
556
+ }
557
+ //#endregion
507
558
  //#region src/filesystem.ts
508
559
  var filesystem_exports = /* @__PURE__ */ __exportAll({
509
560
  ScreenshotFileSystem: () => ScreenshotFileSystem,
@@ -571,9 +622,8 @@ var ScreenshotFileSystem = class {
571
622
  */
572
623
  async approveFromActualPath(actualFilePath) {
573
624
  const actualAbsolute = this.resolveActualPath(actualFilePath);
574
- const relativePath = node_path.default.relative(this.actual, actualAbsolute);
575
- if (relativePath.startsWith("..")) throw new Error(`Cannot approve screenshot outside of actual directory: ${actualFilePath}`);
576
- return this.approveRelative(relativePath);
625
+ if (!isPathInside(this.actual, actualAbsolute)) throw new Error(`Cannot approve screenshot outside of actual directory: ${actualFilePath}`);
626
+ return this.approveRelative(node_path.default.relative(this.actual, actualAbsolute));
577
627
  }
578
628
  /**
579
629
  * Apply baseline updates for grouped screenshots (same rules as `cappa approve`).
@@ -705,22 +755,33 @@ var ScreenshotFileSystem = class {
705
755
  return this.diff;
706
756
  }
707
757
  /**
758
+ * Resolve a screenshot filename inside one of the buckets.
759
+ *
760
+ * This is the single sink every read and write funnels through, so it is also
761
+ * where an untrusted filename is stopped: plugin-supplied names (Storybook
762
+ * variant filenames come straight from story code) must never escape the
763
+ * bucket they belong to.
764
+ */
765
+ resolveBucketPath(directory, filename) {
766
+ return resolveInside(this[directory], filename, directory);
767
+ }
768
+ /**
708
769
  * Get the path to an actual screenshot file
709
770
  */
710
771
  getActualFilePath(filename) {
711
- return node_path.default.resolve(this.actual, filename);
772
+ return this.resolveBucketPath("actual", filename);
712
773
  }
713
774
  /**
714
775
  * Get the path to an expected screenshot file
715
776
  */
716
777
  getExpectedFilePath(filename) {
717
- return node_path.default.resolve(this.expected, filename);
778
+ return this.resolveBucketPath("expected", filename);
718
779
  }
719
780
  /**
720
781
  * Get the path to a diff screenshot file
721
782
  */
722
783
  getDiffFilePath(filename) {
723
- return node_path.default.resolve(this.diff, filename);
784
+ return this.resolveBucketPath("diff", filename);
724
785
  }
725
786
  /**
726
787
  * Write a file to the actual directory
@@ -785,9 +846,8 @@ var ScreenshotFileSystem = class {
785
846
  return node_path.default.resolve(this.actual, filePath);
786
847
  }
787
848
  getRelativePath(directory, filePath) {
788
- const relativePath = node_path.default.relative(this[directory], filePath);
789
- if (relativePath.startsWith("..")) throw new Error(`Cannot resolve relative path outside of ${directory} directory: ${filePath}`);
790
- return relativePath;
849
+ if (!isPathInside(this[directory], filePath)) throw new Error(`Cannot resolve relative path outside of ${directory} directory: ${filePath}`);
850
+ return node_path.default.relative(this[directory], filePath);
791
851
  }
792
852
  };
793
853
  //#endregion
@@ -1719,8 +1779,21 @@ var ScreenshotTool = class {
1719
1779
  passed: false
1720
1780
  };
1721
1781
  }
1782
+ /**
1783
+ * Resolve the filename a variant is written under.
1784
+ *
1785
+ * An explicit `variant.filename` wins, but it is sanitized first: for
1786
+ * `@cappa/plugin-storybook` it arrives from the browser as a story parameter,
1787
+ * so it is story-author input and could otherwise carry `../` or an absolute
1788
+ * path into the write sink. A name left empty by sanitization falls back to
1789
+ * the derived one.
1790
+ */
1722
1791
  getVariantFilename(filename, variant) {
1723
- if (variant.filename) return variant.filename;
1792
+ if (variant.filename) {
1793
+ const safeFilename = sanitizeScreenshotFilename(variant.filename);
1794
+ if (safeFilename !== variant.filename) this.logger.warn(`Variant filename "${variant.filename}" is not a valid screenshot path, using "${safeFilename || "the derived name"}" instead`);
1795
+ if (safeFilename) return safeFilename;
1796
+ }
1724
1797
  const parsed = node_path.default.parse(filename);
1725
1798
  const suffix = variant.id.trim().replace(/[^a-zA-Z0-9_-]+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "") || "variant";
1726
1799
  const ext = parsed.ext || ".png";
@@ -1881,7 +1954,7 @@ var ScreenshotTool = class {
1881
1954
  baseResult.diffImagePath = baseCaptureResult.diffImagePath;
1882
1955
  baseResult.skipped = baseCaptureResult.skipped;
1883
1956
  for (const variant of variants) {
1884
- const variantFilename = variant.filename || this.getVariantFilename(baseFilename, variant);
1957
+ const variantFilename = this.getVariantFilename(baseFilename, variant);
1885
1958
  const variantOptions = {
1886
1959
  ...baseOptions,
1887
1960
  ...variant.options
@@ -3100,6 +3173,7 @@ exports.mergeCaptureManifest = mergeCaptureManifest;
3100
3173
  exports.readCaptureManifest = readCaptureManifest;
3101
3174
  exports.readDiffMeta = readDiffMeta;
3102
3175
  exports.recordCaptureOrigins = recordCaptureOrigins;
3176
+ exports.sanitizeScreenshotFilename = sanitizeScreenshotFilename;
3103
3177
  exports.selectTasks = selectTasks;
3104
3178
  exports.toDiffMetaPath = toDiffMetaPath;
3105
3179
  exports.toManifestKey = toManifestKey;