@shieldfive/cli 0.2.1 → 0.3.1

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.
@@ -0,0 +1,63 @@
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
+
14
+ const DOMAIN = 'shieldfive/v1/auth/login-secret'
15
+
16
+ export async function deriveLoginSecret({ password, authSalt }) {
17
+ if (typeof password !== 'string' || !password) throw new Error('Password is required')
18
+ if (typeof authSalt !== 'string' || !/^[A-Za-z0-9+/_-]+={0,2}$/.test(authSalt)) {
19
+ throw new Error('Invalid secure sign-in salt')
20
+ }
21
+ // Node's base64 decoder accepts base64 and base64url alike, as the web does.
22
+ const salt = new Uint8Array(Buffer.from(authSalt, 'base64'))
23
+ if (salt.length < 16 || salt.length > 64) throw new Error('Invalid secure sign-in salt')
24
+ const { masterSecret } = await deriveMasterSecret({
25
+ passphrase: `${DOMAIN}\n${password}`, salt, preset: 'moderate',
26
+ })
27
+ try { return Buffer.from(masterSecret).toString('base64url') }
28
+ finally { masterSecret.fill(0) }
29
+ }
30
+
31
+ /**
32
+ * The credential half of the sign-in body, per POST /api/auth/salt:
33
+ *
34
+ * version 2 -> { loginSecret } (no password)
35
+ * version 1, migration on -> { password, loginSecret, authSalt } (server verifies, then migrates)
36
+ * version 1, migration off -> { password }
37
+ *
38
+ * The raw password is only ever returned when the server AFFIRMATIVELY said
39
+ * version 1. Unlike the web's fetchAuthSalt, a failed, throttled or malformed
40
+ * lookup never falls back to version 1: for a migrated account that would post
41
+ * the password to the server and still not sign in. Throws before anything is
42
+ * posted to the login route.
43
+ */
44
+ export async function buildSignInCredentials({ apiBaseUrl, email, password, fetchImpl = fetch }) {
45
+ let response
46
+ try {
47
+ response = await fetchImpl(new URL('/api/auth/salt', apiBaseUrl), {
48
+ method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
49
+ body: JSON.stringify({ email }), signal: AbortSignal.timeout(30_000),
50
+ })
51
+ } catch {
52
+ throw new Error('Unable to prepare secure sign-in. Check your connection and try again.')
53
+ }
54
+ if (response.status === 429) throw new Error('Too many sign-in attempts. Try again shortly.')
55
+ const body = await response.json().catch(() => null)
56
+ if (!response.ok || !body || ![1, 2].includes(body.authVersion) ||
57
+ typeof body.authSalt !== 'string' || !body.authSalt) {
58
+ throw new Error('Unable to prepare secure sign-in. Please try again.')
59
+ }
60
+ if (body.authVersion === 1 && body.migrationEnabled !== true) return { password }
61
+ const loginSecret = await deriveLoginSecret({ password, authSalt: body.authSalt })
62
+ return body.authVersion === 2 ? { loginSecret } : { password, loginSecret, authSalt: body.authSalt }
63
+ }
@@ -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/prompt.mjs ADDED
@@ -0,0 +1,79 @@
1
+ // ShieldFive CLI — terminal prompts.
2
+ //
3
+ // The password prompt reads in raw mode and echoes nothing, not even asterisks:
4
+ // a count of characters is a fact about the password. There is deliberately no
5
+ // way to pass a password on the command line, where it would sit in shell
6
+ // history and in every process listing on the machine.
7
+
8
+ import { createInterface } from 'node:readline'
9
+
10
+ // Written as escapes on purpose. Literal control characters in source are
11
+ // invisible in review and easy to lose in an editor; a prompt that silently
12
+ // stopped matching Ctrl-C would trap the user in raw mode.
13
+ const CTRL_C = '\u0003'
14
+ const CTRL_D = '\u0004'
15
+ const BACKSPACE = '\u007f'
16
+ const CTRL_H = '\b'
17
+
18
+ /** Prompt for a single visible line (an email address, a TOTP code). */
19
+ export function promptLine(question, { input = process.stdin, output = process.stderr } = {}) {
20
+ return new Promise((resolve, reject) => {
21
+ const rl = createInterface({ input, output })
22
+ rl.question(question, (answer) => {
23
+ rl.close()
24
+ resolve(answer)
25
+ })
26
+ rl.on('error', reject)
27
+ })
28
+ }
29
+
30
+ /** Prompt for a secret without echoing it. Needs a terminal. */
31
+ export function promptHidden(question, { input = process.stdin, output = process.stderr } = {}) {
32
+ if (!input.isTTY || typeof input.setRawMode !== 'function') {
33
+ return Promise.reject(
34
+ new Error(
35
+ 'A password prompt needs a terminal. Run sf login in an interactive ' +
36
+ 'shell; it cannot read a password from a pipe.',
37
+ ),
38
+ )
39
+ }
40
+
41
+ return new Promise((resolve, reject) => {
42
+ const wasRaw = Boolean(input.isRaw)
43
+ let value = ''
44
+
45
+ const finish = (err) => {
46
+ input.off('data', onData)
47
+ input.setRawMode(wasRaw)
48
+ input.pause()
49
+ output.write('\n')
50
+ if (err) reject(err)
51
+ else resolve(value)
52
+ }
53
+
54
+ const onData = (chunk) => {
55
+ for (const ch of String(chunk)) {
56
+ if (ch === '\r' || ch === '\n') return finish()
57
+ if (ch === CTRL_C) return finish(new Error('Cancelled.'))
58
+ if (ch === CTRL_D) {
59
+ if (!value) return finish(new Error('Cancelled.'))
60
+ continue
61
+ }
62
+ if (ch === BACKSPACE || ch === CTRL_H) {
63
+ value = value.slice(0, -1)
64
+ continue
65
+ }
66
+ // Other control characters, including the escape sequences arrow keys
67
+ // send, are not part of a password.
68
+ if (ch < ' ') continue
69
+ value += ch
70
+ }
71
+ }
72
+
73
+ output.write(question)
74
+ input.setEncoding('utf8')
75
+ input.setRawMode(true)
76
+ input.resume()
77
+ input.on('data', onData)
78
+ })
79
+ }
package/src/sync.mjs CHANGED
@@ -11,9 +11,11 @@
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
20
  import { open, readFile, readdir, rename, stat } from 'node:fs/promises'
