pompelmi 0.35.4 → 1.0.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.
- package/.claude/settings.local.json +40 -0
- package/LICENSE +12 -18
- package/README.md +160 -143
- package/eslint.config.mjs +8 -0
- package/package.json +26 -251
- package/src/ClamAVDatabaseUpdater.js +48 -0
- package/src/ClamAVInstaller.js +49 -0
- package/src/ClamAVScanner.js +31 -0
- package/src/InstallerCommand.js +11 -0
- package/src/config.js +22 -0
- package/src/constants.js +3 -0
- package/src/favicon.ico +0 -0
- package/src/grapefruit.png +0 -0
- package/src/index.js +5 -0
- package/CHANGELOG.md +0 -71
- package/dist/pompelmi.audit.cjs +0 -128
- package/dist/pompelmi.audit.cjs.map +0 -1
- package/dist/pompelmi.audit.esm.js +0 -107
- package/dist/pompelmi.audit.esm.js.map +0 -1
- package/dist/pompelmi.browser.cjs +0 -1493
- package/dist/pompelmi.browser.cjs.map +0 -1
- package/dist/pompelmi.browser.esm.js +0 -1467
- package/dist/pompelmi.browser.esm.js.map +0 -1
- package/dist/pompelmi.cjs +0 -2535
- package/dist/pompelmi.cjs.map +0 -1
- package/dist/pompelmi.esm.js +0 -2469
- package/dist/pompelmi.esm.js.map +0 -1
- package/dist/pompelmi.hooks.cjs +0 -75
- package/dist/pompelmi.hooks.cjs.map +0 -1
- package/dist/pompelmi.hooks.esm.js +0 -72
- package/dist/pompelmi.hooks.esm.js.map +0 -1
- package/dist/pompelmi.policy-packs.cjs +0 -240
- package/dist/pompelmi.policy-packs.cjs.map +0 -1
- package/dist/pompelmi.policy-packs.esm.js +0 -232
- package/dist/pompelmi.policy-packs.esm.js.map +0 -1
- package/dist/pompelmi.quarantine.cjs +0 -317
- package/dist/pompelmi.quarantine.cjs.map +0 -1
- package/dist/pompelmi.quarantine.esm.js +0 -293
- package/dist/pompelmi.quarantine.esm.js.map +0 -1
- package/dist/pompelmi.react.cjs +0 -1524
- package/dist/pompelmi.react.cjs.map +0 -1
- package/dist/pompelmi.react.esm.js +0 -1497
- package/dist/pompelmi.react.esm.js.map +0 -1
- package/dist/types/audit.d.ts +0 -84
- package/dist/types/browser-index.d.ts +0 -29
- package/dist/types/config.d.ts +0 -143
- package/dist/types/engines/dynamic-taint.d.ts +0 -102
- package/dist/types/engines/hybrid-orchestrator.d.ts +0 -65
- package/dist/types/engines/hybrid-taint-integration.d.ts +0 -129
- package/dist/types/engines/taint-policies.d.ts +0 -84
- package/dist/types/hipaa-compliance.d.ts +0 -110
- package/dist/types/hooks.d.ts +0 -89
- package/dist/types/index.d.ts +0 -29
- package/dist/types/magic.d.ts +0 -7
- package/dist/types/node/scanDir.d.ts +0 -30
- package/dist/types/policy-packs.d.ts +0 -98
- package/dist/types/policy.d.ts +0 -12
- package/dist/types/presets.d.ts +0 -72
- package/dist/types/quarantine/index.d.ts +0 -18
- package/dist/types/quarantine/storage.d.ts +0 -77
- package/dist/types/quarantine/types.d.ts +0 -78
- package/dist/types/quarantine/workflow.d.ts +0 -97
- package/dist/types/react-index.d.ts +0 -13
- package/dist/types/risk.d.ts +0 -18
- package/dist/types/scan/remote.d.ts +0 -12
- package/dist/types/scan.d.ts +0 -17
- package/dist/types/scanners/common-heuristics.d.ts +0 -14
- package/dist/types/scanners/zip-bomb-guard.d.ts +0 -9
- package/dist/types/scanners/zipTraversalGuard.d.ts +0 -19
- package/dist/types/src/audit.d.ts +0 -84
- package/dist/types/src/browser-index.d.ts +0 -29
- package/dist/types/src/config.d.ts +0 -143
- package/dist/types/src/engines/dynamic-taint.d.ts +0 -102
- package/dist/types/src/engines/hybrid-orchestrator.d.ts +0 -65
- package/dist/types/src/engines/hybrid-taint-integration.d.ts +0 -129
- package/dist/types/src/engines/taint-policies.d.ts +0 -84
- package/dist/types/src/hipaa-compliance.d.ts +0 -110
- package/dist/types/src/hooks.d.ts +0 -89
- package/dist/types/src/index.d.ts +0 -29
- package/dist/types/src/magic.d.ts +0 -7
- package/dist/types/src/node/scanDir.d.ts +0 -30
- package/dist/types/src/policy-packs.d.ts +0 -98
- package/dist/types/src/policy.d.ts +0 -12
- package/dist/types/src/presets.d.ts +0 -72
- package/dist/types/src/quarantine/index.d.ts +0 -18
- package/dist/types/src/quarantine/storage.d.ts +0 -77
- package/dist/types/src/quarantine/types.d.ts +0 -78
- package/dist/types/src/quarantine/workflow.d.ts +0 -97
- package/dist/types/src/react-index.d.ts +0 -13
- package/dist/types/src/risk.d.ts +0 -18
- package/dist/types/src/scan/remote.d.ts +0 -12
- package/dist/types/src/scan.d.ts +0 -17
- package/dist/types/src/scanners/common-heuristics.d.ts +0 -14
- package/dist/types/src/scanners/zip-bomb-guard.d.ts +0 -9
- package/dist/types/src/scanners/zipTraversalGuard.d.ts +0 -19
- package/dist/types/src/stream.d.ts +0 -10
- package/dist/types/src/types/decompilation.d.ts +0 -96
- package/dist/types/src/types/taint-tracking.d.ts +0 -495
- package/dist/types/src/types.d.ts +0 -48
- package/dist/types/src/useFileScanner.d.ts +0 -15
- package/dist/types/src/utils/advanced-detection.d.ts +0 -21
- package/dist/types/src/utils/batch-scanner.d.ts +0 -62
- package/dist/types/src/utils/cache-manager.d.ts +0 -95
- package/dist/types/src/utils/export.d.ts +0 -51
- package/dist/types/src/utils/performance-metrics.d.ts +0 -68
- package/dist/types/src/utils/threat-intelligence.d.ts +0 -96
- package/dist/types/src/validate.d.ts +0 -7
- package/dist/types/src/verdict.d.ts +0 -2
- package/dist/types/src/yara/browser.d.ts +0 -7
- package/dist/types/src/yara/index.d.ts +0 -17
- package/dist/types/src/yara/node.d.ts +0 -2
- package/dist/types/src/yara/remote.d.ts +0 -10
- package/dist/types/src/yara-bridge.d.ts +0 -3
- package/dist/types/src/zip.d.ts +0 -13
- package/dist/types/stream.d.ts +0 -10
- package/dist/types/types/decompilation.d.ts +0 -96
- package/dist/types/types/taint-tracking.d.ts +0 -495
- package/dist/types/types.d.ts +0 -48
- package/dist/types/useFileScanner.d.ts +0 -15
- package/dist/types/utils/advanced-detection.d.ts +0 -21
- package/dist/types/utils/batch-scanner.d.ts +0 -62
- package/dist/types/utils/cache-manager.d.ts +0 -95
- package/dist/types/utils/export.d.ts +0 -51
- package/dist/types/utils/performance-metrics.d.ts +0 -68
- package/dist/types/utils/threat-intelligence.d.ts +0 -96
- package/dist/types/validate.d.ts +0 -7
- package/dist/types/verdict.d.ts +0 -2
- package/dist/types/yara/browser.d.ts +0 -7
- package/dist/types/yara/index.d.ts +0 -17
- package/dist/types/yara/node.d.ts +0 -2
- package/dist/types/yara/remote.d.ts +0 -10
- package/dist/types/yara-bridge.d.ts +0 -3
- package/dist/types/zip.d.ts +0 -13
|
@@ -1,317 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var crypto = require('crypto');
|
|
4
|
-
var fs = require('fs');
|
|
5
|
-
var path = require('path');
|
|
6
|
-
|
|
7
|
-
function _interopNamespaceDefault(e) {
|
|
8
|
-
var n = Object.create(null);
|
|
9
|
-
if (e) {
|
|
10
|
-
Object.keys(e).forEach(function (k) {
|
|
11
|
-
if (k !== 'default') {
|
|
12
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
get: function () { return e[k]; }
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
n.default = e;
|
|
21
|
-
return Object.freeze(n);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
var crypto__namespace = /*#__PURE__*/_interopNamespaceDefault(crypto);
|
|
25
|
-
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
26
|
-
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Quarantine storage adapter interface and filesystem reference implementation.
|
|
30
|
-
*
|
|
31
|
-
* The `QuarantineStorage` interface decouples the quarantine workflow from any
|
|
32
|
-
* specific persistence layer. You can implement it for S3, GCS, a database,
|
|
33
|
-
* or any other backend.
|
|
34
|
-
*
|
|
35
|
-
* The built-in `FilesystemQuarantineStorage` stores files and metadata as JSON
|
|
36
|
-
* in a local directory — suitable for development, self-hosted, and on-premise
|
|
37
|
-
* deployments where data must not leave the machine.
|
|
38
|
-
*
|
|
39
|
-
* @module quarantine/storage
|
|
40
|
-
*/
|
|
41
|
-
/**
|
|
42
|
-
* Reference implementation of `QuarantineStorage` backed by the local filesystem.
|
|
43
|
-
*
|
|
44
|
-
* File layout:
|
|
45
|
-
* <dir>/files/<storageKey> — raw file bytes
|
|
46
|
-
* <dir>/meta/<id>.json — QuarantineEntry JSON
|
|
47
|
-
*
|
|
48
|
-
* Suitable for single-process servers. For multi-process or distributed
|
|
49
|
-
* deployments, implement `QuarantineStorage` against a shared backend.
|
|
50
|
-
*/
|
|
51
|
-
class FilesystemQuarantineStorage {
|
|
52
|
-
constructor(options) {
|
|
53
|
-
this.filesDir = path__namespace.join(options.dir, "files");
|
|
54
|
-
this.metaDir = path__namespace.join(options.dir, "meta");
|
|
55
|
-
if (options.createIfMissing !== false) {
|
|
56
|
-
fs__namespace.mkdirSync(this.filesDir, { recursive: true });
|
|
57
|
-
fs__namespace.mkdirSync(this.metaDir, { recursive: true });
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
async saveFile(id, bytes) {
|
|
61
|
-
// Use a safe, collision-resistant filename derived from the entry id.
|
|
62
|
-
const storageKey = `${id}-${crypto__namespace.randomBytes(4).toString("hex")}`;
|
|
63
|
-
const filePath = path__namespace.join(this.filesDir, storageKey);
|
|
64
|
-
await fs__namespace.promises.writeFile(filePath, bytes);
|
|
65
|
-
return storageKey;
|
|
66
|
-
}
|
|
67
|
-
async getFile(storageKey) {
|
|
68
|
-
const filePath = path__namespace.join(this.filesDir, safeBasename(storageKey));
|
|
69
|
-
try {
|
|
70
|
-
const buf = await fs__namespace.promises.readFile(filePath);
|
|
71
|
-
return new Uint8Array(buf);
|
|
72
|
-
}
|
|
73
|
-
catch {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
async deleteFile(storageKey) {
|
|
78
|
-
const filePath = path__namespace.join(this.filesDir, safeBasename(storageKey));
|
|
79
|
-
await fs__namespace.promises.unlink(filePath).catch(() => {
|
|
80
|
-
/* already gone */
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
async saveEntry(entry) {
|
|
84
|
-
const metaPath = path__namespace.join(this.metaDir, `${safeBasename(entry.id)}.json`);
|
|
85
|
-
await fs__namespace.promises.writeFile(metaPath, JSON.stringify(entry, null, 2), "utf8");
|
|
86
|
-
}
|
|
87
|
-
async getEntry(id) {
|
|
88
|
-
const metaPath = path__namespace.join(this.metaDir, `${safeBasename(id)}.json`);
|
|
89
|
-
try {
|
|
90
|
-
const raw = await fs__namespace.promises.readFile(metaPath, "utf8");
|
|
91
|
-
return JSON.parse(raw);
|
|
92
|
-
}
|
|
93
|
-
catch {
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
async updateEntry(id, patch) {
|
|
98
|
-
const existing = await this.getEntry(id);
|
|
99
|
-
if (!existing)
|
|
100
|
-
throw new Error(`Quarantine entry not found: ${id}`);
|
|
101
|
-
const updated = { ...existing, ...patch };
|
|
102
|
-
await this.saveEntry(updated);
|
|
103
|
-
return updated;
|
|
104
|
-
}
|
|
105
|
-
async listEntries(filter) {
|
|
106
|
-
const files = await fs__namespace.promises.readdir(this.metaDir).catch(() => []);
|
|
107
|
-
const entries = [];
|
|
108
|
-
for (const file of files) {
|
|
109
|
-
if (!file.endsWith(".json"))
|
|
110
|
-
continue;
|
|
111
|
-
try {
|
|
112
|
-
const raw = await fs__namespace.promises.readFile(path__namespace.join(this.metaDir, file), "utf8");
|
|
113
|
-
entries.push(JSON.parse(raw));
|
|
114
|
-
}
|
|
115
|
-
catch {
|
|
116
|
-
// Skip unreadable entries.
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return applyFilter(entries, filter);
|
|
120
|
-
}
|
|
121
|
-
async countEntries(filter) {
|
|
122
|
-
const entries = await this.listEntries(filter);
|
|
123
|
-
return entries.length;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
127
|
-
/**
|
|
128
|
-
* Strip directory separators to prevent path-traversal attacks when using
|
|
129
|
-
* user-supplied or derived keys as filenames.
|
|
130
|
-
*/
|
|
131
|
-
function safeBasename(key) {
|
|
132
|
-
return path__namespace.basename(key).replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
133
|
-
}
|
|
134
|
-
function applyFilter(entries, filter) {
|
|
135
|
-
if (!filter)
|
|
136
|
-
return entries;
|
|
137
|
-
let result = entries;
|
|
138
|
-
if (filter.status !== undefined) {
|
|
139
|
-
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
140
|
-
result = result.filter((e) => statuses.includes(e.status));
|
|
141
|
-
}
|
|
142
|
-
if (filter.after) {
|
|
143
|
-
result = result.filter((e) => e.quarantinedAt >= filter.after);
|
|
144
|
-
}
|
|
145
|
-
if (filter.before) {
|
|
146
|
-
result = result.filter((e) => e.quarantinedAt <= filter.before);
|
|
147
|
-
}
|
|
148
|
-
if (filter.limit !== undefined) {
|
|
149
|
-
result = result.slice(0, filter.limit);
|
|
150
|
-
}
|
|
151
|
-
return result;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Quarantine workflow — core API for the quarantine/review/resolve lifecycle.
|
|
156
|
-
*
|
|
157
|
-
* Usage (Node.js):
|
|
158
|
-
*
|
|
159
|
-
* ```ts
|
|
160
|
-
* import { scanBytes } from 'pompelmi';
|
|
161
|
-
* import { QuarantineManager, FilesystemQuarantineStorage } from 'pompelmi/quarantine';
|
|
162
|
-
*
|
|
163
|
-
* const quarantine = new QuarantineManager({
|
|
164
|
-
* storage: new FilesystemQuarantineStorage({ dir: './quarantine' }),
|
|
165
|
-
* });
|
|
166
|
-
*
|
|
167
|
-
* const report = await scanBytes(fileBytes, { ctx: { filename: file.name } });
|
|
168
|
-
*
|
|
169
|
-
* if (report.verdict !== 'clean') {
|
|
170
|
-
* const entry = await quarantine.quarantine(fileBytes, report, {
|
|
171
|
-
* originalName: file.name,
|
|
172
|
-
* sizeBytes: fileBytes.length,
|
|
173
|
-
* uploadedBy: req.user?.id,
|
|
174
|
-
* });
|
|
175
|
-
* console.log('Quarantined:', entry.id);
|
|
176
|
-
* }
|
|
177
|
-
* ```
|
|
178
|
-
*
|
|
179
|
-
* @module quarantine/workflow
|
|
180
|
-
*/
|
|
181
|
-
// ── Manager ───────────────────────────────────────────────────────────────────
|
|
182
|
-
/**
|
|
183
|
-
* Manages the full lifecycle of quarantined files:
|
|
184
|
-
* scan → quarantine → review → promote | delete
|
|
185
|
-
*/
|
|
186
|
-
class QuarantineManager {
|
|
187
|
-
constructor(options) {
|
|
188
|
-
this.storage = options.storage;
|
|
189
|
-
this.quarantineSuspicious = options.quarantineSuspicious ?? true;
|
|
190
|
-
this.quarantineMalicious = options.quarantineMalicious ?? true;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Determine whether a scan report should trigger quarantine per the
|
|
194
|
-
* configured policy.
|
|
195
|
-
*/
|
|
196
|
-
shouldQuarantine(report) {
|
|
197
|
-
if (report.verdict === "malicious")
|
|
198
|
-
return this.quarantineMalicious;
|
|
199
|
-
if (report.verdict === "suspicious")
|
|
200
|
-
return this.quarantineSuspicious;
|
|
201
|
-
return false;
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Quarantine a file: save the bytes in storage, create the metadata entry,
|
|
205
|
-
* and return the entry.
|
|
206
|
-
*
|
|
207
|
-
* @param bytes Raw file bytes.
|
|
208
|
-
* @param report The scan report that triggered quarantine.
|
|
209
|
-
* @param fileInfo Partial metadata; `sha256` is derived from `bytes` if omitted.
|
|
210
|
-
*/
|
|
211
|
-
async quarantine(bytes, report, fileInfo) {
|
|
212
|
-
const id = generateId();
|
|
213
|
-
const sha256 = fileInfo.sha256 ?? computeSha256(bytes);
|
|
214
|
-
const now = new Date().toISOString();
|
|
215
|
-
const storageKey = await this.storage.saveFile(id, bytes);
|
|
216
|
-
const entry = {
|
|
217
|
-
id,
|
|
218
|
-
storageKey,
|
|
219
|
-
file: { ...fileInfo, sha256 },
|
|
220
|
-
scanReport: report,
|
|
221
|
-
quarantinedAt: now,
|
|
222
|
-
status: "pending",
|
|
223
|
-
updatedAt: now,
|
|
224
|
-
};
|
|
225
|
-
await this.storage.saveEntry(entry);
|
|
226
|
-
return entry;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Mark an entry as being actively reviewed.
|
|
230
|
-
*/
|
|
231
|
-
async startReview(id, reviewedBy) {
|
|
232
|
-
return this.storage.updateEntry(id, {
|
|
233
|
-
status: "reviewing",
|
|
234
|
-
reviewedBy,
|
|
235
|
-
updatedAt: new Date().toISOString(),
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Resolve a quarantine entry with a final decision.
|
|
240
|
-
*
|
|
241
|
-
* - `promote`: the file is cleared — bytes remain in storage for the caller
|
|
242
|
-
* to move to its final destination.
|
|
243
|
-
* - `delete`: the bytes are permanently removed from quarantine storage.
|
|
244
|
-
*/
|
|
245
|
-
async resolve(id, review) {
|
|
246
|
-
const entry = await this.storage.getEntry(id);
|
|
247
|
-
if (!entry)
|
|
248
|
-
throw new Error(`Quarantine entry not found: ${id}`);
|
|
249
|
-
if (entry.status === "promoted" || entry.status === "deleted") {
|
|
250
|
-
throw new Error(`Quarantine entry ${id} is already resolved (${entry.status}).`);
|
|
251
|
-
}
|
|
252
|
-
const now = new Date().toISOString();
|
|
253
|
-
const newStatus = review.decision === "promote" ? "promoted" : "deleted";
|
|
254
|
-
if (review.decision === "delete") {
|
|
255
|
-
await this.storage.deleteFile(entry.storageKey);
|
|
256
|
-
}
|
|
257
|
-
return this.storage.updateEntry(id, {
|
|
258
|
-
status: newStatus,
|
|
259
|
-
reviewedBy: review.reviewedBy,
|
|
260
|
-
reviewNote: review.reviewNote,
|
|
261
|
-
resolvedAt: now,
|
|
262
|
-
updatedAt: now,
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Retrieve the raw bytes of a promoted file so the caller can move it to
|
|
267
|
-
* permanent storage. Returns `null` if the entry is not found or has been
|
|
268
|
-
* deleted.
|
|
269
|
-
*/
|
|
270
|
-
async getFile(id) {
|
|
271
|
-
const entry = await this.storage.getEntry(id);
|
|
272
|
-
if (!entry)
|
|
273
|
-
return null;
|
|
274
|
-
return this.storage.getFile(entry.storageKey);
|
|
275
|
-
}
|
|
276
|
-
// ── Query helpers ───────────────────────────────────────────────────────────
|
|
277
|
-
getEntry(id) {
|
|
278
|
-
return this.storage.getEntry(id);
|
|
279
|
-
}
|
|
280
|
-
listEntries(filter) {
|
|
281
|
-
return this.storage.listEntries(filter);
|
|
282
|
-
}
|
|
283
|
-
listPending() {
|
|
284
|
-
return this.storage.listEntries({ status: "pending" });
|
|
285
|
-
}
|
|
286
|
-
countEntries(filter) {
|
|
287
|
-
return this.storage.countEntries(filter);
|
|
288
|
-
}
|
|
289
|
-
// ── Reporting ───────────────────────────────────────────────────────────────
|
|
290
|
-
/**
|
|
291
|
-
* Generate a structured JSON report of all quarantine entries matching the
|
|
292
|
-
* filter — suitable for audit logs and dashboards.
|
|
293
|
-
*/
|
|
294
|
-
async report(filter) {
|
|
295
|
-
const entries = await this.storage.listEntries(filter);
|
|
296
|
-
const byStatus = { pending: 0, reviewing: 0, promoted: 0, deleted: 0 };
|
|
297
|
-
for (const e of entries)
|
|
298
|
-
byStatus[e.status]++;
|
|
299
|
-
return {
|
|
300
|
-
generatedAt: new Date().toISOString(),
|
|
301
|
-
totalEntries: entries.length,
|
|
302
|
-
byStatus,
|
|
303
|
-
entries,
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
308
|
-
function generateId() {
|
|
309
|
-
return crypto__namespace.randomUUID();
|
|
310
|
-
}
|
|
311
|
-
function computeSha256(bytes) {
|
|
312
|
-
return crypto__namespace.createHash("sha256").update(bytes).digest("hex");
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
exports.FilesystemQuarantineStorage = FilesystemQuarantineStorage;
|
|
316
|
-
exports.QuarantineManager = QuarantineManager;
|
|
317
|
-
//# sourceMappingURL=pompelmi.quarantine.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pompelmi.quarantine.cjs","sources":["../../src/quarantine/storage.ts","../../src/quarantine/workflow.ts"],"sourcesContent":["/**\n * Quarantine storage adapter interface and filesystem reference implementation.\n *\n * The `QuarantineStorage` interface decouples the quarantine workflow from any\n * specific persistence layer. You can implement it for S3, GCS, a database,\n * or any other backend.\n *\n * The built-in `FilesystemQuarantineStorage` stores files and metadata as JSON\n * in a local directory — suitable for development, self-hosted, and on-premise\n * deployments where data must not leave the machine.\n *\n * @module quarantine/storage\n */\n\nimport * as crypto from \"crypto\";\nimport * as fs from \"fs\";\nimport * as path from \"path\";\nimport type { QuarantineEntry, QuarantineFilter } from \"./types\";\n\n// ── Adapter interface ─────────────────────────────────────────────────────────\n\n/**\n * Storage adapter for the quarantine workflow.\n * Implement this interface to support any backend (S3, GCS, DB, etc.).\n */\nexport interface QuarantineStorage {\n /**\n * Persist the raw bytes of a quarantined file.\n * Returns a `storageKey` that can later be used to retrieve or delete the bytes.\n */\n saveFile(id: string, bytes: Uint8Array): Promise<string>;\n\n /**\n * Retrieve the raw bytes of a quarantined file.\n * Returns `null` if the file is not found.\n */\n getFile(storageKey: string): Promise<Uint8Array | null>;\n\n /**\n * Permanently remove the raw bytes of a quarantined file.\n * No-op if already removed.\n */\n deleteFile(storageKey: string): Promise<void>;\n\n /** Persist a quarantine entry (metadata + scan report). */\n saveEntry(entry: QuarantineEntry): Promise<void>;\n\n /** Load a quarantine entry by id. Returns `null` if not found. */\n getEntry(id: string): Promise<QuarantineEntry | null>;\n\n /** Update an existing quarantine entry (partial update). */\n updateEntry(id: string, patch: Partial<QuarantineEntry>): Promise<QuarantineEntry>;\n\n /** List quarantine entries matching the given filter. */\n listEntries(filter?: QuarantineFilter): Promise<QuarantineEntry[]>;\n\n /** Return the total count of quarantine entries matching the filter. */\n countEntries(filter?: QuarantineFilter): Promise<number>;\n}\n\n// ── Filesystem implementation ─────────────────────────────────────────────────\n\nexport interface FilesystemQuarantineStorageOptions {\n /**\n * Root directory for quarantine storage.\n * Two subdirectories are created: `files/` (raw bytes) and `meta/` (JSON).\n */\n dir: string;\n /** Create the directory if it does not exist (default: true). */\n createIfMissing?: boolean;\n}\n\n/**\n * Reference implementation of `QuarantineStorage` backed by the local filesystem.\n *\n * File layout:\n * <dir>/files/<storageKey> — raw file bytes\n * <dir>/meta/<id>.json — QuarantineEntry JSON\n *\n * Suitable for single-process servers. For multi-process or distributed\n * deployments, implement `QuarantineStorage` against a shared backend.\n */\nexport class FilesystemQuarantineStorage implements QuarantineStorage {\n private readonly filesDir: string;\n private readonly metaDir: string;\n\n constructor(options: FilesystemQuarantineStorageOptions) {\n this.filesDir = path.join(options.dir, \"files\");\n this.metaDir = path.join(options.dir, \"meta\");\n if (options.createIfMissing !== false) {\n fs.mkdirSync(this.filesDir, { recursive: true });\n fs.mkdirSync(this.metaDir, { recursive: true });\n }\n }\n\n async saveFile(id: string, bytes: Uint8Array): Promise<string> {\n // Use a safe, collision-resistant filename derived from the entry id.\n const storageKey = `${id}-${crypto.randomBytes(4).toString(\"hex\")}`;\n const filePath = path.join(this.filesDir, storageKey);\n await fs.promises.writeFile(filePath, bytes);\n return storageKey;\n }\n\n async getFile(storageKey: string): Promise<Uint8Array | null> {\n const filePath = path.join(this.filesDir, safeBasename(storageKey));\n try {\n const buf = await fs.promises.readFile(filePath);\n return new Uint8Array(buf);\n } catch {\n return null;\n }\n }\n\n async deleteFile(storageKey: string): Promise<void> {\n const filePath = path.join(this.filesDir, safeBasename(storageKey));\n await fs.promises.unlink(filePath).catch(() => {\n /* already gone */\n });\n }\n\n async saveEntry(entry: QuarantineEntry): Promise<void> {\n const metaPath = path.join(this.metaDir, `${safeBasename(entry.id)}.json`);\n await fs.promises.writeFile(metaPath, JSON.stringify(entry, null, 2), \"utf8\");\n }\n\n async getEntry(id: string): Promise<QuarantineEntry | null> {\n const metaPath = path.join(this.metaDir, `${safeBasename(id)}.json`);\n try {\n const raw = await fs.promises.readFile(metaPath, \"utf8\");\n return JSON.parse(raw) as QuarantineEntry;\n } catch {\n return null;\n }\n }\n\n async updateEntry(id: string, patch: Partial<QuarantineEntry>): Promise<QuarantineEntry> {\n const existing = await this.getEntry(id);\n if (!existing) throw new Error(`Quarantine entry not found: ${id}`);\n const updated: QuarantineEntry = { ...existing, ...patch };\n await this.saveEntry(updated);\n return updated;\n }\n\n async listEntries(filter?: QuarantineFilter): Promise<QuarantineEntry[]> {\n const files = await fs.promises.readdir(this.metaDir).catch(() => [] as string[]);\n const entries: QuarantineEntry[] = [];\n\n for (const file of files) {\n if (!file.endsWith(\".json\")) continue;\n try {\n const raw = await fs.promises.readFile(path.join(this.metaDir, file), \"utf8\");\n entries.push(JSON.parse(raw) as QuarantineEntry);\n } catch {\n // Skip unreadable entries.\n }\n }\n\n return applyFilter(entries, filter);\n }\n\n async countEntries(filter?: QuarantineFilter): Promise<number> {\n const entries = await this.listEntries(filter);\n return entries.length;\n }\n}\n\n// ── Helpers ───────────────────────────────────────────────────────────────────\n\n/**\n * Strip directory separators to prevent path-traversal attacks when using\n * user-supplied or derived keys as filenames.\n */\nfunction safeBasename(key: string): string {\n return path.basename(key).replace(/[^a-zA-Z0-9._-]/g, \"_\");\n}\n\nfunction applyFilter(entries: QuarantineEntry[], filter?: QuarantineFilter): QuarantineEntry[] {\n if (!filter) return entries;\n\n let result = entries;\n\n if (filter.status !== undefined) {\n const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];\n result = result.filter((e) => statuses.includes(e.status));\n }\n if (filter.after) {\n result = result.filter((e) => e.quarantinedAt >= filter.after!);\n }\n if (filter.before) {\n result = result.filter((e) => e.quarantinedAt <= filter.before!);\n }\n if (filter.limit !== undefined) {\n result = result.slice(0, filter.limit);\n }\n\n return result;\n}\n","/**\n * Quarantine workflow — core API for the quarantine/review/resolve lifecycle.\n *\n * Usage (Node.js):\n *\n * ```ts\n * import { scanBytes } from 'pompelmi';\n * import { QuarantineManager, FilesystemQuarantineStorage } from 'pompelmi/quarantine';\n *\n * const quarantine = new QuarantineManager({\n * storage: new FilesystemQuarantineStorage({ dir: './quarantine' }),\n * });\n *\n * const report = await scanBytes(fileBytes, { ctx: { filename: file.name } });\n *\n * if (report.verdict !== 'clean') {\n * const entry = await quarantine.quarantine(fileBytes, report, {\n * originalName: file.name,\n * sizeBytes: fileBytes.length,\n * uploadedBy: req.user?.id,\n * });\n * console.log('Quarantined:', entry.id);\n * }\n * ```\n *\n * @module quarantine/workflow\n */\n\nimport * as crypto from \"crypto\";\nimport type { ScanReport } from \"../types\";\nimport type { QuarantineStorage } from \"./storage\";\nimport type {\n QuarantinedFileInfo,\n QuarantineEntry,\n QuarantineFilter,\n QuarantineReport,\n QuarantineReview,\n QuarantineStatus,\n} from \"./types\";\n\n// ── Options ───────────────────────────────────────────────────────────────────\n\nexport interface QuarantineManagerOptions {\n /** Storage adapter — use `FilesystemQuarantineStorage` for local deployments. */\n storage: QuarantineStorage;\n\n /**\n * If true, files with a 'suspicious' verdict are also quarantined.\n * Default: true.\n */\n quarantineSuspicious?: boolean;\n\n /**\n * If true, files with a 'malicious' verdict are also quarantined.\n * Default: true.\n */\n quarantineMalicious?: boolean;\n}\n\n// ── Manager ───────────────────────────────────────────────────────────────────\n\n/**\n * Manages the full lifecycle of quarantined files:\n * scan → quarantine → review → promote | delete\n */\nexport class QuarantineManager {\n private readonly storage: QuarantineStorage;\n private readonly quarantineSuspicious: boolean;\n private readonly quarantineMalicious: boolean;\n\n constructor(options: QuarantineManagerOptions) {\n this.storage = options.storage;\n this.quarantineSuspicious = options.quarantineSuspicious ?? true;\n this.quarantineMalicious = options.quarantineMalicious ?? true;\n }\n\n /**\n * Determine whether a scan report should trigger quarantine per the\n * configured policy.\n */\n shouldQuarantine(report: ScanReport): boolean {\n if (report.verdict === \"malicious\") return this.quarantineMalicious;\n if (report.verdict === \"suspicious\") return this.quarantineSuspicious;\n return false;\n }\n\n /**\n * Quarantine a file: save the bytes in storage, create the metadata entry,\n * and return the entry.\n *\n * @param bytes Raw file bytes.\n * @param report The scan report that triggered quarantine.\n * @param fileInfo Partial metadata; `sha256` is derived from `bytes` if omitted.\n */\n async quarantine(\n bytes: Uint8Array,\n report: ScanReport,\n fileInfo: Omit<QuarantinedFileInfo, \"sha256\"> & { sha256?: string },\n ): Promise<QuarantineEntry> {\n const id = generateId();\n const sha256 = fileInfo.sha256 ?? computeSha256(bytes);\n const now = new Date().toISOString();\n\n const storageKey = await this.storage.saveFile(id, bytes);\n\n const entry: QuarantineEntry = {\n id,\n storageKey,\n file: { ...fileInfo, sha256 },\n scanReport: report,\n quarantinedAt: now,\n status: \"pending\",\n updatedAt: now,\n };\n\n await this.storage.saveEntry(entry);\n return entry;\n }\n\n /**\n * Mark an entry as being actively reviewed.\n */\n async startReview(id: string, reviewedBy?: string): Promise<QuarantineEntry> {\n return this.storage.updateEntry(id, {\n status: \"reviewing\",\n reviewedBy,\n updatedAt: new Date().toISOString(),\n });\n }\n\n /**\n * Resolve a quarantine entry with a final decision.\n *\n * - `promote`: the file is cleared — bytes remain in storage for the caller\n * to move to its final destination.\n * - `delete`: the bytes are permanently removed from quarantine storage.\n */\n async resolve(id: string, review: QuarantineReview): Promise<QuarantineEntry> {\n const entry = await this.storage.getEntry(id);\n if (!entry) throw new Error(`Quarantine entry not found: ${id}`);\n if (entry.status === \"promoted\" || entry.status === \"deleted\") {\n throw new Error(`Quarantine entry ${id} is already resolved (${entry.status}).`);\n }\n\n const now = new Date().toISOString();\n const newStatus: QuarantineStatus = review.decision === \"promote\" ? \"promoted\" : \"deleted\";\n\n if (review.decision === \"delete\") {\n await this.storage.deleteFile(entry.storageKey);\n }\n\n return this.storage.updateEntry(id, {\n status: newStatus,\n reviewedBy: review.reviewedBy,\n reviewNote: review.reviewNote,\n resolvedAt: now,\n updatedAt: now,\n });\n }\n\n /**\n * Retrieve the raw bytes of a promoted file so the caller can move it to\n * permanent storage. Returns `null` if the entry is not found or has been\n * deleted.\n */\n async getFile(id: string): Promise<Uint8Array | null> {\n const entry = await this.storage.getEntry(id);\n if (!entry) return null;\n return this.storage.getFile(entry.storageKey);\n }\n\n // ── Query helpers ───────────────────────────────────────────────────────────\n\n getEntry(id: string): Promise<QuarantineEntry | null> {\n return this.storage.getEntry(id);\n }\n\n listEntries(filter?: QuarantineFilter): Promise<QuarantineEntry[]> {\n return this.storage.listEntries(filter);\n }\n\n listPending(): Promise<QuarantineEntry[]> {\n return this.storage.listEntries({ status: \"pending\" });\n }\n\n countEntries(filter?: QuarantineFilter): Promise<number> {\n return this.storage.countEntries(filter);\n }\n\n // ── Reporting ───────────────────────────────────────────────────────────────\n\n /**\n * Generate a structured JSON report of all quarantine entries matching the\n * filter — suitable for audit logs and dashboards.\n */\n async report(filter?: QuarantineFilter): Promise<QuarantineReport> {\n const entries = await this.storage.listEntries(filter);\n const byStatus = { pending: 0, reviewing: 0, promoted: 0, deleted: 0 };\n for (const e of entries) byStatus[e.status]++;\n return {\n generatedAt: new Date().toISOString(),\n totalEntries: entries.length,\n byStatus,\n entries,\n };\n }\n}\n\n// ── Helpers ───────────────────────────────────────────────────────────────────\n\nfunction generateId(): string {\n return crypto.randomUUID();\n}\n\nfunction computeSha256(bytes: Uint8Array): string {\n return crypto.createHash(\"sha256\").update(bytes).digest(\"hex\");\n}\n"],"names":["path","fs","crypto"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;AAYG;AA4DH;;;;;;;;;AAS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'fs';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Quarantine storage adapter interface and filesystem reference implementation.
|
|
7
|
-
*
|
|
8
|
-
* The `QuarantineStorage` interface decouples the quarantine workflow from any
|
|
9
|
-
* specific persistence layer. You can implement it for S3, GCS, a database,
|
|
10
|
-
* or any other backend.
|
|
11
|
-
*
|
|
12
|
-
* The built-in `FilesystemQuarantineStorage` stores files and metadata as JSON
|
|
13
|
-
* in a local directory — suitable for development, self-hosted, and on-premise
|
|
14
|
-
* deployments where data must not leave the machine.
|
|
15
|
-
*
|
|
16
|
-
* @module quarantine/storage
|
|
17
|
-
*/
|
|
18
|
-
/**
|
|
19
|
-
* Reference implementation of `QuarantineStorage` backed by the local filesystem.
|
|
20
|
-
*
|
|
21
|
-
* File layout:
|
|
22
|
-
* <dir>/files/<storageKey> — raw file bytes
|
|
23
|
-
* <dir>/meta/<id>.json — QuarantineEntry JSON
|
|
24
|
-
*
|
|
25
|
-
* Suitable for single-process servers. For multi-process or distributed
|
|
26
|
-
* deployments, implement `QuarantineStorage` against a shared backend.
|
|
27
|
-
*/
|
|
28
|
-
class FilesystemQuarantineStorage {
|
|
29
|
-
constructor(options) {
|
|
30
|
-
this.filesDir = path.join(options.dir, "files");
|
|
31
|
-
this.metaDir = path.join(options.dir, "meta");
|
|
32
|
-
if (options.createIfMissing !== false) {
|
|
33
|
-
fs.mkdirSync(this.filesDir, { recursive: true });
|
|
34
|
-
fs.mkdirSync(this.metaDir, { recursive: true });
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
async saveFile(id, bytes) {
|
|
38
|
-
// Use a safe, collision-resistant filename derived from the entry id.
|
|
39
|
-
const storageKey = `${id}-${crypto.randomBytes(4).toString("hex")}`;
|
|
40
|
-
const filePath = path.join(this.filesDir, storageKey);
|
|
41
|
-
await fs.promises.writeFile(filePath, bytes);
|
|
42
|
-
return storageKey;
|
|
43
|
-
}
|
|
44
|
-
async getFile(storageKey) {
|
|
45
|
-
const filePath = path.join(this.filesDir, safeBasename(storageKey));
|
|
46
|
-
try {
|
|
47
|
-
const buf = await fs.promises.readFile(filePath);
|
|
48
|
-
return new Uint8Array(buf);
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async deleteFile(storageKey) {
|
|
55
|
-
const filePath = path.join(this.filesDir, safeBasename(storageKey));
|
|
56
|
-
await fs.promises.unlink(filePath).catch(() => {
|
|
57
|
-
/* already gone */
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
async saveEntry(entry) {
|
|
61
|
-
const metaPath = path.join(this.metaDir, `${safeBasename(entry.id)}.json`);
|
|
62
|
-
await fs.promises.writeFile(metaPath, JSON.stringify(entry, null, 2), "utf8");
|
|
63
|
-
}
|
|
64
|
-
async getEntry(id) {
|
|
65
|
-
const metaPath = path.join(this.metaDir, `${safeBasename(id)}.json`);
|
|
66
|
-
try {
|
|
67
|
-
const raw = await fs.promises.readFile(metaPath, "utf8");
|
|
68
|
-
return JSON.parse(raw);
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async updateEntry(id, patch) {
|
|
75
|
-
const existing = await this.getEntry(id);
|
|
76
|
-
if (!existing)
|
|
77
|
-
throw new Error(`Quarantine entry not found: ${id}`);
|
|
78
|
-
const updated = { ...existing, ...patch };
|
|
79
|
-
await this.saveEntry(updated);
|
|
80
|
-
return updated;
|
|
81
|
-
}
|
|
82
|
-
async listEntries(filter) {
|
|
83
|
-
const files = await fs.promises.readdir(this.metaDir).catch(() => []);
|
|
84
|
-
const entries = [];
|
|
85
|
-
for (const file of files) {
|
|
86
|
-
if (!file.endsWith(".json"))
|
|
87
|
-
continue;
|
|
88
|
-
try {
|
|
89
|
-
const raw = await fs.promises.readFile(path.join(this.metaDir, file), "utf8");
|
|
90
|
-
entries.push(JSON.parse(raw));
|
|
91
|
-
}
|
|
92
|
-
catch {
|
|
93
|
-
// Skip unreadable entries.
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return applyFilter(entries, filter);
|
|
97
|
-
}
|
|
98
|
-
async countEntries(filter) {
|
|
99
|
-
const entries = await this.listEntries(filter);
|
|
100
|
-
return entries.length;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
104
|
-
/**
|
|
105
|
-
* Strip directory separators to prevent path-traversal attacks when using
|
|
106
|
-
* user-supplied or derived keys as filenames.
|
|
107
|
-
*/
|
|
108
|
-
function safeBasename(key) {
|
|
109
|
-
return path.basename(key).replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
110
|
-
}
|
|
111
|
-
function applyFilter(entries, filter) {
|
|
112
|
-
if (!filter)
|
|
113
|
-
return entries;
|
|
114
|
-
let result = entries;
|
|
115
|
-
if (filter.status !== undefined) {
|
|
116
|
-
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
117
|
-
result = result.filter((e) => statuses.includes(e.status));
|
|
118
|
-
}
|
|
119
|
-
if (filter.after) {
|
|
120
|
-
result = result.filter((e) => e.quarantinedAt >= filter.after);
|
|
121
|
-
}
|
|
122
|
-
if (filter.before) {
|
|
123
|
-
result = result.filter((e) => e.quarantinedAt <= filter.before);
|
|
124
|
-
}
|
|
125
|
-
if (filter.limit !== undefined) {
|
|
126
|
-
result = result.slice(0, filter.limit);
|
|
127
|
-
}
|
|
128
|
-
return result;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Quarantine workflow — core API for the quarantine/review/resolve lifecycle.
|
|
133
|
-
*
|
|
134
|
-
* Usage (Node.js):
|
|
135
|
-
*
|
|
136
|
-
* ```ts
|
|
137
|
-
* import { scanBytes } from 'pompelmi';
|
|
138
|
-
* import { QuarantineManager, FilesystemQuarantineStorage } from 'pompelmi/quarantine';
|
|
139
|
-
*
|
|
140
|
-
* const quarantine = new QuarantineManager({
|
|
141
|
-
* storage: new FilesystemQuarantineStorage({ dir: './quarantine' }),
|
|
142
|
-
* });
|
|
143
|
-
*
|
|
144
|
-
* const report = await scanBytes(fileBytes, { ctx: { filename: file.name } });
|
|
145
|
-
*
|
|
146
|
-
* if (report.verdict !== 'clean') {
|
|
147
|
-
* const entry = await quarantine.quarantine(fileBytes, report, {
|
|
148
|
-
* originalName: file.name,
|
|
149
|
-
* sizeBytes: fileBytes.length,
|
|
150
|
-
* uploadedBy: req.user?.id,
|
|
151
|
-
* });
|
|
152
|
-
* console.log('Quarantined:', entry.id);
|
|
153
|
-
* }
|
|
154
|
-
* ```
|
|
155
|
-
*
|
|
156
|
-
* @module quarantine/workflow
|
|
157
|
-
*/
|
|
158
|
-
// ── Manager ───────────────────────────────────────────────────────────────────
|
|
159
|
-
/**
|
|
160
|
-
* Manages the full lifecycle of quarantined files:
|
|
161
|
-
* scan → quarantine → review → promote | delete
|
|
162
|
-
*/
|
|
163
|
-
class QuarantineManager {
|
|
164
|
-
constructor(options) {
|
|
165
|
-
this.storage = options.storage;
|
|
166
|
-
this.quarantineSuspicious = options.quarantineSuspicious ?? true;
|
|
167
|
-
this.quarantineMalicious = options.quarantineMalicious ?? true;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Determine whether a scan report should trigger quarantine per the
|
|
171
|
-
* configured policy.
|
|
172
|
-
*/
|
|
173
|
-
shouldQuarantine(report) {
|
|
174
|
-
if (report.verdict === "malicious")
|
|
175
|
-
return this.quarantineMalicious;
|
|
176
|
-
if (report.verdict === "suspicious")
|
|
177
|
-
return this.quarantineSuspicious;
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Quarantine a file: save the bytes in storage, create the metadata entry,
|
|
182
|
-
* and return the entry.
|
|
183
|
-
*
|
|
184
|
-
* @param bytes Raw file bytes.
|
|
185
|
-
* @param report The scan report that triggered quarantine.
|
|
186
|
-
* @param fileInfo Partial metadata; `sha256` is derived from `bytes` if omitted.
|
|
187
|
-
*/
|
|
188
|
-
async quarantine(bytes, report, fileInfo) {
|
|
189
|
-
const id = generateId();
|
|
190
|
-
const sha256 = fileInfo.sha256 ?? computeSha256(bytes);
|
|
191
|
-
const now = new Date().toISOString();
|
|
192
|
-
const storageKey = await this.storage.saveFile(id, bytes);
|
|
193
|
-
const entry = {
|
|
194
|
-
id,
|
|
195
|
-
storageKey,
|
|
196
|
-
file: { ...fileInfo, sha256 },
|
|
197
|
-
scanReport: report,
|
|
198
|
-
quarantinedAt: now,
|
|
199
|
-
status: "pending",
|
|
200
|
-
updatedAt: now,
|
|
201
|
-
};
|
|
202
|
-
await this.storage.saveEntry(entry);
|
|
203
|
-
return entry;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Mark an entry as being actively reviewed.
|
|
207
|
-
*/
|
|
208
|
-
async startReview(id, reviewedBy) {
|
|
209
|
-
return this.storage.updateEntry(id, {
|
|
210
|
-
status: "reviewing",
|
|
211
|
-
reviewedBy,
|
|
212
|
-
updatedAt: new Date().toISOString(),
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Resolve a quarantine entry with a final decision.
|
|
217
|
-
*
|
|
218
|
-
* - `promote`: the file is cleared — bytes remain in storage for the caller
|
|
219
|
-
* to move to its final destination.
|
|
220
|
-
* - `delete`: the bytes are permanently removed from quarantine storage.
|
|
221
|
-
*/
|
|
222
|
-
async resolve(id, review) {
|
|
223
|
-
const entry = await this.storage.getEntry(id);
|
|
224
|
-
if (!entry)
|
|
225
|
-
throw new Error(`Quarantine entry not found: ${id}`);
|
|
226
|
-
if (entry.status === "promoted" || entry.status === "deleted") {
|
|
227
|
-
throw new Error(`Quarantine entry ${id} is already resolved (${entry.status}).`);
|
|
228
|
-
}
|
|
229
|
-
const now = new Date().toISOString();
|
|
230
|
-
const newStatus = review.decision === "promote" ? "promoted" : "deleted";
|
|
231
|
-
if (review.decision === "delete") {
|
|
232
|
-
await this.storage.deleteFile(entry.storageKey);
|
|
233
|
-
}
|
|
234
|
-
return this.storage.updateEntry(id, {
|
|
235
|
-
status: newStatus,
|
|
236
|
-
reviewedBy: review.reviewedBy,
|
|
237
|
-
reviewNote: review.reviewNote,
|
|
238
|
-
resolvedAt: now,
|
|
239
|
-
updatedAt: now,
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Retrieve the raw bytes of a promoted file so the caller can move it to
|
|
244
|
-
* permanent storage. Returns `null` if the entry is not found or has been
|
|
245
|
-
* deleted.
|
|
246
|
-
*/
|
|
247
|
-
async getFile(id) {
|
|
248
|
-
const entry = await this.storage.getEntry(id);
|
|
249
|
-
if (!entry)
|
|
250
|
-
return null;
|
|
251
|
-
return this.storage.getFile(entry.storageKey);
|
|
252
|
-
}
|
|
253
|
-
// ── Query helpers ───────────────────────────────────────────────────────────
|
|
254
|
-
getEntry(id) {
|
|
255
|
-
return this.storage.getEntry(id);
|
|
256
|
-
}
|
|
257
|
-
listEntries(filter) {
|
|
258
|
-
return this.storage.listEntries(filter);
|
|
259
|
-
}
|
|
260
|
-
listPending() {
|
|
261
|
-
return this.storage.listEntries({ status: "pending" });
|
|
262
|
-
}
|
|
263
|
-
countEntries(filter) {
|
|
264
|
-
return this.storage.countEntries(filter);
|
|
265
|
-
}
|
|
266
|
-
// ── Reporting ───────────────────────────────────────────────────────────────
|
|
267
|
-
/**
|
|
268
|
-
* Generate a structured JSON report of all quarantine entries matching the
|
|
269
|
-
* filter — suitable for audit logs and dashboards.
|
|
270
|
-
*/
|
|
271
|
-
async report(filter) {
|
|
272
|
-
const entries = await this.storage.listEntries(filter);
|
|
273
|
-
const byStatus = { pending: 0, reviewing: 0, promoted: 0, deleted: 0 };
|
|
274
|
-
for (const e of entries)
|
|
275
|
-
byStatus[e.status]++;
|
|
276
|
-
return {
|
|
277
|
-
generatedAt: new Date().toISOString(),
|
|
278
|
-
totalEntries: entries.length,
|
|
279
|
-
byStatus,
|
|
280
|
-
entries,
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
285
|
-
function generateId() {
|
|
286
|
-
return crypto.randomUUID();
|
|
287
|
-
}
|
|
288
|
-
function computeSha256(bytes) {
|
|
289
|
-
return crypto.createHash("sha256").update(bytes).digest("hex");
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
export { FilesystemQuarantineStorage, QuarantineManager };
|
|
293
|
-
//# sourceMappingURL=pompelmi.quarantine.esm.js.map
|