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

security(a02): fail closed on staged BAK or directory weak ACL and allow only dedicated SQL service read trustee

davoodmehraban89 committed Oct 30, 2026 at 23:03 UTC dd8d12d610fa8bfe06f5ef61e6242122ef3fdaf0
1 file changed +45 -1
scripts/a02-private-owner-local-lab.ps1
+45 -1
@@ -41,6 +41,45 @@ function Assert-ManifestDestination([string]$path){
41 Require ($drive.IsReady -and $drive.DriveType -eq [IO.DriveType]::Fixed -and
42 $drive.AvailableFreeSpace -ge 100MB) 'A02_MANIFEST_DISK_INSUFFICIENT'
43 }
44 +function Assert-A02StagedAcl($descriptor){
45 + $owner=[Security.Principal.WindowsIdentity]::GetCurrent().User.Value
46 + Require ($descriptor.AreAccessRulesProtected -and
47 + $descriptor.GetOwner([Security.Principal.SecurityIdentifier]).Value -eq $owner) 'A02_STAGED_BACKUP_ACL_OWNER_OR_INHERITANCE_UNSAFE'
48 + $trusted=@($owner,'S-1-5-18','S-1-5-32-544') | Select-Object -Unique
49 + # Optional, explicitly operator-authorized read access only for the
50 + # dedicated SQL service SID. Other extra trustees are not accepted.
51 + $serviceSid=$null
52 + try {
53 + $serviceName=[Security.Principal.NTAccount]::new('NT SERVICE','MSSQL$FINORAMIG')
54 + $serviceSid=$serviceName.Translate([Security.Principal.SecurityIdentifier]).Value
55 + } catch { }
56 + $seen=[Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
57 + $rules=@($descriptor.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]))
58 + Require ($rules.Count -ge $trusted.Count -and
59 + $rules.Count -le ($trusted.Count+1)) 'A02_STAGED_BACKUP_ACL_UNEXPECTED_TRUSTEES'
60 + foreach($rule in $rules){
61 + $sid=$rule.IdentityReference.Value
62 + Require (-not $rule.IsInherited -and
63 + $rule.AccessControlType -eq [Security.AccessControl.AccessControlType]::Allow -and
64 + $seen.Add($sid)) 'A02_STAGED_BACKUP_ACL_NONEXPLICIT_OR_DUPLICATE'
65 + if($sid -in $trusted){
66 + Require ($rule.FileSystemRights -eq [Security.AccessControl.FileSystemRights]::FullControl) 'A02_STAGED_BACKUP_CORE_ACCESS_CHANGED'
67 + }else{
68 + Require ($null -ne $serviceSid -and $sid -eq $serviceSid) 'A02_STAGED_BACKUP_EXTRA_TRUSTEE_UNSAFE'
69 + $writeBits=(
70 + [Security.AccessControl.FileSystemRights]::Write -bor
71 + [Security.AccessControl.FileSystemRights]::Modify -bor
72 + [Security.AccessControl.FileSystemRights]::Delete -bor
73 + [Security.AccessControl.FileSystemRights]::ChangePermissions -bor
74 + [Security.AccessControl.FileSystemRights]::TakeOwnership
75 + )
76 + Require (($rule.FileSystemRights -band $writeBits) -eq 0) 'A02_SQL_SERVICE_MUST_BE_READ_ONLY'
77 + }
78 + }
79 + foreach($sid in $trusted){
80 + Require ($seen.Contains($sid)) 'A02_STAGED_BACKUP_CORE_ACCESS_MISSING'
81 + }
82 +}
83 function Assert-StagedBackup([string]$value){
84 Require (-not [string]::IsNullOrWhiteSpace($value) -and
85 $value -match '^[A-Za-z]:\\' -and
@@ -69,6 +108,8 @@ function Assert-StagedBackup([string]$value){
108 Require (($parent.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0) 'A02_STAGED_BACKUP_REPARSE_PARENT'
109 $parent=$parent.Parent
110 }
111 + Assert-A02StagedAcl ($file.GetAccessControl())
112 + Assert-A02StagedAcl ($file.Directory.GetAccessControl())
113 return $full
114 }
115 function Invoke-JsonStep([string]$scriptName,[string[]]$stepArgs,[string]$expected){
@@ -155,6 +196,8 @@ try{
196 ) 'private-local-zip-staged-crc-and-tape-verified'
197 Require ($staged.crcVerified -eq $true -and
198 $staged.sqlTapeHeaderVerified -eq $true -and
199 + $staged.stageAclRestricted -eq $true -and
200 + $staged.backupFileAclRestricted -eq $true -and
201 $staged.sqlBackupVerified -eq $false -and
202 $staged.databaseModified -eq $false) 'A02_ZIP_STAGE_INCOMPLETE'
203 $script:BackupPath=Assert-StagedBackup ([string]$staged.stagedBackupPath)
@@ -164,7 +207,8 @@ try{
207 stagedBackupPath=$script:BackupPath
208 stagedBackupSha256=$stageDigest
209 archiveBytes=$probe.archiveBytes;backupBytes=$staged.backupBytes
167 - crcVerified=$true;sqlBackupVerified=$false;restoreExecuted=$false
210 + crcVerified=$true;stageAclRestricted=$true;backupFileAclRestricted=$true
211 + sqlBackupVerified=$false;restoreExecuted=$false
212 financialMigrationPerformed=$false;canImport=$false
213 } | ConvertTo-Json -Compress
214 return