advisers 0.2.3 → 0.3.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.
Files changed (70) hide show
  1. package/LICENSE +0 -0
  2. package/NOTICE +0 -0
  3. package/README-zh.md +10 -6
  4. package/README.md +15 -6
  5. package/THIRD-PARTY-NOTICES.md +1 -1
  6. package/bin/advisers.js +3323 -222
  7. package/index.js +1 -1
  8. package/lib/agent.js +0 -0
  9. package/lib/attachments.js +0 -0
  10. package/lib/audit-anchor.js +683 -0
  11. package/lib/backup.js +1345 -0
  12. package/lib/capture.js +0 -0
  13. package/lib/context-window.js +0 -0
  14. package/lib/digest.js +0 -0
  15. package/lib/doc-review.js +0 -0
  16. package/lib/egress.js +0 -0
  17. package/lib/error-codes.js +1373 -0
  18. package/lib/evidence.js +202 -26
  19. package/lib/fs-safe.js +70 -7
  20. package/lib/ical.js +0 -0
  21. package/lib/imap.js +6 -2
  22. package/lib/importers.js +0 -0
  23. package/lib/learning.js +0 -0
  24. package/lib/ledger-read.js +608 -0
  25. package/lib/mcp-server.js +294 -38
  26. package/lib/model.js +0 -0
  27. package/lib/node-warning-filter.js +15 -0
  28. package/lib/notify.js +13 -3
  29. package/lib/nudge.js +0 -0
  30. package/lib/ocr.js +0 -0
  31. package/lib/pdf.js +0 -0
  32. package/lib/pet-brief-store.js +0 -0
  33. package/lib/pet-brief.js +102 -39
  34. package/lib/receipt-verify.js +213 -15
  35. package/lib/relative-date.js +0 -0
  36. package/lib/remind.js +91 -36
  37. package/lib/research.js +0 -0
  38. package/lib/review.js +0 -0
  39. package/lib/routine.js +0 -0
  40. package/lib/safety/classifier.js +0 -0
  41. package/lib/safety/explain.js +0 -0
  42. package/lib/safety/index.js +0 -0
  43. package/lib/safety/redact.js +0 -0
  44. package/lib/safety/rules.js +0 -0
  45. package/lib/scheduler.js +0 -0
  46. package/lib/source.js +0 -0
  47. package/lib/sqlite.js +1214 -24
  48. package/lib/sync.js +0 -0
  49. package/lib/text-safe.js +229 -0
  50. package/lib/timestamp.js +0 -0
  51. package/lib/tos/index.js +1 -1
  52. package/lib/tos/manifest.json +6 -6
  53. package/lib/tos/privacy.zh-CN.md +12 -9
  54. package/lib/tos/safety.zh-CN.md +4 -3
  55. package/lib/tos/summary.zh-CN.md +3 -3
  56. package/lib/tos/terms.zh-CN.md +8 -6
  57. package/lib/tui/color.js +164 -0
  58. package/lib/tui/index.js +33 -0
  59. package/lib/tui/json.js +124 -0
  60. package/lib/tui/labels.js +50 -0
  61. package/lib/tui/policy.js +108 -0
  62. package/lib/tui/render.js +371 -0
  63. package/lib/tui/sanitize.js +142 -0
  64. package/lib/tui/text-file.js +116 -0
  65. package/lib/tui/width.js +577 -0
  66. package/lib/ui.js +0 -0
  67. package/lib/undo.js +0 -0
  68. package/lib/untrusted.js +0 -0
  69. package/lib/vault.js +0 -0
  70. package/package.json +6 -5
