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

fix(a02): reconstruct truncated PowerShell hash validation and remove duplicated script tail

davoodmehraban89 committed Oct 30, 2026 at 22:25 UTC f55454eec8651515b89a2e5e0504c55804a55df8
1 file changed +23 -75
scripts/a02-private-owner-local-lab.ps1
+23 -75
@@ -169,79 +169,15 @@ try{
169 } | ConvertTo-Json -Compress
170 return
171 }
172 - # Never create a new ZIP/BAK copy at this stage. The operator can grant
173 - # the named SQL service only the minimum read access to THIS existing copy
174 - # in a private and approved environment before SQL preflight.
172 + # Resume with the SAME guarded private copy. No second ZIP extraction.
173 Require ([string]::IsNullOrWhiteSpace($SourceZip) -and
176 - [string]::IsNullOrWhiteSpace($StagingConsent)) 'A02_RESTORED_STAGE_MUST_NOT_REEXTRACT'
174 + [string]::IsNullOrWhiteSpace($StagingConsent)) 'A02_NO_SECOND_ZIP_EXTRACTION'
175 $script:Phase='validate-staged-file'
176 $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'
177 + Require ($ExpectedBackupSha256 -cmatch '^[a-fA-F0-9]{64}$') 'A02_EXPECTED_DIGEST_REQUIRED'
178 + $before=(Get-FileHash -LiteralPath $script:BackupPath -Algorithm SHA256).Hash
179 + Require ($before -ieq $ExpectedBackupSha256) 'A02_BACKUP_CHANGED_SINCE_STAGE'
180 +
181 if($Mode -eq 'RestoreAndInventory'){
182 Require ($RestoreConsent -ceq 'I_AUTHORIZE_A_NEW_ISOLATED_SQL_DATABASE') 'A02_RESTORE_CONSENT_REQUIRED'
183 $script:Phase='validate-manifest-destination'
@@ -250,6 +186,7 @@ try{
186 Require ([string]::IsNullOrWhiteSpace($RestoreConsent) -and
187 [string]::IsNullOrWhiteSpace($ManifestOutputPath)) 'A02_INSPECT_FORBIDS_RESTORE_ARGS'
188 }
189 +
190 $script:Phase='sql-header-filelist-verifyonly'
191 $check=Invoke-JsonStep 'a02-isolated-sql-restore.ps1' @(
192 '-Mode','Inspect','-BackupPath',$script:BackupPath
@@ -257,26 +194,34 @@ try{
194 Require ($check.restoreExecuted -eq $false -and
195 $check.financialReconciled -eq $false -and
196 $check.sourceSqlMajor -le $check.engineSqlMajor) 'A02_SQL_INSPECT_CONTRACT_INVALID'
197 + $after=(Get-FileHash -LiteralPath $script:BackupPath -Algorithm SHA256).Hash
198 + Require ($after -ieq $ExpectedBackupSha256) 'A02_BACKUP_CHANGED_DURING_SQL_VERIFY'
199 +
200 if($Mode -eq 'InspectStaged'){
201 [pscustomobject]@{
202 status='private-staged-sql-verifyonly-passed'
203 stagedBackupPath=$script:BackupPath
204 sourceSqlMajor=$check.sourceSqlMajor;engineSqlMajor=$check.engineSqlMajor
205 engineEdition=$check.engineEdition;fileCount=$check.fileCount
266 - sqlBackupVerified=$true;restoreExecuted=$false
267 - financialMigrationPerformed=$false;canImport=$false
206 + sqlBackupVerified=$true;backupDigestContinuityVerified=$true
207 + restoreExecuted=$false;financialMigrationPerformed=$false;canImport=$false
208 } | ConvertTo-Json -Compress
209 return
210 }
211 +
212 $script:Phase='sql-isolated-restore'
213 $restored=Invoke-JsonStep 'a02-isolated-sql-restore.ps1' @(
273 - '-Mode','Restore','-BackupPath',$script:BackupPath,'-ConsentPhrase',$RestoreConsent
214 + '-Mode','Restore','-BackupPath',$script:BackupPath,
215 + '-ConsentPhrase',$RestoreConsent
216 ) 'isolated-restore-completed-needs-mapping-and-reconciliation'
217 $database=[string]$restored.databaseName
218 Require ($database -cmatch '^FinoraHoloo_[0-9a-f]{32}$' -and
219 $restored.financialReconciled -eq $false -and
220 $restored.importPerformed -eq $false -and
221 $restored.dbccCheckDbPassed -eq $true) 'A02_RESTORE_CONTRACT_INVALID'
222 + $restoredHash=(Get-FileHash -LiteralPath $script:BackupPath -Algorithm SHA256).Hash
223 + Require ($restoredHash -ieq $ExpectedBackupSha256) 'A02_BACKUP_CHANGED_DURING_SQL_RESTORE'
224 +
225 $script:Phase='private-schema-export'
226 $schema=Invoke-JsonStep 'a02-isolated-schema-inventory.ps1' @(
227 '-Mode','PrivateManifest','-InstanceName','FINORAMIG',
@@ -286,17 +231,20 @@ try{
231 $schema.fileAclRestricted -eq $true -and
232 $schema.userRowsRead -eq $false -and
233 $schema.databaseModified -eq $false) 'A02_PRIVATE_SCHEMA_ENCRYPTION_INVALID'
234 +
235 [pscustomobject]@{
236 status='isolated-restore-and-encrypted-schema-created'
237 isolatedDatabaseName=$database;stagedBackupPath=$script:BackupPath
238 encryptedSchemaManifestPath=$ManifestOutputPath
239 tableCount=$schema.tableCount;columnCount=$schema.columnCount
240 sqlBackupVerified=$true;restoreExecuted=$true;dbccCheckDbPassed=$true
241 + backupDigestContinuityVerified=$true
242 financialReconciled=$false;financialMigrationPerformed=$false;canImport=$false
243 } | ConvertTo-Json -Compress
244 }catch{
298 - # Failed restore can leave a NEW incomplete DB. Never silently delete it.
245 + # Failure can leave an incomplete NEW isolated DB and unencrypted BAK copy.
246 + # Never automatically DROP or DELETE owner's private data.
247 Write-Error ('A02_PRIVATE_LAB_BLOCKED_AT_'+$script:Phase.ToUpperInvariant().Replace('-','_')+
300 - '_NO_IMPORT_MANUAL_REVIEW_OF_PRIVATE_STAGING_AND_SQL_MAY_BE_REQUIRED')
248 + '_NO_IMPORT_STAGED_BACKUP_OR_ISOLATED_DB_MAY_REQUIRE_MANUAL_REVIEW')
249 exit 2
250 }