فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
test(a02): verify DPAPI private schema manifest roundtrip, PK/FK metadata and restrictive ACL on restored synthetic LocalDB
davoodmehraban89 committed
Oct 30, 2026 at 20:28 UTC
e2f70ba0befd9f89783a5271fb35ae9b0bf5b2e3
1 file changed
+84
scripts/a02-synthetic-sql-localdb-restore-smoke.ps1
+84
@@ -108,6 +108,90 @@ try{
108
$validFingerprint -and $inventory.userRowsRead -eq $false -and
109
$inventory.databaseModified -eq $false -and $inventory.canImport -eq $false)
110
Require $accepted 'A02_SCHEMA_INVENTORY_CONTRACT_FAILED'
111
+ $phase='private-schema-manifest-dpapi-roundtrip'
112
+ $encryptedManifest=Join-Path $folder 'synthetic_schema.dpapi'
113
+ $plainManifest=Join-Path $folder 'synthetic_schema.private.json'
114
+ $manifestOutput=@(& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $inventoryScript -Mode PrivateManifest -InstanceName $instance -DatabaseName $restored -OutputPath $encryptedManifest)
115
+ Require ($LASTEXITCODE -eq 0) 'A02_PRIVATE_SCHEMA_MANIFEST_CREATE_FAILED'
116
+ $manifestStatus=($manifestOutput | Out-String | ConvertFrom-Json)
117
+ Require ($manifestStatus.status -eq 'private-schema-manifest-encrypted' -and
118
+ $manifestStatus.tableCount -eq 1 -and $manifestStatus.columnCount -eq 2 -and
119
+ $manifestStatus.primaryKeyCount -eq 1 -and $manifestStatus.foreignKeyCount -eq 0 -and
120
+ $manifestStatus.dpapiScope -eq 'CurrentUser' -and
121
+ $manifestStatus.fileAclRestricted -eq $true -and
122
+ $manifestStatus.userRowsRead -eq $false -and
123
+ $manifestStatus.databaseModified -eq $false -and
124
+ $manifestStatus.canImport -eq $false) 'A02_PRIVATE_SCHEMA_MANIFEST_CREATE_CONTRACT_FAILED'
125
+ $decryptOutput=@(& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $inventoryScript -Mode DecryptManifest -InputPath $encryptedManifest -OutputPath $plainManifest)
126
+ Require ($LASTEXITCODE -eq 0) 'A02_PRIVATE_SCHEMA_MANIFEST_DECRYPT_FAILED'
127
+ $decryptStatus=($decryptOutput | Out-String | ConvertFrom-Json)
128
+ Require ($decryptStatus.status -eq 'private-schema-manifest-decrypted' -and
129
+ $decryptStatus.plaintextFileAclRestricted -eq $true -and
130
+ $decryptStatus.userRowsRead -eq $false -and
131
+ $decryptStatus.databaseModified -eq $false -and
132
+ $decryptStatus.canImport -eq $false) 'A02_PRIVATE_SCHEMA_MANIFEST_DECRYPT_CONTRACT_FAILED'
133
+ $privateManifest=Get-Content -LiteralPath $plainManifest -Raw | ConvertFrom-Json
134
+ Require ($privateManifest.format -eq 'factor-easypro-a02-schema-manifest' -and
135
+ $privateManifest.formatVersion -eq 1 -and
136
+ $privateManifest.schemaFingerprintSha256 -cmatch '^[0-9a-f]{64} $phase='synthetic-reconciliation'
137
+ $row=QuerySql $conn ('SELECT COUNT_BIG(*) AS C,SUM(amount) AS T FROM ['+$restored+'].dbo.A02Synthetic;')
138
+ Require ($row.Rows.Count -eq 1 -and [long]$row.Rows[0]['C'] -eq 2 -and
139
+ [long]$row.Rows[0]['T'] -eq 80235) 'A02_RESTORED_SYNTHETIC_TOTALS_MISMATCH'
140
+ [pscustomobject]@{
141
+ status='synthetic-localdb-restore-verified';ownerBackupUsed=$false
142
+ headerVerified=$true;fileListVerified=$true;verifyOnlyPassed=$true
143
+ restoreExecuted=$true;dbccPassed=$true;syntheticRecords=2
144
+ syntheticTotal=80235;schemaOnlyInventoryVerified=$true
145
+ privateManifestRoundTripVerified=$true
146
+ financialMigrationPerformed=$false;canImport=$false
147
+ }|ConvertTo-Json -Compress
148
+}catch{
149
+ # Only a bounded phase and exception *type* are public; never emit raw SQL,
150
+ # paths, backup bytes, identity, or exception messages.
151
+ $kind=$_.Exception.GetType().Name
152
+ Write-Error ('A02_SYNTHETIC_LOCALDB_RESTORE_FAILED phase='+$phase+' type='+$kind)
153
+ exit 2
154
+}finally{
155
+ if($null -ne $conn){
156
+ try{
157
+ foreach($name in @($restored,$source)){
158
+ ExecSql $conn ('IF DB_ID('+(Literal $name)+') IS NOT NULL BEGIN '+
159
+ 'ALTER DATABASE ['+$name+'] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; '+
160
+ 'DROP DATABASE ['+$name+']; END;')
161
+ }
162
+ }catch{}
163
+ $conn.Dispose()
164
+ }
165
+ if($created){
166
+ try{& $localdb stop $instance -k | Out-Null}catch{}
167
+ try{& $localdb delete $instance | Out-Null}catch{}
168
+ }
169
+ if([IO.Directory]::Exists($folder)){try{[IO.Directory]::Delete($folder,$true)}catch{}}
170
+}
171
+ -and
172
+ $privateManifest.tables.Count -eq 1 -and
173
+ $privateManifest.tables[0].name -eq 'A02Synthetic' -and
174
+ $privateManifest.tables[0].columns.Count -eq 2 -and
175
+ $privateManifest.tables[0].indexes.Count -eq 1 -and
176
+ $privateManifest.tables[0].indexes[0].primaryKey -eq $true -and
177
+ $privateManifest.tables[0].indexes[0].keyColumns[0].name -eq 'id' -and
178
+ $privateManifest.userRowsRead -eq $false -and
179
+ $privateManifest.databaseModified -eq $false -and
180
+ $privateManifest.canImport -eq $false) 'A02_PRIVATE_SCHEMA_MANIFEST_CONTENT_INVALID'
181
+ $cipherBytes=[IO.File]::ReadAllBytes($encryptedManifest)
182
+ $cipherText=[Text.Encoding]::UTF8.GetString($cipherBytes)
183
+ Require (-not $cipherText.Contains('A02Synthetic') -and
184
+ -not $cipherText.Contains('amount')) 'A02_PRIVATE_SCHEMA_MANIFEST_NOT_ENCRYPTED'
185
+ $acl=Get-Acl -LiteralPath $plainManifest
186
+ Require ($acl.AreAccessRulesProtected) 'A02_PRIVATE_SCHEMA_PLAINTEXT_ACL_NOT_PROTECTED'
187
+ $allowedSids=@([Security.Principal.WindowsIdentity]::GetCurrent().User.Value,'S-1-5-18','S-1-5-32-544') | Select-Object -Unique
188
+ $aclRules=@($acl.GetAccessRules($true,$false,[Security.Principal.SecurityIdentifier]))
189
+ Require ($aclRules.Count -eq $allowedSids.Count) 'A02_PRIVATE_SCHEMA_ACL_RULE_COUNT_INVALID'
190
+ foreach($rule in $aclRules){
191
+ Require ($rule.AccessControlType -eq [Security.AccessControl.AccessControlType]::Allow -and
192
+ $rule.FileSystemRights -eq [Security.AccessControl.FileSystemRights]::FullControl -and
193
+ $rule.IdentityReference.Value -in $allowedSids) 'A02_PRIVATE_SCHEMA_ACL_RULE_INVALID'
194
+ }
195
$phase='synthetic-reconciliation'
196
$row=QuerySql $conn ('SELECT COUNT_BIG(*) AS C,SUM(amount) AS T FROM ['+$restored+'].dbo.A02Synthetic;')
197
Require ($row.Rows.Count -eq 1 -and [long]$row.Rows[0]['C'] -eq 2 -and