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/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from 'advisers-core'
2
2
 
3
- export const version = '0.2.0'
3
+ export const version = '0.2.2'
4
4
  export const distribution = 'cordis'
5
5
  export const status = 'bootstrapping'
@@ -1,6 +1,6 @@
1
1
  import { readFile, stat } from 'node:fs/promises'
2
2
  import { existsSync, lstatSync, realpathSync } from 'node:fs'
3
- import { extname, isAbsolute, relative, resolve } from 'node:path'
3
+ import { basename, extname, isAbsolute, relative, resolve } from 'node:path'
4
4
  import JSZip from 'jszip'
5
5
  import { runLocalOcr, ocrConfigured } from './ocr.js'
6
6
  import { classify, safetyErrorFrom } from './safety/index.js'
@@ -244,7 +244,10 @@ export async function extractAttachment(input, { cwd = process.cwd(), hmacKey }
244
244
  if (!info.isFile()) throw new Error(`not a file: ${input}`)
245
245
  if (info.size > MAX_FILE_BYTES) throw new Error(`attachment is larger than 10 MB: ${input}`)
246
246
  const ext = extname(path).toLowerCase()
247
- const name = path.split('/').pop()
247
+ // path.basename is separator-aware; the old split('/') kept the whole path on
248
+ // Windows, leaking an absolute path into the attachment name (robustness
249
+ // audit P2-6, 2026-11-17).
250
+ const name = basename(path)
248
251
  if (TEXT_EXT.has(ext)) {
249
252
  const raw = await readFile(path, 'utf8')
250
253
  const { text, truncated } = truncate(raw)
package/lib/notify.js CHANGED
@@ -11,9 +11,35 @@ import { existsSync } from 'node:fs'
11
11
  import { spawn } from 'node:child_process'
12
12
  import { localDay } from './remind.js'
13
13
 
14
+ /**
15
+ * Environment for the notification subprocess: an explicit allowlist.
16
+ *
17
+ * The command is a local third-party program (custom notifier, osascript,
18
+ * notify-send, powershell). Inheriting process.env would hand it every Advisers
19
+ * secret — ADVISERS_API_KEY, ADVISERS_AUDIT_KEY (the audit HMAC key),
20
+ * vault/sync/export passphrases and provider URLs (security audit SEC2-03,
21
+ * 2026-11-17). Only OS/runtime basics needed to start a program are passed
22
+ * through; credentials never are.
23
+ */
24
+ const ADAPTER_ENV_KEYS = Object.freeze([
25
+ 'PATH', 'Path', 'PATHEXT', 'COMSPEC', 'SYSTEMROOT', 'SystemRoot', 'windir',
26
+ 'HOME', 'USERPROFILE', 'HOMEDRIVE', 'HOMEPATH', 'APPDATA', 'LOCALAPPDATA',
27
+ 'TEMP', 'TMP', 'TMPDIR', 'LANG', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES', 'TZ',
28
+ 'PSModulePath',
29
+ ])
30
+
31
+ function adapterEnv(source = process.env) {
32
+ const env = {}
33
+ for (const key of ADAPTER_ENV_KEYS) {
34
+ const value = source?.[key]
35
+ if (typeof value === 'string' && value) env[key] = value
36
+ }
37
+ return env
38
+ }
39
+
14
40
  function run(cmd, args, timeoutMs = 5000) {
15
41
  return new Promise((resolve, reject) => {
16
- const child = spawn(cmd, args, { stdio: ['ignore', 'ignore', 'pipe'], timeout: timeoutMs, windowsHide: true })
42
+ const child = spawn(cmd, args, { stdio: ['ignore', 'ignore', 'pipe'], timeout: timeoutMs, windowsHide: true, env: adapterEnv() })
17
43
  let err = ''
18
44
  child.stderr.on('data', (b) => { err += b.toString('utf8') })
19
45
  child.on('error', reject)
package/lib/ocr.js CHANGED
@@ -11,6 +11,31 @@ import { spawn } from 'node:child_process'
11
11
  import { statSync } from 'node:fs'
12
12
  import { resolve, isAbsolute, relative } from 'node:path'
13
13
 
14
+ /**
15
+ * Environment for the adapter subprocess: an explicit allowlist.
16
+ *
17
+ * The adapter is a local third-party program (e.g. an OCR engine). Inheriting
18
+ * process.env would hand it every Advisers secret — ADVISERS_API_KEY,
19
+ * ADVISERS_AUDIT_KEY (the audit HMAC key), vault/sync/export passphrases and
20
+ * provider URLs (security audit SEC2-03, 2026-11-17). Only OS/runtime basics
21
+ * needed to start a program are passed through; credentials never are.
22
+ */
23
+ const ADAPTER_ENV_KEYS = Object.freeze([
24
+ 'PATH', 'Path', 'PATHEXT', 'COMSPEC', 'SYSTEMROOT', 'SystemRoot', 'windir',
25
+ 'HOME', 'USERPROFILE', 'HOMEDRIVE', 'HOMEPATH', 'APPDATA', 'LOCALAPPDATA',
26
+ 'TEMP', 'TMP', 'TMPDIR', 'LANG', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES', 'TZ',
27
+ 'PSModulePath',
28
+ ])
29
+
30
+ function adapterEnv(source = process.env) {
31
+ const env = {}
32
+ for (const key of ADAPTER_ENV_KEYS) {
33
+ const value = source?.[key]
34
+ if (typeof value === 'string' && value) env[key] = value
35
+ }
36
+ return env
37
+ }
38
+
14
39
  export const MAX_OCR_BYTES = 200_000
15
40
  export const MAX_OCR_MS = 30_000
16
41
 
@@ -32,7 +57,7 @@ export async function runLocalOcr(file, { cmd = process.env.ADVISERS_OCR_CMD, ti
32
57
  if (!cmd) throw new Error('OCR is not configured; set ADVISERS_OCR_CMD to a local OCR executable (no cloud OCR is used)')
33
58
  const abs = assertOcrPath(file, cwd)
34
59
  return await new Promise((resolvePromise, reject) => {
35
- const child = spawn(cmd, [abs], { stdio: ['ignore', 'pipe', 'pipe'], timeout: timeoutMs, windowsHide: true })
60
+ const child = spawn(cmd, [abs], { stdio: ['ignore', 'pipe', 'pipe'], timeout: timeoutMs, windowsHide: true, env: adapterEnv() })
36
61
  let out = '', err = ''
37
62
  child.stdout.on('data', (b) => { out += b.toString('utf8'); if (out.length > MAX_OCR_BYTES) { child.kill(); reject(new Error(`OCR output exceeds ${MAX_OCR_BYTES} chars`)) } })
38
63
  child.stderr.on('data', (b) => { err += b.toString('utf8') })
package/lib/pdf.js CHANGED
@@ -81,6 +81,14 @@ export async function buildSimplePdf({ title = 'Advisers', paragraphs = [], font
81
81
  if (!fontPath) throw new Error('no CJK font found; set ADVISERS_PDF_FONT to a .ttf/.otf font (e.g. simhei.ttf)')
82
82
  const bytes = readFileSync(fontPath)
83
83
  const doc = await PDFDocument.create()
84
+ // PDFDocument.create() calls updateInfoDict(), which stamps /ModDate and
85
+ // /CreationDate with the wall clock. Two runs over identical input therefore
86
+ // produced different bytes whenever they crossed a wall-clock second (the PDF
87
+ // date format has second precision), which made the perf regression test and
88
+ // any byte-level receipt comparison flake. Pin both dates to the caller-
89
+ // supplied generation time so one input always serializes to the same bytes.
90
+ doc.setCreationDate(createdAt)
91
+ doc.setModificationDate(createdAt)
84
92
  doc.registerFontkit(fontkit)
85
93
  const font = await doc.embedFont(bytes, { subset: true })
86
94
  const size = 10.5
@@ -18,6 +18,12 @@
18
18
  * every checksum — that is what an external signature/anchor would add, and the
19
19
  * README must disclose when there is none.
20
20
  *
21
+ * Anchors (manifest.anchors) are NOT verified by this tool (security audit
22
+ * SEC2-05, 2026-11-17): only their shape and their file digest cross-check are
23
+ * validated, and the proof itself is opaque. A pack with recorded anchors is
24
+ * therefore reported as anchorsVerified:false / anchored:false with an explicit
25
+ * warning; the adapter named in each anchor must verify its proof out of band.
26
+ *
21
27
  * The verifier deliberately does not trust manifest.json before SHA256SUMS
22
28
  * matches: checksums are verified first, then the manifest schema is checked.
23
29
  */
@@ -391,7 +397,7 @@ function validateEvidence(evidence, errors) {
391
397
  }
392
398
  }
393
399
 
394
- function validateReceiptManifest(manifest, sums, errors) {
400
+ function validateReceiptManifest(manifest, sums, errors, result) {
395
401
  if (!isPlainObject(manifest)) {
396
402
  addError(errors, 'schema: manifest.json must be a JSON object')
397
403
  return
@@ -453,7 +459,15 @@ function validateReceiptManifest(manifest, sums, errors) {
453
459
  }
454
460
 
455
461
  if (!Array.isArray(manifest.anchors)) addError(errors, 'schema: manifest.anchors must be an array')
456
- else manifest.anchors.forEach((anchor, index) => validateAnchor(anchor, index, sums, errors))
462
+ else {
463
+ result.anchorsRecorded = manifest.anchors.length
464
+ manifest.anchors.forEach((anchor, index) => validateAnchor(anchor, index, sums, errors))
465
+ if (manifest.anchors.length > 0) {
466
+ // Never present a recorded anchor as verified: the proof was not checked
467
+ // against the named adapter (SEC2-05).
468
+ result.warnings.push('anchors are recorded but NOT cryptographically verified by this tool; verify each proof with the adapter that issued it and do not treat this pack as externally anchored')
469
+ }
470
+ }
457
471
 
458
472
  validateEvidence(manifest.evidence, errors)
459
473
 
@@ -477,7 +491,8 @@ function validateReceiptManifest(manifest, sums, errors) {
477
491
  * @param {string} dir pack directory produced by buildReceiptPack({outDir})
478
492
  * @returns {{ok: boolean, errors: string[], warnings: string[], dir: string,
479
493
  * fileCount: number, checksumCount: number, files: string[],
480
- * checksums: Record<string, string>, manifest: object|null}}
494
+ * checksums: Record<string, string>, manifest: object|null,
495
+ * anchorsRecorded: number, anchorsVerified: boolean, anchored: boolean}}
481
496
  */
482
497
  export function verifyReceiptPackDir(dir) {
483
498
  const errors = []
@@ -493,6 +508,11 @@ export function verifyReceiptPackDir(dir) {
493
508
  files,
494
509
  checksums: {},
495
510
  manifest: null,
511
+ // Anchors are recorded, never verified, by this offline tool (SEC2-05).
512
+ // `ok` covers pack integrity only; it must not be read as "anchored".
513
+ anchorsRecorded: 0,
514
+ anchorsVerified: false,
515
+ anchored: false,
496
516
  }
497
517
  let root
498
518
  try {
@@ -589,7 +609,7 @@ export function verifyReceiptPackDir(dir) {
589
609
  return result
590
610
  }
591
611
  result.manifest = manifest
592
- validateReceiptManifest(manifest, parsed.entries, errors)
612
+ validateReceiptManifest(manifest, parsed.entries, errors, result)
593
613
 
594
614
  // Size statements in the manifest must match the bytes on disk.
595
615
  if (isPlainObject(manifest)) {
package/lib/research.js CHANGED
@@ -147,11 +147,17 @@ export async function research({
147
147
  request.catch?.(() => {})
148
148
  const response = await Promise.race([request, deadline])
149
149
  if (!response.ok) {
150
- const raw = await response.text().catch(() => '')
150
+ // Never copy the upstream body into the error message (privacy audit
151
+ // P1-2, 2026-11-17). A provider 401 can echo the submitted API key and
152
+ // the body may contain control characters; both used to reach stderr,
153
+ // CI logs and screenshots through the uncaught error. Only stable
154
+ // fields (status + code) are kept. The body is discarded.
155
+ try { await response.body?.cancel?.() } catch {}
151
156
  const retryable = RETRY_STATUS.has(response.status)
152
- const error = new ResearchError(`research HTTP ${response.status}: ${raw.slice(0, 300)}`, {
157
+ const code = response.status === 401 || response.status === 403 ? 'AUTH' : response.status === 429 ? 'RATE_LIMITED' : response.status === 400 ? 'INVALID_REQUEST' : 'UPSTREAM'
158
+ const error = new ResearchError(`research HTTP ${response.status} (${code})`, {
153
159
  status: response.status,
154
- code: response.status === 401 || response.status === 403 ? 'AUTH' : response.status === 429 ? 'RATE_LIMITED' : response.status === 400 ? 'INVALID_REQUEST' : 'UPSTREAM',
160
+ code,
155
161
  retryable,
156
162
  })
157
163
  if (!retryable || attempt === retries) throw error
@@ -21,11 +21,18 @@
21
21
  * severity: 'none' | 'low' | 'medium' | 'high' | 'critical',
22
22
  * crisis: boolean, // self-harm: show crisis hint, never block
23
23
  * downgraded: boolean,
24
- * truncated: boolean,
24
+ * truncated: boolean, // true when only the first maxChars were scanned
25
25
  * hits: [{ ruleId, category, subtype, code, severity, action, confidence,
26
26
  * hardBlock, crisis, downgraded }]
27
27
  * }
28
28
  *
29
+ * Truncation fails CLOSED (content-safety audit SEC2-02, 2026-11-17): when the
30
+ * input is longer than maxChars the tail is unscannable, so a benign-looking
31
+ * prefix is never returned as 'allow'. Egress surfaces (text that can leave the
32
+ * machine, see SURFACES) return 'block' with code INPUT_TRUNCATED; local-file
33
+ * surfaces return 'confirm' and let the caller decide. The escalation only ever
34
+ * raises the action of an existing decision.
35
+ *
29
36
  * The caller owns the audit key: pass { hmacKey } to get an evidenceHash.
30
37
  * Without a key evidenceHash is null (never a plaintext or unkeyed fallback).
31
38
  */
@@ -237,14 +244,58 @@ function allowResult(views, surface, ruleset, truncated) {
237
244
  }
238
245
  }
239
246
 
247
+ /** The stable fail-closed decision for a truncated payload on an egress surface. */
248
+ function truncationBlockResult(surface, ruleset) {
249
+ return {
250
+ action: 'block',
251
+ categories: [],
252
+ ruleIds: [],
253
+ confidence: 0.5,
254
+ evidenceHash: null,
255
+ code: 'INPUT_TRUNCATED',
256
+ rulesetVersion: ruleset.version,
257
+ rulesetDigest: ruleset.digest,
258
+ surface,
259
+ severity: 'medium',
260
+ crisis: false,
261
+ downgraded: false,
262
+ truncated: true,
263
+ hits: [],
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Fail closed when the text was truncated: the unscanned tail could contain a
269
+ * hard-block rule hit, so the prefix result cannot be trusted as 'allow'.
270
+ * Egress surfaces refuse the payload outright (and skip the expensive prefix
271
+ * scan: the verdict does not depend on it); local-file surfaces keep scanning
272
+ * so a hard block in the scanned prefix still wins, then ask for confirmation.
273
+ * Escalation only raises an action, never lowers one.
274
+ */
275
+ function applyTruncationFailClosed(result, policy) {
276
+ if (!result.truncated) return result
277
+ const floor = policy.egress ? 'block' : 'confirm'
278
+ if (rankOf(result.action) >= rankOf(floor)) return result
279
+ return {
280
+ ...result,
281
+ action: floor,
282
+ code: 'INPUT_TRUNCATED',
283
+ severity: result.severity === 'none' ? 'medium' : result.severity,
284
+ confidence: Math.max(result.confidence, 0.5),
285
+ }
286
+ }
287
+
240
288
  function runClassify(ruleset, text, options = {}) {
241
289
  const surface = normalizeSurface(options.surface)
242
290
  const policy = SURFACES[surface] || SURFACES.unknown
243
291
  const maxChars = Number.isInteger(options.maxChars) && options.maxChars > 0 ? options.maxChars : MAX_SCAN_CHARS
244
292
  const raw = String(text == null ? '' : text)
245
293
  const truncated = raw.length > maxChars
294
+ // Egress + truncation can only end in block, so settle it before the rule
295
+ // scan (deterministic and bounded even for a multi-megabyte paste).
296
+ if (truncated && policy.egress) return truncationBlockResult(surface, ruleset)
246
297
  const views = normalizeText(truncated ? raw.slice(0, maxChars) : raw)
247
- if (!views.compact) return allowResult(views, surface, ruleset, truncated)
298
+ if (!views.compact) return applyTruncationFailClosed(allowResult(views, surface, ruleset, truncated), policy)
248
299
 
249
300
  const contextDowngrade = hasContextDowngrade(views)
250
301
  const hits = []
@@ -308,7 +359,7 @@ function runClassify(ruleset, text, options = {}) {
308
359
  : 0
309
360
  const evidenceHash = hmacEvidence(ordered.map((hit) => hit.snippet), options.hmacKey)
310
361
 
311
- return {
362
+ const result = {
312
363
  action,
313
364
  categories,
314
365
  ruleIds,
@@ -335,6 +386,7 @@ function runClassify(ruleset, text, options = {}) {
335
386
  downgraded: hit.downgraded,
336
387
  })),
337
388
  }
389
+ return applyTruncationFailClosed(result, policy)
338
390
  }
339
391
 
340
392
  /**