1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! The batch gate's component walker: the final name a path resolves to under Win32's
//! normalisation, as measured.
use is_batch_program;
/// The final component of `prog` as Win32 will resolve it, collapsing `.` and `..` and stripping
/// the trailing dots and spaces Win32 removes from every component.
///
/// `Path::file_name()` is not good enough here, and the gap is exploitable. It returns `None` for
/// any path whose last component is `..`, so `x.bat\y\..` slipped the gate untouched — while
/// `GetFullPathNameW` collapses it straight back to `x.bat`, and `std::process` then detects the
/// batch extension and hands the program to `cmd.exe`. The same file refused as `x.bat` was
/// accepted spelled `x.bat\y\..`.
///
/// Only a segment that is exactly `..` pops, and only one that is exactly `.` is skipped. A
/// segment that is only dots and spaces yet is neither (`...`, `.. `, `.. .`, a lone space) is
/// something else, and what depends on where it stands:
///
/// - **Final**, it trims away to nothing and DROPS OUT, popping nothing. Measured on x64 and arm64
/// runners for `...`, `....`, `.. .`, `.. ..`, `" "`, `". "` and `".. "`: `x.bat\y\` plus any
/// of them resolves to `…\x.bat\y\`, so `y` survives and the batch file stays covered.
/// - **Interior**, it is kept as a name a later `..` pops — a lone trailing `.` comes off, a run of
/// two or more does not, spaces never do. Measured on x64 and arm64 runners, for segments ending
/// in a period and in a space alike, by the Windows path canary's
/// `an_interior_segment_loses_only_a_single_trailing_period`: `y\x.bat\...\..` resolves to
/// `y\x.bat`. Microsoft's path-format rules read it the same way (only the final segment is
/// trimmed, and three or more periods are "a valid file/directory name"), as does Wine's
/// `collapse_path`.
///
/// `None` means the path named no file of its own: it was empty, was a bare root or drive prefix,
/// or popped its own components away. That last case did NOT collapse to nothing — a relative
/// path goes on popping into the ancestors of the current directory, so `x\..` resolves to
/// whatever the cwd is and `.` resolves to the cwd itself, while a rooted one clamps at its root.
/// The name is real, this gate just cannot see it, which is why [`reject_batch_path_on`] refuses
/// `None` rather than accepting it. A data-stream spelling is not that — `x.bat:` names a file and
/// comes back as one.
///
/// # A UNC root has a NAME, and `..` never pops it
///
/// `\\server\share` is a root the way `C:\` is, but unlike `C:\` its last component is a name.
/// Win32 skips server and share before it collapses anything (ReactOS's `RtlpCollapsePath` calls
/// `RtlpSkipUNCPrefix` first; .NET pins `\\LOCALHOST\share5\..` resolving to `\\LOCALHOST\share5`),
/// so no number of `..` reaches past the share, and a share named `x.bat` stays the effective name.
/// Walk a UNC path as if it had no root and every one of `\\srv\x.bat\..`, `//srv/x.bat/..`,
/// `\/srv\x.cmd\..` and `\\srv\x.bat\y\..\..` reduces to `srv` — not a batch
/// name, so the gate returns `Ok` on a token `std::process` hands straight to `cmd.exe`, and
/// `make_bat_command_line` appends the rest of a `.commandline()` verbatim. `GetFullPathNameW`
/// does no I/O, so the share need not exist for that to happen.
///
/// Server and share are POSITIONAL: Win32 takes the two segments after the `\\` without reading
/// them, so a `..`, dots-only, empty or one-space segment there is part of the root rather than an
/// operation on it (measured by the Windows path probe's
/// `dotdot_stops_at_the_unc_share_but_not_at_a_device_name`). A lone `.` or `?` in the server slot is the exception: that is a device root, below.
/// That position has to be exact, not merely deep enough. A floor set one component too DEEP
/// suppresses a pop Win32 performs, and the final name moves to a later component: skip the
/// dots-only server in `\\...\x.bat\y\..` and the root becomes `x.bat\y`, the pop is clamped
/// away, and the gate judges `y` while Win32 resolves `\\...\x.bat`.
///
/// # A device root is `\\.\` alone, and `..` pops the device name
///
/// A device path is not a UNC path with a `.` server. Measured on x64 and arm64 runners (the
/// Windows path probe's `dotdot_stops_at_the_unc_share_but_not_at_a_device_name` canary):
/// `\\.\C:\x.bat\..` resolves to `\\.\C:`, `\\.\C:\..` and `\\.\x.bat\y\..\..` to the bare
/// `\\.\`, and `\\.\C:\..\..\x.bat` to `\\.\x.bat`. So the device name is an ordinary component —
/// `\\.\C:` names `C:`, a device rather than a bare drive — and a path popped back to `\\.\`
/// names no file, which is refused like any other.
///
/// `?` in the same slot is the same root when any separator in the marker is a `/`: `//?/`,
/// `\\?/`, `/\?\` and `\/?\` resolve like `\\?\` in `GetFullPathNameW`, and `//?/` and `\\?/` open
/// `x.bat` through `x.bat.` like a plain path (measured, `verbatim_marker_spellings_resolve_alike`
/// and `a_trailing_dot_or_space_reaches_the_batch_file_only_when_plain`). Only the literal `\\?\`
/// is verbatim to std, and it never arrives: [`verbatim_refusal`] owns it.
///
/// # The token, not the resolved path
///
/// This runs on the program token AS WRITTEN, so it has to predict what that string resolves to
/// instead of resolving it. Two over-refusals are the price, both unchanged by the measurement
/// above. A path that pops past its own first component lands somewhere only the cwd can name, so
/// the gate refuses every one rather than guess. And a path whose final component drops out
/// resolves to a name with a trailing separator — `x.bat\...` is `…\x.bat\`, which std's
/// `has_bat_extension` does NOT read as a batch file — yet the gate judges the exposed `x.bat` and
/// refuses. Moving resolution ahead of the gate collapses both into a suffix test on the resolved
/// path and leaves nothing left to predict.
pub
/// The name a path collapsed onto its UNC root resolves to, judged conservatively.
///
/// Win32 resolves it to `\\server\share`, so the share is the name std tests. The server is
/// judged as well, and a batch-named server is returned in preference to the share: an
/// over-refusal, since a `..` inside the root is never collapsed and the server keeps its trailing
/// dots and spaces (measured: `\\...\x.bat\y\..` resolves to `\\...\x.bat` and `\\srv.\x.bat\y\..`
/// to `\\srv.\x.bat`). A share that trims away to nothing names no file, which is refused too.
/// Whether the path opens with the two separators that make Win32 read a UNC root. Either
/// separator spells it in either position: `\\srv`, `//srv` and `\/srv` are one path to Win32.
/// A bare drive prefix: one UTF-16 unit, then `:`, and nothing else.
/// The byte length of the drive prefix `text` opens with, if any: any ONE UTF-16 unit, then `:`,
/// as Win32 reads `Path[1]` (ReactOS `RtlDetermineDosPathNameType_Ustr`, Wine
/// `RtlDetermineDosPathNameType_U`). `1:`, `é:`, and the U+FFFD a lone surrogate becomes through
/// `to_string_lossy` are drives; `𝒳:` (two units) is not. The rule is
/// `crate::resolve::drive_len`'s, so the batch gate and the resolver read one drive the same way.
pub