| 1 |
package pathutil |
| 2 |
|
| 3 |
import "strings" |
| 4 |
|
| 5 |
// IsNTFSDotGit ports upstream Git's is_ntfs_dotgit. It detects path |
| 6 |
// components that NTFS would resolve to ".git": the canonical name |
| 7 |
// itself and its 8.3 short-name alias "git~1", each followed by any |
| 8 |
// number of trailing spaces or periods (which NTFS silently trims) |
| 9 |
// and an optional Alternate Data Stream suffix (":<stream>"). The |
| 10 |
// bare strings ".git" and "git~1" also match, mirroring upstream. |
| 11 |
// |
| 12 |
// Reference: upstream Git path.c is_ntfs_dotgit at L1415-L1449 |
| 13 |
// in tag v2.54.0[1]. |
| 14 |
// |
| 15 |
// [1]: https://github.com/git/git/blob/v2.54.0/path.c#L1415-L1449 |
| 16 |
func IsNTFSDotGit(part string) bool { |
| 17 |
var i int |
| 18 |
switch { |
| 19 |
case len(part) >= 4 && part[0] == '.' && |
| 20 |
asciiToLower(part[1]) == 'g' && |
| 21 |
asciiToLower(part[2]) == 'i' && |
| 22 |
asciiToLower(part[3]) == 't': |
| 23 |
i = 4 |
| 24 |
case len(part) >= 5 && |
| 25 |
asciiToLower(part[0]) == 'g' && |
| 26 |
asciiToLower(part[1]) == 'i' && |
| 27 |
asciiToLower(part[2]) == 't' && |
| 28 |
part[3] == '~' && part[4] == '1': |
| 29 |
i = 5 |
| 30 |
default: |
| 31 |
return false |
| 32 |
} |
| 33 |
|
| 34 |
for ; i < len(part); i++ { |
| 35 |
c := part[i] |
| 36 |
if c == ':' { |
| 37 |
return true |
| 38 |
} |
| 39 |
if c != '.' && c != ' ' { |
| 40 |
return false |
| 41 |
} |
| 42 |
} |
| 43 |
return true |
| 44 |
} |
| 45 |
|
| 46 |
// WindowsValidPath reports whether part is a valid Windows / NTFS |
| 47 |
// path component for the worktree filesystem abstraction. It rejects |
| 48 |
// NTFS-disguised variants of `.git` and `git~1` (trailing spaces, |
| 49 |
// periods, Alternate Data Streams) and Windows reserved device |
| 50 |
// names. Bare `.git` and `git~1` are allowed at this layer; the |
| 51 |
// caller decides whether they are permissible at the current path |
| 52 |
// position. |
| 53 |
func WindowsValidPath(part string) bool { |
| 54 |
if IsNTFSDotGit(part) && !IsDotGitName(part) { |
| 55 |
return false |
| 56 |
} |
| 57 |
return !isWindowsReservedName(part) |
| 58 |
} |
| 59 |
|
| 60 |
// windowsReservedNames lists the Windows reserved device names. |
| 61 |
// A path component is reserved if its base name (ignoring trailing |
| 62 |
// spaces, extensions, and NTFS Alternate Data Streams) matches one of |
| 63 |
// these case-insensitively. |
| 64 |
// |
| 65 |
// See upstream Git compat/mingw.c is_valid_win32_path(). |
| 66 |
var windowsReservedNames = []string{ |
| 67 |
"CON", "PRN", "AUX", "NUL", |
| 68 |
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", |
| 69 |
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", |
| 70 |
"CONIN$", "CONOUT$", |
| 71 |
} |
| 72 |
|
| 73 |
func isWindowsReservedName(part string) bool { |
| 74 |
for _, name := range windowsReservedNames { |
| 75 |
if len(part) < len(name) { |
| 76 |
continue |
| 77 |
} |
| 78 |
if !strings.EqualFold(part[:len(name)], name) { |
| 79 |
continue |
| 80 |
} |
| 81 |
// Exact match or followed by space, dot, colon (ADS), or separator. |
| 82 |
if len(part) == len(name) { |
| 83 |
return true |
| 84 |
} |
| 85 |
switch part[len(name)] { |
| 86 |
case ' ', '.', ':': |
| 87 |
return true |
| 88 |
} |
| 89 |
} |
| 90 |
return false |
| 91 |
} |
| 92 |
|
| 93 |
// IsNTFSDot ports upstream Git's is_ntfs_dot_generic. It detects NTFS |
| 94 |
// path-component variants of a dotfile name that attackers can use to |
| 95 |
// bypass case-insensitive comparisons against the canonical name on |
| 96 |
// Windows. The dotgit parameter is the lowercase name without the |
| 97 |
// leading dot (e.g. "gitmodules"); shortnamePrefix is the canonical |
| 98 |
// 6-character NTFS short-name prefix used as a fall-back match |
| 99 |
// (e.g. "gi7eba" for ".gitmodules"). |
| 100 |
// |
| 101 |
// Reference: upstream Git path.c is_ntfs_dot_generic at L1451-L1507 |
| 102 |
// in tag v2.54.0[1]. |
| 103 |
// |
| 104 |
// [1]: https://github.com/git/git/blob/v2.54.0/path.c#L1451-L1507 |
| 105 |
func IsNTFSDot(name, dotgit, shortnamePrefix string) bool { |
| 106 |
// onlySpacesAndPeriods returns true when the suffix from start |
| 107 |
// onwards consists only of trailing spaces and periods, possibly |
| 108 |
// terminated by a NTFS Alternate Data Stream colon. Mirrors the |
| 109 |
// only_spaces_and_periods label in upstream's is_ntfs_dot_generic. |
| 110 |
onlySpacesAndPeriods := func(start int) bool { |
| 111 |
for i := start; i < len(name); i++ { |
| 112 |
c := name[i] |
| 113 |
if c == ':' { |
| 114 |
return true |
| 115 |
} |
| 116 |
if c != ' ' && c != '.' { |
| 117 |
return false |
| 118 |
} |
| 119 |
} |
| 120 |
return true |
| 121 |
} |
| 122 |
|
| 123 |
// Pattern 1: ".<dotgit>" prefix + trailing spaces / periods / ADS. |
| 124 |
if len(name) >= len(dotgit)+1 && name[0] == '.' && |
| 125 |
strings.EqualFold(name[1:1+len(dotgit)], dotgit) { |
| 126 |
if onlySpacesAndPeriods(len(dotgit) + 1) { |
| 127 |
return true |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// Pattern 2: standard NTFS short name <dotgit[:6]>~[1-4]. |
| 132 |
if len(dotgit) >= 6 && len(name) >= 8 && |
| 133 |
strings.EqualFold(name[:6], dotgit[:6]) && |
| 134 |
name[6] == '~' && name[7] >= '1' && name[7] <= '4' { |
| 135 |
if onlySpacesAndPeriods(8) { |
| 136 |
return true |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// Pattern 3: fall-back NTFS short name keyed by shortnamePrefix. |
| 141 |
if len(shortnamePrefix) < 6 || len(name) < 8 { |
| 142 |
return false |
| 143 |
} |
| 144 |
sawTilde := false |
| 145 |
i := 0 |
| 146 |
for i < 8 { |
| 147 |
c := name[i] |
| 148 |
switch { |
| 149 |
case sawTilde: |
| 150 |
if c < '0' || c > '9' { |
| 151 |
return false |
| 152 |
} |
| 153 |
case c == '~': |
| 154 |
i++ |
| 155 |
if i >= len(name) || name[i] < '1' || name[i] > '9' { |
| 156 |
return false |
| 157 |
} |
| 158 |
sawTilde = true |
| 159 |
case i >= 6: |
| 160 |
return false |
| 161 |
case c&0x80 != 0: |
| 162 |
return false |
| 163 |
default: |
| 164 |
if asciiToLower(c) != shortnamePrefix[i] { |
| 165 |
return false |
| 166 |
} |
| 167 |
} |
| 168 |
i++ |
| 169 |
} |
| 170 |
return onlySpacesAndPeriods(8) |
| 171 |
} |
| 172 |
|
| 173 |
// IsNTFSDotGitmodules reports whether part is an NTFS-equivalent of |
| 174 |
// ".gitmodules" — the file name (or any of its variants that NTFS |
| 175 |
// would resolve to it) that attackers can use to plant submodule |
| 176 |
// configuration disguised as a symlink. The 6-character canonical |
| 177 |
// short-name prefix "gi7eba" mirrors upstream Git's is_ntfs_dotgitmodules. |
| 178 |
func IsNTFSDotGitmodules(part string) bool { |
| 179 |
return IsNTFSDot(part, "gitmodules", "gi7eba") |
| 180 |
} |
| 181 |
|
| 182 |
func asciiToLower(c byte) byte { |
| 183 |
if c >= 'A' && c <= 'Z' { |
| 184 |
return c + ('a' - 'A') |
| 185 |
} |
| 186 |
return c |
| 187 |
} |