Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.2.0] Respect 'writable' appdir flag on update #35097

Merged
merged 1 commit into from
Apr 29, 2019
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
10 changes: 6 additions & 4 deletions lib/private/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,18 @@ public static function updateApp($info= [], $isShipped=false) {
$info = self::checkAppsIntegrity($info, $extractDir, $path, $isShipped);

$currentDir = OC_App::getAppPath($info['id']);
if (\is_dir("$currentDir/.git")) {
throw new AppAlreadyInstalledException("App <{$info['id']}> is a git clone - it will not be updated.");
}

$basedir = OC_App::getInstallPath();
$basedir .= '/';
$basedir .= $info['id'];

if ($currentDir !== false && \is_writable($currentDir)) {
if ($currentDir !== false && OC_App::isAppDirWritable($info['id'])) {
$basedir = $currentDir;
}
if (\is_dir("$basedir/.git")) {
throw new AppAlreadyInstalledException("App <{$info['id']}> is a git clone - it will not be updated.");
}

if (\is_dir($basedir)) {
OC_Helper::rmdirr($basedir);
}
Expand Down
16 changes: 15 additions & 1 deletion lib/private/legacy/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,20 @@ public static function getAppWebPath($appId) {
*/
public static function isAppDirWritable($appId) {
$path = self::getAppPath($appId);
// Check if the parent directory is marked as writable in config.php
if ($path !== false) {
$appDir = \substr($path, 0, -\strlen("/$appId"));
foreach (OC::$APPSROOTS as $dir) {
if ($dir['path'] !== $appDir) {
continue;
}
if (!isset($dir['writable'])
|| $dir['writable'] !== true
) {
return false;
}
}
}
return ($path !== false) ? \is_writable($path) : false;
}

Expand Down Expand Up @@ -942,6 +956,7 @@ public static function getAppVersions() {
* @return bool
*/
public static function updateApp($appId) {
\OC::$server->getAppManager()->clearAppsCache();
$appPath = self::getAppPath($appId);
if ($appPath === false) {
return false;
Expand All @@ -958,7 +973,6 @@ public static function updateApp($appId) {
}
self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
\OC::$server->getAppManager()->clearAppsCache();
// run upgrade code
if (\file_exists($appPath . '/appinfo/update.php')) {
self::loadApp($appId, false);
Expand Down
68 changes: 67 additions & 1 deletion tests/lib/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ protected function setUp() {
parent::setUp();

Installer::removeApp(self::$appid);
\OC::$server->getConfig()->deleteAppValues(self::$appid);
}

protected function tearDown() {
Installer::removeApp(self::$appid);

\OC::$server->getConfig()->deleteAppValues(self::$appid);
parent::tearDown();
}

Expand Down Expand Up @@ -89,4 +90,69 @@ public function testUpdateApp() {

$this->assertNotEquals($oldVersionNumber, $newVersionNumber);
}

/**
* Tests that update is installed into writable app dir if the original app dir is not writable
*/
public function testUpdateIntoWritableAppDir() {
$oldAppRoots = \OC::$APPSROOTS;
$relativePath = "/anotherdir";

// Install old version
$pathOfOldTestApp = __DIR__ . '/../data/testapp.zip';
$oldTmp = \OC::$server->getTempManager()->getTemporaryFile('.zip');
\OC_Helper::copyr($pathOfOldTestApp, $oldTmp);
$oldData = [
'path' => $oldTmp,
'source' => 'path',
'appdata' => [
'id' => 'testapp',
'level' => 100,
]
];
$installResult = Installer::installApp($oldData);
$this->assertEquals('testapp', $installResult);
$oldAppPath = \OC_App::getAppPath(self::$appid);

// Mark the first app as dir non-writable and create the second as writable
$firstAppDir = \array_shift(\OC::$APPSROOTS);
$firstAppDir['writable'] = false;

$path = \dirname($firstAppDir['path']) . $relativePath;
\mkdir($path);
\clearstatcache();
\OC::$APPSROOTS = [
$firstAppDir,
[
'path' => $path,
'url' => $relativePath,
'writable' => true
]
];
\OC::$server->getAppManager()->clearAppsCache();

// Update app
$pathOfNewTestApp = __DIR__ . '/../data/testapp2.zip';
$newTmp = \OC::$server->getTempManager()->getTemporaryFile('.zip');
\OC_Helper::copyr($pathOfNewTestApp, $newTmp);

$newData = [
'path' => $newTmp,
'source' => 'path',
'appdata' => [
'id' => 'testapp',
'level' => 100,
]
];
$updateResult = Installer::updateApp($newData);
$this->assertTrue($updateResult);
$newAppPath = \OC_App::getAppPath(self::$appid);

$this->assertNotEquals($oldAppPath, $newAppPath);
$this->assertStringStartsWith($path, $newAppPath);

\OC_Helper::rmdirr($path);
\OC::$APPSROOTS = $oldAppRoots;
\OC::$server->getAppManager()->clearAppsCache();
}
}