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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::{PathBoundary, StrictPathError};
use std::path::Path;
// ============================================================
// StrictPath-Only Core Security Tests (No virtual-path feature)
// ============================================================
//
// These tests exercise the PathBoundary/StrictPath dimension WITHOUT
// requiring the virtual-path feature. This ensures the 90% use case
// (StrictPath only) has security test coverage even when virtual-path
// is not enabled.
/// Core attack patterns tested through StrictPath only (no VirtualPath required).
/// Mirrors the patterns from the virtual-path-gated test_known_cve_patterns.
#[test]
fn test_strict_path_core_attack_patterns() {
let temp = tempfile::tempdir().unwrap();
let restriction: PathBoundary = PathBoundary::try_new(temp.path()).unwrap();
let attack_patterns: &[&str] = &[
// Classic traversal
"../../../../etc/passwd",
"..\\..\\..\\..\\windows\\system32\\config\\sam",
"..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..",
"../../../../../../proc/self/environ",
"../../../var/log/auth.log",
// Doubled-dot variants
"....//....//....//etc/shadow",
// URL-encoded (literal percent, not decoded)
"..%2F..%2F..%2Fetc%2Fpasswd",
// File protocol
"file:///etc/passwd",
// UNC
"\\\\server\\share\\sensitive.txt",
// Relative dot-segments sneaking upward
"../.env",
"../../config/database.yml",
];
for attack_input in attack_patterns {
let candidate = Path::new(attack_input);
let has_parent = candidate
.components()
.any(|c| matches!(c, std::path::Component::ParentDir));
let is_absolute = candidate.is_absolute() || attack_input.starts_with("file://");
match restriction.strict_join(attack_input) {
Ok(validated_path) => {
// If accepted (e.g., literal percent chars), must remain inside boundary
assert!(
validated_path.strictpath_starts_with(restriction.interop_path()),
"Attack pattern '{attack_input}' escaped boundary: {validated_path:?}"
);
}
Err(StrictPathError::PathEscapesBoundary { .. })
| Err(StrictPathError::PathResolutionError { .. }) => {
// Expected rejection for patterns with parent dirs or absolute paths
assert!(
has_parent || is_absolute,
"Unexpected rejection for benign pattern '{attack_input}'"
);
}
Err(other) => panic!("Unexpected error for '{attack_input}': {other:?}"),
}
}
}
/// Null byte path truncation attack: attackers use embedded NUL to truncate
/// paths in C-based implementations (e.g., `../../../etc/passwd\0.jpg`).
/// Rust's OsString handles NUL differently, but we must ensure no escape.
#[test]
fn test_strict_path_null_byte_truncation_attack() {
use std::ffi::{OsStr, OsString};
let temp = tempfile::tempdir().unwrap();
let restriction: PathBoundary = PathBoundary::try_new(temp.path()).unwrap();
// Truncation attack: path looks like .jpg but NUL truncates to passwd
let truncation_payloads: Vec<OsString> = vec![
{
let mut s = OsString::from("../../../etc/passwd");
s.push(OsStr::new("\0"));
s.push(OsStr::new(".jpg"));
s
},
{
let mut s = OsString::from("safe_file.txt");
s.push(OsStr::new("\0"));
s.push(OsStr::new("/../../../etc/shadow"));
s
},
{
let mut s = OsString::from("uploads/avatar");
s.push(OsStr::new("\0"));
s.push(OsStr::new(".png"));
s
},
];
for attack_input in &truncation_payloads {
match restriction.strict_join(attack_input) {
Ok(validated_path) => {
// If accepted, must remain within boundary
assert!(
validated_path.strictpath_starts_with(restriction.interop_path()),
"Null truncation attack escaped boundary: {validated_path:?}"
);
}
Err(StrictPathError::PathEscapesBoundary { .. })
| Err(StrictPathError::PathResolutionError { .. }) => {
// Clean rejection is acceptable
}
Err(other) => {
panic!("Unexpected error for null truncation payload: {other:?}");
}
}
}
}
/// Empty and whitespace-only path segments should be handled safely.
#[test]
fn test_strict_path_empty_and_whitespace_segments() {
let temp = tempfile::tempdir().unwrap();
let restriction: PathBoundary = PathBoundary::try_new(temp.path()).unwrap();
let edge_case_inputs: &[&str] = &[
// Empty string
"",
// Whitespace-only segments
" ",
" ",
// Path with doubled separators (empty segments)
"path//to//file",
// Path with only separators
"///",
// Path with whitespace segments
"path/ /to/file",
// Current-dir segments
"path/./to/./file",
// Mixed current-dir and empty
"./././.",
// Trailing separator
"data/",
// Leading dot
".",
// Just dots
"..",
];
for attack_input in edge_case_inputs {
match restriction.strict_join(attack_input) {
Ok(validated_path) => {
assert!(
validated_path.strictpath_starts_with(restriction.interop_path()),
"Edge case '{attack_input}' escaped boundary: {validated_path:?}"
);
}
Err(StrictPathError::PathEscapesBoundary { .. })
| Err(StrictPathError::PathResolutionError { .. }) => {
// Clean rejection is acceptable (e.g., ".." escapes)
}
Err(other) => panic!("Unexpected error for '{attack_input}': {other:?}"),
}
}
}
/// Windows device namespace paths (`\\.\`, `\\?\GLOBALROOT\`) must be rejected
/// or safely contained. These can access raw devices and bypass file system security.
#[test]
#[cfg(windows)]
fn test_strict_path_windows_device_paths_rejected() {
let temp = tempfile::tempdir().unwrap();
let restriction: PathBoundary = PathBoundary::try_new(temp.path()).unwrap();
let device_paths: &[&str] = &[
r"\\.\PhysicalDrive0",
r"\\.\CONIN$",
r"\\.\CONOUT$",
r"\\.\pipe\name",
r"\\.\COM1",
r"\\.\PHYSICALDRIVE0",
r"\\?\GLOBALROOT\Device\HarddiskVolume1",
r"\\?\GLOBALROOT\Device\Null",
r"\\.\Volume{GUID}",
];
for device_path in device_paths {
let result = restriction.strict_join(device_path);
// All device namespace paths must be rejected or safely contained
match result {
Ok(validated_path) => {
// If somehow accepted (should not happen), verify containment
assert!(
validated_path.strictpath_starts_with(restriction.interop_path()),
"Device path '{device_path}' escaped boundary: {validated_path:?}"
);
}
Err(StrictPathError::PathEscapesBoundary { .. })
| Err(StrictPathError::PathResolutionError { .. }) => {
// Expected: device paths are absolute and escape the boundary
}
Err(other) => {
panic!("Unexpected error for device path '{device_path}': {other:?}");
}
}
}
}
/// Windows reserved filenames in subdirectories must be properly handled.
/// CON, NUL, AUX etc. are special even when in subdirectories on Windows.
#[test]
#[cfg(windows)]
fn test_strict_path_windows_reserved_names_in_subdirs() {
let temp = tempfile::tempdir().unwrap();
let restriction: PathBoundary = PathBoundary::try_new_create(temp.path()).unwrap();
// Create a subdirectory for testing
let sub = restriction.strict_join("subdir").unwrap();
sub.create_dir_all().unwrap();
let reserved_in_subdirs: &[&str] = &[
"subdir/CON",
"subdir/NUL",
"subdir/AUX",
"subdir/PRN",
"subdir/COM1",
"subdir/LPT1",
"subdir/CON.txt",
"subdir/NUL.txt",
"subdir/COM1.log",
];
for reserved_path in reserved_in_subdirs {
match restriction.strict_join(reserved_path) {
Ok(validated_path) => {
// If accepted, must remain within boundary
assert!(
validated_path.strictpath_starts_with(restriction.interop_path()),
"Reserved name '{reserved_path}' escaped boundary: {validated_path:?}"
);
// On Windows, writes to reserved names may behave unexpectedly
// (e.g., CON maps to console). Verify no security escape occurred.
}
Err(_) => {
// Rejection is acceptable; reserved names on Windows are tricky
}
}
}
}
/// Mixed absolute-relative attacks: absolute path components embedded within
/// relative-looking paths. On Unix, backslashes in paths are literal filename
/// characters, not separators.
#[test]
#[cfg(unix)]
fn test_strict_path_unix_backslash_literal_filename() {
let temp = tempfile::tempdir().unwrap();
let restriction: PathBoundary = PathBoundary::try_new(temp.path()).unwrap();
// On Unix, backslash is a valid filename character, not a separator.
// These should be treated as literal filenames (single component), not traversal.
let backslash_paths: &[&str] = &[r"..\..\etc\passwd", r"..\secret.txt", r"data\..\config"];
for attack_input in backslash_paths {
match restriction.strict_join(attack_input) {
Ok(validated_path) => {
// On Unix, the backslash is a literal filename character.
// The path is treated as a single component, not traversal.
assert!(
validated_path.strictpath_starts_with(restriction.interop_path()),
"Backslash path '{attack_input}' escaped boundary on Unix: {validated_path:?}"
);
}
Err(StrictPathError::PathEscapesBoundary { .. })
| Err(StrictPathError::PathResolutionError { .. }) => {
// Also acceptable: even on Unix, rejecting these is safe
}
Err(other) => panic!("Unexpected error for '{attack_input}': {other:?}"),
}
}
}
/// Double URL encoding attacks: `%252e%252e%252f` decodes to `%2e%2e%2f` (first
/// decode) then `../` (second decode). While URL decoding is the application's
/// responsibility, the literal encoded strings must not escape if passed directly.
#[test]
fn test_strict_path_double_url_encoding_containment() {
let temp = tempfile::tempdir().unwrap();
let restriction: PathBoundary = PathBoundary::try_new(temp.path()).unwrap();
let double_encoded_attacks: &[&str] = &[
// Double-encoded `../`
"%252e%252e%252f%252e%252e%252fetc%252fpasswd",
// Double-encoded `..\\`
"%252e%252e%255c%252e%252e%255cWindows%255cSystem32",
// Triple-encoded for good measure
"%25252e%25252e%25252f",
// Mixed single and double encoding
"%252e%252e/%2e%2e/etc/passwd",
];
for attack_input in double_encoded_attacks {
match restriction.strict_join(attack_input) {
Ok(validated_path) => {
// Literal percent characters are valid filename chars; path
// must still be contained within boundary
assert!(
validated_path.strictpath_starts_with(restriction.interop_path()),
"Double-encoded attack '{attack_input}' escaped boundary: {validated_path:?}"
);
}
Err(StrictPathError::PathEscapesBoundary { .. })
| Err(StrictPathError::PathResolutionError { .. }) => {
// Clean rejection is also acceptable
}
Err(other) => {
panic!("Unexpected error for double-encoded '{attack_input}': {other:?}");
}
}
}
}