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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
//! Windows path checks for the Kitty graphics protocol file and
//! temporary file mediums.
//!
//! On POSIX the file transmission blocklist is a few prefixes
//! (`/proc`, `/sys`, `/dev`) applied to the canonical path of the
//! opened file. Windows needs more care because some paths are
//! dangerous to even open: a UNC path makes this process resolve and
//! authenticate to the named SMB server, and the Win32 device and NT
//! object namespaces reach raw volumes and named pipes, where the
//! open or the read can block.
//!
//! Everything here is pure string logic, so it is tested on every host.
const std = @import("std");
pub const Error = error{
/// A UNC path such as `\\server\share`, in any mix of `\` and `/`.
/// Opening it resolves the host name and authenticates to the
/// server over SMB.
UncPath,
/// A Win32 device path (`\\.\`, `\\?\`) or an NT object path
/// (`\??\`). These reach raw volumes, named pipes, and through
/// `GLOBALROOT` any kernel object.
DevicePath,
/// A component names a reserved DOS device such as CON or COM1.
/// Windows resolves these to the device from inside any directory.
ReservedDeviceName,
/// The canonical path of an opened file is not `X:\...`, so the
/// file is not on a local volume with a drive letter. The canonical
/// path of a file on an SMB share is a UNC path, for example.
NotDriveAbsolute,
};
/// Check a path supplied by a client before it is opened. This never
/// touches the filesystem. A plain local path of any shape passes:
/// drive-absolute, drive-relative, rooted, or relative. Whether it
/// resolves to a local regular file is decided after the open with
/// `checkCanonicalPath`.
pub fn checkPath(path: []const u8) Error!void {
if (path.len >= 2 and isSep(path[0]) and isSep(path[1])) {
// `\\?\` and `\\.\` are the Win32 device namespace. Anything
// else after two separators is a server name.
if (path.len >= 4 and
(path[2] == '?' or path[2] == '.') and
isSep(path[3])) return error.DevicePath;
return error.UncPath;
}
if (path.len >= 4 and
isSep(path[0]) and
path[1] == '?' and
path[2] == '?' and
isSep(path[3])) return error.DevicePath;
var it = std.mem.splitAny(u8, path, "\\/");
while (it.next()) |component| {
if (isReservedDeviceName(component)) return error.ReservedDeviceName;
}
}
/// Check the canonical path of an opened file, as produced by
/// `GetFinalPathNameByHandle`. The file must be on a local volume with
/// a drive letter, and the checks of `checkPath` apply as well.
pub fn checkCanonicalPath(path: []const u8) Error!void {
if (!isDriveAbsolute(path)) return error.NotDriveAbsolute;
try checkPath(path);
}
/// Returns true if `path` is a drive-absolute path (`C:\...`), the
/// only shape the canonical path of a file on a local volume with a
/// drive letter takes.
pub fn isDriveAbsolute(path: []const u8) bool {
return path.len >= 3 and
std.ascii.isAlphabetic(path[0]) and
path[1] == ':' and
path[2] == '\\';
}
/// Returns true if `c` is a Windows path separator.
fn isSep(c: u8) bool {
return std.fs.path.PathType.windows.isSep(u8, c);
}
/// Returns true if a path component names a reserved DOS device: CON,
/// PRN, AUX, NUL, COM0 to COM9, LPT0 to LPT9, and the superscript digit
/// variants.
fn isReservedDeviceName(component: []const u8) bool {
// The name ends at the extension or stream separator.
const end = std.mem.findAny(u8, component, ".:") orelse component.len;
const stem = std.mem.trimEnd(u8, component[0..end], " ");
if (stem.len < 3) return false;
const base = stem[0..3];
const rest = stem[3..];
if (rest.len == 0) {
return std.ascii.eqlIgnoreCase(base, "CON") or
std.ascii.eqlIgnoreCase(base, "PRN") or
std.ascii.eqlIgnoreCase(base, "AUX") or
std.ascii.eqlIgnoreCase(base, "NUL");
}
if (!std.ascii.eqlIgnoreCase(base, "COM") and
!std.ascii.eqlIgnoreCase(base, "LPT")) return false;
return switch (rest.len) {
1 => std.ascii.isDigit(rest[0]),
// U+00B9, U+00B2, U+00B3 (¹ ² ³) encoded as WTF-8.
2 => rest[0] == 0xC2 and
(rest[1] == 0xB9 or rest[1] == 0xB2 or rest[1] == 0xB3),
else => false,
};
}
test "checkPath rejects UNC paths" {
const testing = std.testing;
const unc = [_][]const u8{
"\\\\server\\share\\tty-graphics-protocol-image.data",
"//server/share/tty-graphics-protocol-image.data",
"\\/server/share/image.data",
"/\\server\\share\\image.data",
"\\\\.x\\share\\image.data",
"\\\\",
};
for (unc) |path| {
try testing.expectError(error.UncPath, checkPath(path));
}
}
test "checkPath rejects device and NT namespaces" {
const testing = std.testing;
const device = [_][]const u8{
"\\\\?\\UNC\\server\\share\\image.data",
"\\\\?\\C:\\Windows\\Temp\\image.data",
"//?/C:/Windows/Temp/image.data",
"\\\\.\\C:\\Windows\\Temp\\image.data",
"\\\\.\\pipe\\image",
"\\\\.\\PhysicalDrive0",
"\\\\?\\GLOBALROOT\\Device\\HarddiskVolume1\\image.data",
"//?/GLOBALROOT/Device/HarddiskVolume1/image.data",
"\\??\\C:\\Windows\\Temp\\image.data",
"\\??\\Device\\HarddiskVolume1\\image.data",
"\\??\\UNC\\server\\share\\image.data",
"/??/C:/image.data",
};
for (device) |path| {
try testing.expectError(error.DevicePath, checkPath(path));
}
}
test "checkPath accepts plain local paths" {
const accepted = [_][]const u8{
"C:\\Windows\\Temp\\tty-graphics-protocol-image.data",
"C:/Windows/Temp/tty-graphics-protocol-image.data",
"c:\\image.data",
"C:image.data",
"\\Windows\\Temp\\image.data",
"/Windows/Temp/image.data",
"image.data",
".zig-cache\\tmp\\image.data",
"C:\\Device\\HarddiskVolume1\\image.data",
"C:\\GLOBALROOT\\image.data",
"C:\\dir\\console.png",
"\\?\\image.data",
"",
};
for (accepted) |path| {
try checkPath(path);
}
}
test "checkPath rejects reserved device names in any component" {
const testing = std.testing;
const reserved = [_][]const u8{
"CON",
"con",
"CON.png",
"CON.tar.gz",
"CON.",
"CON ",
"CON .png",
"NUL:stream",
"PRN",
"AUX",
"nul",
"COM1",
"COM0",
"COM9.rgba",
"com5",
"LPT1",
"LPT9",
"lpt0.png",
"COM\xC2\xB9",
"LPT\xC2\xB3.png",
"C:\\Windows\\Temp\\NUL.rgba",
"C:\\CON\\image.data",
"dir/aux/image.data",
"C:/Windows/Temp/com1",
"..\\PRN",
};
for (reserved) |path| {
try testing.expectError(error.ReservedDeviceName, checkPath(path));
}
const plain = [_][]const u8{
"CONSOLE",
"CON1",
"xCON",
" CON",
"COM",
"COM10",
"COMA",
"COM\xC2\xB9\xC2\xB9",
"COM\xC2\xB0",
"LPT",
"LPT.png",
"conx.png",
"C:",
"C:\\",
"image.data",
"C:\\Windows\\Temp\\tty-graphics-protocol-image.data",
"C:\\Windows\\Temp\\console.png",
"",
};
for (plain) |path| {
try checkPath(path);
}
}
test "checkCanonicalPath requires a drive-absolute local path" {
const testing = std.testing;
try checkCanonicalPath("C:\\Windows\\Temp\\image.data");
try checkCanonicalPath("c:\\image.data");
// Shapes GetFinalPathNameByHandle produces for files that are not
// on a local drive-letter volume.
try testing.expectError(
error.NotDriveAbsolute,
checkCanonicalPath("\\\\server\\share\\image.data"),
);
try testing.expectError(
error.NotDriveAbsolute,
checkCanonicalPath("\\\\?\\UNC\\server\\share\\image.data"),
);
try testing.expectError(
error.NotDriveAbsolute,
checkCanonicalPath("\\\\?\\Volume{383da0b0-717f-41b6-8c36-00500992b58d}\\image.data"),
);
// The raw path checks still apply to the canonical form.
try testing.expectError(
error.ReservedDeviceName,
checkCanonicalPath("C:\\Windows\\Temp\\CON"),
);
}
test isDriveAbsolute {
const testing = std.testing;
try testing.expect(isDriveAbsolute("C:\\Windows\\Temp\\image.data"));
try testing.expect(isDriveAbsolute("c:\\image.data"));
try testing.expect(isDriveAbsolute("Z:\\"));
try testing.expect(!isDriveAbsolute("\\\\server\\share\\image.data"));
try testing.expect(!isDriveAbsolute("UNC\\server\\share\\image.data"));
try testing.expect(!isDriveAbsolute("C:/Windows/image.data"));
try testing.expect(!isDriveAbsolute("C:image.data"));
try testing.expect(!isDriveAbsolute("\\Windows\\image.data"));
try testing.expect(!isDriveAbsolute("1:\\image.data"));
try testing.expect(!isDriveAbsolute("C:"));
try testing.expect(!isDriveAbsolute(""));
}
|