pmsec 0.10.0 → 0.11.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 (3) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +2 -1
  3. package/src/cli.mjs +87 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Hikaru Egashira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmsec",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -25,6 +25,7 @@
25
25
  "files": [
26
26
  "bin",
27
27
  "src",
28
+ "LICENSE",
28
29
  "README.md"
29
30
  ],
30
31
  "scripts": {
package/src/cli.mjs CHANGED
@@ -1,5 +1,6 @@
1
- import { homedir } from "node:os";
2
- import { readFileSync } from "node:fs";
1
+ import { homedir, userInfo } from "node:os";
2
+ import { readFileSync, accessSync, constants as fsConstants, statSync } from "node:fs";
3
+ import { dirname } from "node:path";
3
4
  import { shellQuote } from "./util/io.mjs";
4
5
  import * as npm from "./tools/npm.mjs";
5
6
  import * as pnpm from "./tools/pnpm.mjs";
@@ -27,6 +28,7 @@ attestation re-verification, ...). No knobs.
27
28
  Options:
28
29
  --check Verify the bundle is in place (exit 1 if anything missing)
29
30
  --disable Remove the hardening bundle from selected tools
31
+ --doctor Diagnose effective paths/owner/uid (read-only; for MDM debugging)
30
32
  --tool TOOL[,TOOL] Restrict to specific tools (npm,pnpm,yarn,bun,cargo,mise,uv)
31
33
  --days N Override cooldown days (default 1)
32
34
  --force Overwrite stricter existing cooldowns (otherwise enable is monotonic)
