diff --git a/apps/workflowengine/lib/Check/RequestUserAgent.php b/apps/workflowengine/lib/Check/RequestUserAgent.php index 572ef56707463..64624ba6213e0 100644 --- a/apps/workflowengine/lib/Check/RequestUserAgent.php +++ b/apps/workflowengine/lib/Check/RequestUserAgent.php @@ -10,6 +10,30 @@ use OCP\IRequest; class RequestUserAgent extends AbstractStringCheck { + /** + * User agent patterns per predefined client. A request "is" a client when + * any of its patterns matches. + * + * @var array> + */ + private const CLIENT_PATTERNS = [ + 'android' => [ + IRequest::USER_AGENT_CLIENT_ANDROID, + IRequest::USER_AGENT_CLIENT_ANDROID_WEBVIEW, + IRequest::USER_AGENT_CLIENT_ANDROID_NO_VERSION, + ], + 'ios' => [ + IRequest::USER_AGENT_CLIENT_IOS, + IRequest::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 +53,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; - break; - case 'ios': - $operator = $operator === 'is' ? 'matches' : '!matches'; - $value = IRequest::USER_AGENT_CLIENT_IOS; + 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 '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 09eaea6555b57..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('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') diff --git a/lib/public/IRequest.php b/lib/public/IRequest.php index 913ecbdd4cd1d..1c4be3e80c892 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 32.0.16 + */ + 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 32.0.16 + */ + 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 32.0.16 + */ + 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