advisers 0.2.0 → 0.2.2

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/lib/sync.js CHANGED
@@ -11,7 +11,8 @@
11
11
  */
12
12
  import { chmodSync, closeSync, copyFileSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
13
13
  import { createHash } from 'node:crypto'
14
- import { basename, join } from 'node:path'
14
+ import { basename, dirname, join } from 'node:path'
15
+ import { DatabaseSync } from 'node:sqlite'
15
16
  import { decryptJson, encryptJson, isVaultEnvelope } from './vault.js'
16
17
 
17
18
  /** fsync a directory entry so a rename survives a crash. */
@@ -37,6 +38,45 @@ function writeAtomic(file, data, { mode = 0o600 } = {}) {
37
38
  fsyncDir(join(file, '..'))
38
39
  }
39
40
 
41
+ const PULL_TMP_PREFIX = '.advisers-pull-verify-'
42
+
43
+ /**
44
+ * Verify that a database file is a structurally valid SQLite database before it
45
+ * is allowed to replace the main database (robustness audit P1-4, 2026-11-17).
46
+ *
47
+ * The snapshot SHA-256 only proves the envelope payload is self-consistent; a
48
+ * byte-corrupted payload is still "consistent" after re-encryption and used to
49
+ * be installed as the live library. PRAGMA quick_check is O(file) and catches
50
+ * corrupt b-trees/pages. The original file is only opened read-only.
51
+ */
52
+ function assertDatabaseIntegrity(file) {
53
+ let db
54
+ try {
55
+ // A 0-byte file is a valid "empty database" to SQLite (quick_check says ok),
56
+ // so a truncated snapshot used to replace the live library silently. Treat it
57
+ // as corrupt (robustness audit P2-2 / P1-4) and keep the local database.
58
+ if (statSync(file).size === 0) throw new Error('E_DB_CORRUPT: the snapshot database is 0 bytes (truncated)')
59
+ db = new DatabaseSync(file, { readOnly: true })
60
+ const row = db.prepare('PRAGMA quick_check').get()
61
+ const verdict = String(row?.quick_check ?? 'unknown').slice(0, 120)
62
+ if (verdict !== 'ok') throw new Error(`quick_check reported ${verdict}`)
63
+ } catch (error) {
64
+ throw new Error(`snapshot database failed the integrity check: ${String(error?.message ?? error).slice(0, 200)}`)
65
+ } finally {
66
+ try { db?.close() } catch {}
67
+ }
68
+ }
69
+
70
+ /** Put the pre-pull database (and its WAL/SHM siblings) back after a failed replace. */
71
+ function restoreBackup(backup, dbPath) {
72
+ copyFileSync(backup, dbPath)
73
+ for (const suffix of ['-wal', '-shm']) {
74
+ if (existsSync(backup + suffix)) copyFileSync(backup + suffix, dbPath + suffix)
75
+ else { try { rmSync(dbPath + suffix) } catch {} }
76
+ }
77
+ try { chmodSync(dbPath, 0o600) } catch {}
78
+ }
79
+
40
80
  export const SYNC_PREFIX = 'advisers-'
41
81
  export const SYNC_SUFFIX = '.enc.json'
42
82
 
@@ -113,17 +153,53 @@ export function pullSnapshot({ dir, dbPath, passphrase, file, force = false, loc
113
153
  if (localAt > snapshotAt) throw new Error(`local database was written after snapshot ${chosen.name} (local ${new Date(localAt).toISOString()} > snapshot ${new Date(snapshotAt).toISOString()}); refusing to overwrite. If those local writes matter, back them up/export them first; re-run with --force to overwrite anyway (a backup is kept).`)
114
154
  }
115
155
  }
156
+ const parent = dirname(dbPath)
157
+ mkdirSync(parent, { recursive: true, mode: 0o700 })
158
+ // Stage the snapshot bytes next to the live database and verify them BEFORE
159
+ // anything replaces the library (robustness audit P1-4). The backup copy is
160
+ // taken first and kept on every failure path; the live WAL/SHM are only
161
+ // removed once the staged database has passed the integrity check.
162
+ const tmp = join(parent, `${PULL_TMP_PREFIX}${process.pid}-${Math.random().toString(36).slice(2, 8)}`)
116
163
  let backup = null
117
- if (existsSync(dbPath)) {
118
- backup = `${dbPath}.before-sync-${Date.now()}`
119
- copyFileSync(dbPath, backup)
120
- for (const suffix of ['-wal', '-shm']) {
121
- try { if (existsSync(dbPath + suffix)) copyFileSync(dbPath + suffix, backup + suffix) } catch {}
164
+ try {
165
+ writeFileSync(tmp, bytes, { mode: 0o600, flush: true })
166
+ const hadLocal = existsSync(dbPath)
167
+ if (hadLocal) {
168
+ backup = `${dbPath}.before-sync-${Date.now()}`
169
+ copyFileSync(dbPath, backup)
170
+ for (const suffix of ['-wal', '-shm']) {
171
+ try { if (existsSync(dbPath + suffix)) copyFileSync(dbPath + suffix, backup + suffix) } catch {}
172
+ }
173
+ }
174
+ try {
175
+ assertDatabaseIntegrity(tmp)
176
+ } catch (error) {
177
+ throw new Error(`refusing to replace the local database: ${error.message}`)
122
178
  }
123
179
  for (const suffix of ['-wal', '-shm']) { try { rmSync(dbPath + suffix) } catch {} }
124
- } else {
125
- mkdirSync(join(dbPath, '..'), { recursive: true, mode: 0o700 })
180
+ try {
181
+ // Rename the verified bytes into place (same directory: atomic).
182
+ renameSync(tmp, dbPath)
183
+ try { chmodSync(dbPath, 0o600) } catch {}
184
+ fsyncDir(parent)
185
+ } catch (error) {
186
+ // A failure after the rename must never leave a half-installed library:
187
+ // restore the pre-pull database and keep the backup for inspection.
188
+ let restored = false
189
+ try {
190
+ if (hadLocal && backup) {
191
+ restoreBackup(backup, dbPath)
192
+ restored = true
193
+ } else {
194
+ rmSync(dbPath, { force: true })
195
+ restored = true
196
+ }
197
+ } catch {}
198
+ const hint = restored ? 'the previous local database was restored' : 'restore the backup manually from ' + String(backup)
199
+ throw new Error(`failed to install the verified snapshot; ${hint}: ${error.message}`)
200
+ }
201
+ return { file: chosen.name, sha256, changed: true, backup, device: payload.device, createdAt: payload.createdAt }
202
+ } finally {
203
+ try { if (existsSync(tmp)) rmSync(tmp) } catch {}
126
204
  }
127
- writeAtomic(dbPath, bytes, { mode: 0o600 })
128
- return { file: chosen.name, sha256, changed: true, backup, device: payload.device, createdAt: payload.createdAt }
129
205
  }
package/lib/timestamp.js CHANGED
@@ -10,6 +10,31 @@ import { spawn } from 'node:child_process'
10
10
  import { statSync } from 'node:fs'
11
11
  import { isAbsolute, relative, resolve } from 'node:path'
12
12
 
13
+ /**
14
+ * Environment for the adapter subprocess: an explicit allowlist.
15
+ *
16
+ * The adapter is a local third-party program (RFC3161 client / signer).
17
+ * Inheriting process.env would hand it every Advisers secret — ADVISERS_API_KEY,
18
+ * ADVISERS_AUDIT_KEY (the audit HMAC key), vault/sync/export passphrases and
19
+ * provider URLs (security audit SEC2-03, 2026-11-17). Only OS/runtime basics
20
+ * needed to start a program are passed through; credentials never are.
21
+ */
22
+ const ADAPTER_ENV_KEYS = Object.freeze([
23
+ 'PATH', 'Path', 'PATHEXT', 'COMSPEC', 'SYSTEMROOT', 'SystemRoot', 'windir',
24
+ 'HOME', 'USERPROFILE', 'HOMEDRIVE', 'HOMEPATH', 'APPDATA', 'LOCALAPPDATA',
25
+ 'TEMP', 'TMP', 'TMPDIR', 'LANG', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES', 'TZ',
26
+ 'PSModulePath',
27
+ ])
28
+
29
+ function adapterEnv(source = process.env) {
30
+ const env = {}
31
+ for (const key of ADAPTER_ENV_KEYS) {
32
+ const value = source?.[key]
33
+ if (typeof value === 'string' && value) env[key] = value
34
+ }
35
+ return env
36
+ }
37
+
13
38
  export const MAX_PROOF_BYTES = 200_000
14
39
 
15
40
  export function timestampConfigured() {
@@ -30,7 +55,7 @@ export async function runLocalTimestamp(file, { cmd = process.env.ADVISERS_TIMES
30
55
  if (!cmd) throw new Error('timestamp is not configured; set ADVISERS_TIMESTAMP_CMD to a local timestamp/signing executable')
31
56
  const abs = assertProofFile(file, cwd)
32
57
  return await new Promise((resolvePromise, reject) => {
33
- const child = spawn(cmd, [abs], { stdio: ['ignore', 'pipe', 'pipe'], timeout: timeoutMs, windowsHide: true })
58
+ const child = spawn(cmd, [abs], { stdio: ['ignore', 'pipe', 'pipe'], timeout: timeoutMs, windowsHide: true, env: adapterEnv() })
34
59
  let out = '', err = ''
35
60
  child.stdout.on('data', (b) => { out += b.toString('utf8'); if (out.length > MAX_PROOF_BYTES) { child.kill(); reject(new Error(`timestamp proof exceeds ${MAX_PROOF_BYTES} bytes`)) } })
36
61
  child.stderr.on('data', (b) => { err += b.toString('utf8') })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "advisers",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "A staff of AI advisers: a cordis-native personal secretary runtime with scoped memory, commitments, and reviewable routines.",
5
5
  "keywords": [
6
6
  "cordis",
@@ -20,7 +20,8 @@
20
20
  },
21
21
  "exports": {
22
22
  ".": "./index.js",
23
- "./package.json": "./package.json"
23
+ "./package.json": "./package.json",
24
+ "./lib/safety/index.js": "./lib/safety/index.js"
24
25
  },
25
26
  "files": [
26
27
  "index.js",
@@ -34,8 +35,8 @@
34
35
  ],
35
36
  "dependencies": {
36
37
  "@pdf-lib/fontkit": "^1.1.1",
37
- "advisers-core": "0.2.0",
38
- "advisers-policy-strict": "0.2.0",
38
+ "advisers-core": "0.2.2",
39
+ "advisers-policy-strict": "0.2.2",
39
40
  "jszip": "^3.10.2",
40
41
  "pdf-lib": "^1.17.1",
41
42
  "pdfjs-dist": "^6.5.438"
@@ -53,13 +54,13 @@
53
54
  "access": "public"
54
55
  },
55
56
  "engines": {
56
- "node": ">=22.13"
57
+ "node": ">=22.16"
57
58
  },
58
59
  "scripts": {
59
60
  "test": "node --experimental-strip-types --test test/*.test.js"
60
61
  },
61
62
  "peerDependencies": {
62
- "advisers-office": "0.2.0"
63
+ "advisers-office": "0.2.2"
63
64
  },
64
65
  "peerDependenciesMeta": {
65
66
  "advisers-office": {