فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
security(a02): bind private staging and SQL restore to owner-held SHA-256 digest across all steps
davoodmehraban89 committed
Oct 30, 2026 at 22:21 UTC
19f45830776487da41bfa75a146fc0af64163547
1 file changed
+71
scripts/a02-private-owner-local-lab.ps1
+71
@@ -10,6 +10,7 @@ param(
10
[string]$Mode='Test',
11
[string]$SourceZip='',
12
[string]$StagedBackupPath='',
13
+ [string]$ExpectedBackupSha256='',
14
[string]$ManifestOutputPath='',
15
[string]$StagingConsent='',
16
[string]$RestoreConsent=''
@@ -87,6 +88,7 @@ function Invoke-JsonStep([string]$scriptName,[string[]]$stepArgs,[string]$expect
88
if($Mode -eq 'Test'){
89
Require ([string]::IsNullOrWhiteSpace($SourceZip) -and
90
[string]::IsNullOrWhiteSpace($StagedBackupPath) -and
91
+ [string]::IsNullOrWhiteSpace($ExpectedBackupSha256) -and
92
[string]::IsNullOrWhiteSpace($ManifestOutputPath) -and
93
[string]::IsNullOrWhiteSpace($StagingConsent) -and
94
[string]::IsNullOrWhiteSpace($RestoreConsent)) 'A02_SELFTEST_REQUIRES_SYNTHETIC_ONLY'
@@ -138,6 +140,7 @@ try{
140
Require ($StagingConsent -ceq 'I_AUTHORIZE_PRIVATE_HOLOO_STAGING' -and
141
-not [string]::IsNullOrWhiteSpace($SourceZip)) 'A02_STAGING_CONSENT_AND_ZIP_REQUIRED'
142
Require ([string]::IsNullOrWhiteSpace($StagedBackupPath) -and
143
+ [string]::IsNullOrWhiteSpace($ExpectedBackupSha256) -and
144
[string]::IsNullOrWhiteSpace($ManifestOutputPath) -and
145
[string]::IsNullOrWhiteSpace($RestoreConsent)) 'A02_STAGE_FORBIDS_RESTORE_ARGUMENTS'
146
$script:Phase='zip-envelope'
@@ -155,9 +158,11 @@ try{
158
$staged.sqlBackupVerified -eq $false -and
159
$staged.databaseModified -eq $false) 'A02_ZIP_STAGE_INCOMPLETE'
160
$script:BackupPath=Assert-StagedBackup ([string]$staged.stagedBackupPath)
161
+ $stageDigest=(Get-FileHash -LiteralPath $script:BackupPath -Algorithm SHA256).Hash.ToUpperInvariant()
162
[pscustomobject]@{
163
status='private-zip-staged-crc-verified'
164
stagedBackupPath=$script:BackupPath
165
+ stagedBackupSha256=$stageDigest
166
archiveBytes=$probe.archiveBytes;backupBytes=$staged.backupBytes
167
crcVerified=$true;sqlBackupVerified=$false;restoreExecuted=$false
168
financialMigrationPerformed=$false;canImport=$false
@@ -171,6 +176,72 @@ try{
176
[string]::IsNullOrWhiteSpace($StagingConsent)) 'A02_RESTORED_STAGE_MUST_NOT_REEXTRACT'
177
$script:Phase='validate-staged-file'
178
$script:BackupPath=Assert-StagedBackup $StagedBackupPath
179
+ Require ($ExpectedBackupSha256 -cmatch '^[a-fA-F0-9]{64}
180
+ Require ($RestoreConsent -ceq 'I_AUTHORIZE_A_NEW_ISOLATED_SQL_DATABASE') 'A02_RESTORE_CONSENT_REQUIRED'
181
+ $script:Phase='validate-manifest-destination'
182
+ Assert-ManifestDestination $ManifestOutputPath
183
+ }else{
184
+ Require ([string]::IsNullOrWhiteSpace($RestoreConsent) -and
185
+ [string]::IsNullOrWhiteSpace($ManifestOutputPath)) 'A02_INSPECT_FORBIDS_RESTORE_ARGS'
186
+ }
187
+ $script:Phase='sql-header-filelist-verifyonly'
188
+ $check=Invoke-JsonStep 'a02-isolated-sql-restore.ps1' @(
189
+ '-Mode','Inspect','-BackupPath',$script:BackupPath
190
+ ) 'backup-verifyonly-passed'
191
+ Require ($check.restoreExecuted -eq $false -and
192
+ $check.financialReconciled -eq $false -and
193
+ $check.sourceSqlMajor -le $check.engineSqlMajor) 'A02_SQL_INSPECT_CONTRACT_INVALID'
194
+ Require ((Get-FileHash -LiteralPath $script:BackupPath -Algorithm SHA256).Hash -ieq
195
+ $ExpectedBackupSha256) 'A02_STAGED_BACKUP_CHANGED_DURING_VERIFYONLY'
196
+ if($Mode -eq 'InspectStaged'){
197
+ [pscustomobject]@{
198
+ status='private-staged-sql-verifyonly-passed'
199
+ stagedBackupPath=$script:BackupPath
200
+ sourceSqlMajor=$check.sourceSqlMajor;engineSqlMajor=$check.engineSqlMajor
201
+ engineEdition=$check.engineEdition;fileCount=$check.fileCount
202
+ sqlBackupVerified=$true;backupDigestContinuityVerified=$true;restoreExecuted=$false
203
+ financialMigrationPerformed=$false;canImport=$false
204
+ } | ConvertTo-Json -Compress
205
+ return
206
+ }
207
+ $script:Phase='sql-isolated-restore'
208
+ $restored=Invoke-JsonStep 'a02-isolated-sql-restore.ps1' @(
209
+ '-Mode','Restore','-BackupPath',$script:BackupPath,'-ConsentPhrase',$RestoreConsent
210
+ ) 'isolated-restore-completed-needs-mapping-and-reconciliation'
211
+ $database=[string]$restored.databaseName
212
+ Require ($database -cmatch '^FinoraHoloo_[0-9a-f]{32}$' -and
213
+ $restored.financialReconciled -eq $false -and
214
+ $restored.importPerformed -eq $false -and
215
+ $restored.dbccCheckDbPassed -eq $true) 'A02_RESTORE_CONTRACT_INVALID'
216
+ Require ((Get-FileHash -LiteralPath $script:BackupPath -Algorithm SHA256).Hash -ieq
217
+ $ExpectedBackupSha256) 'A02_STAGED_BACKUP_CHANGED_DURING_RESTORE'
218
+ $script:Phase='private-schema-export'
219
+ $schema=Invoke-JsonStep 'a02-isolated-schema-inventory.ps1' @(
220
+ '-Mode','PrivateManifest','-InstanceName','FINORAMIG',
221
+ '-DatabaseName',$database,'-OutputPath',$ManifestOutputPath
222
+ ) 'private-schema-manifest-encrypted'
223
+ Require ($schema.dpapiScope -ceq 'CurrentUser' -and
224
+ $schema.fileAclRestricted -eq $true -and
225
+ $schema.userRowsRead -eq $false -and
226
+ $schema.databaseModified -eq $false) 'A02_PRIVATE_SCHEMA_ENCRYPTION_INVALID'
227
+ [pscustomobject]@{
228
+ status='isolated-restore-and-encrypted-schema-created'
229
+ isolatedDatabaseName=$database;stagedBackupPath=$script:BackupPath
230
+ encryptedSchemaManifestPath=$ManifestOutputPath
231
+ tableCount=$schema.tableCount;columnCount=$schema.columnCount
232
+ sqlBackupVerified=$true;restoreExecuted=$true;dbccCheckDbPassed=$true
233
+ backupDigestContinuityVerified=$true
234
+ financialReconciled=$false;financialMigrationPerformed=$false;canImport=$false
235
+ } | ConvertTo-Json -Compress
236
+}catch{
237
+ # Failed restore can leave a NEW incomplete DB. Never silently delete it.
238
+ Write-Error ('A02_PRIVATE_LAB_BLOCKED_AT_'+$script:Phase.ToUpperInvariant().Replace('-','_')+
239
+ '_NO_IMPORT_MANUAL_REVIEW_OF_PRIVATE_STAGING_AND_SQL_MAY_BE_REQUIRED')
240
+ exit 2
241
+}
242
+) 'A02_EXPECTED_BACKUP_DIGEST_REQUIRED'
243
+ $initialDigest=(Get-FileHash -LiteralPath $script:BackupPath -Algorithm SHA256).Hash
244
+ Require ($initialDigest -ieq $ExpectedBackupSha256) 'A02_STAGED_BACKUP_CHANGED_SINCE_CRC_STAGING'
245
if($Mode -eq 'RestoreAndInventory'){
246
Require ($RestoreConsent -ceq 'I_AUTHORIZE_A_NEW_ISOLATED_SQL_DATABASE') 'A02_RESTORE_CONSENT_REQUIRED'
247
$script:Phase='validate-manifest-destination'