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

qa/native-click-coverage-audit-20261030
a02/strict-export-preflight-20261018 audit/a01-readonly-invoice-metadata-20261101 ci/a06-require-trusted-issuer-for-release ci/main-postmerge-phase-a-validation-20261020 ci/phase-a-controlled-public-release design/issue-5-commercial-corrections-adr docs/a01-a02-a05-oct23-verified-evidence docs/a01-cheque-endorsement-evidence-20261022 docs/a01-cheque-event-date-evidence-20261021 docs/a01-explicit-refund-reason-evidence-20261021 docs/a01-refund-auth-status-20261020 docs/a01-refund-trace-evidence-20261020 docs/a02-102-evidence-20261018 docs/a02-pr104-evidence-20261018 docs/a02-pr106-evidence-20261019 docs/a02-pr108-ten-tests-20261019 docs/a02-private-sql-backup-evidence-20261111 docs/a03-merge-status-sync-20261020 docs/a04-software-gate-acceptance-20261020 docs/feature-matrix-20261014 docs/finora-master-execution-playbook docs/four-phase-execution-20261014 docs/four-phase-roadmap-20261014 docs/holoo-lab-stabilization-phase1-20261111 docs/holoo-runtime-schema-audit-20261111 docs/ops-142-governance-5-1-3-20261030 docs/phase-a-a01-a02-a06-a08-verified-evidence-20261023 docs/phase-a-a01-a05-a06-evidence-20261023 docs/phase-a-independent-acceptance-evidence docs/phase-a-pr110-evidence-20261019 docs/project-status-phase-a-20261014 feat/a01-referenced-return-corrections feat/a01-referenced-returns-on-main feat/a02-atomic-opening-ledger-preflight feat/a02-holoo-compatibility-preflight feat/a02-holoo-declared-control-totals feat/a02-holoo-explicit-csv-gate feat/a02-holoo-safe-zip-intake-offline-webview2-20261030 feat/a02-offline-sql-server-provisioning-20261030 feat/a02-opening-balance-dry-run feat/a02-opening-ledger-admin-ui feat/a02-opt-in-isolated-sql-restore-executor-20261030 feat/a02-private-holoo-zip-staging-20261030 feat/a02-safe-isolated-restore-plan-20261030 feat/a02-sql-backup-header-verifyonly-20261030 feat/a03-audit-schema-atomic-events feat/a03-complete-manual-journal-audit feat/a05-cheque-treasury-reconciliation feat/a06-signed-offline-license-activation feat/a08-synthetic-performance-baseline feat/phase-a-accounting-benchmark-return-preview feat/phase-a-ledger-settlement-diagnostics feature/v2-reconciliation-20261013 fix/a01-atomic-confirm-ui-one-shot-20261031 fix/a01-cheque-clearance-explicit-account-20261023 fix/a01-cheque-contractual-due-date-validity-20261023 fix/a01-cheque-endorsement-counterparty-20261022 fix/a01-cheque-event-date-open-period-20261021 fix/a01-cumulative-return-amount-cap fix/a01-explicit-refund-reason-approval-20261021 fix/a01-manager-authorized-customer-refunds-20261020 fix/a01-no-implicit-refund fix/a01-one-shot-confirm-on-atomic-import-20261031 fix/a01-refund-and-overcollection-net-cash fix/a01-refund-correction-trace-20261020 fix/a02-atomic-excel-master-merge-20261031 fix/a02-canonical-mapping-contract-20261018 fix/a02-disable-destructive-excel-replace-20261031 fix/a02-holoo-invoice-line-coverage-20261023 fix/a02-holoo-preflight-required-invoice-lines-20261023 fix/a02-mapping-contract-20261018 fix/a02-opening-preflight-unbalanced-report fix/a02-orphan-mapping-20261018 fix/a02-private-excel-import-session-20261031 fix/a02-strict-export-input-20261019 fix/a03-append-only-full-backup-import fix/a03-append-only-write-guard fix/a03-block-transaction-clear-append-only-20261016 fix/a03-chain-validation-20261020 fix/a03-company-scoped-audit-data-access fix/a03-deny-direct-audit-write-20261019 fix/a03-escaped-expense-list-20261031 fix/a03-escaped-financial-record-ids-20261031 fix/a03-financial-ledger-actions-and-print-20261031 fix/a04-backup-checksum-preflight fix/a04-company-audit-replay-stable-compare fix/a04-company-backup-source-isolation-20261020 fix/a04-full-restore-posted-financial-integrity fix/a04-reject-mixed-company-backup-20261019 fix/a04-replace-immutable-posted-ledger fix/a04-restore-financial-journal-references fix/a04-service-source-company-boundary-20261020 fix/a05-block-inactive-treasury-settlements fix/a05-cheque-cash-evidence-reconciliation-20261023 fix/a05-cheque-ledger-and-bank-reconciliation-20261023 fix/a05-fiscal-close-invariant-gates fix/a06-backup-restore-user-privileges fix/a06-license-user-company-quota-20261023 fix/a06-malformed-trial-state-fail-closed fix/a06-offline-clock-fail-closed-20261019 fix/a07-indexeddb-blocked-startup fix/a07-offline-bundled-assets fix/a07-version-and-startup-banner fix/issue-7-company-backup-isolation fix/p0-deterministic-fractional-invoice-money-20261031 fix/p0-fractional-quantity-input-20261031 fix/p0-fractional-quantity-integrated-20261031 fix/p0-invoice-posting-invariants-20261031 fix/p0-legacy-stock-and-product-inputs-20261031 fix/p0-reports-immutable-cogs-20261101 fix/p0-verified-financial-percent-20261031 fix/p0-zero-discount-report-revenue-20261101 fix/phase-a-backup-app-version fix/phase-a-backup-integrity-checksum fix/phase-a-backup-record-id-validation-20261014 fix/phase-a-backup-version-validation-20261014 fix/phase-a-closure-pack3-20261101 fix/phase-a-entitlement-payload-validation fix/phase-a-gate-exact-evidence-20261019 fix/phase-a-pack4-stock-scope-20261101 fix/phase-a-pack5-integrated-evidence-20261101 fix/phase-a-pack6-atomic-legacy-20261101 fix/phase-a-restore-envelope-and-rollback fix/phase-a-settings-admin-gate fix/print-browser-header-footer-20261014 fix/q01-expense-security-probe-exact-option-20261031 fix/q01-financial-grid-labels-20261031 fix/q01-form-label-association-20261031 fix/q01-native-evidence-integrity-20261030 fix/q01-number-input-persian-decimal-20261031 fix/q01-responsive-dashboard-and-cheque-table-20261031 fix/q01-safe-ui-action-audit-coverage-20261031 fix/q01-visible-desktop-sidebar-and-safe-confirm-20261030 fix/roadmap-metadata-sync-20261014 gate/a03-audit-metadata-validation-20261019 integration/a02-windows-holoo-setup-candidate-20261030 license-server main perf/a07-lazy-load-excel perf/a08-cheque-reconciliation-account-index-20261023 perf/a08-paginated-reports-20261101 phase1-restore-hardening planning/holoo-execution-20261013 planning/roadmap-project-management-20261014 qa/native-click-coverage-audit-20261030 release/v2.0.0 test/a01-native-endorsement-mandatory-20261023 test/a01-native-purchase-return-guardrails test/a01-native-sale-return-financial-guardrails test/a01-safe-integer-boundary-20261019 test/a02-native-opening-dry-run-rejections test/a02-native-positive-opening-ledger test/a04-native-corrupt-fbackup-20261016 test/a04-native-encrypted-company-backup test/a04-native-encrypted-company-backup-main test/a04-native-late-transaction-rollback test/a04-native-windows-backup-recovery test/a04-real-edge-v6-v7-migration test/a04-real-native-interrupted-restore test/a05-native-draft-ledger-and-repeat-close-20261016 test/a05-native-fiscal-close-guardrails test/a07-native-multipage-print-engine test/a07-native-origin-provenance test/a07-native-webview2-offline-routes test/a07-windows-offline-startup-smoke test/a08-installed-webview2-60k-baseline test/a08-native-webview2-heap-metrics test/a08-windows-synthetic-performance-baseline test/github-write-access-20261009 test/native-win-ui-routes-after-finance-backup test/phase-a-combined-a01-a05-a08-final-evidence test/phase-a-combined-native-acceptance-matrix test/phase-a-ledger-invariants-20261014 test/phase-a-native-all-routes-on-merged-lab test/phase-a-native-encrypted-backup test/phase-a-native-finance-backup-matrix test/phase-a-native-ui-and-backup-smoke test/phase-a-windows-native-installer-lab work/a08-dispatch-activation-20261123 work/a08-memory-stability-20261123 work/finance-curated-20261123
ps1 247 lines 10.5 KB
Raw
1 #requires -Version 5.1
2 <#
3 A02: owner-local private Holoo ZIP intake. This script never connects to SQL
4 Server, never imports accounting records, and never sends data to a network.
5 Mode Test uses a temporary SYNTHETIC archive only, then removes it.
6 Mode Probe reads only ZIP metadata. Mode Stage extracts one private .bak copy
7 into the current Windows user's LOCALAPPDATA after explicit consent.
8 #>
9 [CmdletBinding()]
10 param(
11 [ValidateSet('Test','Probe','Stage')]
12 [string]$Mode = 'Test',
13 [string]$SourceZip = '',
14 [string]$ConsentPhrase = ''
15 )
16 Set-StrictMode -Version Latest
17 $ErrorActionPreference = 'Stop'
18
19 function Assert-PrivateLocalZip([string]$Source) {
20 if ([string]::IsNullOrWhiteSpace($Source) -or
21 $Source -notmatch '^[A-Za-z]:\\' -or
22 $Source -match '[/\x00-\x1f<>"|?*]' -or
23 $Source.Substring(2).Contains(':')) {
24 throw 'A02_SOURCE_NOT_A_LOCAL_ABSOLUTE_PATH'
25 }
26 foreach ($component in $Source.Substring(3).Split('\')) {
27 if ([string]::IsNullOrWhiteSpace($component) -or
28 $component -eq '.' -or $component -eq '..' -or
29 $component.EndsWith(' ') -or $component.EndsWith('.')) {
30 throw 'A02_SOURCE_PATH_AMBIGUOUS'
31 }
32 }
33 $info = [System.IO.FileInfo]::new($Source)
34 if (-not $info.Exists -or $info.Length -le 0 -or $info.Length -gt 2GB) {
35 throw 'A02_SOURCE_ZIP_FILE_OR_SIZE_INVALID'
36 }
37 $drive = [System.IO.DriveInfo]::new($info.FullName.Substring(0, 1))
38 if ($drive.DriveType -ne [System.IO.DriveType]::Fixed -and
39 $drive.DriveType -ne [System.IO.DriveType]::Removable) {
40 throw 'A02_MAPPED_OR_NETWORK_DRIVE_DISALLOWED'
41 }
42 if (($info.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
43 throw 'A02_SOURCE_LINK_DISALLOWED'
44 }
45 $parent = $info.Directory
46 while ($null -ne $parent) {
47 if (($parent.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
48 throw 'A02_SOURCE_PARENT_LINK_DISALLOWED'
49 }
50 $parent = $parent.Parent
51 }
52 return $info.FullName
53 }
54
55 # A local BAK is plaintext financial material. Protect the NEW extraction
56 # directory before any bytes are written, and protect the resulting BAK
57 # explicitly. Never relax permissions on the owner's source ZIP.
58 function Get-A02PrivateSids {
59 $current=[Security.Principal.WindowsIdentity]::GetCurrent().User
60 return @($current.Value,'S-1-5-18','S-1-5-32-544') | Select-Object -Unique
61 }
62 function Assert-A02RestrictedAcl($acl) {
63 $allowed=@(Get-A02PrivateSids)
64 $owner=[Security.Principal.WindowsIdentity]::GetCurrent().User.Value
65 if(-not $acl.AreAccessRulesProtected -or
66 $acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -ne $owner) {
67 throw 'A02_PLAINTEXT_STAGING_ACL_OR_OWNER_UNSAFE'
68 }
69 $rules=@($acl.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]))
70 if($rules.Count -ne $allowed.Count){
71 throw 'A02_PLAINTEXT_STAGING_ACL_RULE_COUNT_UNSAFE'
72 }
73 foreach($rule in $rules){
74 if($rule.IsInherited -or
75 $rule.AccessControlType -ne [Security.AccessControl.AccessControlType]::Allow -or
76 $rule.FileSystemRights -ne [Security.AccessControl.FileSystemRights]::FullControl -or
77 $rule.IdentityReference.Value -notin $allowed){
78 throw 'A02_PLAINTEXT_STAGING_ACL_RULE_UNSAFE'
79 }
80 }
81 }
82 function Protect-A02StageDirectory([string]$path){
83 $sid=[Security.Principal.WindowsIdentity]::GetCurrent().User
84 $acl=[Security.AccessControl.DirectorySecurity]::new()
85 $acl.SetAccessRuleProtection($true,$false)
86 $acl.SetOwner($sid)
87 foreach($sidText in @(Get-A02PrivateSids)){
88 $identity=[Security.Principal.SecurityIdentifier]::new([string]$sidText)
89 $rule=[Security.AccessControl.FileSystemAccessRule]::new(
90 $identity,[Security.AccessControl.FileSystemRights]::FullControl,
91 ([Security.AccessControl.InheritanceFlags]::ContainerInherit -bor
92 [Security.AccessControl.InheritanceFlags]::ObjectInherit),
93 [Security.AccessControl.PropagationFlags]::None,
94 [Security.AccessControl.AccessControlType]::Allow)
95 [void]$acl.AddAccessRule($rule)
96 }
97 $dir=[IO.DirectoryInfo]::new($path)
98 $dir.SetAccessControl($acl)
99 Assert-A02RestrictedAcl $dir.GetAccessControl()
100 }
101 function Protect-A02StageFile([string]$path){
102 $sid=[Security.Principal.WindowsIdentity]::GetCurrent().User
103 $acl=[Security.AccessControl.FileSecurity]::new()
104 $acl.SetAccessRuleProtection($true,$false)
105 $acl.SetOwner($sid)
106 foreach($sidText in @(Get-A02PrivateSids)){
107 $identity=[Security.Principal.SecurityIdentifier]::new([string]$sidText)
108 $rule=[Security.AccessControl.FileSystemAccessRule]::new(
109 $identity,[Security.AccessControl.FileSystemRights]::FullControl,
110 [Security.AccessControl.AccessControlType]::Allow)
111 [void]$acl.AddAccessRule($rule)
112 }
113 $file=[IO.FileInfo]::new($path)
114 $file.SetAccessControl($acl)
115 Assert-A02RestrictedAcl $file.GetAccessControl()
116 }
117 function Invoke-A02AclSyntheticTest {
118 $folder=[IO.Path]::Combine([IO.Path]::GetTempPath(),
119 'A02SyntheticPrivateAcl_'+[guid]::NewGuid().ToString('N'))
120 $null=[IO.Directory]::CreateDirectory($folder)
121 try {
122 Protect-A02StageDirectory $folder
123 $file=[IO.Path]::Combine($folder,'synthetic.bak')
124 [IO.File]::WriteAllBytes($file,[Text.Encoding]::ASCII.GetBytes('TAPE-FAKE-NO-OWNER-DATA'))
125 Protect-A02StageFile $file
126 $folderAcl=([IO.DirectoryInfo]::new($folder)).GetAccessControl()
127 $fileAcl=([IO.FileInfo]::new($file)).GetAccessControl()
128 Assert-A02RestrictedAcl $folderAcl
129 Assert-A02RestrictedAcl $fileAcl
130 # Negative control: public readable access must fail the validator.
131 $everyone=[Security.Principal.SecurityIdentifier]::new('S-1-1-0')
132 $badRule=[Security.AccessControl.FileSystemAccessRule]::new(
133 $everyone,[Security.AccessControl.FileSystemRights]::Read,
134 [Security.AccessControl.AccessControlType]::Allow)
135 [void]$fileAcl.AddAccessRule($badRule)
136 $blocked=$false
137 try { Assert-A02RestrictedAcl $fileAcl } catch { $blocked=$true }
138 if(-not $blocked){throw 'A02_SELFTEST_PUBLIC_PLAINTEXT_ACL_ACCEPTED'}
139 } finally {
140 if([IO.Directory]::Exists($folder)){
141 [IO.Directory]::Delete($folder,$true)
142 }
143 }
144 }
145
146 $sourceFile = $null
147 try {
148 Add-Type -Path (Join-Path $PSScriptRoot 'a02-private-holoo-zip-staging.cs') -ReferencedAssemblies @('System.IO.Compression', 'System.IO.Compression.FileSystem') -ErrorAction Stop
149
150 if ($Mode -eq 'Test') {
151 [Finora.Migration.SafeHolooZip]::SelfTest()
152 Invoke-A02AclSyntheticTest
153 [pscustomobject]@{
154 status = 'synthetic-zip-staging-selftest-passed'
155 ownerBackupUsed = $false
156 sqlConnected = $false
157 financialDataImported = $false
158 canImport = $false
159 } | ConvertTo-Json -Compress
160 return
161 }
162
163 $sourceFile = Assert-PrivateLocalZip $SourceZip
164 $report = [Finora.Migration.SafeHolooZip]::Probe($sourceFile)
165 if ($Mode -eq 'Probe') {
166 [pscustomobject]@{
167 status = 'zip-envelope-identified'
168 archiveBytes = $report.ArchiveBytes
169 backupBytes = $report.BackupBytes
170 crcVerified = $false
171 sqlBackupVerified = $false
172 canImport = $false
173 databaseModified = $false
174 } | ConvertTo-Json -Compress
175 return
176 }
177
178 if ($ConsentPhrase -cne 'I_AUTHORIZE_PRIVATE_HOLOO_STAGING') {
179 throw 'A02_STAGING_EXPLICIT_CONSENT_REQUIRED'
180 }
181 $root = [System.Environment]::GetFolderPath(
182 [System.Environment+SpecialFolder]::LocalApplicationData)
183 if ([string]::IsNullOrWhiteSpace($root) -or
184 $root -notmatch '^[A-Za-z]:\\') {
185 throw 'A02_USER_PRIVATE_STAGING_ROOT_UNAVAILABLE'
186 }
187 $rootDrive = [System.IO.DriveInfo]::new($root.Substring(0, 1))
188 if (-not $rootDrive.IsReady -or
189 $rootDrive.DriveType -ne [System.IO.DriveType]::Fixed -or
190 $rootDrive.AvailableFreeSpace -lt ($report.BackupBytes + 2GB)) {
191 throw 'A02_STAGING_SPACE_INSUFFICIENT'
192 }
193
194 # Staging is under this user's profile. SQL Server service permissions
195 # require a separate explicitly authorized step in a future release.
196 $privateRoot = [System.IO.Path]::Combine(
197 $root, 'FactorEasyPro', 'HolooMigrationStaging')
198 $current = [System.IO.DirectoryInfo]::new($root)
199 while ($null -ne $current) {
200 if (($current.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
201 throw 'A02_PRIVATE_ROOT_LINK_DISALLOWED'
202 }
203 $current = $current.Parent
204 }
205 $null = [System.IO.Directory]::CreateDirectory($privateRoot)
206 # A preexisting junction at our application/staging subfolder must not
207 # redirect extracted private backup bytes outside the user's profile.
208 $stagingParent = [System.IO.DirectoryInfo]::new($privateRoot)
209 while ($null -ne $stagingParent) {
210 if (($stagingParent.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
211 throw 'A02_STAGING_ROOT_LINK_DISALLOWED'
212 }
213 $stagingParent = $stagingParent.Parent
214 }
215 $uniqueName = 'stage_' + [System.Guid]::NewGuid().ToString('N')
216 $newDirectory = [System.IO.Path]::Combine($privateRoot, $uniqueName)
217 $null = [System.IO.Directory]::CreateDirectory($newDirectory)
218 Protect-A02StageDirectory $newDirectory
219 $output = [System.IO.Path]::Combine($newDirectory, 'HolooBackupCopy.bak')
220 try {
221 $staged = [Finora.Migration.SafeHolooZip]::Stage($sourceFile, $output)
222 Protect-A02StageFile $output
223 } catch {
224 # Only delete our newly created staging directory (never source).
225 try { [System.IO.Directory]::Delete($newDirectory, $true) } catch {}
226 throw
227 }
228
229 [pscustomobject]@{
230 status = 'private-local-zip-staged-crc-and-tape-verified'
231 stagedBackupPath = $output
232 stageAclRestricted = $true
233 backupFileAclRestricted = $true
234 backupBytes = $staged.BackupBytes
235 crcVerified = $staged.CrcVerified
236 sqlTapeHeaderVerified = $staged.SqlTapeHeaderVerified
237 sqlBackupVerified = $false
238 sqlConnected = $false
239 databaseModified = $false
240 financialDataImported = $false
241 canImport = $false
242 } | ConvertTo-Json -Compress
243 } catch {
244 # Avoid publishing owner paths, ZIP entry names or raw exception strings.
245 Write-Error 'A02_ZIP_STAGING_FAILED_CHECK_PRIVATE_LOCAL_DIAGNOSTICS'
246 exit 2
247 }