فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
feat(a02): fail-closed pure isolated SQL restore and verify plan generator
davoodmehraban89 committed
Oct 30, 2026 at 17:19 UTC
57de448feb2183e91c6ecc7d1f9816d416f3d1ed
1 file changed
+97
src/core/HolooIsolatedRestorePlan.mjs
new
+97
@@ -0,0 +1,97 @@
1
+// A02: pure SQL Server isolated restore plan generator. NO execution or I/O.
2
+// Input must be obtained from a privately verified RESTORE FILELISTONLY result.
3
+// Never send generated SQL/physical paths/logical names to public CI or logs.
4
+const reject = code => { throw new Error(code); };
5
+const safeWindowsPath = input => {
6
+ if (typeof input !== 'string' || input.length > 240 ||
7
+ !/^[A-Za-z]:\\/.test(input) || input.includes('/') ||
8
+ /[<>"|?*\u0000-\u001f]/.test(input)) reject('A02_UNSAFE_LOCAL_PATH');
9
+ const parts = input.slice(3).split('\\');
10
+ if (parts.some(p => !p || p === '.' || p === '..' ||
11
+ p.endsWith(' ') || p.endsWith('.') || p.includes(':'))) {
12
+ reject('A02_UNSAFE_LOCAL_PATH');
13
+ }
14
+ return input;
15
+};
16
+const sqlString = text => {
17
+ if (typeof text !== 'string' || text.length === 0 ||
18
+ /[\u0000-\u001f]/.test(text)) reject('A02_SQL_LITERAL_INVALID');
19
+ return "N'" + text.replaceAll("'", "''") + "'";
20
+};
21
+
22
+/**
23
+ * Generate but NEVER execute a restore plan. The caller must later verify:
24
+ * dedicated local SQL instance, backup/version/VERIFYONLY, target files absent,
25
+ * adequate disk space, admin/user consent, and rollback policy.
26
+ */
27
+export function planIsolatedHolooRestore({
28
+ backupFile, sqlDataDirectory, restoreToken, backupSetPosition = 1,
29
+ files
30
+} = {}) {
31
+ const source = safeWindowsPath(backupFile);
32
+ const directory = safeWindowsPath(sqlDataDirectory);
33
+ if (source.toLowerCase() === directory.toLowerCase() ||
34
+ !/^[0-9a-f]{32}$/.test(restoreToken ?? '')) {
35
+ reject('A02_RESTORE_DESTINATION_OR_TOKEN_INVALID');
36
+ }
37
+ if (backupSetPosition !== 1) reject('A02_BACKUP_SET_UNSUPPORTED');
38
+ if (!Array.isArray(files) || files.length < 2 || files.length > 16) {
39
+ reject('A02_FILELIST_COUNT_INVALID');
40
+ }
41
+ const db = 'FinoraHoloo_' + restoreToken;
42
+ const names = new Set();
43
+ let data = 0, logs = 0;
44
+ const moves = files.map((f, index) => {
45
+ if (!f || typeof f !== 'object' || Array.isArray(f) ||
46
+ !['D', 'L'].includes(f.type) ||
47
+ typeof f.logicalName !== 'string' ||
48
+ f.logicalName.length > 128 ||
49
+ !f.logicalName.trim() ||
50
+ /[\u0000-\u001f]/.test(f.logicalName) ||
51
+ !Number.isSafeInteger(f.sizeBytes) || f.sizeBytes <= 0) {
52
+ reject('A02_FILELIST_ENTRY_INVALID');
53
+ }
54
+ const canonical = f.logicalName.toLocaleLowerCase('en-US');
55
+ if (names.has(canonical)) reject('A02_FILELIST_DUPLICATE_LOGICAL_NAME');
56
+ names.add(canonical);
57
+ if (f.type === 'D') data++; else logs++;
58
+ const extension = f.type === 'L' ? 'ldf' : data === 1 ? 'mdf' : 'ndf';
59
+ const target = directory + '\\' + db + '_file' +
60
+ String(index + 1).padStart(2, '0') + '.' + extension;
61
+ return Object.freeze({
62
+ sourceLogicalName: f.logicalName,
63
+ targetPath: target,
64
+ type: f.type,
65
+ sourceBytes: f.sizeBytes
66
+ });
67
+ });
68
+ if (data < 1 || logs < 1) reject('A02_DATA_AND_LOG_REQUIRED');
69
+ const clause = moves.map(f =>
70
+ 'MOVE ' + sqlString(f.sourceLogicalName) + ' TO ' + sqlString(f.targetPath)
71
+ ).join(', ');
72
+ const base = 'FROM DISK = ' + sqlString(source) +
73
+ ' WITH FILE = 1, ' + clause;
74
+ const verifySql = 'RESTORE VERIFYONLY ' + base + ';';
75
+ const existsCheck =
76
+ 'IF DB_ID(' + sqlString(db) + ') IS NOT NULL ' +
77
+ "THROW 51001, 'A02_ISOLATED_DATABASE_ALREADY_EXISTS', 1;";
78
+ const restoreSql = existsCheck + '\nRESTORE DATABASE [' + db + '] ' +
79
+ base + ', RECOVERY, STATS = 10;';
80
+ const totalBytes = files.reduce((sum, f) => sum + BigInt(f.sizeBytes), 0n);
81
+ return Object.freeze({
82
+ databaseName: db,
83
+ moves: Object.freeze(moves),
84
+ estimatedSourceFileBytes: totalBytes.toString(10),
85
+ verifySql, restoreSql,
86
+ // A plan is NOT approval to run it; executor is intentionally absent.
87
+ requiresExclusiveDedicatedLocalInstance: true,
88
+ requiresTargetFileNonexistenceCheck: true,
89
+ requiresFileSpaceAndVersionChecks: true,
90
+ requiresExplicitOwnerConsent: true,
91
+ requiresAccountantReconciliation: true,
92
+ sourceDataRead: false,
93
+ databaseModified: false,
94
+ restoreExecuted: false,
95
+ canImport: false
96
+ });
97
+}