Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
chore: Migrate cleanAppId and getAppPath calls to IAppManager from OC…
…_App

Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Sep 13, 2024
commit 7ed583cb8ee64f77696b0e23f79d8d1b4038bcbc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OC\AppConfig;
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
Expand All @@ -35,6 +36,7 @@ public function __construct(
private IL10N $l10n,
private IGroupManager $groupManager,
private IManager $settingManager,
private IAppManager $appManager,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -171,11 +173,10 @@ public function deleteKey(string $app, string $key): DataResponse {
}

/**
* @param string $app
* @throws \InvalidArgumentException
*/
protected function verifyAppId(string $app) {
if (\OC_App::cleanAppId($app) !== $app) {
protected function verifyAppId(string $app): void {
if ($this->appManager->cleanAppId($app) !== $app) {
throw new \InvalidArgumentException('Invalid app id given');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use OC\AppConfig;
use OCA\Provisioning_API\Controller\AppConfigController;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Exceptions\AppConfigUnknownKeyException;
Expand All @@ -17,6 +18,7 @@
use OCP\IUser;
use OCP\IUserSession;
use OCP\Settings\IManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
use function json_decode;
use function json_encode;
Expand All @@ -28,25 +30,22 @@
*/
class AppConfigControllerTest extends TestCase {

/** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
private $appConfig;
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
private $userSession;
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
private $l10n;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
private $settingManager;
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
private $groupManager;
private IAppConfig&MockObject $appConfig;
private IUserSession&MockObject $userSession;
private IL10N&MockObject $l10n;
private IManager&MockObject $settingManager;
private IGroupManager&MockObject $groupManager;
private IAppManager $appManager;

protected function setUp(): void {
parent::setUp();

$this->appConfig = $this->createMock(AppConfig::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->l10n = $this->createMock(IL10N::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->settingManager = $this->createMock(IManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->appManager = \OCP\Server::get(IAppManager::class);
}

/**
Expand All @@ -64,7 +63,8 @@ protected function getInstance(array $methods = []) {
$this->userSession,
$this->l10n,
$this->groupManager,
$this->settingManager
$this->settingManager,
$this->appManager,
);
} else {
return $this->getMockBuilder(AppConfigController::class)
Expand All @@ -75,7 +75,8 @@ protected function getInstance(array $methods = []) {
$this->userSession,
$this->l10n,
$this->groupManager,
$this->settingManager
$this->settingManager,
$this->appManager,
])
->setMethods($methods)
->getMock();
Expand Down
11 changes: 5 additions & 6 deletions apps/settings/lib/Controller/AppSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use OC\App\DependencyAnalyzer;
use OC\App\Platform;
use OC\Installer;
use OC_App;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
Expand Down Expand Up @@ -479,7 +478,7 @@ public function enableApps(array $appIds, array $groups = []): JSONResponse {
$updateRequired = false;

foreach ($appIds as $appId) {
$appId = OC_App::cleanAppId($appId);
$appId = $this->appManager->cleanAppId($appId);

// Check if app is already downloaded
/** @var Installer $installer */
Expand Down Expand Up @@ -537,7 +536,7 @@ public function disableApp(string $appId): JSONResponse {
public function disableApps(array $appIds): JSONResponse {
try {
foreach ($appIds as $appId) {
$appId = OC_App::cleanAppId($appId);
$appId = $this->appManager->cleanAppId($appId);
$this->appManager->disableApp($appId);
}
return new JSONResponse([]);
Expand All @@ -553,7 +552,7 @@ public function disableApps(array $appIds): JSONResponse {
*/
#[PasswordConfirmationRequired]
public function uninstallApp(string $appId): JSONResponse {
$appId = OC_App::cleanAppId($appId);
$appId = $this->appManager->cleanAppId($appId);
$result = $this->installer->removeApp($appId);
if ($result !== false) {
$this->appManager->clearAppsCache();
Expand All @@ -567,7 +566,7 @@ public function uninstallApp(string $appId): JSONResponse {
* @return JSONResponse
*/
public function updateApp(string $appId): JSONResponse {
$appId = OC_App::cleanAppId($appId);
$appId = $this->appManager->cleanAppId($appId);

$this->config->setSystemValue('maintenance', true);
try {
Expand All @@ -594,7 +593,7 @@ private function sortApps($a, $b) {
}

public function force(string $appId): JSONResponse {
$appId = OC_App::cleanAppId($appId);
$appId = $this->appManager->cleanAppId($appId);
$this->appManager->ignoreNextcloudRequirementForApp($appId);
return new JSONResponse();
}
Expand Down
31 changes: 8 additions & 23 deletions apps/theming/tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@

class UtilTest extends TestCase {

/** @var Util */
protected $util;
/** @var IConfig|MockObject */
protected $config;
/** @var IAppData|MockObject */
protected $appData;
/** @var IAppManager|MockObject */
protected $appManager;
/** @var ImageManager|MockObject */
protected $imageManager;
protected Util $util;
protected IConfig&MockObject $config;
protected IAppData&MockObject $appData;
protected IAppManager $appManager;
protected ImageManager&MockObject $imageManager;

protected function setUp(): void {
parent::setUp();
$this->config = $this->createMock(IConfig::class);
$this->appData = $this->createMock(IAppData::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->appManager = \OCP\Server::get(IAppManager::class);
$this->imageManager = $this->createMock(ImageManager::class);
$this->util = new Util($this->config, $this->appManager, $this->appData, $this->imageManager);
}
Expand Down Expand Up @@ -152,19 +147,15 @@ public function testGetAppIcon($app, $expected) {
->method('getFolder')
->with('global/images')
->willThrowException(new NotFoundException());
$this->appManager->expects($this->once())
->method('getAppPath')
->with($app)
->willReturn(\OC_App::getAppPath($app));
$icon = $this->util->getAppIcon($app);
$this->assertEquals($expected, $icon);
}

public function dataGetAppIcon() {
return [
['user_ldap', \OC_App::getAppPath('user_ldap') . '/img/app.svg'],
['user_ldap', \OCP\Server::get(IAppManager::class)->getAppPath('user_ldap') . '/img/app.svg'],
['noapplikethis', \OC::$SERVERROOT . '/core/img/logo/logo.svg'],
['comments', \OC_App::getAppPath('comments') . '/img/comments.svg'],
['comments', \OCP\Server::get(IAppManager::class)->getAppPath('comments') . '/img/comments.svg'],
];
}

Expand All @@ -187,12 +178,6 @@ public function testGetAppIconThemed() {
* @dataProvider dataGetAppImage
*/
public function testGetAppImage($app, $image, $expected) {
if ($app !== 'core') {
$this->appManager->expects($this->once())
->method('getAppPath')
->with($app)
->willReturn(\OC_App::getAppPath($app));
}
$this->assertEquals($expected, $this->util->getAppImage($app, $image));
}

Expand Down
7 changes: 6 additions & 1 deletion core/Command/L10n/CreateJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use DirectoryIterator;

use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
Expand Down Expand Up @@ -150,7 +151,11 @@ public function completeArgumentValues($argumentName, CompletionContext $context
return \OC_App::getAllApps();
} elseif ($argumentName === 'lang') {
$appName = $context->getWordAtIndex($context->getWordIndex() - 1);
return $this->getAllLanguages(\OC_App::getAppPath($appName));
try {
return $this->getAllLanguages($this->appManager->getAppPath($appName));
} catch(AppPathNotFoundException) {
return [];
}
}
return [];
}
Expand Down
9 changes: 6 additions & 3 deletions lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,9 @@ public function disableApp($appId, $automaticDisabled = false) {
/**
* Get the directory for the given app.
*
* @param string $appId
* @return string
* @throws AppPathNotFoundException if app folder can't be found
*/
public function getAppPath($appId) {
public function getAppPath(string $appId): string {
$appPath = \OC_App::getAppPath($appId);
if ($appPath === false) {
throw new AppPathNotFoundException('Could not find path for ' . $appId);
Expand Down Expand Up @@ -877,4 +875,9 @@ public function isBackendRequired(string $backend): bool {

return false;
}

public function cleanAppId(string $app): string {
// FIXME should list allowed characters instead
return str_replace(['<', '>', '"', "'", '\0', '/', '\\', '..'], '', $app);
}
}
30 changes: 11 additions & 19 deletions lib/private/Authentication/TwoFactorAuth/ProviderLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
namespace OC\Authentication\TwoFactorAuth;

use Exception;
use OC;
use OC_App;
use OC\AppFramework\Bootstrap\Coordinator;
use OCP\App\IAppManager;
use OCP\AppFramework\QueryException;
use OCP\Authentication\TwoFactorAuth\IProvider;
Expand All @@ -19,15 +18,10 @@
class ProviderLoader {
public const BACKUP_CODES_APP_ID = 'twofactor_backupcodes';

/** @var IAppManager */
private $appManager;

/** @var OC\AppFramework\Bootstrap\Coordinator */
private $coordinator;

public function __construct(IAppManager $appManager, OC\AppFramework\Bootstrap\Coordinator $coordinator) {
$this->appManager = $appManager;
$this->coordinator = $coordinator;
public function __construct(
private IAppManager $appManager,
private Coordinator $coordinator,
) {
}

/**
Expand Down Expand Up @@ -58,12 +52,12 @@ public function getProviders(IUser $user): array {
}
}

$registeredProviders = $this->coordinator->getRegistrationContext()->getTwoFactorProviders();
$registeredProviders = $this->coordinator->getRegistrationContext()?->getTwoFactorProviders() ?? [];
foreach ($registeredProviders as $provider) {
try {
$this->loadTwoFactorApp($provider->getAppId());
$provider = \OCP\Server::get($provider->getService());
$providers[$provider->getId()] = $provider;
$providerInstance = \OCP\Server::get($provider->getService());
$providers[$providerInstance->getId()] = $providerInstance;
} catch (QueryException $exc) {
// Provider class can not be resolved
throw new Exception('Could not load two-factor auth provider ' . $provider->getService());
Expand All @@ -75,12 +69,10 @@ public function getProviders(IUser $user): array {

/**
* Load an app by ID if it has not been loaded yet
*
* @param string $appId
*/
protected function loadTwoFactorApp(string $appId) {
if (!OC_App::isAppLoaded($appId)) {
OC_App::loadApp($appId);
protected function loadTwoFactorApp(string $appId): void {
if (!$this->appManager->isAppLoaded($appId)) {
$this->appManager->loadApp($appId);
}
}
}
2 changes: 1 addition & 1 deletion lib/private/L10N/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(
*/
public function get($app, $lang = null, $locale = null) {
return new LazyL10N(function () use ($app, $lang, $locale) {
$app = \OC_App::cleanAppId($app);
$app = $this->appManager->cleanAppId($app);
if ($lang !== null) {
$lang = str_replace(['\0', '/', '\\', '..'], '', $lang);
}
Expand Down
26 changes: 17 additions & 9 deletions lib/private/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getRoutingFiles() {
*/
public function loadRoutes($app = null) {
if (is_string($app)) {
$app = \OC_App::cleanAppId($app);
$app = $this->appManager->cleanAppId($app);
}

$requestedApp = $app;
Expand All @@ -123,11 +123,15 @@ public function loadRoutes($app = null) {
if (isset($this->loadedApps[$app])) {
return;
}
$appPath = \OC_App::getAppPath($app);
$file = $appPath . '/appinfo/routes.php';
if ($appPath !== false && file_exists($file)) {
$routingFiles = [$app => $file];
} else {
try {
$appPath = $this->appManager->getAppPath($app);
$file = $appPath . '/appinfo/routes.php';
if (file_exists($file)) {
$routingFiles = [$app => $file];
} else {
$routingFiles = [];
}
} catch (AppPathNotFoundException) {
$routingFiles = [];
}

Expand Down Expand Up @@ -238,14 +242,14 @@ public function findMatchingRoute(string $url): array {
// empty string / 'apps' / $app / rest of the route
[, , $app,] = explode('/', $url, 4);

$app = \OC_App::cleanAppId($app);
$app = $this->appManager->cleanAppId($app);
\OC::$REQUESTEDAPP = $app;
$this->loadRoutes($app);
} elseif (str_starts_with($url, '/ocsapp/apps/')) {
// empty string / 'ocsapp' / 'apps' / $app / rest of the route
[, , , $app,] = explode('/', $url, 5);

$app = \OC_App::cleanAppId($app);
$app = $this->appManager->cleanAppId($app);
\OC::$REQUESTEDAPP = $app;
$this->loadRoutes($app);
} elseif (str_starts_with($url, '/settings/')) {
Expand Down Expand Up @@ -433,7 +437,11 @@ private function getAttributeRoutes(string $app): array {
$appControllerPath = __DIR__ . '/../../../core/Controller';
$appNameSpace = 'OC\\Core';
} else {
$appControllerPath = \OC_App::getAppPath($app) . '/lib/Controller';
try {
$appControllerPath = $this->appManager->getAppPath($app) . '/lib/Controller';
} catch (AppPathNotFoundException) {
return [];
}
$appNameSpace = App::buildAppNamespace($app);
}

Expand Down
Loading