Skip to content

Commit ad35ae6

Browse files
committed
Decorate hte attachment download and generic web provider with the NoPrivateNetworkHttpClient
This is for security hardening to prevent SSRF attacks
1 parent f12f808 commit ad35ae6

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/Services/Attachments/AttachmentSubmitHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use App\Settings\SystemSettings\AttachmentsSettings;
4545
use Hshn\Base64EncodedFile\HttpFoundation\File\Base64EncodedFile;
4646
use Hshn\Base64EncodedFile\HttpFoundation\File\UploadedBase64EncodedFile;
47+
use Symfony\Component\HttpClient\NoPrivateNetworkHttpClient;
4748
use const DIRECTORY_SEPARATOR;
4849
use InvalidArgumentException;
4950
use RuntimeException;
@@ -95,6 +96,8 @@ public function __construct(
9596
UserAttachment::class => 'user',
9697
LabelAttachment::class => 'label_profile',
9798
];
99+
100+
$this->httpClient = new NoPrivateNetworkHttpClient($this->httpClient);
98101
}
99102

100103
/**
@@ -373,6 +376,7 @@ protected function downloadURL(Attachment $attachment, bool $secureAttachment):
373376
],
374377

375378
];
379+
376380
$response = $this->httpClient->request('GET', $url, $opts);
377381
//Digikey wants TLSv1.3, so try again with that if we get a 403
378382
if ($response->getStatusCode() === 403) {
@@ -434,8 +438,8 @@ protected function downloadURL(Attachment $attachment, bool $secureAttachment):
434438
$new_path = $this->pathResolver->realPathToPlaceholder($new_path);
435439
//Save the path to the attachment
436440
$attachment->setInternalPath($new_path);
437-
} catch (TransportExceptionInterface) {
438-
throw new AttachmentDownloadException('Transport error!');
441+
} catch (TransportExceptionInterface $exception) {
442+
throw new AttachmentDownloadException('Transport error: '.$exception->getMessage());
439443
}
440444

441445
return $attachment;

src/Services/InfoProviderSystem/Providers/GenericWebProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use Brick\Schema\SchemaReader;
4343
use Brick\Schema\SchemaTypeList;
4444
use Symfony\Component\DomCrawler\Crawler;
45+
use Symfony\Component\HttpClient\NoPrivateNetworkHttpClient;
4546
use Symfony\Contracts\HttpClient\HttpClientInterface;
4647

4748
class GenericWebProvider implements InfoProviderInterface
@@ -55,7 +56,8 @@ public function __construct(HttpClientInterface $httpClient, private readonly Ge
5556
private readonly ProviderRegistry $providerRegistry, private readonly PartInfoRetriever $infoRetriever,
5657
)
5758
{
58-
$this->httpClient = (new RandomizeUseragentHttpClient($httpClient))->withOptions(
59+
//Use NoPrivateNetworkHttpClient to prevent SSRF vulnerabilities, and RandomizeUseragentHttpClient to make it harder for servers to block us
60+
$this->httpClient = (new RandomizeUseragentHttpClient(new NoPrivateNetworkHttpClient($httpClient)))->withOptions(
5961
[
6062
'timeout' => 15,
6163
]

0 commit comments

Comments
 (0)