Repository navigation
Expand file tree
/
Copy pathPathValidatorTest.php
More file actions
234 lines (200 loc) · 9.58 KB
/
Copy pathPathValidatorTest.php
File metadata and controls
234 lines (200 loc) · 9.58 KB
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
<?php
declare(strict_types=1);
namespace IDCT\Networking\Ssh\Tests;
use IDCT\Networking\Ssh\Exception\ConfigurationException;
use IDCT\Networking\Ssh\Exception\InvalidPathException;
use IDCT\Networking\Ssh\Exception\SshException;
use IDCT\Networking\Ssh\Path\PathValidator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(PathValidator::class)]
#[UsesClass(InvalidPathException::class)]
#[UsesClass(ConfigurationException::class)]
#[UsesClass(SshException::class)]
final class PathValidatorTest extends TestCase
{
// ─── happy path ─────────────────────────────────────────────────────
/**
* @return iterable<string, array{string}>
*/
public static function validPaths(): iterable
{
yield 'simple absolute' => ['/uploads/file.bin'];
yield 'simple relative' => ['report.csv'];
yield 'nested absolute' => ['/a/b/c/d.txt'];
yield 'nested relative' => ['dir/sub/file.txt'];
yield 'root only' => ['/'];
yield 'filename with spaces' => ['/uploads/my file.txt'];
yield 'utf-8 filename' => ['/uploads/ünîçødé.txt'];
yield 'trailing slash' => ['/uploads/'];
yield 'underscores and hyphens' => ['/var-data/sub_dir/file-name_1.txt'];
yield 'dot inside filename' => ['/uploads/file.with.dots.txt'];
yield 'leading dot filename' => ['/uploads/.hidden'];
yield 'numeric filename' => ['/uploads/12345'];
}
#[DataProvider('validPaths')]
public function testValidPathsAreAccepted(string $path): void
{
self::assertSame($path, PathValidator::validateRemotePath($path));
}
// ─── T1/T5: path traversal ──────────────────────────────────────────
/**
* @return iterable<string, array{string}>
*/
public static function traversalPaths(): iterable
{
yield 'literal ..' => ['..'];
yield 'literal .' => ['.'];
yield 'leading ../' => ['../etc/passwd'];
yield 'middle /../' => ['/uploads/../etc/passwd'];
yield 'trailing /..' => ['/uploads/..'];
yield 'lone . component' => ['/uploads/./file.txt'];
yield 'relative ..' => ['foo/../bar'];
yield 'all-dots multi' => ['./../etc'];
}
#[DataProvider('traversalPaths')]
public function testTraversalPathsAreRejected(string $path): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('not permitted (traversal)');
PathValidator::validateRemotePath($path);
}
// ─── T2/T3: control characters ──────────────────────────────────────
public function testNullByteRejected(): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('null byte');
PathValidator::validateRemotePath("/uploads/good\0bad");
}
public function testCarriageReturnRejected(): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('CR or LF');
PathValidator::validateRemotePath("/uploads/good\rbad");
}
public function testLineFeedRejected(): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('CR or LF');
PathValidator::validateRemotePath("/uploads/good\nbad");
}
public function testPathWithBothCrAndLfRejected(): void
{
// Exercises the short-circuit path where the LHS (`str_contains
// $path, "\r"`) returns true and the RHS never evaluates. Without
// this case the `||` branch coverage for line 74 has one arm
// (LHS-true-RHS-skipped) that's never hit, since the existing
// testCarriageReturnRejected sends a path with `\r` but no `\n`
// — both halves of the OR get evaluated for that specific input.
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('CR or LF');
PathValidator::validateRemotePath("/uploads/g\r\nood");
}
/**
* @return iterable<string, array{string}>
*/
public static function otherControlChars(): iterable
{
yield 'tab' => ["/uploads/good\tbad"];
yield 'bell' => ["/uploads/good\x07bad"];
yield 'escape' => ["/uploads/good\x1Bbad"];
yield 'DEL' => ["/uploads/good\x7Fbad"];
}
#[DataProvider('otherControlChars')]
public function testOtherControlCharsRejected(string $path): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('control character');
PathValidator::validateRemotePath($path);
}
// ─── T4: length / empty ─────────────────────────────────────────────
public function testEmptyPathRejected(): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('must not be empty');
PathValidator::validateRemotePath('');
}
public function testDefaultLengthBoundaryAccepted(): void
{
$path = '/' . str_repeat('a', PathValidator::DEFAULT_MAX_LENGTH - 1);
self::assertSame($path, PathValidator::validateRemotePath($path));
}
public function testOverLengthRejected(): void
{
$path = '/' . str_repeat('a', PathValidator::DEFAULT_MAX_LENGTH);
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('exceeds 4096 bytes');
PathValidator::validateRemotePath($path);
}
public function testCustomMaxLengthHonored(): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('exceeds 10 bytes');
PathValidator::validateRemotePath('/a-path-longer-than-ten', maxLength: 10);
}
// ─── T6: absolute-vs-relative + allowAbsolute flag ─────────────────
public function testAbsoluteAllowedByDefault(): void
{
self::assertSame('/etc/passwd', PathValidator::validateRemotePath('/etc/passwd'));
}
public function testAbsoluteRejectedWhenDisallowed(): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('Absolute remote path not permitted');
PathValidator::validateRemotePath('/etc/passwd', allowAbsolute: false);
}
public function testRelativeAcceptedWhenDisallowed(): void
{
self::assertSame('report.csv', PathValidator::validateRemotePath('report.csv', allowAbsolute: false));
}
// ─── joinRemote semantics ──────────────────────────────────────────
public function testJoinRemoteConcatenatesRelativePath(): void
{
self::assertSame('/uploads/report.csv', PathValidator::joinRemote('/uploads/', 'report.csv'));
}
public function testJoinRemoteWithEmptyPrefixPassesRelative(): void
{
self::assertSame('report.csv', PathValidator::joinRemote('', 'report.csv'));
}
public function testJoinRemoteAbsolutePathBypassesPrefix(): void
{
self::assertSame('/abs/path', PathValidator::joinRemote('/uploads/', '/abs/path'));
}
public function testJoinRemoteRejectsTraversalThroughPrefix(): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('not permitted (traversal)');
PathValidator::joinRemote('/uploads/', '../etc/passwd');
}
public function testJoinRemoteRejectsTraversalInJoinedResult(): void
{
// Caller's path component "files/x" is benign on its own, but the
// (malicious) prefix is the attack — join still rejects via the
// second-pass validation on the composed string.
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('not permitted (traversal)');
PathValidator::joinRemote('/safe/../', 'files/x');
}
public function testJoinRemoteRejectsEmptyPathInput(): void
{
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('must not be empty');
PathValidator::joinRemote('/uploads/', '');
}
// ─── exception contract ────────────────────────────────────────────
public function testInvalidPathExceptionExtendsConfigurationAndShsException(): void
{
try {
PathValidator::validateRemotePath('');
self::fail('expected exception');
} catch (InvalidPathException $e) {
// Single-parent rule: every library exception extends SshException;
// InvalidPathException specifically also extends ConfigurationException
// so callers can narrow on "bad caller input" without enumerating types.
self::assertInstanceOf(ConfigurationException::class, $e);
self::assertInstanceOf(SshException::class, $e);
}
}
}