فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

feat(a02): add bounded read-only Holoo ZIP envelope probe

davoodmehraban89 committed Oct 30, 2026 at 16:55 UTC 943d279148db11c04e72416f11b4d122f5d6e785
1 file changed +86
src/core/HolooBackupProbe.mjs new
+86
@@ -0,0 +1,86 @@
1 +// A02: browser-only, read-only ZIP envelope profiler. Never decompress or import.
2 +// Local file bytes and source names are never logged or sent over the network.
3 +const MAX_ARCHIVE_BYTES = 2 * 1024 ** 3;
4 +const MAX_ENTRY_BYTES = 4 * 1024 ** 3 - 1;
5 +const MAX_NAME_BYTES = 255;
6 +const MAX_EOCD_SCAN = 22 + 65535;
7 +const fail = code => { throw new Error(code); };
8 +const LE = (view, offset, n) => n === 2
9 + ? view.getUint16(offset, true) : view.getUint32(offset, true);
10 +
11 +/** Inspect a local ZIP envelope only. Does not attest Holoo, SQL format or ZIP CRC. */
12 +export async function probeHolooBackupZip(file) {
13 + if (!file || typeof file.size !== 'number' ||
14 + typeof file.slice !== 'function' || !Number.isSafeInteger(file.size)) {
15 + throw new TypeError('HOLOO_FILE_REQUIRED');
16 + }
17 + if (file.size < 52) fail('HOLOO_ZIP_TOO_SMALL');
18 + if (file.size > MAX_ARCHIVE_BYTES) fail('HOLOO_ZIP_TOO_LARGE');
19 + const bytes = file.size;
20 + const prefix = new DataView(await file.slice(0, 30).arrayBuffer());
21 + if (LE(prefix, 0, 4) !== 0x04034b50) fail('HOLOO_NOT_ZIP');
22 + const flags = LE(prefix, 6, 2);
23 + if ((flags & 1) || (flags & 0x40)) fail('HOLOO_ENCRYPTED_ZIP');
24 + const method = LE(prefix, 8, 2);
25 + if (method !== 0 && method !== 8) fail('HOLOO_ZIP_METHOD_UNSUPPORTED');
26 + const nameLength = LE(prefix, 26, 2);
27 + const extraLength = LE(prefix, 28, 2);
28 + if (!nameLength || nameLength > MAX_NAME_BYTES ||
29 + 30 + nameLength + extraLength >= bytes) fail('HOLOO_ZIP_INVALID_ENTRY');
30 +
31 + const trailerStart = Math.max(0, bytes - MAX_EOCD_SCAN);
32 + const trailer = new DataView(await file.slice(trailerStart).arrayBuffer());
33 + let eocd = -1;
34 + for (let pos = trailer.byteLength - 22; pos >= 0; pos--) {
35 + if (LE(trailer, pos, 4) === 0x06054b50 &&
36 + pos + 22 + LE(trailer, pos + 20, 2) === trailer.byteLength) {
37 + eocd = pos;
38 + break;
39 + }
40 + }
41 + if (eocd < 0) fail('HOLOO_ZIP_MISSING_END');
42 + if (LE(trailer, eocd + 4, 2) !== 0 || LE(trailer, eocd + 6, 2) !== 0 ||
43 + LE(trailer, eocd + 8, 2) !== 1 || LE(trailer, eocd + 10, 2) !== 1) {
44 + fail('HOLOO_ZIP_SINGLE_ENTRY_REQUIRED');
45 + }
46 + const directorySize = LE(trailer, eocd + 12, 4);
47 + const directoryOffset = LE(trailer, eocd + 16, 4);
48 + const directoryEnd = trailerStart + eocd;
49 + if (directoryOffset === 0xffffffff || directorySize === 0xffffffff ||
50 + directorySize < 46 || directoryOffset + directorySize !== directoryEnd) {
51 + fail('HOLOO_ZIP_CENTRAL_DIRECTORY_INVALID');
52 + }
53 + const central = new DataView(await file.slice(directoryOffset, directoryOffset + 46).arrayBuffer());
54 + if (central.byteLength !== 46 || LE(central, 0, 4) !== 0x02014b50 ||
55 + LE(central, 28, 2) !== nameLength ||
56 + LE(central, 42, 4) !== 0 || LE(central, 10, 2) !== method ||
57 + LE(central, 8, 2) !== flags) fail('HOLOO_ZIP_CENTRAL_DIRECTORY_INVALID');
58 + const compressedSize = LE(central, 20, 4);
59 + const uncompressedSize = LE(central, 24, 4);
60 + if (compressedSize === 0xffffffff || uncompressedSize === 0xffffffff ||
61 + uncompressedSize > MAX_ENTRY_BYTES || compressedSize > bytes ||
62 + 30 + nameLength + extraLength + compressedSize > directoryOffset) {
63 + fail('HOLOO_ZIP_INVALID_SIZES');
64 + }
65 + const nameBytes = new Uint8Array(await file.slice(30, 30 + nameLength).arrayBuffer());
66 + const centralName = new Uint8Array(await file.slice(directoryOffset + 46, directoryOffset + 46 + nameLength).arrayBuffer());
67 + if (centralName.length !== nameLength || nameBytes.some((b, i) => b !== centralName[i])) {
68 + fail('HOLOO_ZIP_ENTRY_MISMATCH');
69 + }
70 + let entryName;
71 + try { entryName = new TextDecoder('utf-8', { fatal: true }).decode(nameBytes); }
72 + catch { fail('HOLOO_ZIP_FILENAME_ENCODING'); }
73 + if (!entryName || entryName.includes('/') || entryName.includes('\\') ||
74 + entryName.includes(':') || entryName.includes('..') ||
75 + /[\u0000-\u001f]/.test(entryName)) fail('HOLOO_ZIP_UNSAFE_ENTRY_NAME');
76 + if (LE(central, 32, 2) !== 0 || LE(central, 30, 2) !== extraLength) {
77 + // Reject ambiguous central metadata rather than implying it was verified.
78 + fail('HOLOO_ZIP_CENTRAL_DIRECTORY_INVALID');
79 + }
80 + return Object.freeze({
81 + kind: 'single-entry-zip', entryName, compressedSize, uncompressedSize,
82 + archiveSize: bytes, method: method === 8 ? 'deflate' : 'stored',
83 + zipEnvelopeIdentified: true, crcVerified: false, sqlBackupVerified: false,
84 + migrationSupported: false, canCommit: false, writePerformed: false
85 + });
86 +}