@summerscar/dsh-mnemon-sync 0.1.8 → 0.1.9

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.
Files changed (2) hide show
  1. package/index.js +44 -13
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -487,14 +487,21 @@ export function apply(ctx, config = {}) {
487
487
  }))
488
488
  const restorable = requested.filter((component) => present.includes(component))
489
489
 
490
- // Plan first, so a dry run and a real run describe the same work.
490
+ // Plan first, so a dry run and a real run describe the same work. Entries
491
+ // whose path does not resolve into a component directory (a foreign file
492
+ // smuggled into the archive) are rejected outright rather than skipped:
493
+ // a listing that silently drops entries would hide what a restore does.
491
494
  const plan = []
492
495
  for (const component of restorable) {
493
496
  for (const [relative, content] of files.entries()) {
494
497
  if (relative === 'manifest.json') continue
495
498
  const director = componentDirectoryOf(relative)
496
499
  if (director !== component) continue
497
- plan.push({ component, relative, destination: destinationFor(home, relative), bytes: content.length })
500
+ const destination = destinationFor(home, relative)
501
+ if (destination === undefined) {
502
+ throw new Error(`archive entry cannot be restored safely: ${relative}`)
503
+ }
504
+ plan.push({ component, relative, destination, bytes: content.length })
498
505
  }
499
506
  }
500
507
 
@@ -594,20 +601,44 @@ function componentDirectoryOf(relative) {
594
601
  }
595
602
 
596
603
  /**
597
- * Where one archive path lands in the Mnemon home.
604
+ * Archive payload directories that a restore may write, mapped to the live
605
+ * directories they land in. `state/` is deliberately absent: it holds this
606
+ * plugin's credentials and dsh-mnemon's machine identity, and no backup
607
+ * component backs it up.
608
+ */
609
+ const RESTORE_DIRECTORIES = {
610
+ data: 'data',
611
+ documents: 'documents',
612
+ runtime: 'runtime',
613
+ }
614
+
615
+ /**
616
+ * Where one archive path lands in the Mnemon home, or `undefined` when the
617
+ * path is not part of a restorable component.
598
618
  *
599
- * `payload/` is dropped and the component directories are the same names the
600
- * store itself uses, so a restored tree is byte-identical in layout to a live
601
- * one. The path is resolved and checked so a malformed archive cannot walk out
602
- * of the home directory.
619
+ * The check is deliberately stricter than "stays inside the home": a restore
620
+ * must only ever touch the three component directories (`data/`, `documents/`,
621
+ * `runtime/`). Everything else in the home — `state/` with this plugin's own
622
+ * credentials and dsh-mnemon's machine identity, `skills/`, the archive
623
+ * workroot — must be unreachable from an archive, because the archive is
624
+ * untrusted data. A zip entry like `payload/data/../state/mnemon-sync.json`
625
+ * resolves inside the home yet would overwrite the stored credentials, so the
626
+ * first path segment after `payload/` is matched exactly against the three
627
+ * component names and no `..` or nested separator survives.
603
628
  */
604
629
  function destinationFor(home, relative) {
605
- const stripped = relative.replace(/^payload\//u, '')
606
- const destination = resolve(home, stripped)
607
- const root = resolve(home)
608
- if (destination !== root && !destination.startsWith(`${root}/`)) {
609
- throw new Error(`archive entry escapes the Mnemon home: ${relative}`)
610
- }
630
+ if (!relative.startsWith('payload/')) return undefined
631
+ const stripped = relative.slice('payload/'.length)
632
+ const separator = stripped.indexOf('/')
633
+ if (separator <= 0) return undefined
634
+ const topDirectory = stripped.slice(0, separator)
635
+ const componentDirectory = RESTORE_DIRECTORIES[topDirectory]
636
+ if (componentDirectory === undefined) return undefined
637
+ const rest = stripped.slice(separator + 1)
638
+ if (rest === '' || rest.includes('\0') || rest.split('/').includes('..')) return undefined
639
+ const destination = resolve(home, componentDirectory, ...rest.split('/'))
640
+ const root = resolve(home, componentDirectory)
641
+ if (!destination.startsWith(`${root}/`)) return undefined
611
642
  return destination
612
643
  }
613
644
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@summerscar/dsh-mnemon-sync",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Back up and restore dsh-mnemon memory components (memory spaces, project documents, runtime memory) to S3-compatible storage or WebDAV.",
5
5
  "type": "module",
6
6
  "main": "index.js",