-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFuzzNetworkTrait.php
More file actions
140 lines (122 loc) · 4.11 KB
/
Copy pathFuzzNetworkTrait.php
File metadata and controls
140 lines (122 loc) · 4.11 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
<?php
declare(strict_types=1);
namespace FediE2EE\PKDServer\Fuzzing;
use FediE2EE\PKDServer\Exceptions\NetTraitException;
use FediE2EE\PKDServer\Traits\NetworkTrait;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\Stream;
use PhpFuzzer\Config;
use TypeError;
/** @var Config $config */
require_once dirname(__DIR__) . '/vendor/autoload.php';
/**
* Anonymous class that uses NetworkTrait for testing.
*/
$testClass = new class {
use NetworkTrait;
};
$config->setTarget(function (string $input) use ($testClass): void {
// Test IPv4 mask with various mask bits
try {
if (strlen($input) >= 5) {
$maskBits = ord($input[0]) % 64 - 16; // Range: -16 to 47 (to test boundaries)
$potentialIp = substr($input, 1, 15);
if (filter_var($potentialIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$result = $testClass->ipv4Mask($potentialIp, $maskBits);
assert(is_string($result));
}
}
} catch (NetTraitException) {
// Expected for invalid mask bits
}
// Test IPv6 mask with various mask bits
try {
if (strlen($input) >= 40) {
$maskBits = ord($input[0]) % 160 - 16; // Range: -16 to 143 (to test boundaries)
$potentialIp = substr($input, 1, 39);
if (filter_var($potentialIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$result = $testClass->ipv6Mask($potentialIp, $maskBits);
assert(is_string($result));
}
}
} catch (NetTraitException) {
// Expected for invalid mask bits
}
// Test X-Forwarded-For parsing
try {
$stream = new Stream('php://memory', 'rw');
$stream->write($input);
$stream->rewind();
$request = new ServerRequest(
['REMOTE_ADDR' => '127.0.0.1'],
[],
'/',
'POST',
$stream,
['X-Forwarded-For' => $input]
);
// Test with trusted proxy
$result = $testClass->extractIPFromRequest($request, ['127.0.0.1']);
assert(is_string($result));
// Test without trusted proxy
$result = $testClass->extractIPFromRequest($request, []);
assert(is_string($result));
} catch (TypeError) {
// Expected for malformed input
}
// Test getRequestIPSubnet with fuzzed IP in REMOTE_ADDR
try {
$decoded = json_decode($input, true);
if (is_array($decoded)) {
$remoteAddr = $decoded['ip'] ?? $input;
$v4Bits = ($decoded['v4'] ?? 32) % 64;
$v6Bits = ($decoded['v6'] ?? 128) % 192;
$stream = new Stream('php://memory', 'rw');
$request = new ServerRequest(
['REMOTE_ADDR' => $remoteAddr],
[],
'/',
'POST',
$stream
);
$result = $testClass->getRequestIPSubnet(
$request,
[],
$v4Bits,
$v6Bits
);
assert(is_string($result));
}
} catch (TypeError|NetTraitException) {
// Expected for malformed input
}
// Test getRequestActor with fuzzed JSON body
try {
$stream = new Stream('php://memory', 'rw');
$stream->write($input);
$stream->rewind();
$request = new ServerRequest(
['REMOTE_ADDR' => '127.0.0.1'],
[],
'/',
'POST',
$stream
);
$actor = $testClass->getRequestActor($request);
assert(is_null($actor) || is_string($actor));
// Also test domain extraction
$stream->rewind();
$domain = $testClass->getRequestDomain($request);
assert(is_null($domain) || is_string($domain));
} catch (TypeError) {
// Expected for malformed input
}
// Test stringToByteArray and byteArrayToString round-trip
try {
$array = $testClass::stringToByteArray($input);
$result = $testClass->byteArrayToString($array);
assert($result === $input);
} catch (TypeError) {
// Expected for edge cases
}
});