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

fix(a02): split owner lab into Stage/InspectStaged/Restore steps without duplicate sensitive copies

davoodmehraban89 committed Oct 30, 2026 at 22:17 UTC c790d30e7e020d1e09a7f2b3dc0296b5d0d48fd5
1 file changed +75 -25
scripts/a02-private-owner-local-lab.ps1
+75 -25
@@ -6,9 +6,10 @@ automatic privilege elevation, ACL modification, or automatic DROP DATABASE.
6 #>
7 [CmdletBinding()]
8 param(
9 - [ValidateSet('Test','StageAndInspect','RestoreAndInventory')]
9 + [ValidateSet('Test','Stage','InspectStaged','RestoreAndInventory')]
10 [string]$Mode='Test',
11 [string]$SourceZip='',
12 + [string]$StagedBackupPath='',
13 [string]$ManifestOutputPath='',
14 [string]$StagingConsent='',
15 [string]$RestoreConsent=''
@@ -39,6 +40,36 @@ function Assert-ManifestDestination([string]$path){
40 Require ($drive.IsReady -and $drive.DriveType -eq [IO.DriveType]::Fixed -and
41 $drive.AvailableFreeSpace -ge 100MB) 'A02_MANIFEST_DISK_INSUFFICIENT'
42 }
43 +function Assert-StagedBackup([string]$value){
44 + Require (-not [string]::IsNullOrWhiteSpace($value) -and
45 + $value -match '^[A-Za-z]:\\' -and
46 + $value -notmatch '[/\x00-\x1f<>"|?*]' -and
47 + -not $value.Substring(2).Contains(':')) 'A02_STAGED_BACKUP_PATH_INVALID'
48 + foreach($part in $value.Substring(3).Split('\')){
49 + Require (-not [string]::IsNullOrWhiteSpace($part) -and
50 + $part -ne '.' -and $part -ne '..' -and
51 + -not $part.EndsWith(' ') -and -not $part.EndsWith('.')) 'A02_STAGED_BACKUP_AMBIGUOUS_PATH'
52 + }
53 + $full=[IO.Path]::GetFullPath($value)
54 + $root=[IO.Path]::Combine([Environment]::GetFolderPath(
55 + [Environment+SpecialFolder]::LocalApplicationData),
56 + 'FactorEasyPro','HolooMigrationStaging')
57 + Require (-not [string]::IsNullOrWhiteSpace($root) -and
58 + $full.StartsWith($root+'\',[StringComparison]::OrdinalIgnoreCase)) 'A02_STAGED_BACKUP_NOT_IN_PRIVATE_STAGING'
59 + $relative=$full.Substring($root.Length+1)
60 + Require ($relative -cmatch '(?i)^stage_[0-9a-f]{32}\\HolooBackupCopy\.bak$') 'A02_STAGED_BACKUP_NOT_GENERATED_NAME'
61 + $file=[IO.FileInfo]::new($full)
62 + Require ($file.Exists -and $file.Length -gt 4 -and $file.Length -le 4GB) 'A02_STAGED_BACKUP_FILE_MISSING'
63 + Require (($file.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0) 'A02_STAGED_BACKUP_REPARSE_FILE'
64 + $drive=[IO.DriveInfo]::new($full.Substring(0,1))
65 + Require ($drive.IsReady -and $drive.DriveType -eq [IO.DriveType]::Fixed) 'A02_STAGED_BACKUP_DRIVE_UNSAFE'
66 + $parent=$file.Directory
67 + while($null -ne $parent){
68 + Require (($parent.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0) 'A02_STAGED_BACKUP_REPARSE_PARENT'
69 + $parent=$parent.Parent
70 + }
71 + return $full
72 +}
73 function Invoke-JsonStep([string]$scriptName,[string[]]$args,[string]$expected){
74 $file=Join-Path $PSScriptRoot $scriptName
75 Require ([IO.File]::Exists($file)) 'A02_REQUIRED_HELPER_MISSING'
@@ -55,6 +86,7 @@ function Invoke-JsonStep([string]$scriptName,[string[]]$args,[string]$expected){
86
87 if($Mode -eq 'Test'){
88 Require ([string]::IsNullOrWhiteSpace($SourceZip) -and
89 + [string]::IsNullOrWhiteSpace($StagedBackupPath) -and
90 [string]::IsNullOrWhiteSpace($ManifestOutputPath) -and
91 [string]::IsNullOrWhiteSpace($StagingConsent) -and
92 [string]::IsNullOrWhiteSpace($RestoreConsent)) 'A02_SELFTEST_REQUIRES_SYNTHETIC_ONLY'
@@ -77,30 +109,51 @@ if($Mode -eq 'Test'){
109 }
110
111 try{
80 - Require ($StagingConsent -ceq 'I_AUTHORIZE_PRIVATE_HOLOO_STAGING') 'A02_STAGING_CONSENT_REQUIRED'
81 - Require (-not [string]::IsNullOrWhiteSpace($SourceZip)) 'A02_OWNER_ZIP_PATH_REQUIRED'
112 + if($Mode -eq 'Stage'){
113 + Require ($StagingConsent -ceq 'I_AUTHORIZE_PRIVATE_HOLOO_STAGING' -and
114 + -not [string]::IsNullOrWhiteSpace($SourceZip)) 'A02_STAGING_CONSENT_AND_ZIP_REQUIRED'
115 + Require ([string]::IsNullOrWhiteSpace($StagedBackupPath) -and
116 + [string]::IsNullOrWhiteSpace($ManifestOutputPath) -and
117 + [string]::IsNullOrWhiteSpace($RestoreConsent)) 'A02_STAGE_FORBIDS_RESTORE_ARGUMENTS'
118 + $script:Phase='zip-envelope'
119 + $probe=Invoke-JsonStep 'a02-private-holoo-zip-staging.ps1' @(
120 + '-Mode','Probe','-SourceZip',$SourceZip
121 + ) 'zip-envelope-identified'
122 + Require ($probe.crcVerified -eq $false -and
123 + $probe.databaseModified -eq $false) 'A02_ZIP_PROBE_INVALID'
124 + $script:Phase='zip-stage-private'
125 + $staged=Invoke-JsonStep 'a02-private-holoo-zip-staging.ps1' @(
126 + '-Mode','Stage','-SourceZip',$SourceZip,'-ConsentPhrase',$StagingConsent
127 + ) 'private-local-zip-staged-crc-and-tape-verified'
128 + Require ($staged.crcVerified -eq $true -and
129 + $staged.sqlTapeHeaderVerified -eq $true -and
130 + $staged.sqlBackupVerified -eq $false -and
131 + $staged.databaseModified -eq $false) 'A02_ZIP_STAGE_INCOMPLETE'
132 + $script:BackupPath=Assert-StagedBackup ([string]$staged.stagedBackupPath)
133 + [pscustomobject]@{
134 + status='private-zip-staged-crc-verified'
135 + stagedBackupPath=$script:BackupPath
136 + archiveBytes=$probe.archiveBytes;backupBytes=$staged.backupBytes
137 + crcVerified=$true;sqlBackupVerified=$false;restoreExecuted=$false
138 + financialMigrationPerformed=$false;canImport=$false
139 + } | ConvertTo-Json -Compress
140 + return
141 + }
142 + # Never create a new ZIP/BAK copy at this stage. The operator can grant
143 + # the named SQL service only the minimum read access to THIS existing copy
144 + # in a private and approved environment before SQL preflight.
145 + Require ([string]::IsNullOrWhiteSpace($SourceZip) -and
146 + [string]::IsNullOrWhiteSpace($StagingConsent)) 'A02_RESTORED_STAGE_MUST_NOT_REEXTRACT'
147 + $script:Phase='validate-staged-file'
148 + $script:BackupPath=Assert-StagedBackup $StagedBackupPath
149 if($Mode -eq 'RestoreAndInventory'){
150 Require ($RestoreConsent -ceq 'I_AUTHORIZE_A_NEW_ISOLATED_SQL_DATABASE') 'A02_RESTORE_CONSENT_REQUIRED'
151 $script:Phase='validate-manifest-destination'
152 Assert-ManifestDestination $ManifestOutputPath
153 }else{
87 - Require ([string]::IsNullOrWhiteSpace($ManifestOutputPath) -and
88 - [string]::IsNullOrWhiteSpace($RestoreConsent)) 'A02_INSPECT_FORBIDS_RESTORE_ARGS'
154 + Require ([string]::IsNullOrWhiteSpace($RestoreConsent) -and
155 + [string]::IsNullOrWhiteSpace($ManifestOutputPath)) 'A02_INSPECT_FORBIDS_RESTORE_ARGS'
156 }
90 -
91 - $script:Phase='zip-envelope'
92 - $probe=Invoke-JsonStep 'a02-private-holoo-zip-staging.ps1' @('-Mode','Probe','-SourceZip',$SourceZip) 'zip-envelope-identified'
93 - Require ($probe.databaseModified -eq $false -and $probe.crcVerified -eq $false) 'A02_ZIP_PROBE_INVALID'
94 -
95 - $script:Phase='zip-stage-private'
96 - $staged=Invoke-JsonStep 'a02-private-holoo-zip-staging.ps1' @(
97 - '-Mode','Stage','-SourceZip',$SourceZip,'-ConsentPhrase',$StagingConsent
98 - ) 'private-local-zip-staged-crc-and-tape-verified'
99 - Require ($staged.crcVerified -eq $true -and $staged.sqlTapeHeaderVerified -eq $true -and
100 - $staged.sqlBackupVerified -eq $false -and $staged.databaseModified -eq $false) 'A02_ZIP_STAGE_INCOMPLETE'
101 - $script:BackupPath=[string]$staged.stagedBackupPath
102 - Require (-not [string]::IsNullOrWhiteSpace($script:BackupPath)) 'A02_STAGED_BACKUP_PATH_MISSING'
103 -
157 $script:Phase='sql-header-filelist-verifyonly'
158 $check=Invoke-JsonStep 'a02-isolated-sql-restore.ps1' @(
159 '-Mode','Inspect','-BackupPath',$script:BackupPath
@@ -108,10 +161,9 @@ try{
161 Require ($check.restoreExecuted -eq $false -and
162 $check.financialReconciled -eq $false -and
163 $check.sourceSqlMajor -le $check.engineSqlMajor) 'A02_SQL_INSPECT_CONTRACT_INVALID'
111 -
112 - if($Mode -eq 'StageAndInspect'){
164 + if($Mode -eq 'InspectStaged'){
165 [pscustomobject]@{
114 - status='private-backup-staged-and-sql-verified'
166 + status='private-staged-sql-verifyonly-passed'
167 stagedBackupPath=$script:BackupPath
168 sourceSqlMajor=$check.sourceSqlMajor;engineSqlMajor=$check.engineSqlMajor
169 engineEdition=$check.engineEdition;fileCount=$check.fileCount
@@ -129,7 +181,6 @@ try{
181 $restored.financialReconciled -eq $false -and
182 $restored.importPerformed -eq $false -and
183 $restored.dbccCheckDbPassed -eq $true) 'A02_RESTORE_CONTRACT_INVALID'
132 -
184 $script:Phase='private-schema-export'
185 $schema=Invoke-JsonStep 'a02-isolated-schema-inventory.ps1' @(
186 '-Mode','PrivateManifest','-InstanceName','FINORAMIG',
@@ -148,8 +199,7 @@ try{
199 financialReconciled=$false;financialMigrationPerformed=$false;canImport=$false
200 } | ConvertTo-Json -Compress
201 }catch{
151 - # A failed restore can leave a NEW incomplete DB or plaintext staged backup.
152 - # Never silently delete data or a SQL DB; leave inspection to the owner.
202 + # Failed restore can leave a NEW incomplete DB. Never silently delete it.
203 Write-Error ('A02_PRIVATE_LAB_BLOCKED_AT_'+$script:Phase.ToUpperInvariant().Replace('-','_')+
204 '_NO_IMPORT_MANUAL_REVIEW_OF_PRIVATE_STAGING_AND_SQL_MAY_BE_REQUIRED')
205 exit 2