From 1aa67747bb9781c7b08ed5d5ac51158fa6b3e319 Mon Sep 17 00:00:00 2001 From: Adrien ERAUD Date: Tue, 18 Feb 2025 12:22:36 +0100 Subject: [PATCH 1/2] Define a custom user-agent for wallabag --- app/config/services.yml | 3 +++ app/config/wallabag.yml | 1 + src/SiteConfig/LoginFormAuthenticator.php | 5 +++++ tests/SiteConfig/LoginFormAuthenticatorTest.php | 12 ++++++------ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/config/services.yml b/app/config/services.yml index 620c71087..5aeb4a94f 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -31,6 +31,7 @@ services: $supportUrl: '@=service(''craue_config'').get(''wallabag_support_url'')' $fonts: '%wallabag.fonts%' $defaultIgnoreOriginInstanceRules: '%wallabag.default_ignore_origin_instance_rules%' + $defaultUserAgent: "%wallabag.user_agent%" Wallabag\: resource: '../../src/*' @@ -206,6 +207,8 @@ services: $config: error_message: '%wallabag.fetching_error_message%' error_message_title: '%wallabag.fetching_error_message_title%' + http_client: + ua_browser: "%wallabag.user_agent%" $client: '@psr18.wallabag.client' calls: - [ setLogger, [ "@logger" ] ] diff --git a/app/config/wallabag.yml b/app/config/wallabag.yml index 02106a33b..98f8d77c2 100644 --- a/app/config/wallabag.yml +++ b/app/config/wallabag.yml @@ -183,3 +183,4 @@ parameters: - 'text/html' - 'application/vnd.ms-excel' wallabag.resource_dir: "%kernel.project_dir%/web/uploads/import" + wallabag.user_agent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.92 Safari/535.2" diff --git a/src/SiteConfig/LoginFormAuthenticator.php b/src/SiteConfig/LoginFormAuthenticator.php index 5afd89648..391cda9bb 100644 --- a/src/SiteConfig/LoginFormAuthenticator.php +++ b/src/SiteConfig/LoginFormAuthenticator.php @@ -14,6 +14,7 @@ class LoginFormAuthenticator public function __construct( private readonly HttpBrowser $browser, AuthenticatorProvider $authenticatorProvider, + private readonly string $defaultUserAgent, ) { $this->expressionLanguage = new ExpressionLanguage(null, [$authenticatorProvider]); } @@ -84,6 +85,10 @@ class LoginFormAuthenticator $headers["HTTP_$headerName"] = $headerValue; } + if (empty($headers['HTTP_user-agent'])) { + $headers['HTTP_user-agent'] = $this->defaultUserAgent; + } + return $headers; } diff --git a/tests/SiteConfig/LoginFormAuthenticatorTest.php b/tests/SiteConfig/LoginFormAuthenticatorTest.php index 6b87cd605..b4295cfc8 100644 --- a/tests/SiteConfig/LoginFormAuthenticatorTest.php +++ b/tests/SiteConfig/LoginFormAuthenticatorTest.php @@ -37,7 +37,7 @@ class LoginFormAuthenticatorTest extends TestCase $requestHtmlFunctionClient = new MockHttpClient([$requestHtmlFunctionResponse]); $authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient); - $auth = new LoginFormAuthenticator($browser, $authenticatorProvider); + $auth = new LoginFormAuthenticator($browser, $authenticatorProvider, 'Wallabag (Symfony HttpClient/5)'); $res = $auth->login($siteConfig); @@ -68,7 +68,7 @@ class LoginFormAuthenticatorTest extends TestCase $requestHtmlFunctionClient = new MockHttpClient([$requestHtmlFunctionResponse]); $authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient); - $auth = new LoginFormAuthenticator($browser, $authenticatorProvider); + $auth = new LoginFormAuthenticator($browser, $authenticatorProvider, 'Wallabag (Symfony HttpClient/5)'); $res = $auth->login($siteConfig); @@ -126,7 +126,7 @@ class LoginFormAuthenticatorTest extends TestCase ; $authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient); - $auth = new LoginFormAuthenticator($browser, $authenticatorProvider); + $auth = new LoginFormAuthenticator($browser, $authenticatorProvider, 'Wallabag (Symfony HttpClient/5)'); $res = $auth->login($siteConfig); @@ -151,7 +151,7 @@ class LoginFormAuthenticatorTest extends TestCase $requestHtmlFunctionClient = new MockHttpClient([$requestHtmlFunctionResponse]); $authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient); - $auth = new LoginFormAuthenticator($browser, $authenticatorProvider); + $auth = new LoginFormAuthenticator($browser, $authenticatorProvider, 'Wallabag (Symfony HttpClient/5)'); $loginRequired = $auth->isLoginRequired($siteConfig, file_get_contents(__DIR__ . '/../fixtures/nextinpact-login.html')); @@ -177,7 +177,7 @@ class LoginFormAuthenticatorTest extends TestCase $requestHtmlFunctionClient = new MockHttpClient([$requestHtmlFunctionResponse]); $authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient); - $auth = new LoginFormAuthenticator($browser, $authenticatorProvider); + $auth = new LoginFormAuthenticator($browser, $authenticatorProvider, 'Wallabag (Symfony HttpClient/5)'); $loginRequired = $auth->isLoginRequired($siteConfig, file_get_contents(__DIR__ . '/../fixtures/nextinpact-article.html')); @@ -235,7 +235,7 @@ class LoginFormAuthenticatorTest extends TestCase ; $authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient); - $auth = new LoginFormAuthenticator($browser, $authenticatorProvider); + $auth = new LoginFormAuthenticator($browser, $authenticatorProvider, 'Wallabag (Symfony HttpClient/5)'); $res = $auth->login($siteConfig); From 189e8f6857ce00d97b80567cda2ae3763ad342d0 Mon Sep 17 00:00:00 2001 From: Adrien ERAUD Date: Fri, 13 Jun 2025 15:57:56 +0100 Subject: [PATCH 2/2] Move wallabag.user_agent to app/config/parameters.yml.dist --- app/config/parameters.yml.dist | 2 ++ app/config/services.yml | 4 ++-- app/config/wallabag.yml | 1 - 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index e41091752..74c79e5f0 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -28,6 +28,8 @@ parameters: mailer_dsn: smtp://127.0.0.1 + wallabag_user_agent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.92 Safari/535.2" + locale: en # A secret key that's used to generate certain security-related tokens diff --git a/app/config/services.yml b/app/config/services.yml index 5aeb4a94f..76794dcc8 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -31,7 +31,7 @@ services: $supportUrl: '@=service(''craue_config'').get(''wallabag_support_url'')' $fonts: '%wallabag.fonts%' $defaultIgnoreOriginInstanceRules: '%wallabag.default_ignore_origin_instance_rules%' - $defaultUserAgent: "%wallabag.user_agent%" + $defaultUserAgent: "%wallabag_user_agent%" Wallabag\: resource: '../../src/*' @@ -208,7 +208,7 @@ services: error_message: '%wallabag.fetching_error_message%' error_message_title: '%wallabag.fetching_error_message_title%' http_client: - ua_browser: "%wallabag.user_agent%" + ua_browser: "%wallabag_user_agent%" $client: '@psr18.wallabag.client' calls: - [ setLogger, [ "@logger" ] ] diff --git a/app/config/wallabag.yml b/app/config/wallabag.yml index 98f8d77c2..02106a33b 100644 --- a/app/config/wallabag.yml +++ b/app/config/wallabag.yml @@ -183,4 +183,3 @@ parameters: - 'text/html' - 'application/vnd.ms-excel' wallabag.resource_dir: "%kernel.project_dir%/web/uploads/import" - wallabag.user_agent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.92 Safari/535.2"