@shieldfive/cli 0.3.0 → 0.4.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,12 +31,19 @@ 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',
37
39
  ' encrypt <folder> encrypt each file locally, upload nothing',
40
+ ' inspect <folder> count a migration source recursively; upload nothing',
38
41
  ' --help',
39
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
+ '',
40
47
  'After sf login, push / sync / verify go through the agent and need nothing',
41
48
  'in the environment. Without an agent, or when SF_EMAIL and SF_PASSWORD are',
42
49
  'set, push and sync sign in from those [SF_VAULT_PASSWORD if your vault',
@@ -47,10 +54,15 @@ function usage() {
47
54
  )
48
55
  }
49
56
 
50
- 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 } = {}) {
51
62
  for (const name of await readdir(folder)) {
63
+ if (!includeDotfiles && name.startsWith('.')) continue
52
64
  const path = join(folder, name)
53
- const info = await stat(path)
65
+ const info = await lstat(path)
54
66
  if (info.isFile()) yield { name, path, size: info.size }
55
67
  }
56
68
  }
@@ -98,7 +110,7 @@ function envCredentialsSet() {
98
110
  return Boolean(process.env.SF_EMAIL && process.env.SF_PASSWORD)
99
111
  }
100
112
 
101
- function readLiveConfig() {
113
+ function readLiveConfig(flags = {}) {
102
114
  const e = process.env
103
115
  const email = e.SF_EMAIL
104
116
  const password = e.SF_PASSWORD
@@ -118,6 +130,7 @@ function readLiveConfig() {
118
130
  email,
119
131
  password,
120
132
  vaultPassword: e.SF_VAULT_PASSWORD || password,
133
+ allowLegacyLogin: flags['allow-legacy-login'] === true,
121
134
  }
122
135
  }
123
136
 
@@ -140,15 +153,19 @@ async function readTotpCode() {
140
153
  // lazily so `sf encrypt` runs without @supabase/supabase-js.
141
154
  async function authAndUnlock(cfg) {
142
155
  const { signIn } = await import('./auth.mjs')
156
+ const { fileAuthPin } = await import('./authPin.mjs')
143
157
  const { fetchAndUnlockVault } = await import('./vault.mjs')
144
158
 
145
159
  process.stderr.write('signing in…\n')
146
- const { accessToken } = await signIn({
160
+ const { accessToken, refreshToken, expiresAt } = await signIn({
161
+ apiBaseUrl: cfg.apiBaseUrl,
147
162
  supabaseUrl: cfg.supabaseUrl,
148
163
  anonKey: cfg.anonKey,
149
164
  email: cfg.email,
150
165
  password: cfg.password,
151
166
  getTotpCode: readTotpCode,
167
+ authPin: fileAuthPin(),
168
+ allowLegacyLogin: cfg.allowLegacyLogin === true,
152
169
  })
153
170
 
154
171
  process.stderr.write('unlocking vault…\n')
@@ -157,7 +174,13 @@ async function authAndUnlock(cfg) {
157
174
  accessToken,
158
175
  password: cfg.vaultPassword,
159
176
  })
160
- return { accessToken, rootKey }
177
+ // `sf sync --watch` outlives the access token (about an hour); it refreshes
178
+ // through this, the same exchange the agent uses.
179
+ const refreshSession = async (token) => {
180
+ const { refreshAccessToken } = await import('./auth.mjs')
181
+ return refreshAccessToken({ supabaseUrl: cfg.supabaseUrl, anonKey: cfg.anonKey, refreshToken: token })
182
+ }
183
+ return { accessToken, refreshToken, expiresAt, refreshSession, rootKey }
161
184
  }
162
185
 
163
186
  async function runningAgent() {
@@ -290,10 +313,15 @@ async function cmdLogin(flags) {
290
313
  (await promptHidden('Vault password (press Enter if it is the same): ')) || password
291
314
 
292
315
  const { signIn } = await import('./auth.mjs')
316
+ const { fileAuthPin } = await import('./authPin.mjs')
293
317
  const { fetchAndUnlockVault } = await import('./vault.mjs')
294
318
 
295
319
  process.stderr.write('signing in…\n')
296
- 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
+ })
297
325
 
298
326
  process.stderr.write('unlocking vault…\n')
299
327
  const rootKey = await fetchAndUnlockVault({
@@ -370,14 +398,22 @@ async function cmdLogout(flags) {
370
398
  }
371
399
 
372
400
  // login -> unlock -> encrypt -> upload every file in the folder, once.
373
- async function cmdPush(folder) {
401
+ async function cmdPush(folder, flags) {
374
402
  const { agent } = await chooseAuth()
375
403
  if (agent) {
376
404
  const { request } = await import('./agent/client.mjs')
377
405
  process.stderr.write(`pushing to account ${agent.account} through the agent\n`)
378
406
  let differs = 0
379
- for await (const { name, path } of filesIn(resolve(folder))) {
380
- const result = await request('upload', { path }, { timeoutMs: 0 })
407
+ for await (const { name, path } of filesIn(resolve(folder), pushListOptions(flags))) {
408
+ let result
409
+ try {
410
+ result = await request('upload', { path }, { timeoutMs: 0 })
411
+ } catch (err) {
412
+ if (err.code !== 'empty_file_unsupported') throw err
413
+ process.stdout.write(`skipped ${name}: ${err.message}\n`)
414
+ differs++
415
+ continue
416
+ }
381
417
  if (result.matchesFileNow) {
382
418
  process.stdout.write(`pushed ${name}\n`)
383
419
  } else {
@@ -394,21 +430,34 @@ async function cmdPush(folder) {
394
430
  return
395
431
  }
396
432
 
397
- const cfg = readLiveConfig()
433
+ const cfg = readLiveConfig(flags)
398
434
  const { uploadFile } = await import('./upload.mjs')
399
435
  const { accessToken, rootKey } = await authAndUnlock(cfg)
400
436
 
401
- for await (const { name, path, size } of filesIn(folder)) {
402
- await uploadFile({
403
- apiBaseUrl: cfg.apiBaseUrl,
404
- accessToken,
405
- rootKey,
406
- name,
407
- path,
408
- size,
409
- })
437
+ let skipped = 0
438
+ for await (const { name, path, size } of filesIn(folder, pushListOptions(flags))) {
439
+ try {
440
+ await uploadFile({
441
+ apiBaseUrl: cfg.apiBaseUrl,
442
+ accessToken,
443
+ rootKey,
444
+ name,
445
+ path,
446
+ size,
447
+ })
448
+ } catch (err) {
449
+ if (err.code !== 'empty_file_unsupported') throw err
450
+ process.stdout.write(`skipped ${name}: ${err.message}\n`)
451
+ skipped++
452
+ continue
453
+ }
410
454
  process.stdout.write(`pushed ${name}\n`)
411
455
  }
456
+ if (skipped) process.exitCode = 2
457
+ }
458
+
459
+ function pushListOptions(flags) {
460
+ return { includeDotfiles: flags['include-dotfiles'] === true }
412
461
  }
413
462
 
414
463
  async function syncViaAgent(folder, { watch, intervalMs }, agent) {
@@ -458,11 +507,11 @@ async function syncViaAgent(folder, { watch, intervalMs }, agent) {
458
507
  }
459
508
 
460
509
  // login -> unlock -> mirror new/changed files into the vault, once or on a loop.
461
- async function cmdSync(folder, { watch, intervalMs }) {
510
+ async function cmdSync(folder, { watch, intervalMs }, flags) {
462
511
  const { agent } = await chooseAuth()
463
512
  if (agent) return syncViaAgent(resolve(folder), { watch, intervalMs }, agent)
464
513
 
465
- const cfg = readLiveConfig()
514
+ const cfg = readLiveConfig(flags)
466
515
  const { uploadFile } = await import('./upload.mjs')
467
516
  const { runSync } = await import('./sync.mjs')
468
517
 
@@ -540,9 +589,15 @@ async function main() {
540
589
  } else if (cmd === 'encrypt') {
541
590
  if (!folder) throw new Error('encrypt needs a <folder>')
542
591
  await cmdEncrypt(folder)
592
+ } else if (cmd === 'inspect') {
593
+ if (!folder) throw new Error('inspect needs a <folder>')
594
+ const { inspectMigrationSource } = await import('./migrationInventory.mjs')
595
+ const result = await inspectMigrationSource(folder)
596
+ process.stdout.write(JSON.stringify(result) + '\n')
597
+ if (result.skipped) process.exitCode = 2
543
598
  } else if (cmd === 'push') {
544
599
  if (!folder) throw new Error('push needs a <folder>')
545
- await cmdPush(folder)
600
+ await cmdPush(folder, flags)
546
601
  } else if (cmd === 'sync') {
547
602
  if (!folder) throw new Error('sync needs a <folder>')
548
603
  // `--interval 10` (space-separated) would otherwise parse as a bare flag and
@@ -555,7 +610,7 @@ async function main() {
555
610
  await cmdSync(folder, {
556
611
  watch: Boolean(flags.watch),
557
612
  intervalMs: Math.round(intervalSec * 1000),
558
- })
613
+ }, flags)
559
614
  } else if (cmd === 'verify') {
560
615
  await cmdVerify(positionals)
561
616
  } else {
@@ -0,0 +1,72 @@
1
+ // Client-side derivation of the LOGIN SECRET, byte-identical to the web
2
+ // (utils/auth/loginSecret.ts) and Android (src/features/auth/loginSecret.ts):
3
+ //
4
+ // loginSecret = base64url_unpadded(
5
+ // Argon2id( DOMAIN || "\n" || password, authSalt, moderate ) )
6
+ //
7
+ // The password is used EXACTLY as typed: no trim, no Unicode normalisation.
8
+ // That is what web sign-in, signup, reset and change-password all feed this
9
+ // derivation, so it is what the secret GoTrue holds was derived from. (The
10
+ // vault KDF is different: the web keyring trims the vault password. See
11
+ // src/unlock.mjs.)
12
+ import { deriveMasterSecret } from './argon2.mjs'
13
+ import { LegacyLoginRefusedError } from './authPin.mjs'
14
+
15
+ const DOMAIN = 'shieldfive/v1/auth/login-secret'
16
+
17
+ export async function deriveLoginSecret({ password, authSalt }) {
18
+ if (typeof password !== 'string' || !password) throw new Error('Password is required')
19
+ if (typeof authSalt !== 'string' || !/^[A-Za-z0-9+/_-]+={0,2}$/.test(authSalt)) {
20
+ throw new Error('Invalid secure sign-in salt')
21
+ }
22
+ // Node's base64 decoder accepts base64 and base64url alike, as the web does.
23
+ const salt = new Uint8Array(Buffer.from(authSalt, 'base64'))
24
+ if (salt.length < 16 || salt.length > 64) throw new Error('Invalid secure sign-in salt')
25
+ const { masterSecret } = await deriveMasterSecret({
26
+ passphrase: `${DOMAIN}\n${password}`, salt, preset: 'moderate',
27
+ })
28
+ try { return Buffer.from(masterSecret).toString('base64url') }
29
+ finally { masterSecret.fill(0) }
30
+ }
31
+
32
+ /**
33
+ * The credential half of the sign-in body, per POST /api/auth/salt:
34
+ *
35
+ * version 2 -> { loginSecret } (no password)
36
+ * version 1, migration on -> { password, loginSecret, authSalt } (server verifies, then migrates)
37
+ * version 1, migration off -> { password }
38
+ *
39
+ * The raw password is only ever returned when the server AFFIRMATIVELY said
40
+ * version 1. Unlike the web's fetchAuthSalt, a failed, throttled or malformed
41
+ * lookup never falls back to version 1: for a migrated account that would post
42
+ * the password to the server and still not sign in. Throws before anything is
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.
49
+ */
50
+ export async function buildSignInCredentials({
51
+ apiBaseUrl, email, password, fetchImpl = fetch, pinnedV2 = false, allowLegacyLogin = false,
52
+ }) {
53
+ let response
54
+ try {
55
+ response = await fetchImpl(new URL('/api/auth/salt', apiBaseUrl), {
56
+ method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
57
+ body: JSON.stringify({ email }), signal: AbortSignal.timeout(30_000),
58
+ })
59
+ } catch {
60
+ throw new Error('Unable to prepare secure sign-in. Check your connection and try again.')
61
+ }
62
+ if (response.status === 429) throw new Error('Too many sign-in attempts. Try again shortly.')
63
+ const body = await response.json().catch(() => null)
64
+ if (!response.ok || !body || ![1, 2].includes(body.authVersion) ||
65
+ typeof body.authSalt !== 'string' || !body.authSalt) {
66
+ throw new Error('Unable to prepare secure sign-in. Please try again.')
67
+ }
68
+ if (body.authVersion === 1 && pinnedV2 && !allowLegacyLogin) throw new LegacyLoginRefusedError()
69
+ if (body.authVersion === 1 && body.migrationEnabled !== true) return { password }
70
+ const loginSecret = await deriveLoginSecret({ password, authSalt: body.authSalt })
71
+ return body.authVersion === 2 ? { loginSecret } : { password, loginSecret, authSalt: body.authSalt }
72
+ }
@@ -0,0 +1,45 @@
1
+ import { lstat, opendir } from 'node:fs/promises'
2
+ import { join, relative, resolve, sep } from 'node:path'
3
+
4
+ // Read-only preflight: includes hidden files and empty directories, preserves
5
+ // names, and reports unsupported entries instead of silently omitting them.
6
+ // It streams metadata without reading file contents or holding an inventory in
7
+ // memory. Upload must revalidate each source: this is not an immutable snapshot.
8
+ export async function* inventoryFolder(folder) {
9
+ const root = resolve(folder)
10
+ const initial = await lstat(root)
11
+ if (!initial.isDirectory() || initial.isSymbolicLink()) throw new Error('Select a real source folder, not a symbolic link')
12
+ const pending = [root]
13
+ while (pending.length) {
14
+ const directory = pending.pop()
15
+ const directoryInfo = await lstat(directory)
16
+ if (directoryInfo.isSymbolicLink() || !directoryInfo.isDirectory()) {
17
+ yield { kind: 'skipped', path: relative(root, directory).split(sep).join('/'), reason: 'source_changed' }
18
+ continue
19
+ }
20
+ for await (const entry of await opendir(directory)) {
21
+ const absolutePath = join(directory, entry.name)
22
+ const path = relative(root, absolutePath).split(sep).join('/')
23
+ let info
24
+ try { info = await lstat(absolutePath) }
25
+ catch (error) { yield { kind: 'skipped', path, reason: error.code ?? 'unreadable' }; continue }
26
+ if (info.isSymbolicLink()) yield { kind: 'skipped', path, reason: 'symbolic_link' }
27
+ else if (info.isDirectory()) {
28
+ yield { kind: 'directory', path }
29
+ pending.push(absolutePath)
30
+ } else if (info.isFile()) yield { kind: 'file', path, size: info.size, mtimeMs: info.mtimeMs }
31
+ else yield { kind: 'skipped', path, reason: 'unsupported_file_type' }
32
+ }
33
+ }
34
+ }
35
+
36
+ export async function inspectMigrationSource(folder) {
37
+ const totals = { files: 0, directories: 0, bytes: 0, skipped: 0 }
38
+ for await (const entry of inventoryFolder(folder)) {
39
+ if (entry.kind === 'file') { totals.files++; totals.bytes += entry.size }
40
+ else if (entry.kind === 'directory') totals.directories++
41
+ else totals.skipped++
42
+ if (!Number.isSafeInteger(totals.bytes)) throw new Error('Source size exceeds the supported inventory limit')
43
+ }
44
+ return totals
45
+ }
package/src/sync.mjs CHANGED
@@ -11,11 +11,13 @@
11
11
  // Scope notes: change is detected by (size, mtimeMs) — the rsync default, cheap
12
12
  // and good enough; a same-size same-mtime edit is not re-uploaded. Deletions
13
13
  // and renames are NOT mirrored (a file removed locally stays in the vault); sync
14
- // is append-only for now. Each changed file becomes a new vault upload (the
15
- // server does not overwrite by name), which is the intended versioning
16
- // behaviour.
14
+ // is append-only for now. Each changed file becomes a new vault upload at the
15
+ // vault root (the server does not overwrite by name); earlier versions are kept,
16
+ // not replaced or trashed, so they count against storage until removed in the
17
+ // app. Replacing the previous fileId needs the Bin's folder key to re-wrap it,
18
+ // which this client does not hold yet.
17
19
 
18
- import { open, readFile, readdir, rename, stat } from 'node:fs/promises'
20
+ import { lstat, open, readFile, readdir, rename } from 'node:fs/promises'
19
21
  import { join } from 'node:path'
20
22
 
21
23
  export const MANIFEST_NAME = '.shieldfive-sync.json'
@@ -26,7 +28,8 @@ export function signatureOf({ size, mtimeMs }) {
26
28
  return `${size}:${mtimeMs}`
27
29
  }
28
30
 
29
- // List regular files to consider for sync: skips directories, dotfiles (which
31
+ // List regular files to consider for sync: skips directories, symlinks (lstat,
32
+ // so a link never pulls in a file from outside the folder), dotfiles (which
30
33
  // includes the manifest itself), and any explicitly excluded names.
31
34
  export async function scanFiles(folder, { exclude = [] } = {}) {
32
35
  const skip = new Set(exclude)
@@ -34,7 +37,7 @@ export async function scanFiles(folder, { exclude = [] } = {}) {
34
37
  for (const name of await readdir(folder)) {
35
38
  if (name.startsWith('.') || skip.has(name)) continue
36
39
  const path = join(folder, name)
37
- const info = await stat(path)
40
+ const info = await lstat(path)
38
41
  if (info.isFile()) {
39
42
  entries.push({ name, path, size: info.size, mtimeMs: info.mtimeMs })
40
43
  }
@@ -58,7 +61,10 @@ export async function loadManifest(manifestPath) {
58
61
  }
59
62
 
60
63
  // Atomic-ish write: write a temp sibling then rename over the target, so a crash
61
- // during the write can't leave a half-written manifest.
64
+ // during the write can't leave a half-written manifest. Mode 0600: the manifest
65
+ // lists every synced file name with its vault file id, which is nobody else's
66
+ // business on a shared machine. chmod as well as the open mode, because a temp
67
+ // file left by a crashed run keeps whatever mode it had.
62
68
  export async function saveManifest(manifestPath, manifest) {
63
69
  const body = JSON.stringify(
64
70
  { version: MANIFEST_VERSION, files: manifest.files },
@@ -66,8 +72,9 @@ export async function saveManifest(manifestPath, manifest) {
66
72
  2,
67
73
  )
68
74
  const tmp = `${manifestPath}.tmp`
69
- const handle = await open(tmp, 'w')
75
+ const handle = await open(tmp, 'w', 0o600)
70
76
  try {
77
+ await handle.chmod(0o600)
71
78
  await handle.writeFile(body)
72
79
  await handle.sync()
73
80
  } finally {
@@ -95,6 +102,11 @@ export function planSync(entries, manifest) {
95
102
  // One reconcile pass. uploadFn is injected (defaults to the real uploadFile) so
96
103
  // the pass is unit-testable without a network. Returns a summary and mutates
97
104
  // `manifest` in place, persisting after each successful upload.
105
+ //
106
+ // `accessToken` is a string, or a function returning one; the function is
107
+ // called before every upload, so a long pass picks up a refreshed session. If
108
+ // it throws (the session cannot be refreshed) the pass stops and the error
109
+ // propagates: nothing after it could succeed.
98
110
  export async function syncOnce({
99
111
  apiBaseUrl,
100
112
  accessToken,
@@ -112,11 +124,13 @@ export async function syncOnce({
112
124
  let failed = 0
113
125
  const errors = []
114
126
 
127
+ const tokenFor = typeof accessToken === 'function' ? accessToken : async () => accessToken
115
128
  for (const entry of toUpload) {
129
+ const token = await tokenFor()
116
130
  try {
117
131
  const { fileId } = await uploadFn({
118
132
  apiBaseUrl,
119
- accessToken,
133
+ accessToken: token,
120
134
  rootKey,
121
135
  name: entry.name,
122
136
  path: entry.path,
@@ -156,9 +170,47 @@ function sleep(ms, signal) {
156
170
  })
157
171
  }
158
172
 
173
+ // Refresh this long before the access token expires (seconds).
174
+ const REFRESH_MARGIN_S = 120
175
+
176
+ /**
177
+ * The access token for the next request: the current one while it has more
178
+ * than REFRESH_MARGIN_S left, otherwise a refreshed one. Supabase rotates
179
+ * refresh tokens and revokes the session if one is presented twice, so the
180
+ * refresh is single-flight and the rotated token replaces the old one.
181
+ * Without an expiry or a refresh function (tests, old callers) the token is
182
+ * used as is.
183
+ */
184
+ export function createSessionTokens({ accessToken, refreshToken, expiresAt, refresh, now = () => Date.now() }) {
185
+ let tokens = { access: accessToken, refresh: refreshToken, expiresAt }
186
+ let inflight = null
187
+ return async function current() {
188
+ if (!refresh || !tokens.expiresAt || now() / 1000 < tokens.expiresAt - REFRESH_MARGIN_S) {
189
+ return tokens.access
190
+ }
191
+ inflight ??= refresh(tokens.refresh)
192
+ .then((s) => {
193
+ tokens = { access: s.accessToken, refresh: s.refreshToken, expiresAt: s.expiresAt }
194
+ return tokens.access
195
+ })
196
+ .finally(() => {
197
+ inflight = null
198
+ })
199
+ return inflight
200
+ }
201
+ }
202
+
159
203
  // Orchestrates a real sync: sign in + unlock once, then run one pass (or loop on
160
204
  // an interval under --watch). authAndUnlock is injected so the CLI can share its
161
- // sign-in/unlock path and tests can stub it.
205
+ // sign-in/unlock path and tests can stub it. It may also return refreshToken,
206
+ // expiresAt (unix seconds) and refreshSession(refreshToken); with them the
207
+ // session is refreshed before it expires, so `--watch` keeps working past the
208
+ // access token's lifetime (about an hour). A refresh that fails ends the sync
209
+ // with an error (non-zero exit) instead of failing every file on every pass
210
+ // while looking healthy to a supervisor.
211
+ //
212
+ // Returns the last pass's summary. A single pass with failed files rejects, so
213
+ // `sf sync` exits non-zero; under --watch failed files are retried next pass.
162
214
  export async function runSync({
163
215
  cfg,
164
216
  folder,
@@ -168,8 +220,17 @@ export async function runSync({
168
220
  uploadFn,
169
221
  log = (m) => process.stdout.write(`${m}\n`),
170
222
  status = (m) => process.stderr.write(`${m}\n`),
223
+ now = () => Date.now(),
171
224
  }) {
172
- const { accessToken, rootKey } = await authAndUnlock(cfg)
225
+ const session = await authAndUnlock(cfg)
226
+ const { rootKey } = session
227
+ const accessToken = createSessionTokens({
228
+ accessToken: session.accessToken,
229
+ refreshToken: session.refreshToken,
230
+ expiresAt: session.expiresAt,
231
+ refresh: session.refreshSession,
232
+ now,
233
+ })
173
234
  const manifestPath = join(folder, MANIFEST_NAME)
174
235
  const manifest = await loadManifest(manifestPath)
175
236
 
@@ -180,9 +241,10 @@ export async function runSync({
180
241
  }
181
242
  if (watch) process.on('SIGINT', onSigint)
182
243
 
244
+ let summary
183
245
  try {
184
246
  do {
185
- const summary = await syncOnce({
247
+ summary = await syncOnce({
186
248
  apiBaseUrl: cfg.apiBaseUrl,
187
249
  accessToken,
188
250
  rootKey,
@@ -202,4 +264,8 @@ export async function runSync({
202
264
  } finally {
203
265
  if (watch) process.off('SIGINT', onSigint)
204
266
  }
267
+ if (!watch && summary?.failed) {
268
+ throw new Error(`${summary.failed} file(s) failed to sync.`)
269
+ }
270
+ return summary
205
271
  }
package/src/unlock.mjs CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  import { webcrypto } from 'node:crypto'
12
12
 
13
- import { deriveMasterSecret } from '@shieldfive/crypto/kdf/argon2id'
13
+ import { deriveMasterSecret } from './argon2.mjs'
14
14
 
15
15
  const subtle = webcrypto.subtle
16
16
 
@@ -62,9 +62,9 @@ export async function deriveUserKey({
62
62
  salt,
63
63
  preset: argon2Preset,
64
64
  })
65
- return subtle.importKey('raw', masterSecret, { name: 'AES-GCM' }, false, [
66
- 'decrypt',
67
- ])
65
+ try {
66
+ return await subtle.importKey('raw', masterSecret, { name: 'AES-GCM' }, false, ['decrypt'])
67
+ } finally { masterSecret.fill(0) }
68
68
  }
69
69
  throw new Error(`Unsupported vault KDF: ${kdf}`)
70
70
  }
@@ -73,21 +73,37 @@ export async function deriveUserKey({
73
73
  * Unwrap the vault root key from the password. `vaultKey` carries the base64
74
74
  * fields of the server's vault_keys row (same shape as the export bundle's
75
75
  * vault.json): ukSalt, ukIv, ukKdf, ukIterations?, ukArgon2Preset?, rkWrappedByUk.
76
+ *
77
+ * The web keyring (utils/keyring.ts) TRIMS the vault password before every
78
+ * wrap and unwrap, so a vault created or re-keyed on the web was wrapped under
79
+ * the trimmed password. That is tried first. Only if it fails AES-GCM
80
+ * authentication, and the password actually has surrounding whitespace, is the
81
+ * password tried exactly as typed (a vault wrapped by a client that does not
82
+ * trim). Nothing here is sent anywhere.
76
83
  * @returns {Promise<Uint8Array>} the 32-byte root key
77
84
  */
78
85
  export async function unlockRootKey({ password, vaultKey }) {
79
- const uk = await deriveUserKey({
80
- password,
81
- salt: base64ToBytes(vaultKey.ukSalt),
82
- kdf: vaultKey.ukKdf,
83
- iterations: vaultKey.ukIterations,
84
- argon2Preset: vaultKey.ukArgon2Preset,
85
- })
86
- return aesGcmDecrypt(
87
- uk,
88
- base64ToBytes(vaultKey.ukIv),
89
- base64ToBytes(vaultKey.rkWrappedByUk),
90
- )
86
+ if (typeof password !== 'string' || !password) throw new Error('Vault password is required')
87
+ const trimmed = password.trim()
88
+ const candidates = trimmed && trimmed !== password ? [trimmed, password] : [password]
89
+ for (let i = 0; i < candidates.length; i++) {
90
+ const uk = await deriveUserKey({
91
+ password: candidates[i],
92
+ salt: base64ToBytes(vaultKey.ukSalt),
93
+ kdf: vaultKey.ukKdf,
94
+ iterations: vaultKey.ukIterations,
95
+ argon2Preset: vaultKey.ukArgon2Preset,
96
+ })
97
+ try {
98
+ return await aesGcmDecrypt(
99
+ uk,
100
+ base64ToBytes(vaultKey.ukIv),
101
+ base64ToBytes(vaultKey.rkWrappedByUk),
102
+ )
103
+ } catch (err) {
104
+ if (i === candidates.length - 1 || err?.name !== 'OperationError') throw err
105
+ }
106
+ }
91
107
  }
92
108
 
93
109
  /** Recovery-key path: unwrap the root key with the base64 recovery key. */