@workover/cli 0.14.1 → 0.14.4

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/bin/workover.mjs CHANGED
@@ -13,7 +13,6 @@ import {
13
13
  writeFileSync,
14
14
  mkdirSync,
15
15
  existsSync,
16
- chmodSync,
17
16
  rmSync,
18
17
  } from 'fs';
19
18
  import { homedir } from 'os';
@@ -23,6 +22,7 @@ import { fileURLToPath } from 'url';
23
22
  import {
24
23
  normalizeApi,
25
24
  browserCommand,
25
+ writePrivateFile,
26
26
  pollDecision,
27
27
  summariseEvaluations,
28
28
  parseArgs,
@@ -56,13 +56,9 @@ const API = normalizeApi(process.env.WORKOVER_API || 'https://api.workover.io');
56
56
  // preview) can be used without replacing the one in ~/.workover. The MCP server reads the same
57
57
  // variable, so both see the same sign-in.
58
58
  const CONFIG_FILE = process.env.WORKOVER_CONFIG || join(homedir(), '.workover', 'config.json');
59
- const CONFIG_DIR = dirname(CONFIG_FILE);
60
59
 
61
- const saveConfig = (cfg) => {
62
- mkdirSync(CONFIG_DIR, { recursive: true });
63
- writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2));
64
- chmodSync(CONFIG_FILE, 0o600); // owner read/write only — it holds a bearer token
65
- };
60
+ // Owner read/write only, from the first byte — it holds a bearer token (see writePrivateFile).
61
+ const saveConfig = (cfg) => writePrivateFile(CONFIG_FILE, JSON.stringify(cfg, null, 2));
66
62
  const loadConfig = () => {
67
63
  try {
68
64
  return JSON.parse(readFileSync(CONFIG_FILE, 'utf8'));
@@ -72,7 +68,9 @@ const loadConfig = () => {
72
68
  };
73
69
 
74
70
  const openBrowser = (url) => {
75
- const { cmd, args } = browserCommand(process.platform, url);
71
+ const command = browserCommand(process.platform, url);
72
+ if (!command) return; // not an http(s) URL — it is printed anyway
73
+ const { cmd, args } = command;
76
74
  try {
77
75
  spawn(cmd, args, { stdio: 'ignore', detached: true }).unref();
78
76
  } catch {
@@ -235,7 +233,6 @@ async function login() {
235
233
 
236
234
  async function logout() {
237
235
  const cfg = loadConfig();
238
- let revoked = true;
239
236
  if (cfg?.token) {
240
237
  /*
241
238
  * Revoke on the api this sign-in BELONGS to, which login recorded as `cfg.api`.
@@ -247,7 +244,7 @@ async function logout() {
247
244
  * Possessing the token authorizes revoking it; best-effort, but no longer silent when it fails.
248
245
  */
249
246
  const base = cfg.api ? normalizeApi(cfg.api) : API;
250
- revoked = await fetch(`${base}/cli-auth/logout`, {
247
+ const revoked = await fetch(`${base}/cli-auth/logout`, {
251
248
  method: 'POST',
252
249
  headers: { 'content-type': 'application/json' },
253
250
  body: JSON.stringify({ token: cfg.token }),
package/lib.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  // Pure helpers for the Workover CLI, split out so they can be unit-tested without importing
2
2
  // bin/workover.mjs (which runs a command + calls process.exit on load).
3
+ import { mkdirSync, writeFileSync, renameSync, chmodSync, rmSync } from 'fs';
4
+ import { dirname, join, basename } from 'path';
3
5
 
4
6
  /**
5
7
  * Strip trailing slashes from the API base URL (so `${API}${path}` never doubles up).
@@ -20,11 +22,52 @@ export const normalizeApi = (raw) => {
20
22
  return s.slice(0, end);
21
23
  };
22
24
 
23
- /** Map a platform + URL to the browser-open command used to approve the device login. */
25
+ /**
26
+ * Map a platform + URL to the browser-open command used to approve the device login.
27
+ *
28
+ * Windows does NOT go through `cmd /c start`: cmd re-parses its command line, and Node only quotes
29
+ * arguments that contain spaces, so a URL holding `&` (every query string with two parameters) was
30
+ * split into a second command. `rundll32 url.dll,FileProtocolHandler` takes the URL as a plain
31
+ * argument with no shell in between. Only http(s) URLs are opened at all; anything else returns
32
+ * null and the caller just prints the URL.
33
+ */
24
34
  export const browserCommand = (platform, url) => {
25
- if (platform === 'darwin') return { cmd: 'open', args: [url] };
26
- if (platform === 'win32') return { cmd: 'cmd', args: ['/c', 'start', '', url] };
27
- return { cmd: 'xdg-open', args: [url] };
35
+ let parsed;
36
+ try {
37
+ parsed = new URL(url);
38
+ } catch {
39
+ return null;
40
+ }
41
+ if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') return null;
42
+ const href = parsed.href;
43
+ if (platform === 'darwin') return { cmd: 'open', args: [href] };
44
+ if (platform === 'win32') return { cmd: 'rundll32', args: ['url.dll,FileProtocolHandler', href] };
45
+ return { cmd: 'xdg-open', args: [href] };
46
+ };
47
+
48
+ /**
49
+ * Write a file that holds a secret (the bearer token) so it is never readable by anyone else, not
50
+ * even for the moment between write and chmod: the directory is created 0700, the contents go to a
51
+ * temp file created 0600 in the same directory, and a rename swaps it in atomically. The chmod after
52
+ * covers a file that already existed with looser bits from an older CLI (rename keeps the temp
53
+ * file's mode, but an umask-free platform may still widen it).
54
+ */
55
+ export const writePrivateFile = (file, contents) => {
56
+ const dir = dirname(file);
57
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
58
+ const tmp = join(dir, `.${basename(file)}.${process.pid}.${Date.now()}.tmp`);
59
+ try {
60
+ writeFileSync(tmp, contents, { mode: 0o600, flag: 'wx' });
61
+ renameSync(tmp, file);
62
+ } catch (err) {
63
+ rmSync(tmp, { force: true });
64
+ throw err;
65
+ }
66
+ try {
67
+ chmodSync(file, 0o600);
68
+ } catch {
69
+ /* Windows: POSIX modes are advisory */
70
+ }
28
71
  };
29
72
 
30
73
  /**
@@ -275,6 +318,17 @@ export const threadComments = (comments, { includeResolved = false } = {}) => {
275
318
  const UUID_RE = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i;
276
319
  const FULL_UUID_RE = new RegExp(`^${UUID_RE.source}$`, 'i');
277
320
 
321
+ /**
322
+ * `value` without any run of `chars` at its end. A loop rather than `/[…]+$/`: a regex anchored only
323
+ * at the end is retried from every position, which is quadratic on a long run of those characters
324
+ * that is not at the end (code scanning, js/polynomial-redos).
325
+ */
326
+ const stripTrailing = (value, chars) => {
327
+ let end = value.length;
328
+ while (end > 0 && chars.includes(value[end - 1])) end -= 1;
329
+ return value.slice(0, end);
330
+ };
331
+
278
332
  /**
279
333
  * Which document somebody meant, from whatever they pasted.
280
334
  *
@@ -294,10 +348,12 @@ const FULL_UUID_RE = new RegExp(`^${UUID_RE.source}$`, 'i');
294
348
  */
295
349
  export const docRef = (value) => {
296
350
  // Trim the wrapping that pasted links arrive in: <angle brackets>, quotes, a trailing full stop.
297
- const s = String(value ?? '')
298
- .trim()
299
- .replace(/^[<"'([]+/, '')
300
- .replace(/[>"')\].,;]+$/, '');
351
+ const s = stripTrailing(
352
+ String(value ?? '')
353
+ .trim()
354
+ .replace(/^[<"'([]+/, ''),
355
+ '>"\')].,;',
356
+ );
301
357
  if (!s) return null;
302
358
  const id = rowId(s);
303
359
  if (id !== null) return { id };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workover/cli",
3
- "version": "0.14.1",
3
+ "version": "0.14.4",
4
4
  "description": "Workover command line — sign in and cowork on your documents from the terminal.",
5
5
  "license": "UNLICENSED",
6
6
  "author": "ScaleMath",