-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFuzzRequestHandlers.php
More file actions
116 lines (108 loc) · 2.68 KB
/
Copy pathFuzzRequestHandlers.php
File metadata and controls
116 lines (108 loc) · 2.68 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
<?php
declare(strict_types=1);
namespace FediE2EE\PKDServer\Fuzzing;
use FediE2EE\PKD\Crypto\Exceptions\CryptoException;
use FediE2EE\PKDServer\Exceptions\BaseException;
use FediE2EE\PKDServer\Meta\Route;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionMethod;
use ReflectionObject;
use Laminas\Diactoros\{
Stream,
ServerRequest
};
use FediE2EE\PKDServer\RequestHandlers\{
IndexPage
};
use FediE2EE\PKDServer\RequestHandlers\ActivityPub\{
Finger,
Inbox,
UserPage
};
use FediE2EE\PKDServer\RequestHandlers\Api\{
Actor,
BurnDown,
Checkpoint,
Extensions,
GetAuxData,
GetKey,
History,
HistoryCosign,
HistorySince,
HistoryView,
Info,
ListAuxData,
ListKeys,
ReplicaInfo,
Replicas,
Revoke,
ServerPublicKey,
TotpDisenroll,
TotpEnroll,
TotpRotate
};
use PhpFuzzer\Config;
/** @var Config $config */
require_once dirname(__DIR__) . '/vendor/autoload.php';
function get_route(RequestHandlerInterface $handler): ?string
{
$reflection = new ReflectionObject($handler);
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->getName() === 'handle') {
$route = $method->getAttributes(Route::class);
if (count($route) === 1) {
$args = $route[0]->getArguments();
return array_shift($args);
}
}
}
return null;
}
$fuzzableHandlerClasses = [
IndexPage::class,
Finger::class,
Inbox::class,
UserPage::class,
Actor::class,
BurnDown::class,
Checkpoint::class,
Extensions::class,
GetAuxData::class,
GetKey::class,
History::class,
HistoryCosign::class,
HistorySince::class,
HistoryView::class,
Info::class,
ListAuxData::class,
ListKeys::class,
ReplicaInfo::class,
Replicas::class,
Revoke::class,
ServerPublicKey::class,
TotpDisenroll::class,
TotpEnroll::class,
TotpRotate::class,
];
$fuzzableHandlers = [];
foreach ($fuzzableHandlerClasses as $class) {
$cl = new $class();
if (method_exists($cl, 'injectConfig')) {
$cl->injectConfig($GLOBALS['pkdConfig']);
}
$fuzzableHandlers []= $cl;
}
$config->setTarget(function (string $input) use ($fuzzableHandlers) {
$r = random_int(0, count($fuzzableHandlers) - 1);
$handler = $fuzzableHandlers[$r];
$stream = new Stream('php://memory', 'rw');
$stream->write($input);
$stream->rewind();
$uri = get_route($handler);
$request = new ServerRequest([], [], $uri, 'POST', $stream);
try {
$handler->handle($request);
} catch (BaseException|CryptoException) {
// Expected for malformed inputs
}
});