advisers 0.2.3 → 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.
- package/LICENSE +0 -0
- package/NOTICE +0 -0
- package/README-zh.md +15 -6
- package/README.md +22 -6
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/bin/advisers.js +3323 -222
- package/index.js +1 -1
- package/lib/agent.js +0 -0
- package/lib/attachments.js +0 -0
- package/lib/audit-anchor.js +683 -0
- package/lib/backup.js +1345 -0
- package/lib/capture.js +0 -0
- package/lib/context-window.js +0 -0
- package/lib/digest.js +0 -0
- package/lib/doc-review.js +0 -0
- package/lib/egress.js +0 -0
- package/lib/error-codes.js +1373 -0
- package/lib/evidence.js +202 -26
- package/lib/fs-safe.js +70 -7
- package/lib/ical.js +0 -0
- package/lib/imap.js +6 -2
- package/lib/importers.js +0 -0
- package/lib/learning.js +0 -0
- package/lib/ledger-read.js +608 -0
- package/lib/mcp-server.js +294 -38
- package/lib/model.js +0 -0
- package/lib/node-warning-filter.js +15 -0
- package/lib/notify.js +13 -3
- package/lib/nudge.js +0 -0
- package/lib/ocr.js +0 -0
- package/lib/pdf.js +0 -0
- package/lib/pet-brief-store.js +0 -0
- package/lib/pet-brief.js +102 -39
- package/lib/receipt-verify.js +213 -15
- package/lib/relative-date.js +0 -0
- package/lib/remind.js +91 -36
- package/lib/research.js +0 -0
- package/lib/review.js +0 -0
- package/lib/routine.js +0 -0
- package/lib/safety/classifier.js +0 -0
- package/lib/safety/explain.js +0 -0
- package/lib/safety/index.js +0 -0
- package/lib/safety/redact.js +0 -0
- package/lib/safety/rules.js +0 -0
- package/lib/scheduler.js +0 -0
- package/lib/source.js +0 -0
- package/lib/sqlite.js +1214 -24
- package/lib/sync.js +0 -0
- package/lib/text-safe.js +229 -0
- package/lib/timestamp.js +0 -0
- package/lib/tos/index.js +1 -1
- package/lib/tos/manifest.json +6 -6
- package/lib/tos/privacy.zh-CN.md +12 -9
- package/lib/tos/safety.zh-CN.md +4 -3
- package/lib/tos/summary.zh-CN.md +3 -3
- package/lib/tos/terms.zh-CN.md +8 -6
- package/lib/tui/color.js +164 -0
- package/lib/tui/index.js +33 -0
- package/lib/tui/json.js +124 -0
- package/lib/tui/labels.js +50 -0
- package/lib/tui/policy.js +108 -0
- package/lib/tui/render.js +371 -0
- package/lib/tui/sanitize.js +142 -0
- package/lib/tui/text-file.js +116 -0
- package/lib/tui/width.js +577 -0
- package/lib/ui.js +0 -0
- package/lib/undo.js +0 -0
- package/lib/untrusted.js +0 -0
- package/lib/vault.js +0 -0
- 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
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
|
229
|
-
if (
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
|
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(
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
288
|
-
|
|
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
|
|
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,
|
|
297
|
-
const day =
|
|
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
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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
|
|
package/lib/receipt-verify.js
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
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
|
-
|
|
120
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
593
|
-
if (
|
|
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 (!
|
|
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))
|
package/lib/relative-date.js
CHANGED
|
File without changes
|