Skip to content

Commit 5edcc60

Browse files
committed
Randomize UserAgent and prevent access to private networks for AI extractor
1 parent ad096aa commit 5edcc60

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/Services/InfoProviderSystem/Providers/AIInfoExtractor.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace App\Services\InfoProviderSystem\Providers;
2626

2727
use App\Exceptions\ProviderIDNotSupportedException;
28+
use App\Helpers\RandomizeUseragentHttpClient;
2829
use App\Services\AI\AIPlatformRegistry;
2930
use App\Services\InfoProviderSystem\DTOJsonSchemaConverter;
3031
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
@@ -35,6 +36,7 @@
3536
use Symfony\AI\Platform\Message\Message;
3637
use Symfony\AI\Platform\Message\MessageBag;
3738
use Symfony\Component\DomCrawler\Crawler;
39+
use Symfony\Component\HttpClient\NoPrivateNetworkHttpClient;
3840
use Symfony\Component\Intl\Languages;
3941
use Symfony\Contracts\HttpClient\HttpClientInterface;
4042

@@ -55,12 +57,12 @@ public function __construct(
5557
private readonly AIPlatformRegistry $AIPlatformRegistry,
5658
private readonly DTOJsonSchemaConverter $jsonSchemaConverter,
5759
) {
58-
$this->httpClient = $httpClient->withOptions([
59-
'timeout' => 30,
60-
'headers' => [
61-
'User-Agent' => 'Mozilla/5.0 (compatible; Part-DB AI-Extractor/1.0)',
62-
],
63-
]);
60+
//Use NoPrivateNetworkHttpClient to prevent SSRF vulnerabilities, and RandomizeUseragentHttpClient to make it harder for servers to block us
61+
$this->httpClient = (new RandomizeUseragentHttpClient(new NoPrivateNetworkHttpClient($httpClient)))->withOptions(
62+
[
63+
'timeout' => 15,
64+
]
65+
);
6466
}
6567

6668
public function getProviderInfo(): array
@@ -199,7 +201,7 @@ private function callLLM(string $htmlContent, string $url, ?string $structuredDa
199201
$result = $aiPlatform->invoke($this->settings->model ?? throw new \RuntimeException('No model selected'), $input, [
200202
'response_format' => [
201203
'type' => 'json_schema',
202-
'json_schema' => $this->jsonSchemaConverter->getJSONSchema(),
204+
'json_schema' => $this->jsonSchemaConverter->getJSONSchema(),
203205
]
204206
]);
205207
} catch (\Throwable $e) {

0 commit comments

Comments
 (0)