From 22323e9e8f0e749c5ae9b8b9d58fa9baa8e2ce0b Mon Sep 17 00:00:00 2001 From: Salvatore Martire <4652631+salmart-dev@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:25:10 +0100 Subject: [PATCH 1/2] fix: improve user agent detection in workflowengine Signed-off-by: Salvatore Martire <4652631+salmart-dev@users.noreply.github.com> Assisted-by: ClaudeCode:claude-opus-5-5 --- .../lib/Check/RequestUserAgent.php | 71 +++++++++++++------ .../tests/Check/RequestUserAgentTest.php | 37 +++++++++- 2 files changed, 86 insertions(+), 22 deletions(-) diff --git a/apps/workflowengine/lib/Check/RequestUserAgent.php b/apps/workflowengine/lib/Check/RequestUserAgent.php index 572ef56707463..264c67637486b 100644 --- a/apps/workflowengine/lib/Check/RequestUserAgent.php +++ b/apps/workflowengine/lib/Check/RequestUserAgent.php @@ -10,6 +10,49 @@ use OCP\IRequest; class RequestUserAgent extends AbstractStringCheck { + /** + * Web views the iOS app opens for office editors (OnlyOffice, Euro-Office, + * Whiteboard) send "(iPhone) Mobile" or "(iPad)" instead of "(iOS)". + * Note: {@see IRequest::USER_AGENT_CLIENT_IOS} is used for exempting CSRF + * checks and they should apply to web views, so it cannot be included here. + */ + private const string USER_AGENT_CLIENT_IOS_WEBVIEW = '/^Mozilla\/5\.0 \((?:iPhone|iPad)\)(?: Mobile)? (?:ownCloud|Nextcloud)\-iOS\/([^ ]*).*$/'; + + /** + * Web views the Android app opens for office editors send + * "(Android ) Mobile" instead of "(Android)". + */ + private const string USER_AGENT_CLIENT_ANDROID_WEBVIEW = '/^Mozilla\/5\.0 \(Android [^)]*\) Mobile (?:ownCloud|Nextcloud)\-android\/([^ ]*).*$/'; + + /** + * The Android app's image loader sends its user agent without a version. + */ + private const string USER_AGENT_CLIENT_ANDROID_NO_VERSION = '/^Mozilla\/5\.0 \(Android\) (?:ownCloud|Nextcloud)\-android$/'; + + /** + * User agent patterns per predefined client. A request "is" a client when + * any of its patterns matches. + * + * @var array> + */ + private const array CLIENT_PATTERNS = [ + 'android' => [ + IRequest::USER_AGENT_CLIENT_ANDROID, + self::USER_AGENT_CLIENT_ANDROID_WEBVIEW, + self::USER_AGENT_CLIENT_ANDROID_NO_VERSION, + ], + 'ios' => [ + IRequest::USER_AGENT_CLIENT_IOS, + self::USER_AGENT_CLIENT_IOS_WEBVIEW, + ], + 'desktop' => [ + IRequest::USER_AGENT_CLIENT_DESKTOP, + ], + 'mail' => [ + IRequest::USER_AGENT_OUTLOOK_ADDON, + IRequest::USER_AGENT_THUNDERBIRD_ADDON, + ], + ]; /** * @param IL10N $l @@ -29,29 +72,15 @@ public function __construct( */ public function executeCheck($operator, $value) { $actualValue = $this->getActualValue(); - if (in_array($operator, ['is', '!is'], true)) { - switch ($value) { - case 'android': - $operator = $operator === 'is' ? 'matches' : '!matches'; - $value = IRequest::USER_AGENT_CLIENT_ANDROID; + if (in_array($operator, ['is', '!is'], true) && isset(self::CLIENT_PATTERNS[$value])) { + $isClient = false; + foreach (self::CLIENT_PATTERNS[$value] as $pattern) { + if ($this->executeStringCheck('matches', $pattern, $actualValue)) { + $isClient = true; break; - case 'ios': - $operator = $operator === 'is' ? 'matches' : '!matches'; - $value = IRequest::USER_AGENT_CLIENT_IOS; - break; - case 'desktop': - $operator = $operator === 'is' ? 'matches' : '!matches'; - $value = IRequest::USER_AGENT_CLIENT_DESKTOP; - break; - case 'mail': - if ($operator === 'is') { - return $this->executeStringCheck('matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue) - || $this->executeStringCheck('matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue); - } - - return $this->executeStringCheck('!matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue) - && $this->executeStringCheck('!matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue); + } } + return $operator === 'is' ? $isClient : !$isClient; } return $this->executeStringCheck($operator, $value, $actualValue); } diff --git a/apps/workflowengine/tests/Check/RequestUserAgentTest.php b/apps/workflowengine/tests/Check/RequestUserAgentTest.php index 2fbb632a43eda..053151b692d35 100644 --- a/apps/workflowengine/tests/Check/RequestUserAgentTest.php +++ b/apps/workflowengine/tests/Check/RequestUserAgentTest.php @@ -12,6 +12,7 @@ use OCA\WorkflowEngine\Check\RequestUserAgent; use OCP\IL10N; use OCP\IRequest; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -82,7 +83,41 @@ public static function dataExecuteCheck(): array { ]; } - #[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'dataExecuteCheck')] + /** + * User agents sent by the mobile apps outside their main WebDAV client, + * plus regular mobile browsers that must not be treated as the apps. + * Each one is checked against every predefined client with "is" and "!is". + */ + public static function dataExecuteCheckClientVariants(): \Generator { + $userAgents = [ + // iOS: office editor web views (NCDirectEditorAdapter) + 'Mozilla/5.0 (iPhone) Mobile Nextcloud-iOS/35.2.2' => 'ios', + 'Mozilla/5.0 (iPad) Nextcloud-iOS/35.2.2' => 'ios', + // iOS: Text editor web view on iPhone + 'Mozilla/5.0 (iOS) Nextcloud-iOS/35.2.2 AppleWebKit/605.1.15 Mobile/15E148' => 'ios', + // iOS: branded app + 'Mozilla/5.0 (iOS) Nextcloud-iOS/35.2.2-Brand' => 'ios', + // Android: office editor web view (office_user_agent) + 'Mozilla/5.0 (Android 13) Mobile Nextcloud-android/35.1.0' => 'android', + // Android: image loader without version (GlideHelper) + 'Mozilla/5.0 (Android) Nextcloud-android' => 'android', + // Mobile browsers are not the apps + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1' => null, + 'Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1' => null, + 'Mozilla/5.0 (Linux; Android 13; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36' => null, + ]; + + foreach ($userAgents as $userAgent => $expectedClient) { + foreach (['android', 'ios', 'desktop', 'mail'] as $client) { + $isClient = $client === $expectedClient; + yield "is $client: $userAgent" => ['is', $client, $userAgent, $isClient]; + yield "!is $client: $userAgent" => ['!is', $client, $userAgent, !$isClient]; + } + } + } + + #[DataProvider(methodName: 'dataExecuteCheck')] + #[DataProvider(methodName: 'dataExecuteCheckClientVariants')] public function testExecuteCheck(string $operation, string $checkValue, string $actualValue, bool $expected): void { $this->request->expects($this->once()) ->method('getHeader') From 2841166e1c96007783f6c407fcb89b4964be7418 Mon Sep 17 00:00:00 2001 From: Salvatore Martire <4652631+salmart-dev@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:34:09 +0100 Subject: [PATCH 2/2] refactor: move webview user agents to IRequest Signed-off-by: Salvatore Martire <4652631+salmart-dev@users.noreply.github.com> --- .../lib/Check/RequestUserAgent.php | 27 +++---------------- lib/public/IRequest.php | 20 ++++++++++++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/apps/workflowengine/lib/Check/RequestUserAgent.php b/apps/workflowengine/lib/Check/RequestUserAgent.php index 264c67637486b..64624ba6213e0 100644 --- a/apps/workflowengine/lib/Check/RequestUserAgent.php +++ b/apps/workflowengine/lib/Check/RequestUserAgent.php @@ -10,40 +10,21 @@ use OCP\IRequest; class RequestUserAgent extends AbstractStringCheck { - /** - * Web views the iOS app opens for office editors (OnlyOffice, Euro-Office, - * Whiteboard) send "(iPhone) Mobile" or "(iPad)" instead of "(iOS)". - * Note: {@see IRequest::USER_AGENT_CLIENT_IOS} is used for exempting CSRF - * checks and they should apply to web views, so it cannot be included here. - */ - private const string USER_AGENT_CLIENT_IOS_WEBVIEW = '/^Mozilla\/5\.0 \((?:iPhone|iPad)\)(?: Mobile)? (?:ownCloud|Nextcloud)\-iOS\/([^ ]*).*$/'; - - /** - * Web views the Android app opens for office editors send - * "(Android ) Mobile" instead of "(Android)". - */ - private const string USER_AGENT_CLIENT_ANDROID_WEBVIEW = '/^Mozilla\/5\.0 \(Android [^)]*\) Mobile (?:ownCloud|Nextcloud)\-android\/([^ ]*).*$/'; - - /** - * The Android app's image loader sends its user agent without a version. - */ - private const string USER_AGENT_CLIENT_ANDROID_NO_VERSION = '/^Mozilla\/5\.0 \(Android\) (?:ownCloud|Nextcloud)\-android$/'; - /** * User agent patterns per predefined client. A request "is" a client when * any of its patterns matches. * * @var array> */ - private const array CLIENT_PATTERNS = [ + private const CLIENT_PATTERNS = [ 'android' => [ IRequest::USER_AGENT_CLIENT_ANDROID, - self::USER_AGENT_CLIENT_ANDROID_WEBVIEW, - self::USER_AGENT_CLIENT_ANDROID_NO_VERSION, + IRequest::USER_AGENT_CLIENT_ANDROID_WEBVIEW, + IRequest::USER_AGENT_CLIENT_ANDROID_NO_VERSION, ], 'ios' => [ IRequest::USER_AGENT_CLIENT_IOS, - self::USER_AGENT_CLIENT_IOS_WEBVIEW, + IRequest::USER_AGENT_CLIENT_IOS_WEBVIEW, ], 'desktop' => [ IRequest::USER_AGENT_CLIENT_DESKTOP, diff --git a/lib/public/IRequest.php b/lib/public/IRequest.php index 913ecbdd4cd1d..ef9617e5723c3 100644 --- a/lib/public/IRequest.php +++ b/lib/public/IRequest.php @@ -44,6 +44,19 @@ interface IRequest { */ public const USER_AGENT_CLIENT_ANDROID = '/^Mozilla\/5\.0 \(Android\) (?:ownCloud|Nextcloud)\-android\/([^ ]*).*$/'; + /** + * The Android app's image loader sends its user agent without a version. + * @since 33.0.12 + */ + public const USER_AGENT_CLIENT_ANDROID_NO_VERSION = '/^Mozilla\/5\.0 \(Android\) (?:ownCloud|Nextcloud)\-android$/'; + + /** + * Web views the Android app opens for office editors send + * "(Android ) Mobile" instead of "(Android)". + * @since 33.0.12 + */ + public const USER_AGENT_CLIENT_ANDROID_WEBVIEW = '/^Mozilla\/5\.0 \(Android [^)]*\) Mobile (?:ownCloud|Nextcloud)\-android\/([^ ]*).*$/'; + /** * @since 13.0.0 * @since 28.0.0 The regex has a group matching the version number @@ -68,6 +81,13 @@ interface IRequest { */ public const USER_AGENT_CLIENT_IOS = '/^Mozilla\/5\.0 \(iOS\) (?:ownCloud|Nextcloud)\-iOS\/([^ ]*).*$/'; + /** + * Web views the iOS app opens for office editors (OnlyOffice, Euro-Office, + * Whiteboard) send "(iPhone) Mobile" or "(iPad)" instead of "(iOS)". + * @since 33.0.12 + */ + public const USER_AGENT_CLIENT_IOS_WEBVIEW = '/^Mozilla\/5\.0 \((?:iPhone|iPad)\)(?: Mobile)? (?:ownCloud|Nextcloud)\-iOS\/([^ ]*).*$/'; + /** * @since 13.0.0 * @since 28.0.0 The regex has a group matching the version number