فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
security(a02): reject drive-relative, network-mapped and reparse backup source paths
davoodmehraban89 committed
Oct 30, 2026 at 17:22 UTC
9f50e4d428cec7ac5a19ca0e3727616ecc462c1c
1 file changed
+46
-9
scripts/a02-sql-backup-readonly-preflight.ps1
+46
-9
@@ -31,25 +31,50 @@ function New-SafeBackupCommands([string]$AbsolutePath) {
31
}
32
}
33
34
-function Assert-PrivateLocalBackupFile([string]$PathValue) {
35
- if ([string]::IsNullOrWhiteSpace($PathValue)) {
36
- throw 'BACKUP_FILE_REQUIRED'
37
- }
38
- if (-not [System.IO.Path]::IsPathRooted($PathValue)) {
39
- throw 'BACKUP_ABSOLUTE_PATH_REQUIRED'
34
+function Assert-LocalDrivePathShape([string]$PathValue) {
35
+ # IsPathRooted alone accepts '\relative-to-current-drive', which is
36
+ # ambiguous. Require a fully qualified local DOS drive and safe segments.
37
+ if ([string]::IsNullOrWhiteSpace($PathValue) -or
38
+ $PathValue -notmatch '^[A-Za-z]:\\' -or
39
+ $PathValue -match '[/\x00-\x1f]' -or
40
+ $PathValue.Substring(2).Contains(':')) {
41
+ throw 'BACKUP_LOCAL_DRIVE_PATH_REQUIRED'
42
+ }
43
+ foreach ($segment in $PathValue.Substring(3).Split('\')) {
44
+ if ([string]::IsNullOrWhiteSpace($segment) -or
45
+ $segment -eq '.' -or $segment -eq '..' -or
46
+ $segment.EndsWith(' ') -or $segment.EndsWith('.')) {
47
+ throw 'BACKUP_AMBIGUOUS_PATH_BLOCKED'
48
+ }
49
}
50
+}
51
+
52
+function Assert-PrivateLocalBackupFile([string]$PathValue) {
53
+ Assert-LocalDrivePathShape $PathValue
54
$fullPath = [System.IO.Path]::GetFullPath($PathValue)
42
- if ($fullPath.StartsWith('\\') -or $fullPath.StartsWith('//') -or
43
- $fullPath -match '^\\\\\?\\' -or $fullPath.Substring(2).Contains(':')) {
44
- throw 'BACKUP_NONLOCAL_OR_ALTERNATE_STREAM_BLOCKED'
55
+ # Reject drive letters mapped to a network share. Local USB media is
56
+ # allowed, but the SQL Server service must still be able to read it.
57
+ $drive = New-Object System.IO.DriveInfo($fullPath.Substring(0, 1))
58
+ if ($drive.DriveType -ne [System.IO.DriveType]::Fixed -and
59
+ $drive.DriveType -ne [System.IO.DriveType]::Removable) {
60
+ throw 'BACKUP_NETWORK_DRIVE_BLOCKED'
61
}
62
$info = New-Object System.IO.FileInfo($fullPath)
63
if (-not $info.Exists) {
64
throw 'BACKUP_FILE_NOT_FOUND'
65
}
66
+ # Reject a reparse-point file OR any parent directory junction/symlink.
67
+ # Prevents source redirection from a trusted path to an arbitrary target.
68
if (($info.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
69
throw 'BACKUP_REPARSE_POINT_BLOCKED'
70
}
71
+ $parent = $info.Directory
72
+ while ($null -ne $parent) {
73
+ if (($parent.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
74
+ throw 'BACKUP_PARENT_REPARSE_POINT_BLOCKED'
75
+ }
76
+ $parent = $parent.Parent
77
+ }
78
if ($info.Length -le 0 -or $info.Length -gt 4GB) {
79
throw 'BACKUP_SIZE_OUTSIDE_PREFLIGHT_LIMIT'
80
}
@@ -89,6 +114,18 @@ if ($SelfTest) {
114
"'" + $testPath.Replace("'", "''") + "'")) {
115
throw 'SELFTEST_SQL_LITERAL_MISMATCH'
116
}
117
+ foreach ($badPath in @(
118
+ 'relative\copy.bak', '\rooted\copy.bak',
119
+ '\\server\share\copy.bak', 'C:\safe\..\copy.bak',
120
+ 'C:\safe\.\copy.bak', 'C:\safe\copy.bak:ADS',
121
+ 'C:/safe/copy.bak', 'C:\safe\name.\copy.bak'
122
+ )) {
123
+ $rejected = $false
124
+ try { Assert-LocalDrivePathShape $badPath }
125
+ catch { $rejected = $true }
126
+ if (-not $rejected) { throw 'SELFTEST_UNSAFE_PATH_ACCEPTED' }
127
+ }
128
+ Assert-LocalDrivePathShape 'C:\Private\copy.bak'
129
@{ status = 'selftest-passed'; sourceRead = $false; connectionOpened = $false;
130
databaseModified = $false; canImport = $false } |
131
ConvertTo-Json -Compress