package/lib/pet-brief.js CHANGED
@@ -112,7 +112,13 @@ function looksLikePersonName(value) {
112
112
  * titles/actions. `masked` is a count of replacements (for `redactions`).
113
113
  */
114
114
  function createRedactor(rawNames) {
115
- const names = [...new Set((rawNames ?? []).map(oneLine).filter((name) => looksLikePersonName(name)))]
115
+ // 0.3.0 P0-T2: dedupe the raw candidate strings before oneLine/regex work.
116
+ // The caller passes every commitment creditor + every listFacts() subject
117
+ // (233k strings at a 200k ledger, a few hundred distinct names); the old
118
+ // order ran oneLine + looksLikePersonName on all 233k rows. Deduping first
119
+ // is output-equivalent (the final Set still collapses oneLine collisions)
120
+ // and cuts the redactor setup by ~10x.
121
+ const names = [...new Set([...new Set(rawNames ?? [])].map(oneLine).filter((name) => looksLikePersonName(name)))]
116
122
  .sort((a, b) => b.length - a.length || cmp(a, b))
117
123
  let masked = 0
118
124
  const redact = (value) => {
@@ -143,12 +149,16 @@ function isPublicMatter(matter) {
143
149
  }
144
150
 
145
151
  function confidenceOfCommitment(commitment) {
152
+ // 0.3.0 P0-T2: a narrow projection answers "has evidence" in SQL so the
153
+ // pet-brief read path does not ship 66k evidence JSON strings to JS.
154
+ // Ledger rows (no hasEvidence field) keep the original array check.
155
+ if (commitment.hasEvidence !== undefined) return commitment.hasEvidence ? 0.95 : 0.75
146
156
  return Array.isArray(commitment.evidence) && commitment.evidence.length > 0 ? 0.95 : 0.75
147
157
  }
148
158
 
149
- function riskOfDue(dueMs, nowMs, today) {
159
+ function riskOfDue(dueMs, nowMs, today, dueDay) {
150
160
  if (dueMs < nowMs) return 'high'
151
- const day = localDay(new Date(dueMs).toISOString())
161
+ const day = dueDay ?? localDay(new Date(dueMs).toISOString())
152
162
  if (day === today) return 'high'
153
163
  if (dueMs <= nowMs + 3 * 86_400_000) return 'medium'
154
164
  return 'low'
@@ -166,6 +176,10 @@ const RISK_RANK = { low: 1, medium: 2, high: 3 }
166
176
  * @param {number} [options.maxItems]
167
177
  * @param {number} [options.minConfidence]
168
178
  * @param {'public'|'private'|'none'} [options.displayPolicy='private'] who may see the brief
179
+ * @param {string[]} [options.nameCandidates] Complete redactor name candidate set
180
+ * (all commitment creditors + all listFacts() subjects). Read paths that pass
181
+ * a narrow projection supply it so the redactor does not have to scan the
182
+ * full ledger; when omitted the ledger-derived set is used (unchanged).
169
183
  * @returns {object} contract v0.2 brief (no writes, no network).
170
184
  */
171
185
  export function buildPetBrief(ledger, options = {}) {
@@ -194,11 +208,18 @@ export function buildPetBrief(ledger, options = {}) {
194
208
 
195
209
  const allCommitments = ledger.listCommitments() ?? []
196
210
  const openCommitments = allCommitments.filter((commitment) => commitment.status === 'open')
197
- const rawNames = [
198
- ...allCommitments.map((commitment) => commitment.creditor),
199
- ...(ledger.listFacts() ?? []).map((fact) => fact.subject),
200
- ...allMatters.flatMap((matter) => matter.participants ?? []),
201
- ]
211
+ // 0.3.0 P0-T2: the redactor only needs strings that can match brief output.
212
+ // A caller that hands in a narrow projection supplies the complete candidate
213
+ // set (every commitment creditor + every listFacts() subject) so this view
214
+ // never scans the full ledger for names (233k strings at a 200k ledger).
215
+ const suppliedNames = Array.isArray(options.nameCandidates) ? options.nameCandidates : null
216
+ const rawNames = suppliedNames
217
+ ? [...suppliedNames, ...allMatters.flatMap((matter) => matter.participants ?? [])]
218
+ : [
219
+ ...allCommitments.map((commitment) => commitment.creditor),
220
+ ...(ledger.listFacts() ?? []).map((fact) => fact.subject),
221
+ ...allMatters.flatMap((matter) => matter.participants ?? []),
222
+ ]
202
223
  const redactor = createRedactor(rawNames)
203
224
  const redacted = (value) => redactor.redact(value)
204
225
 
@@ -216,20 +237,51 @@ export function buildPetBrief(ledger, options = {}) {
216
237
  else if (matterSecrecy(matter) >= 2) excludedOpen.secrecy += 1
217
238
  else excludedOpen.inactive += 1
218
239
  }
219
- const dueMsOf = (commitment) => {
220
- if (!commitment.dueAt) return null
221
- const ms = Date.parse(commitment.dueAt)
222
- return Number.isFinite(ms) ? ms : null
240
+ // 0.3.0 P0-T2: parse each distinct dueAt string once (a 200k ledger has a
241
+ // few hundred distinct due dates, not ~67k), and keep only the top `limit`
242
+ // due commitments under the historical comparator instead of sorting all
243
+ // of them -- the caller only ever reads that slice.
244
+ const dueCache = new Map()
245
+ const dueInfoOf = (commitment) => {
246
+ const value = commitment.dueAt
247
+ if (!value) return null
248
+ const key = typeof value === 'string' ? value : String(value)
249
+ let info = dueCache.get(key)
250
+ if (info === undefined) {
251
+ const ms = Date.parse(value)
252
+ info = Number.isFinite(ms) ? { ms, day: localDay(new Date(ms).toISOString()) } : null
253
+ dueCache.set(key, info)
254
+ }
255
+ return info
223
256
  }
257
+ // Comparator of an already-kept entry against a new candidate, matching the
258
+ // historical sort: risk rank desc, dueMs asc, id asc.
259
+ const entryBeforeCandidate = (entry, risk, dueMs, id) =>
260
+ RISK_RANK[risk] - RISK_RANK[entry.risk] || entry.dueMs - dueMs || cmp(entry.commitment.id, id)
224
261
 
225
262
  // --- today: active public matters, urgency first, deterministic tie-break.
226
263
  const urgency = new Map()
264
+ const dueTop = []
227
265
  for (const commitment of publicOpen) {
228
- const ms = dueMsOf(commitment)
229
- if (ms === null) continue
230
- const day = localDay(new Date(ms).toISOString())
231
- if (day !== today && ms >= nowMs) continue
232
- urgency.set(commitment.matterId, (urgency.get(commitment.matterId) ?? 0) + 1)
266
+ const info = dueInfoOf(commitment)
267
+ if (info === null) continue
268
+ if (info.day === today || info.ms < nowMs) urgency.set(commitment.matterId, (urgency.get(commitment.matterId) ?? 0) + 1)
269
+ const risk = riskOfDue(info.ms, nowMs, today, info.day)
270
+ // Bounded insertion keeps the same stable order as the old sort: equal
271
+ // keys keep ledger order, only `limit` rows are ever held, and the
272
+ // confidence/evidence check runs only for rows that enter the top-N.
273
+ let index = dueTop.length
274
+ while (index > 0 && entryBeforeCandidate(dueTop[index - 1], risk, info.ms, commitment.id) > 0) index -= 1
275
+ if (index < limit) {
276
+ dueTop.splice(index, 0, {
277
+ commitment,
278
+ dueMs: info.ms,
279
+ dueDay: info.day,
280
+ risk,
281
+ confidence: confidenceOfCommitment(commitment),
282
+ })
283
+ if (dueTop.length > limit) dueTop.pop()
284
+ }
233
285
  }
234
286
  const todayItems = [...activeMatters]
235
287
  .sort((a, b) => (urgency.get(b.id) ?? 0) - (urgency.get(a.id) ?? 0) || cmp(String(b.updatedAt ?? ''), String(a.updatedAt ?? '')) || cmp(a.id, b.id))
@@ -241,25 +293,19 @@ export function buildPetBrief(ledger, options = {}) {
241
293
  }))
242
294
 
243
295
  // --- commitments: open, due-dated, public; risk ordered then due date.
244
- const dueCommitments = publicOpen
245
- .map((commitment) => ({ commitment, dueMs: dueMsOf(commitment) }))
246
- .filter((row) => row.dueMs !== null)
247
- .map((row) => ({ ...row, risk: riskOfDue(row.dueMs, nowMs, today), confidence: confidenceOfCommitment(row.commitment) }))
248
- .sort((a, b) => RISK_RANK[b.risk] - RISK_RANK[a.risk] || a.dueMs - b.dueMs || cmp(a.commitment.id, b.commitment.id))
249
- .slice(0, limit)
250
- const commitmentItems = dueCommitments.map(({ commitment, dueMs, risk, confidence }) => ({
296
+ const commitmentItems = dueTop.map(({ commitment, dueDay, risk, confidence }) => ({
251
297
  id: clip(commitment.id, 64),
252
298
  matter: clip(commitment.matterId, 64),
253
299
  who: clip(redacted(commitment.creditor), 40),
254
300
  what: clip(redacted(commitment.action), 80),
255
- due: clip(localDay(new Date(dueMs).toISOString()) ?? clip(commitment.dueAt, 32), 32),
301
+ due: clip(dueDay ?? clip(commitment.dueAt, 32), 32),
256
302
  risk,
257
303
  confidence,
258
304
  }))
259
305
 
260
306
  // --- risks: deterministic derivation from the same public commitments.
261
307
  const risks = []
262
- for (const { commitment, dueMs, risk } of dueCommitments) {
308
+ for (const { commitment, dueMs, risk } of dueTop) {
263
309
  const who = clip(redacted(commitment.creditor), 12)
264
310
  const what = clip(redacted(commitment.action), 24)
265
311
  if (risk === 'high') {
@@ -281,20 +327,38 @@ export function buildPetBrief(ledger, options = {}) {
281
327
  })
282
328
  }
283
329
  }
284
- for (const commitment of publicOpen.filter((row) => !row.dueAt && isPublicMatter(matterById.get(row.matterId)))) {
330
+ // Undated public commitments each produce a medium risk. Only `limit` can
331
+ // ever be shown, so the loop keeps the smallest ids (the historical
332
+ // tie-break) instead of building and sorting one object per row; the
333
+ // per-summary safety check still runs for every candidate so the dropped
334
+ // count stays exact.
335
+ const noDueRisks = []
336
+ for (const commitment of publicOpen) {
337
+ if (commitment.dueAt) continue
338
+ if (!isPublicMatter(matterById.get(commitment.matterId))) continue
285
339
  const who = clip(redacted(commitment.creditor), 12)
286
340
  const what = clip(redacted(commitment.action), 24)
287
- risks.push({
288
- id: clip(`risk_${commitment.id}`, 64),
341
+ // The redaction runs before the confidence filter so the reported
342
+ // "masked N names" count matches the pre-0.3.0 whole-list behaviour.
343
+ const summary = clip(`${who}的${what}还没有截止时间`, 80)
344
+ if (0.75 < minConfidence) continue
345
+ if (isBlockedLine(summary, hmacKey)) { dropped.risks += 1; continue }
346
+ const id = clip(`risk_${commitment.id}`, 64)
347
+ let index = noDueRisks.length
348
+ while (index > 0 && cmp(id, noDueRisks[index - 1].id) < 0) index -= 1
349
+ if (index >= limit) continue
350
+ noDueRisks.splice(index, 0, {
351
+ id,
289
352
  level: 'medium',
290
- summary: clip(`${who}的${what}还没有截止时间`, 80),
353
+ summary,
291
354
  evidence: clip(commitment.id, 64),
292
355
  confidence: 0.75,
293
356
  })
357
+ if (noDueRisks.length > limit) noDueRisks.pop()
294
358
  }
295
359
  const byDay = new Map()
296
- for (const { commitment, dueMs } of dueCommitments) {
297
- const day = localDay(new Date(dueMs).toISOString())
360
+ for (const { commitment, dueDay } of dueTop) {
361
+ const day = dueDay
298
362
  if (!day || day < today) continue
299
363
  byDay.set(day, [...(byDay.get(day) ?? []), commitment])
300
364
  }
@@ -308,13 +372,12 @@ export function buildPetBrief(ledger, options = {}) {
308
372
  confidence: 0.72,
309
373
  })
310
374
  }
311
- const riskItems = risks
312
- .filter((risk) => risk.confidence >= minConfidence)
313
- .filter((risk) => {
314
- if (!isBlockedLine(risk.summary, hmacKey)) return true
315
- dropped.risks += 1
316
- return false
317
- })
375
+ for (const risk of risks) {
376
+ if (risk.confidence < minConfidence) continue
377
+ if (isBlockedLine(risk.summary, hmacKey)) { dropped.risks += 1; continue }
378
+ noDueRisks.push(risk)
379
+ }
380
+ const riskItems = noDueRisks
318
381
  .sort((a, b) => RISK_RANK[b.level] - RISK_RANK[a.level] || b.confidence - a.confidence || cmp(a.id, b.id))
319
382
  .slice(0, limit)
320
383
 
@@ -26,9 +26,18 @@
26
26
  *
27
27
  * The verifier deliberately does not trust manifest.json before SHA256SUMS
28
28
  * matches: checksums are verified first, then the manifest schema is checked.
29
+ *
30
+ * Resource limits (0.3.0, 04-0.3-research §4.12): a receipt pack arrives from a
31
+ * counterparty, so every read is capped -- entries, directory depth, single-file
32
+ * size, total bytes, SHA256SUMS size and manifest size (RECEIPT_LIMIT_DEFAULTS).
33
+ * Hits are reported with the stable code E_RECEIPT_LIMIT plus structured
34
+ * metadata {limit, max, actual, file}; files are hashed in 1 MiB chunks instead
35
+ * of being loaded whole, and receipt.docx is scanned in chunks as well. Normal
36
+ * v1/v1.1 packs are far below every limit and are unaffected.
29
37
  */
30
38
 
31
- import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
39
+ import { createHash } from 'node:crypto'
40
+ import { closeSync, existsSync, openSync, readFileSync, readdirSync, readSync, statSync } from 'node:fs'
32
41
  import { join, resolve } from 'node:path'
33
42
  import {
34
43
  MAX_QUOTE_CHARS,
@@ -45,6 +54,56 @@ const MAX_ERRORS = 300
45
54
  const SUMS_LINE_RE = /^([0-9a-f]{64}) (.+)$/
46
55
  const ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/
47
56
 
57
+ /** Stable error code for a receipt pack that exceeds a resource limit. */
58
+ export const RECEIPT_LIMIT_CODE = 'E_RECEIPT_LIMIT'
59
+ /**
60
+ * Resource limits for verifying an UNTRUSTED receipt pack (04-0.3-research
61
+ * §4.12). A receipt pack arrives from a counterparty: without caps, a 100k-file
62
+ * directory, a 100-level path nest, a multi-GiB file or a huge SHA256SUMS could
63
+ * exhaust memory/time before any integrity result is produced. Limits apply to
64
+ * both the directory walk and every read/hash, so a file growing during the
65
+ * check cannot bypass the stat-time cap. Normal packs (5 required files + a
66
+ * handful of deliveries) are orders of magnitude below every limit.
67
+ */
68
+ export const RECEIPT_LIMIT_DEFAULTS = Object.freeze({
69
+ maxEntries: 5000,
70
+ maxDepth: 8,
71
+ maxFileBytes: 32 * 1024 * 1024,
72
+ maxTotalBytes: 256 * 1024 * 1024,
73
+ maxSumsBytes: 64 * 1024,
74
+ maxManifestBytes: 1024 * 1024,
75
+ })
76
+ const LIMIT_LABELS = Object.freeze({
77
+ maxEntries: 'entry count',
78
+ maxDepth: 'directory depth',
79
+ maxFileBytes: 'single file size',
80
+ maxTotalBytes: 'total pack size',
81
+ maxSumsBytes: 'SHA256SUMS size',
82
+ maxManifestBytes: 'manifest size',
83
+ })
84
+
85
+ function normalizeLimits(input) {
86
+ const limits = { ...RECEIPT_LIMIT_DEFAULTS }
87
+ if (!input || typeof input !== 'object') return limits
88
+ for (const key of Object.keys(RECEIPT_LIMIT_DEFAULTS)) {
89
+ if (!(key in input)) continue
90
+ const value = Number(input[key])
91
+ if (Number.isFinite(value) && value > 0) limits[key] = Math.floor(value)
92
+ }
93
+ return limits
94
+ }
95
+
96
+ function limitMessage(violation) {
97
+ const label = LIMIT_LABELS[violation.limit] ?? violation.limit
98
+ return RECEIPT_LIMIT_CODE + ': receipt pack ' + label + ' exceeded the limit ' + violation.max +
99
+ ' (actual ' + violation.actual + (violation.file ? ', first at ' + violation.file : '') + '); refusing to read a pack that could exhaust memory or time. ' +
100
+ 'A legitimate pack is far below these limits; inspect the pack before retrying.'
101
+ }
102
+
103
+ function makeViolation(limits, limit, actual, file) {
104
+ return { code: RECEIPT_LIMIT_CODE, limit, max: limits[limit], actual, file: file ?? null }
105
+ }
106
+
48
107
  function addError(errors, message) {
49
108
  if (errors.length < MAX_ERRORS) errors.push(message)
50
109
  else if (errors.length === MAX_ERRORS) errors.push('... too many errors; further details suppressed')
@@ -112,12 +171,25 @@ export function parseReceiptSums(text) {
112
171
  return { entries, errors }
113
172
  }
114
173
 
115
- function walkPackFiles(root) {
174
+ /**
175
+ * Walk the pack directory with hard caps on entries, depth, per-file size and
176
+ * total size. Stops at the first violation so a hostile pack (100k entries, a
177
+ * deep nest, huge files) is rejected in roughly O(limit) work instead of O(pack).
178
+ * Returns the violation as a structured object for stable CLI/JSON reporting.
179
+ */
180
+ function walkPackFiles(root, limits) {
116
181
  const files = []
117
182
  const errors = []
118
- const stack = ['']
119
- while (stack.length) {
120
- const relativeDir = stack.pop()
183
+ const stack = [{ relativeDir: '', depth: 0 }]
184
+ let entryCount = 0
185
+ let totalBytes = 0
186
+ let violation = null
187
+ const trip = (limit, actual, file) => {
188
+ violation = makeViolation(limits, limit, actual, file)
189
+ addError(errors, limitMessage(violation))
190
+ }
191
+ while (stack.length && !violation) {
192
+ const { relativeDir, depth } = stack.pop()
121
193
  const directory = relativeDir ? join(root, ...relativeDir.split('/')) : root
122
194
  let dirents
123
195
  try {
@@ -127,24 +199,97 @@ function walkPackFiles(root) {
127
199
  continue
128
200
  }
129
201
  for (const dirent of dirents) {
202
+ entryCount += 1
130
203
  const relativePath = relativeDir ? relativeDir + '/' + dirent.name : dirent.name
204
+ if (entryCount > limits.maxEntries) { trip('maxEntries', entryCount, relativePath); break }
131
205
  if (dirent.isSymbolicLink()) {
132
206
  addError(errors, 'symbolic links are not allowed in a receipt pack: ' + relativePath)
133
207
  continue
134
208
  }
135
209
  if (dirent.isDirectory()) {
136
- stack.push(relativePath)
210
+ const childDepth = depth + 1
211
+ if (childDepth > limits.maxDepth) { trip('maxDepth', childDepth, relativePath); break }
212
+ stack.push({ relativeDir: relativePath, depth: childDepth })
137
213
  continue
138
214
  }
139
215
  if (!dirent.isFile()) {
140
216
  addError(errors, 'unsupported file type in receipt pack: ' + relativePath)
141
217
  continue
142
218
  }
219
+ let info
220
+ try {
221
+ info = statSync(join(root, ...relativePath.split('/')))
222
+ } catch (error) {
223
+ addError(errors, 'cannot stat ' + relativePath + ': ' + (error.code || error.message))
224
+ continue
225
+ }
226
+ if (info.size > limits.maxFileBytes) { trip('maxFileBytes', info.size, relativePath); break }
227
+ totalBytes += info.size
228
+ if (totalBytes > limits.maxTotalBytes) { trip('maxTotalBytes', totalBytes, relativePath); break }
143
229
  files.push(relativePath)
144
230
  }
145
231
  }
146
- files.sort()
147
- return { files, errors }
232
+ if (!violation) files.sort()
233
+ return { files, errors, violation, totalBytes, entryCount }
234
+ }
235
+
236
+ /**
237
+ * Streaming sha256 with a hard byte cap (1 MiB chunks, bounded memory).
238
+ * Returns {sha256, bytes} or {violation} when the file grew past maxBytes
239
+ * after the stat-time check (a file can be swapped/grown between stat and read).
240
+ */
241
+ function hashFileWithLimit(file, limits, { chunkSize = 1024 * 1024 } = {}) {
242
+ const hash = createHash('sha256')
243
+ const buffer = Buffer.allocUnsafe(Math.min(chunkSize, 1024 * 1024))
244
+ let bytes = 0
245
+ const fd = openSync(file, 'r')
246
+ try {
247
+ for (;;) {
248
+ const count = readSync(fd, buffer, 0, buffer.length, null)
249
+ if (!count) break
250
+ bytes += count
251
+ if (bytes > limits.maxFileBytes) return { violation: makeViolation(limits, 'maxFileBytes', bytes, null) }
252
+ hash.update(buffer.subarray(0, count))
253
+ }
254
+ } finally {
255
+ try { closeSync(fd) } catch {}
256
+ }
257
+ return { sha256: hash.digest('hex'), bytes }
258
+ }
259
+
260
+ /**
261
+ * receipt.docx structural smoke check without loading the file into memory:
262
+ * ZIP magic in the first bytes + the word/document.xml entry name somewhere in
263
+ * the archive. Never calls Office/WPS. Enforces the single-file byte cap while
264
+ * scanning, since the file may have grown after the walk.
265
+ */
266
+ function checkDocxStructure(file, limits) {
267
+ const needle = Buffer.from('word/document.xml', 'utf8')
268
+ const chunkSize = 64 * 1024
269
+ const buffer = Buffer.allocUnsafe(chunkSize)
270
+ let position = 0
271
+ let bytes = 0
272
+ let zip = false
273
+ let hasDocument = false
274
+ let carry = Buffer.alloc(0)
275
+ const fd = openSync(file, 'r')
276
+ try {
277
+ for (;;) {
278
+ const count = readSync(fd, buffer, 0, buffer.length, position)
279
+ if (!count) break
280
+ position += count
281
+ bytes += count
282
+ if (bytes > limits.maxFileBytes) return { violation: makeViolation(limits, 'maxFileBytes', bytes, null) }
283
+ const chunk = buffer.subarray(0, count)
284
+ if (bytes === count && count >= 4) zip = chunk[0] === 0x50 && chunk[1] === 0x4b
285
+ const scan = carry.length ? Buffer.concat([carry, chunk]) : chunk
286
+ if (!hasDocument && scan.includes(needle)) hasDocument = true
287
+ carry = scan.subarray(Math.max(0, scan.length - (needle.length - 1)))
288
+ }
289
+ } finally {
290
+ try { closeSync(fd) } catch {}
291
+ }
292
+ return { zip, hasDocument, bytes }
148
293
  }
149
294
 
150
295
  // --- manifest schema (format v1; keep it in sync with buildReceiptPack) ------
@@ -489,17 +634,27 @@ function validateReceiptManifest(manifest, sums, errors, result) {
489
634
  * Verify a receipt pack directory offline.
490
635
  *
491
636
  * @param {string} dir pack directory produced by buildReceiptPack({outDir})
637
+ * @param {{limits?: Partial<typeof RECEIPT_LIMIT_DEFAULTS>}} [options]
638
+ * `limits` lowers/raises the resource caps (tests and the future WebCrypto
639
+ * verification page use smaller values; the defaults are the contract).
492
640
  * @returns {{ok: boolean, errors: string[], warnings: string[], dir: string,
493
641
  * fileCount: number, checksumCount: number, files: string[],
494
642
  * checksums: Record<string, string>, manifest: object|null,
495
- * anchorsRecorded: number, anchorsVerified: boolean, anchored: boolean}}
643
+ * anchorsRecorded: number, anchorsVerified: boolean, anchored: boolean,
644
+ * limits: object, totalBytes: number, limitViolation: object|null,
645
+ * code: string|null}}
646
+ * `code` is 'E_RECEIPT_LIMIT' when a resource cap was hit (with metadata in
647
+ * limitViolation: {code, limit, max, actual, file}); ok is false in that case
648
+ * and no manifest/checksum result is trusted.
496
649
  */
497
- export function verifyReceiptPackDir(dir) {
650
+ export function verifyReceiptPackDir(dir, options = {}) {
498
651
  const errors = []
499
652
  const warnings = []
500
653
  const files = []
654
+ const limits = normalizeLimits(options?.limits)
501
655
  const result = {
502
656
  ok: false,
657
+ code: null,
503
658
  errors,
504
659
  warnings,
505
660
  dir: String(dir),
@@ -513,6 +668,15 @@ export function verifyReceiptPackDir(dir) {
513
668
  anchorsRecorded: 0,
514
669
  anchorsVerified: false,
515
670
  anchored: false,
671
+ limits: { ...limits },
672
+ totalBytes: 0,
673
+ limitViolation: null,
674
+ }
675
+ const failLimit = (violation) => {
676
+ result.code = RECEIPT_LIMIT_CODE
677
+ result.limitViolation = violation
678
+ result.ok = false
679
+ return result
516
680
  }
517
681
  let root
518
682
  try {
@@ -538,10 +702,12 @@ export function verifyReceiptPackDir(dir) {
538
702
  return result
539
703
  }
540
704
 
541
- const walked = walkPackFiles(root)
705
+ const walked = walkPackFiles(root, limits)
542
706
  for (const error of walked.errors) addError(errors, error)
543
707
  for (const file of walked.files) files.push(file)
544
708
  result.fileCount = files.length
709
+ result.totalBytes = walked.totalBytes
710
+ if (walked.violation) return failLimit(walked.violation)
545
711
  const fileSet = new Set(files)
546
712
 
547
713
  const sumsPath = join(root, RECEIPT_SHA256SUMS_FILE)
@@ -551,6 +717,12 @@ export function verifyReceiptPackDir(dir) {
551
717
  }
552
718
  let sumsText
553
719
  try {
720
+ const sumsStat = statSync(sumsPath)
721
+ if (sumsStat.size > limits.maxSumsBytes) {
722
+ const violation = makeViolation(limits, 'maxSumsBytes', sumsStat.size, RECEIPT_SHA256SUMS_FILE)
723
+ addError(errors, limitMessage(violation))
724
+ return failLimit(violation)
725
+ }
554
726
  sumsText = readFileSync(sumsPath, 'utf8')
555
727
  } catch (error) {
556
728
  addError(errors, 'cannot read SHA256SUMS: ' + (error.code || error.message))
@@ -573,11 +745,24 @@ export function verifyReceiptPackDir(dir) {
573
745
  if (!parsed.entries.has(name)) addError(errors, 'file not covered by SHA256SUMS: ' + name)
574
746
  }
575
747
 
748
+ let hashedTotal = 0
576
749
  for (const [name, expected] of parsed.entries) {
577
750
  if (!fileSet.has(name)) continue
578
751
  const full = join(root, ...name.split('/'))
579
752
  try {
580
- const actual = sha256Hex(readFileSync(full))
753
+ const hashed = hashFileWithLimit(full, limits)
754
+ if (hashed.violation) {
755
+ hashed.violation.file = name
756
+ addError(errors, limitMessage(hashed.violation))
757
+ return failLimit(hashed.violation)
758
+ }
759
+ hashedTotal += hashed.bytes
760
+ if (hashedTotal > limits.maxTotalBytes) {
761
+ const violation = makeViolation(limits, 'maxTotalBytes', hashedTotal, name)
762
+ addError(errors, limitMessage(violation))
763
+ return failLimit(violation)
764
+ }
765
+ const actual = hashed.sha256
581
766
  if (actual !== expected) {
582
767
  addError(errors, 'hash mismatch: ' + name + ' (expected ' + expected.slice(0, 12) + '..., computed ' + actual.slice(0, 12) + '...)')
583
768
  }
@@ -587,12 +772,19 @@ export function verifyReceiptPackDir(dir) {
587
772
  }
588
773
 
589
774
  // receipt.docx structural smoke check (never call Office/WPS to verify it).
775
+ // Streamed with a byte cap: an untrusted pack must not be able to make the
776
+ // verifier allocate a multi-GiB Buffer before reporting a limit error.
590
777
  if (fileSet.has('receipt.docx')) {
591
778
  try {
592
- const bytes = readFileSync(join(root, 'receipt.docx'))
593
- if (bytes.length < 4 || bytes[0] !== 0x50 || bytes[1] !== 0x4b) {
779
+ const docx = checkDocxStructure(join(root, 'receipt.docx'), limits)
780
+ if (docx.violation) {
781
+ docx.violation.file = 'receipt.docx'
782
+ addError(errors, limitMessage(docx.violation))
783
+ return failLimit(docx.violation)
784
+ }
785
+ if (!docx.zip) {
594
786
  addError(errors, 'receipt.docx is not a ZIP/DOCX file')
595
- } else if (!bytes.includes(Buffer.from('word/document.xml', 'utf8'))) {
787
+ } else if (!docx.hasDocument) {
596
788
  addError(errors, 'receipt.docx is missing word/document.xml')
597
789
  }
598
790
  } catch (error) {
@@ -603,6 +795,12 @@ export function verifyReceiptPackDir(dir) {
603
795
  if (!fileSet.has('manifest.json')) return result
604
796
  let manifest = null
605
797
  try {
798
+ const manifestStat = statSync(join(root, 'manifest.json'))
799
+ if (manifestStat.size > limits.maxManifestBytes) {
800
+ const violation = makeViolation(limits, 'maxManifestBytes', manifestStat.size, 'manifest.json')
801
+ addError(errors, limitMessage(violation))
802
+ return failLimit(violation)
803
+ }
606
804
  manifest = JSON.parse(readFileSync(join(root, 'manifest.json'), 'utf8'))
607
805
  } catch (error) {
608
806
  addError(errors, 'manifest.json is not valid JSON: ' + (error.message || error))
File without changes