-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathinit.php
More file actions
59 lines (50 loc) · 1.88 KB
/
Copy pathinit.php
File metadata and controls
59 lines (50 loc) · 1.88 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
<?php
use Framework\Exceptions\DatabaseNotFound;
use Framework\ServiceContainer;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Models\ItemCreator;
use Models\TagCreator;
use function Utils\ssrfGuardMiddleware;
// Bind DB services
ServiceContainer::bind(PDO::class, function () {
$db_path = Config::getDBPath();
if (!file_exists($db_path)) {
throw new DatabaseNotFound();
}
if (!is_writable($db_path)) {
throw new Exception("Database file not writable: {$db_path}");
}
$pdo = new PDO("sqlite:{$db_path}");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
});
ServiceContainer::bind(Models\Repository::class, function (): Models\Repository {
$pdo = ServiceContainer::get(PDO::class);
return new Models\Repository($pdo);
});
ServiceContainer::bind(TagCreator::class, function (): TagCreator {
$pdo = ServiceContainer::get(PDO::class);
return new TagCreator($pdo);
});
ServiceContainer::bind(ItemCreator::class, function (): ItemCreator {
$pdo = ServiceContainer::get(PDO::class);
return new ItemCreator($pdo);
});
// SSRF-safe outbound HTTP client: the guard validates every request and redirect
// hop, pins the connection to the resolved IP and forces TLS, so callers can
// fetch user-supplied URLs without reaching internal resources.
ServiceContainer::bind(Client::class, function (): Client {
$stack = HandlerStack::create();
$stack->after('allow_redirects', ssrfGuardMiddleware(), 'ssrf_guard');
return new Client([
'handler' => $stack,
'verify' => true,
'allow_redirects' => ['max' => 5, 'strict' => true, 'protocols' => ['http', 'https']],
// Many sites serve degraded content to non-browser agents. Applied as a
// default: Guzzle only adds it when the request sets no User-Agent.
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
],
]);
});