19
21
  import { join } from 'node:path'
@@ -95,6 +97,11 @@ export function planSync(entries, manifest) {
95
97
  // One reconcile pass. uploadFn is injected (defaults to the real uploadFile) so
96
98
  // the pass is unit-testable without a network. Returns a summary and mutates
97
99
  // `manifest` in place, persisting after each successful upload.
100
+ //
101
+ // `accessToken` is a string, or a function returning one; the function is
102
+ // called before every upload, so a long pass picks up a refreshed session. If
103
+ // it throws (the session cannot be refreshed) the pass stops and the error
104
+ // propagates: nothing after it could succeed.
98
105
  export async function syncOnce({
99
106
  apiBaseUrl,
100
107
  accessToken,
@@ -112,11 +119,13 @@ export async function syncOnce({
112
119
  let failed = 0
113
120
  const errors = []
114
121
 
122
+ const tokenFor = typeof accessToken === 'function' ? accessToken : async () => accessToken
115
123
  for (const entry of toUpload) {
124
+ const token = await tokenFor()
116
125
  try {
117
126
  const { fileId } = await uploadFn({
118
127
  apiBaseUrl,
119
- accessToken,
128
+ accessToken: token,
120
129
  rootKey,
121
130
  name: entry.name,
122
131
  path: entry.path,
@@ -156,9 +165,47 @@ function sleep(ms, signal) {
156
165
  })
157
166
  }
158
167
 
168
+ // Refresh this long before the access token expires (seconds).
169
+ const REFRESH_MARGIN_S = 120
170
+
171
+ /**
172
+ * The access token for the next request: the current one while it has more
173
+ * than REFRESH_MARGIN_S left, otherwise a refreshed one. Supabase rotates
174
+ * refresh tokens and revokes the session if one is presented twice, so the
175
+ * refresh is single-flight and the rotated token replaces the old one.
176
+ * Without an expiry or a refresh function (tests, old callers) the token is
177
+ * used as is.
178
+ */
179
+ export function createSessionTokens({ accessToken, refreshToken, expiresAt, refresh, now = () => Date.now() }) {
180
+ let tokens = { access: accessToken, refresh: refreshToken, expiresAt }
181
+ let inflight = null
182
+ return async function current() {
183
+ if (!refresh || !tokens.expiresAt || now() / 1000 < tokens.expiresAt - REFRESH_MARGIN_S) {
184
+ return tokens.access
185
+ }
186
+ inflight ??= refresh(tokens.refresh)
187
+ .then((s) => {
188
+ tokens = { access: s.accessToken, refresh: s.refreshToken, expiresAt: s.expiresAt }
189
+ return tokens.access
190
+ })
191
+ .finally(() => {
192
+ inflight = null
193
+ })
194
+ return inflight
195
+ }
196
+ }
197
+
159
198
  // Orchestrates a real sync: sign in + unlock once, then run one pass (or loop on
160
199
  // an interval under --watch). authAndUnlock is injected so the CLI can share its
161
- // sign-in/unlock path and tests can stub it.
200
+ // sign-in/unlock path and tests can stub it. It may also return refreshToken,
201
+ // expiresAt (unix seconds) and refreshSession(refreshToken); with them the
202
+ // session is refreshed before it expires, so `--watch` keeps working past the
203
+ // access token's lifetime (about an hour). A refresh that fails ends the sync
204
+ // with an error (non-zero exit) instead of failing every file on every pass
205
+ // while looking healthy to a supervisor.
206
+ //
207
+ // Returns the last pass's summary. A single pass with failed files rejects, so
208
+ // `sf sync` exits non-zero; under --watch failed files are retried next pass.
162
209
  export async function runSync({
163
210
  cfg,
164
211
  folder,
@@ -168,8 +215,17 @@ export async function runSync({
168
215
  uploadFn,
169
216
  log = (m) => process.stdout.write(`${m}\n`),
170
217
  status = (m) => process.stderr.write(`${m}\n`),
218
+ now = () => Date.now(),
171
219
  }) {
172
- const { accessToken, rootKey } = await authAndUnlock(cfg)
220
+ const session = await authAndUnlock(cfg)
221
+ const { rootKey } = session
222
+ const accessToken = createSessionTokens({
223
+ accessToken: session.accessToken,
224
+ refreshToken: session.refreshToken,
225
+ expiresAt: session.expiresAt,
226
+ refresh: session.refreshSession,
227
+ now,
228
+ })
173
229
  const manifestPath = join(folder, MANIFEST_NAME)
174
230
  const manifest = await loadManifest(manifestPath)
175
231
 
@@ -180,9 +236,10 @@ export async function runSync({
180
236
  }
181
237
  if (watch) process.on('SIGINT', onSigint)
182
238
 
239
+ let summary
183
240
  try {
184
241
  do {
185
- const summary = await syncOnce({
242
+ summary = await syncOnce({
186
243
  apiBaseUrl: cfg.apiBaseUrl,
187
244
  accessToken,
188
245
  rootKey,
@@ -202,4 +259,8 @@ export async function runSync({
202
259
  } finally {
203
260
  if (watch) process.off('SIGINT', onSigint)
204
261
  }
262
+ if (!watch && summary?.failed) {
263
+ throw new Error(`${summary.failed} file(s) failed to sync.`)
264
+ }
265
+ return summary
205
266
  }
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. */