@muhkoo/connect 0.12.2-alpha.0 → 0.13.0-alpha.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.
@@ -5327,7 +5327,7 @@ function base64UrlDecode(s) {
5327
5327
  * expect (and isn't serving a stale cached bundle). Keep this in sync with
5328
5328
  * `package.json` `version`.
5329
5329
  */
5330
- const VERSION = "0.12.2-alpha.0";
5330
+ const VERSION = "0.13.0-alpha.0";
5331
5331
 
5332
5332
  /**
5333
5333
  * `HttpClient` — the one place the unified {@link Client} talks to the
@@ -12030,13 +12030,40 @@ function isUnder(path, dir) {
12030
12030
  * directory on the next lookup — a file literally named `..` could never be
12031
12031
  * addressed again.
12032
12032
  */
12033
+ /** The longest a single entry name may be. Matches the common filesystem limit. */
12034
+ const MAX_NAME_BYTES = 255;
12035
+ /**
12036
+ * Is this a name we are willing to store, and to hand back as a path segment?
12037
+ *
12038
+ * This is the READ-side check as much as the write-side one, and the read side
12039
+ * is the one that matters: a directory record is sealed with a key that any
12040
+ * holder of the master seed can derive, so its contents are not necessarily
12041
+ * something this SDK wrote. A name arriving from a record is input.
12042
+ *
12043
+ * What each rule is for:
12044
+ * - `/` and backslash would become path structure. Backslash is a separator on
12045
+ * Windows, which makes it traversal there and an odd filename elsewhere.
12046
+ * - `.` and `..` navigate rather than name.
12047
+ * - NUL terminates a path in every syscall that takes one.
12048
+ * - Other control characters and bidi overrides let a name print as something
12049
+ * other than what it is - the classic way a file passes review.
12050
+ * - A length cap, because nothing else imposes one.
12051
+ */
12052
+ function isSafeName(name) {
12053
+ if (!name || name === "." || name === "..")
12054
+ return false;
12055
+ if (name.includes("/") || name.includes(String.fromCharCode(92)))
12056
+ return false;
12057
+ const CONTROL_OR_BIDI = new RegExp("[\\u0000-\\u001f\\u007f-\\u009f\\u202a-\\u202e\\u2066-\\u2069]");
12058
+ if (CONTROL_OR_BIDI.test(name))
12059
+ return false;
12060
+ return new TextEncoder().encode(name).length <= MAX_NAME_BYTES;
12061
+ }
12033
12062
  function assertValidName(name) {
12034
12063
  if (!name)
12035
12064
  throw new Error("VFS: name cannot be empty");
12036
- if (name === "." || name === "..")
12037
- throw new Error(`VFS: "${name}" is not a usable name`);
12038
- if (name.includes("/"))
12039
- throw new Error(`VFS: name cannot contain "/" (got ${JSON.stringify(name)})`);
12065
+ if (!isSafeName(name))
12066
+ throw new Error(`VFS: ${JSON.stringify(name)} is not a usable name`);
12040
12067
  }
12041
12068
  /**
12042
12069
  * MIME type for a filename.
@@ -12148,6 +12175,14 @@ function escapeLiteral(s) {
12148
12175
  * cost storage until a future sweep, which is the trade this shape makes in
12149
12176
  * exchange for single-record writes.
12150
12177
  */
12178
+ /**
12179
+ * How deep a tree may be before we stop walking it.
12180
+ *
12181
+ * Well past anything a real project needs, and low enough that a malformed or
12182
+ * hostile record cannot spend the caller's memory before the cycle guard sees a
12183
+ * repeat.
12184
+ */
12185
+ const MAX_DEPTH = 64;
12151
12186
  const DEFAULT_HISTORY_LIMIT = 20;
12152
12187
  class VfsNamespace {
12153
12188
  deps;
@@ -12276,9 +12311,29 @@ class VfsNamespace {
12276
12311
  const dir = await this.openDir(path);
12277
12312
  const base = this.resolve(path);
12278
12313
  return Object.entries(dir.node.entries)
12314
+ // Validate the name HERE, where it is read.
12315
+ //
12316
+ // A directory record is sealed under a key derivable from the master
12317
+ // seed, so its contents are not necessarily something this SDK
12318
+ // wrote, and the write-side check never sees them. An unusable name
12319
+ // is dropped rather than thrown on: one bad entry must not make the
12320
+ // whole directory unlistable, and callers walk this output straight
12321
+ // into filesystem paths.
12322
+ .filter(([name]) => {
12323
+ if (isSafeName(name))
12324
+ return true;
12325
+ this.warn(`ignoring entry with an unusable name in ${base}`);
12326
+ return false;
12327
+ })
12279
12328
  .map(([name, entry]) => toStat(join(base, name), name, entry))
12280
12329
  .sort(byDirsThenName);
12281
12330
  }
12331
+ warn(message) {
12332
+ // Kept quiet by default in a browser console that the user cannot act
12333
+ // on, but never silent: a dropped entry is a real difference between
12334
+ // what is stored and what is shown.
12335
+ console.warn(`[muhkoo/vfs] ${message}`);
12336
+ }
12282
12337
  /**
12283
12338
  * Every file path under a prefix, depth-first.
12284
12339
  *
@@ -12287,15 +12342,32 @@ class VfsNamespace {
12287
12342
  */
12288
12343
  async walk(path = ".") {
12289
12344
  const out = [];
12290
- const visit = async (dirPath) => {
12345
+ // Guard against a tree that is not one.
12346
+ //
12347
+ // `visit` recurses on a PATH, re-resolved from the root each time, so a
12348
+ // record whose entry points back at an ancestor - or, before names were
12349
+ // validated, an entry simply named ".." - recurses forever and grows
12350
+ // `out` without bound. Nothing in the format prevents that, and `mount`
12351
+ // calls this on every sync pass.
12352
+ const seen = new Set();
12353
+ const visit = async (dirPath, depth) => {
12354
+ if (depth > MAX_DEPTH) {
12355
+ this.warn(`stopping at ${dirPath}: deeper than ${MAX_DEPTH} levels`);
12356
+ return;
12357
+ }
12358
+ if (seen.has(dirPath)) {
12359
+ this.warn(`stopping at ${dirPath}: already visited (the tree contains a cycle)`);
12360
+ return;
12361
+ }
12362
+ seen.add(dirPath);
12291
12363
  for (const entry of await this.list(dirPath)) {
12292
12364
  if (entry.kind === "dir")
12293
- await visit(entry.path);
12365
+ await visit(entry.path, depth + 1);
12294
12366
  else
12295
12367
  out.push(entry.path);
12296
12368
  }
12297
12369
  };
12298
- await visit(this.resolve(path));
12370
+ await visit(this.resolve(path), 0);
12299
12371
  return out;
12300
12372
  }
12301
12373
  async readFile(path) {
@@ -12568,6 +12640,12 @@ class VfsNamespace {
12568
12640
  const root = await this.root();
12569
12641
  const reachable = new Set();
12570
12642
  const visit = async (dir) => {
12643
+ // `reachable` was already the visited set - it just was not being
12644
+ // TESTED before recursing, so a cycle looped forever. Here the
12645
+ // consequence would have been worse than a hang: sweep decides what
12646
+ // to delete, and it never got as far as deciding.
12647
+ if (reachable.has(dirKey(dir.id)))
12648
+ return;
12571
12649
  reachable.add(dirKey(dir.id));
12572
12650
  for (const entry of Object.values(dir.node.entries)) {
12573
12651
  if (entry.kind === "dir") {
@@ -12725,10 +12803,16 @@ class VfsNamespace {
12725
12803
  });
12726
12804
  }
12727
12805
  /** Recursively remove a subtree's records. Best-effort: unreachable ≠ fatal. */
12728
- async purge(dir) {
12806
+ async purge(dir, seen = new Set()) {
12807
+ // A record that references an ancestor would otherwise recurse forever:
12808
+ // the id is only removed from the cache AFTER the loop, so re-entering
12809
+ // it hits the same cached record every time.
12810
+ if (seen.has(dir.id))
12811
+ return;
12812
+ seen.add(dir.id);
12729
12813
  for (const entry of Object.values(dir.node.entries)) {
12730
12814
  if (entry.kind === "dir") {
12731
- await this.purge(await this.openDirById(entry.id, fromBase64(entry.key)));
12815
+ await this.purge(await this.openDirById(entry.id, fromBase64(entry.key)), seen);
12732
12816
  }
12733
12817
  else {
12734
12818
  await this.releaseFile(dir, entry);
@@ -12742,18 +12826,32 @@ class VfsNamespace {
12742
12826
  * directory gets a fresh id and key, so the copy is independent of the
12743
12827
  * original — writing into one must never appear in the other.
12744
12828
  */
12745
- async cloneEntry(entry) {
12829
+ async cloneEntry(entry, seen = new Set()) {
12746
12830
  if (entry.kind === "file") {
12747
12831
  // Same bytes, second owner: take a reference so deleting either one
12748
12832
  // leaves the other readable.
12749
12833
  await this.deps.content.retain(entry.manifest);
12750
12834
  return { kind: "file", id: newId(), manifest: entry.manifest, size: entry.size, mtime: Date.now(), versions: 0 };
12751
12835
  }
12836
+ // The most expensive walker to leave unguarded: it mints a NEW id, key
12837
+ // and stored record at every level, so a cyclic tree does not merely
12838
+ // hang - it writes records to the store, and to the user's bill, in an
12839
+ // unbounded loop.
12840
+ if (seen.has(entry.id) || seen.size > MAX_DEPTH) {
12841
+ throw new VfsConflictError("VFS: refusing to copy a directory that contains a cycle");
12842
+ }
12843
+ seen.add(entry.id);
12752
12844
  const source = await this.openDirById(entry.id, fromBase64(entry.key));
12753
12845
  const clone = { kind: "dir", id: newId(), key: toBase64(newDirKey()), mtime: Date.now() };
12754
12846
  const node = { v: 1, entries: {}, mtime: Date.now() };
12755
12847
  for (const [name, child] of Object.entries(source.node.entries)) {
12756
- node.entries[name] = await this.cloneEntry(child);
12848
+ // Names are validated on read everywhere else; a copy must not be
12849
+ // the way a bad one is carried into a fresh record.
12850
+ if (!isSafeName(name)) {
12851
+ this.warn(`skipping entry with an unusable name while copying`);
12852
+ continue;
12853
+ }
12854
+ node.entries[name] = await this.cloneEntry(child, seen);
12757
12855
  }
12758
12856
  const open = { id: clone.id, key: fromBase64(clone.key), node };
12759
12857
  await this.deps.store.put(dirKey(open.id), await seal(open.key, node));