@@ -52,7 +54,7 @@ function parse(argv) {
52
54
  const opts = { mode: "enable", json: false, only: null, days: BUNDLE_DAYS, force: false, help: false, version: false };
53
55
  let modeSet = false;
54
56
  const setMode = (m) => {
55
- if (modeSet && opts.mode !== m) throw new Error(`--check and --disable are mutually exclusive`);
57
+ if (modeSet && opts.mode !== m) throw new Error(`--check, --disable, and --doctor are mutually exclusive`);
56
58
  opts.mode = m;
57
59
  modeSet = true;
58
60
  };
@@ -65,6 +67,7 @@ function parse(argv) {
65
67
  else if (a === "--force") opts.force = true;
66
68
  else if (a === "--check") setMode("check");
67
69
  else if (a === "--disable") setMode("disable");
70
+ else if (a === "--doctor") setMode("doctor");
68
71
  else if (a === "--tool") opts.only = argv[++i].split(",");
69
72
  else if (a.startsWith("--tool=")) opts.only = a.slice(7).split(",");
70
73
  else if (a === "--days") opts.days = parseDays(argv[++i]);
@@ -130,6 +133,86 @@ async function runCheck(targets, { json, days }, ctx, out, err) {
130
133
  return ok ? 0 : 1;
131
134
  }
132
135
 
136
+ // `pmsec doctor` runs read-only and reports the same path resolution that
137
+ // enable/check/disable would do, plus identity (uid/euid) and parent-dir
138
+ // writability — the smallest set of facts an MDM operator needs to diagnose
139
+ // "pmsec ran but wrote to nowhere" (root's $HOME) or "wrote a file no one can
140
+ // read" (chown failed under SIP/SELinux). Never mutates the filesystem.
141
+ function probePath(p, uid) {
142
+ const parent = dirname(p);
143
+ let exists = false, writable = false, parentExists = false, owner = null;
144
+ try { accessSync(p, fsConstants.F_OK); exists = true; } catch {}
145
+ if (exists) {
146
+ try { accessSync(p, fsConstants.W_OK); writable = true; } catch {}
147
+ try {
148
+ const st = statSync(p);
149
+ // Only POSIX uids are meaningful; on win32 st.uid is 0 and ignored downstream.
150
+ owner = uid === null ? null : st.uid;
151
+ } catch {}
152
+ }
153
+ try { accessSync(parent, fsConstants.F_OK); parentExists = true; } catch {}
154
+ // Walk up to the deepest existing ancestor — pmsec runs mkdir -p, so what
155
+ // matters is whether that ancestor is writable, not the literal parent.
156
+ let probe = parent, ancestor = null;
157
+ while (probe && probe !== dirname(probe)) {
158
+ try { accessSync(probe, fsConstants.F_OK); ancestor = probe; break; } catch {}
159
+ probe = dirname(probe);
160
+ }
161
+ let parentWritable = false;
162
+ if (ancestor) {
163
+ try { accessSync(ancestor, fsConstants.W_OK); parentWritable = true; } catch {}
164
+ }
165
+ return { path: p, parent, exists, writable, parentExists, parentWritable, owner };
166
+ }
167
+
168
+ function runDoctor(targets, json, ctx, out) {
169
+ const isPosix = ctx.platform !== "win32" && typeof process.getuid === "function";
170
+ const uid = isPosix ? process.getuid() : null;
171
+ const euid = isPosix && typeof process.geteuid === "function" ? process.geteuid() : uid;
172
+ let username = null;
173
+ try { username = userInfo().username; } catch {}
174
+ const tools = targets.map(t => {
175
+ const p = t.path(ctx);
176
+ const probe = probePath(p, uid);
177
+ return { tool: t.name, key: t.key, ...probe };
178
+ });
179
+ // doctor.ok is intentionally narrow: the parent directory must be writable
180
+ // by the running uid. Everything else is informational.
181
+ const okFlag = tools.every(t => t.parentWritable);
182
+ const report = {
183
+ doctor: true,
184
+ version: VERSION,
185
+ platform: ctx.platform,
186
+ uid, euid, username,
187
+ home: ctx.home,
188
+ pmsecHome: ctx.env.PMSEC_HOME ?? null,
189
+ pmsecHomeSource: ctx.env.PMSEC_HOME ? "PMSEC_HOME" : "HOME",
190
+ tools,
191
+ ok: okFlag,
192
+ };
193
+ if (json) {
194
+ out.write(JSON.stringify(report, null, 2) + "\n");
195
+ return okFlag ? 0 : 1;
196
+ }
197
+ out.write(`pmsec ${VERSION} doctor\n`);
198
+ out.write(` platform : ${ctx.platform}\n`);
199
+ if (isPosix) out.write(` uid/euid : ${uid}/${euid}${username ? ` (${username})` : ""}\n`);
200
+ out.write(` HOME : ${ctx.home}\n`);
201
+ out.write(` PMSEC_HOME : ${ctx.env.PMSEC_HOME ?? "(unset — using HOME)"}\n\n`);
202
+ for (const t of tools) {
203
+ const flag = t.parentWritable ? "ok " : "BLOCK ";
204
+ const ownerSuffix = t.exists && t.owner !== null ? ` (uid=${t.owner})` : "";
205
+ out.write(`${flag} ${t.tool.padEnd(4)} ${t.path}${ownerSuffix}\n`);
206
+ if (!t.parentWritable) {
207
+ // Only surface noise when there's a real problem. "parent missing but
208
+ // mkdir -p can fix it" is the common case and isn't worth a line.
209
+ out.write(` no writable ancestor for ${t.parent}\n`);
210
+ }
211
+ }
212
+ if (!okFlag) out.write(`\npmsec doctor: at least one parent directory is not writable as uid ${uid ?? "(non-POSIX)"}.\n`);
213
+ return okFlag ? 0 : 1;
214
+ }
215
+
133
216
  function explainFsError(e, tool) {
134
217
  if (e?.code === "EACCES" || e?.code === "EPERM") {
135
218
  const p = e.path ?? "";
@@ -216,6 +299,7 @@ export async function run(argv, {
216
299
  try {
217
300
  if (opts.mode === "check") return await runCheck(targets, opts, ctx, out, err);
218
301
  if (opts.mode === "disable") return await runDisable(targets, opts.json, ctx, out, err);
302
+ if (opts.mode === "doctor") return runDoctor(targets, opts.json, ctx, out);
219
303
  return await runEnable(targets, opts.json, opts.days, opts.force, ctx, out, err);
220
304
  } catch (e) {
221
305
  err.write(`pmsec: ${e.message}\n`);