@shieldfive/cli 0.3.1 → 0.5.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.
package/src/cli.mjs CHANGED
@@ -15,7 +15,7 @@
15
15
  // signed in as another. See docs/agent-design.md.
16
16
 
17
17
  import { webcrypto } from 'node:crypto'
18
- import { readFile, readdir, stat } from 'node:fs/promises'
18
+ import { lstat, readFile, readdir } from 'node:fs/promises'
19
19
  import { join, resolve } from 'node:path'
20
20
 
21
21
  import { promptHidden, promptLine } from './prompt.mjs'
@@ -31,6 +31,8 @@ function usage() {
31
31
  ' logout [--everywhere] revoke the session and stop the agent',
32
32
  ' --everywhere also signs out your browser and phone',
33
33
  ' push <folder> encrypt and upload every file in <folder>',
34
+ ' [--include-dotfiles] also upload names starting with "."',
35
+ ' (symlinks are never followed)',
34
36
  ' sync <folder> [--watch] upload new and changed files',
35
37
  ' [--interval=<seconds>] poll interval for --watch (default 5)',
36
38
  ' verify <file>... report whether each file is safely in your vault',
@@ -38,6 +40,10 @@ function usage() {
38
40
  ' inspect <folder> count a migration source recursively; upload nothing',
39
41
  ' --help',
40
42
  '',
43
+ 'login, push and sync accept --allow-legacy-login: sign in even though the',
44
+ 'server asks for the raw password (auth version 1) for an account this',
45
+ 'computer has signed in to with a login secret. Refused by default.',
46
+ '',
41
47
  'After sf login, push / sync / verify go through the agent and need nothing',
42
48
  'in the environment. Without an agent, or when SF_EMAIL and SF_PASSWORD are',
43
49
  'set, push and sync sign in from those [SF_VAULT_PASSWORD if your vault',
@@ -48,10 +54,15 @@ function usage() {
48
54
  )
49
55
  }
50
56
 
51
- async function* filesIn(folder) {
57
+ // Regular files directly in `folder`. lstat, not stat: a symlink is skipped
58
+ // rather than followed, so `sf push ~/project` cannot upload ~/.ssh/id_ed25519
59
+ // through a link that points out of the folder. Dotfiles (.env, .git, .DS_Store,
60
+ // sync's own manifest) are skipped unless includeDotfiles is set, matching sync.
61
+ async function* filesIn(folder, { includeDotfiles = false } = {}) {
52
62
  for (const name of await readdir(folder)) {
63
+ if (!includeDotfiles && name.startsWith('.')) continue
53
64
  const path = join(folder, name)
54
- const info = await stat(path)
65
+ const info = await lstat(path)
55
66
  if (info.isFile()) yield { name, path, size: info.size }
56
67
  }
57
68
  }
@@ -99,7 +110,7 @@ function envCredentialsSet() {
99
110
  return Boolean(process.env.SF_EMAIL && process.env.SF_PASSWORD)
100
111
  }
101
112
 
102
- function readLiveConfig() {
113
+ function readLiveConfig(flags = {}) {
103
114
  const e = process.env
104
115
  const email = e.SF_EMAIL
105
116
  const password = e.SF_PASSWORD
@@ -119,6 +130,7 @@ function readLiveConfig() {
119
130
  email,
120
131
  password,
121
132
  vaultPassword: e.SF_VAULT_PASSWORD || password,
133
+ allowLegacyLogin: flags['allow-legacy-login'] === true,
122
134
  }
123
135
  }
124
136
 
@@ -141,6 +153,7 @@ async function readTotpCode() {
141
153
  // lazily so `sf encrypt` runs without @supabase/supabase-js.
142
154
  async function authAndUnlock(cfg) {
143
155
  const { signIn } = await import('./auth.mjs')
156
+ const { fileAuthPin } = await import('./authPin.mjs')
144
157
  const { fetchAndUnlockVault } = await import('./vault.mjs')
145
158
 
146
159
  process.stderr.write('signing in…\n')
@@ -151,6 +164,8 @@ async function authAndUnlock(cfg) {
151
164
  email: cfg.email,
152
165
  password: cfg.password,
153
166
  getTotpCode: readTotpCode,
167
+ authPin: fileAuthPin(),
168
+ allowLegacyLogin: cfg.allowLegacyLogin === true,
154
169
  })
155
170
 
156
171
  process.stderr.write('unlocking vault…\n')
@@ -298,10 +313,15 @@ async function cmdLogin(flags) {
298
313
  (await promptHidden('Vault password (press Enter if it is the same): ')) || password
299
314
 
300
315
  const { signIn } = await import('./auth.mjs')
316
+ const { fileAuthPin } = await import('./authPin.mjs')
301
317
  const { fetchAndUnlockVault } = await import('./vault.mjs')
302
318
 
303
319
  process.stderr.write('signing in…\n')
304
- const session = await signIn({ ...cfg, email, password, getTotpCode: readTotpCode })
320
+ const session = await signIn({
321
+ ...cfg, email, password, getTotpCode: readTotpCode,
322
+ authPin: fileAuthPin(),
323
+ allowLegacyLogin: flags['allow-legacy-login'] === true,
324
+ })
305
325
 
306
326
  process.stderr.write('unlocking vault…\n')
307
327
  const rootKey = await fetchAndUnlockVault({
@@ -378,14 +398,33 @@ async function cmdLogout(flags) {
378
398
  }
379
399
 
380
400
  // login -> unlock -> encrypt -> upload every file in the folder, once.
381
- async function cmdPush(folder) {
401
+ async function cmdPush(folder, flags) {
382
402
  const { agent } = await chooseAuth()
383
403
  if (agent) {
404
+ // The agent uploads single files to the vault root; it cannot make vault
405
+ // folders yet. Refuse a tree rather than flatten it.
406
+ const { readdir: readDir } = await import('node:fs/promises')
407
+ const entries = await readDir(resolve(folder), { withFileTypes: true })
408
+ if (entries.some((e) => e.isDirectory() && !e.name.startsWith('.'))) {
409
+ throw new Error(
410
+ 'this folder has subfolders, and pushing through the agent (sf login) only ' +
411
+ 'uploads the files at its top level. To copy the whole tree, run sf logout, ' +
412
+ 'then set SF_EMAIL and SF_PASSWORD and run sf push again.',
413
+ )
414
+ }
384
415
  const { request } = await import('./agent/client.mjs')
385
416
  process.stderr.write(`pushing to account ${agent.account} through the agent\n`)
386
417
  let differs = 0
387
- for await (const { name, path } of filesIn(resolve(folder))) {
388
- const result = await request('upload', { path }, { timeoutMs: 0 })
418
+ for await (const { name, path } of filesIn(resolve(folder), pushListOptions(flags))) {
419
+ let result
420
+ try {
421
+ result = await request('upload', { path }, { timeoutMs: 0 })
422
+ } catch (err) {
423
+ if (err.code !== 'empty_file_unsupported') throw err
424
+ process.stdout.write(`skipped ${name}: ${err.message}\n`)
425
+ differs++
426
+ continue
427
+ }
389
428
  if (result.matchesFileNow) {
390
429
  process.stdout.write(`pushed ${name}\n`)
391
430
  } else {
@@ -402,23 +441,75 @@ async function cmdPush(folder) {
402
441
  return
403
442
  }
404
443
 
405
- const cfg = readLiveConfig()
406
- const { uploadFile } = await import('./upload.mjs')
407
- const { accessToken, rootKey } = await authAndUnlock(cfg)
444
+ const cfg = readLiveConfig(flags)
445
+ const { runPush } = await import('./migrate.mjs')
446
+ const { createSessionTokens } = await import('./sync.mjs')
447
+ const session = await authAndUnlock(cfg)
448
+ const accessToken = createSessionTokens({
449
+ accessToken: session.accessToken,
450
+ refreshToken: session.refreshToken,
451
+ expiresAt: session.expiresAt,
452
+ refresh: session.refreshSession,
453
+ })
408
454
 
409
- for await (const { name, path, size } of filesIn(folder)) {
410
- await uploadFile({
455
+ // First Ctrl-C: start nothing new, let uploads in progress finish and be
456
+ // recorded. Second: quit now. Either way a rerun picks up where this left off.
457
+ const controller = new AbortController()
458
+ const onSigint = () => {
459
+ if (controller.signal.aborted) {
460
+ process.stderr.write('\nquitting now; run the same command again to resume\n')
461
+ process.exit(130)
462
+ }
463
+ controller.abort()
464
+ process.stderr.write(
465
+ '\nstopping: finishing the uploads in progress (Ctrl-C again to quit now). ' +
466
+ 'Run the same command again to resume.\n',
467
+ )
468
+ }
469
+ process.on('SIGINT', onSigint)
470
+ let summary
471
+ try {
472
+ summary = await runPush({
473
+ source: folder,
411
474
  apiBaseUrl: cfg.apiBaseUrl,
412
475
  accessToken,
413
- rootKey,
414
- name,
415
- path,
416
- size,
476
+ rootKey: session.rootKey,
477
+ account: cfg.email.toLowerCase(),
478
+ includeDotfiles: pushListOptions(flags).includeDotfiles,
479
+ signal: controller.signal,
417
480
  })
418
- process.stdout.write(`pushed ${name}\n`)
481
+ } finally {
482
+ process.off('SIGINT', onSigint)
483
+ }
484
+ const fmt = (n) => `${(n / 1e9).toFixed(2)} GB`
485
+ process.stderr.write(
486
+ `done: ${summary.uploaded} uploaded (${fmt(summary.bytesUploaded)}), ` +
487
+ `${summary.alreadyThere} already there, ${summary.foldersCreated} folders created` +
488
+ (summary.failed.length ? `, ${summary.failed.length} FAILED` : '') +
489
+ (summary.changed.length ? `, ${summary.changed.length} changed while uploading` : '') +
490
+ (summary.skippedEmpty.length ? `, ${summary.skippedEmpty.length} empty skipped` : '') +
491
+ '\n',
492
+ )
493
+ if (summary.stoppedBy) {
494
+ process.stderr.write(`stopped: ${summary.stoppedBy.message}\n`)
495
+ process.exitCode = 1
496
+ return
497
+ }
498
+ if (summary.interrupted) {
499
+ process.stderr.write(`interrupted with ${summary.notStarted} files not started; run the same command again to resume\n`)
500
+ process.exitCode = 130
501
+ return
502
+ }
503
+ if (summary.failed.length || summary.changed.length || summary.skippedEmpty.length) {
504
+ process.stderr.write('some files are not in the vault; run the same command again to retry them\n')
505
+ process.exitCode = 2
419
506
  }
420
507
  }
421
508
 
509
+ function pushListOptions(flags) {
510
+ return { includeDotfiles: flags['include-dotfiles'] === true }
511
+ }
512
+
422
513
  async function syncViaAgent(folder, { watch, intervalMs }, agent) {
423
514
  const { AgentUnavailableError, request } = await import('./agent/client.mjs')
424
515
  const controller = new AbortController()
@@ -466,11 +557,11 @@ async function syncViaAgent(folder, { watch, intervalMs }, agent) {
466
557
  }
467
558
 
468
559
  // login -> unlock -> mirror new/changed files into the vault, once or on a loop.
469
- async function cmdSync(folder, { watch, intervalMs }) {
560
+ async function cmdSync(folder, { watch, intervalMs }, flags) {
470
561
  const { agent } = await chooseAuth()
471
562
  if (agent) return syncViaAgent(resolve(folder), { watch, intervalMs }, agent)
472
563
 
473
- const cfg = readLiveConfig()
564
+ const cfg = readLiveConfig(flags)
474
565
  const { uploadFile } = await import('./upload.mjs')
475
566
  const { runSync } = await import('./sync.mjs')
476
567
 
@@ -556,7 +647,7 @@ async function main() {
556
647
  if (result.skipped) process.exitCode = 2
557
648
  } else if (cmd === 'push') {
558
649
  if (!folder) throw new Error('push needs a <folder>')
559
- await cmdPush(folder)
650
+ await cmdPush(folder, flags)
560
651
  } else if (cmd === 'sync') {
561
652
  if (!folder) throw new Error('sync needs a <folder>')
562
653
  // `--interval 10` (space-separated) would otherwise parse as a bare flag and
@@ -569,7 +660,7 @@ async function main() {
569
660
  await cmdSync(folder, {
570
661
  watch: Boolean(flags.watch),
571
662
  intervalMs: Math.round(intervalSec * 1000),
572
- })
663
+ }, flags)
573
664
  } else if (cmd === 'verify') {
574
665
  await cmdVerify(positionals)
575
666
  } else {
@@ -10,6 +10,7 @@
10
10
  // vault KDF is different: the web keyring trims the vault password. See
11
11
  // src/unlock.mjs.)
12
12
  import { deriveMasterSecret } from './argon2.mjs'
13
+ import { LegacyLoginRefusedError } from './authPin.mjs'
13
14
 
14
15
  const DOMAIN = 'shieldfive/v1/auth/login-secret'
15
16
 
@@ -40,8 +41,15 @@ export async function deriveLoginSecret({ password, authSalt }) {
40
41
  * lookup never falls back to version 1: for a migrated account that would post
41
42
  * the password to the server and still not sign in. Throws before anything is
42
43
  * posted to the login route.
44
+ *
45
+ * `pinnedV2`: this computer has already signed in to the account as version 2
46
+ * (src/authPin.mjs). A version-1 answer for it is then refused with
47
+ * LegacyLoginRefusedError, before the password goes anywhere, unless
48
+ * `allowLegacyLogin` (the --allow-legacy-login flag) says the user expects it.
43
49
  */
44
- export async function buildSignInCredentials({ apiBaseUrl, email, password, fetchImpl = fetch }) {
50
+ export async function buildSignInCredentials({
51
+ apiBaseUrl, email, password, fetchImpl = fetch, pinnedV2 = false, allowLegacyLogin = false,
52
+ }) {
45
53
  let response
46
54
  try {
47
55
  response = await fetchImpl(new URL('/api/auth/salt', apiBaseUrl), {
@@ -57,6 +65,7 @@ export async function buildSignInCredentials({ apiBaseUrl, email, password, fetc
57
65
  typeof body.authSalt !== 'string' || !body.authSalt) {
58
66
  throw new Error('Unable to prepare secure sign-in. Please try again.')
59
67
  }
68
+ if (body.authVersion === 1 && pinnedV2 && !allowLegacyLogin) throw new LegacyLoginRefusedError()
60
69
  if (body.authVersion === 1 && body.migrationEnabled !== true) return { password }
61
70
  const loginSecret = await deriveLoginSecret({ password, authSalt: body.authSalt })
62
71
  return body.authVersion === 2 ? { loginSecret } : { password, loginSecret, authSalt: body.authSalt }