فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
feat(a02): add consent-gated private ZIP probe/staging Windows entrypoint
davoodmehraban89 committed
Oct 30, 2026 at 19:17 UTC
3cfe8081dbc59b878909d7ff7b405a6262c71731
1 file changed
+142
scripts/a02-private-holoo-zip-staging.ps1
new
+142
@@ -0,0 +1,142 @@
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
+$sourceFile = $null
56
+try {
57
+ Add-Type -Path (Join-Path $PSScriptRoot 'a02-private-holoo-zip-staging.cs') -ReferencedAssemblies @('System.IO.Compression', 'System.IO.Compression.FileSystem') -ErrorAction Stop
58
+
59
+ if ($Mode -eq 'Test') {
60
+ [Finora.Migration.SafeHolooZip]::SelfTest()
61
+ [pscustomobject]@{
62
+ status = 'synthetic-zip-staging-selftest-passed'
63
+ ownerBackupUsed = $false
64
+ sqlConnected = $false
65
+ financialDataImported = $false
66
+ canImport = $false
67
+ } | ConvertTo-Json -Compress
68
+ return
69
+ }
70
+
71
+ $sourceFile = Assert-PrivateLocalZip $SourceZip
72
+ $report = [Finora.Migration.SafeHolooZip]::Probe($sourceFile)
73
+ if ($Mode -eq 'Probe') {
74
+ [pscustomobject]@{
75
+ status = 'zip-envelope-identified'
76
+ archiveBytes = $report.ArchiveBytes
77
+ backupBytes = $report.BackupBytes
78
+ crcVerified = $false
79
+ sqlBackupVerified = $false
80
+ canImport = $false
81
+ databaseModified = $false
82
+ } | ConvertTo-Json -Compress
83
+ return
84
+ }
85
+
86
+ if ($ConsentPhrase -cne 'I_AUTHORIZE_PRIVATE_HOLOO_STAGING') {
87
+ throw 'A02_STAGING_EXPLICIT_CONSENT_REQUIRED'
88
+ }
89
+ $root = [System.Environment]::GetFolderPath(
90
+ [System.Environment+SpecialFolder]::LocalApplicationData)
91
+ if ([string]::IsNullOrWhiteSpace($root) -or
92
+ $root -notmatch '^[A-Za-z]:\\') {
93
+ throw 'A02_USER_PRIVATE_STAGING_ROOT_UNAVAILABLE'
94
+ }
95
+ $rootDrive = [System.IO.DriveInfo]::new($root.Substring(0, 1))
96
+ if (-not $rootDrive.IsReady -or
97
+ $rootDrive.DriveType -ne [System.IO.DriveType]::Fixed -or
98
+ $rootDrive.AvailableFreeSpace -lt ($report.BackupBytes + 2GB)) {
99
+ throw 'A02_STAGING_SPACE_INSUFFICIENT'
100
+ }
101
+
102
+ # Staging is under this user's profile. SQL Server service permissions
103
+ # require a separate explicitly authorized step in a future release.
104
+ $privateRoot = [System.IO.Path]::Combine(
105
+ $root, 'FactorEasyPro', 'HolooMigrationStaging')
106
+ $current = [System.IO.DirectoryInfo]::new($root)
107
+ while ($null -ne $current) {
108
+ if (($current.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
109
+ throw 'A02_PRIVATE_ROOT_LINK_DISALLOWED'
110
+ }
111
+ $current = $current.Parent
112
+ }
113
+ $null = [System.IO.Directory]::CreateDirectory($privateRoot)
114
+ $uniqueName = 'stage_' + [System.Guid]::NewGuid().ToString('N')
115
+ $newDirectory = [System.IO.Path]::Combine($privateRoot, $uniqueName)
116
+ $null = [System.IO.Directory]::CreateDirectory($newDirectory)
117
+ $output = [System.IO.Path]::Combine($newDirectory, 'HolooBackupCopy.bak')
118
+ try {
119
+ $staged = [Finora.Migration.SafeHolooZip]::Stage($sourceFile, $output)
120
+ } catch {
121
+ # Only delete our newly created staging directory (never source).
122
+ try { [System.IO.Directory]::Delete($newDirectory, $true) } catch {}
123
+ throw
124
+ }
125
+
126
+ [pscustomobject]@{
127
+ status = 'private-local-zip-staged-crc-and-tape-verified'
128
+ stagedBackupPath = $output
129
+ backupBytes = $staged.BackupBytes
130
+ crcVerified = $staged.CrcVerified
131
+ sqlTapeHeaderVerified = $staged.SqlTapeHeaderVerified
132
+ sqlBackupVerified = $false
133
+ sqlConnected = $false
134
+ databaseModified = $false
135
+ financialDataImported = $false
136
+ canImport = $false
137
+ } | ConvertTo-Json -Compress
138
+} catch {
139
+ # Avoid publishing owner paths, ZIP entry names or raw exception strings.
140
+ Write-Error 'A02_ZIP_STAGING_FAILED_CHECK_PRIVATE_LOCAL_DIAGNOSTICS'
141
+ exit 2
142
+}