Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions apps/workflowengine/lib/Check/RequestUserAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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<string, list<string>>
*/
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
Expand All @@ -31,29 +55,15 @@ public function __construct(
#[\Override]
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);
}
Expand Down
37 changes: 36 additions & 1 deletion apps/workflowengine/tests/Check/RequestUserAgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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;

Expand Down Expand Up @@ -83,7 +84,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')
Expand Down
20 changes: 20 additions & 0 deletions lib/public/IRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 36.0.0
*/
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 <os version>) Mobile" instead of "(Android)".
* @since 36.0.0
*/
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
Expand All @@ -73,6 +86,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 36.0.0
*/
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
Expand Down
Loading