@muhkoo/cli 0.12.0-alpha.0 → 0.13.1-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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/lib/mount.js +114 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muhkoo/cli",
3
- "version": "0.12.0-alpha.0",
3
+ "version": "0.13.1-alpha.0",
4
4
  "description": "Command-line interface for the Muhkoo platform — provision backends, deploy apps, and manage agents, functions, databases, and domains.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  "test": "vitest run"
40
40
  },
41
41
  "dependencies": {
42
- "@muhkoo/connect": "0.12.0-alpha.0",
42
+ "@muhkoo/connect": "0.13.1-alpha.0",
43
43
  "snarkjs": "^0.7.0"
44
44
  },
45
45
  "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
package/src/lib/mount.js CHANGED
@@ -22,9 +22,9 @@
22
22
  */
23
23
 
24
24
  import { createHash } from "node:crypto";
25
- import { mkdir, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
25
+ import { lstat, mkdir, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
26
26
  import { watch } from "node:fs";
27
- import { dirname, join, relative, sep } from "node:path";
27
+ import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
28
28
 
29
29
  import { planSync } from "./syncPlan.js";
30
30
  import { loadIgnores } from "./ignore.js";
@@ -138,16 +138,93 @@ async function scanLocal(dir, includeHidden = false, ignore = () => false) {
138
138
  return out;
139
139
  }
140
140
 
141
- /** Hash every file under the mounted VFS subtree. */
142
- async function scanRemote(vfs, root) {
141
+ /**
142
+ * Hash every file under the mounted VFS subtree — through the SAME filters as
143
+ * the local scan.
144
+ *
145
+ * These filters used to run in one direction only. They decided what left the
146
+ * machine and nothing decided what arrived, so a remote `.git/config` was
147
+ * hashed here, found no local counterpart, and was written straight into the
148
+ * developer's repository. `core.pager`, `alias.*` and `core.fsmonitor` are
149
+ * shell commands git runs itself, so that is remote code execution on the next
150
+ * ordinary `git status` — no execute bit required.
151
+ *
152
+ * Anything that can write to a user's VFS is therefore a party that can write
153
+ * files onto the machines they mount from. Treat what comes down as untrusted.
154
+ */
155
+ export async function scanRemote(vfs, root, includeHidden = false, ignore = () => false) {
143
156
  const out = {};
144
157
  if (!(await vfs.exists(root))) return out;
145
158
  for (const abs of await vfs.walk(root)) {
146
- out[abs.slice(root.length) || "/" + abs.split("/").pop()] = sha(await vfs.readFile(abs));
159
+ const key = abs.slice(root.length) || "/" + abs.split("/").pop();
160
+ const rel = key.replace(/^\//, "");
161
+ if (!rel) continue;
162
+ if (isRefused(rel)) {
163
+ warn(`refusing ${key} from your filesystem — that path can execute code on this machine`);
164
+ continue;
165
+ }
166
+ if (ignore(rel, false)) continue;
167
+ // Same dot-entry rule as `scanLocal`, applied to every segment: a remote
168
+ // `.vscode/tasks.json` is as dangerous as a remote `.vscode`.
169
+ if (!includeHidden && rel.split("/").some((part) => isHidden(part) && !KEEP_HIDDEN.has(part))) continue;
170
+ out[key] = sha(await vfs.readFile(abs));
147
171
  }
148
172
  return out;
149
173
  }
150
174
 
175
+ /**
176
+ * Paths no flag may bring down, ever.
177
+ *
178
+ * `--hidden` means "sync dot-files", not "let whatever can write my filesystem
179
+ * rewrite my git config". Each of these reaches code execution without needing
180
+ * an execute bit, which is what the 0644 the writer creates would otherwise
181
+ * deny. Matched on the first segment, or the whole path for the bare files.
182
+ */
183
+ const REFUSED_DIRS = new Set([".git", ".ssh", ".muhkoo", ".vscode", ".idea", ".config", "node_modules"]);
184
+ const REFUSED_FILES = new Set([".envrc", ".npmrc", ".netrc", ".profile", ".bashrc", ".zshrc", ".bash_profile"]);
185
+
186
+ // Exported for tests: these three ARE the security boundary, so they are worth
187
+ // asserting on directly rather than only through a full sync.
188
+ export function isRefused(rel) {
189
+ const parts = rel.split("/");
190
+ if (parts.some((part) => REFUSED_DIRS.has(part))) return true;
191
+ return parts.some((part) => REFUSED_FILES.has(part));
192
+ }
193
+
194
+ /**
195
+ * The local path for a synced path, or null when it escapes the mount.
196
+ *
197
+ * The SDK's `normalizePath` collapses `..` before a path ever leaves `walk()`,
198
+ * so traversal is not reachable that way today — but nothing here depended on
199
+ * that, the guarantee lives in another package, and the state file supplies a
200
+ * second source of keys that never passes through it. This makes the property
201
+ * local and checkable.
202
+ */
203
+ export function localPathFor(dir, path) {
204
+ const candidate = resolve(join(dir, path.replace(/^\//, "").split("/").join(sep)));
205
+ const rel = relative(dir, candidate);
206
+ if (!rel || rel === ".." || rel.startsWith(".." + sep) || isAbsolute(rel)) return null;
207
+ return candidate;
208
+ }
209
+
210
+ /**
211
+ * Is it safe to write here?
212
+ *
213
+ * `writeFile` opens with O_CREAT|O_TRUNC and FOLLOWS symlinks, so a link
214
+ * anywhere in the mounted tree redirects remote content to whatever it points
215
+ * at — the one way content genuinely escapes the mount directory, and it works
216
+ * on every platform. `scanLocal` already refuses to READ anything that is not a
217
+ * regular file (`entry.isFile()`); this makes the write side agree.
218
+ */
219
+ async function writable(localAbs) {
220
+ try {
221
+ const info = await lstat(localAbs);
222
+ return info.isFile();
223
+ } catch {
224
+ return true; // does not exist yet: creating it is fine
225
+ }
226
+ }
227
+
151
228
  /**
152
229
  * Reconcile once, and return the new last-synced view.
153
230
  *
@@ -155,20 +232,27 @@ async function scanRemote(vfs, root) {
155
232
  * record each, and two concurrent writes to the same record would have one
156
233
  * silently lose ([[VfsNamespace]] consistency note).
157
234
  */
158
- async function reconcile({ vfs, dir, root, state, opts = {} }) {
235
+ export async function reconcile({ vfs, dir, root, state, opts = {} }) {
159
236
  // Re-read every pass: editing `.vcsignore` should take effect on the next
160
237
  // sync, not require a restart.
161
238
  const { match } = await loadIgnores(dir, readFile);
162
239
  const [local, remote] = await Promise.all([
163
240
  scanLocal(dir, opts.hidden, match),
164
- scanRemote(vfs, root),
241
+ scanRemote(vfs, root, opts.hidden, match),
165
242
  ]);
166
243
  const plan = planSync({ last: state, local, remote });
167
244
  if (!plan.length) return state;
168
245
 
169
246
  const next = { ...state };
170
247
  for (const { path, action } of plan) {
171
- const localAbs = join(dir, path.slice(1).split("/").join(sep));
248
+ const localAbs = localPathFor(dir, path);
249
+ if (!localAbs) {
250
+ // Warn and skip rather than throw: one bad path must not abandon
251
+ // the rest of the sync.
252
+ warn(`refusing ${path} — it resolves outside ${dir}`);
253
+ delete next[path];
254
+ continue;
255
+ }
172
256
  const remoteAbs = root + path;
173
257
  try {
174
258
  switch (action) {
@@ -180,6 +264,10 @@ async function reconcile({ vfs, dir, root, state, opts = {} }) {
180
264
  break;
181
265
  }
182
266
  case "pull": {
267
+ if (!(await writable(localAbs))) {
268
+ warn(`skipping ${path} — ${localAbs} is a symlink or not a regular file`);
269
+ break;
270
+ }
183
271
  const bytes = await vfs.readFile(remoteAbs);
184
272
  await mkdir(dirname(localAbs), { recursive: true });
185
273
  await writeFile(localAbs, bytes);
@@ -210,6 +298,23 @@ async function reconcile({ vfs, dir, root, state, opts = {} }) {
210
298
  info(`✕ ${path} (removed here)`);
211
299
  break;
212
300
  case "delete-local":
301
+ // Gated like the remote direction. `--allow-delete` used to
302
+ // cover only deletions going UP, so anything able to remove
303
+ // a file from the filesystem removed it from the
304
+ // developer's disk with no flag and no prompt — the exact
305
+ // unrecoverable case the remote branch takes care over.
306
+ if (!opts.allowDelete) {
307
+ warn(
308
+ `${path} is gone from your filesystem but still here — not deleting it locally.\n` +
309
+ ` Pass --allow-delete to propagate deletions.`,
310
+ );
311
+ delete next[path];
312
+ break;
313
+ }
314
+ if (!(await writable(localAbs))) {
315
+ warn(`skipping ${path} — ${localAbs} is a symlink or not a regular file`);
316
+ break;
317
+ }
213
318
  await rm(localAbs, { force: true });
214
319
  delete next[path];
215
320
  info(`✕ ${path} (removed there)`);
@@ -257,7 +362,7 @@ export async function importOnce({ client, dir, root, opts = {} }) {
257
362
  const { match, source } = await loadIgnores(dir, readFile);
258
363
  const [local, remote] = await Promise.all([
259
364
  scanLocal(dir, opts.hidden, match),
260
- scanRemote(vfs, root),
365
+ scanRemote(vfs, root, opts.hidden, match),
261
366
  ]);
262
367
  const state = await loadState(dir, root);
263
368
  const plan = planSync({ last: state, local, remote });