Skip to content

Commit

Permalink
Merge pull request #30432 from owncloud/check-getusersession-stable10
Browse files Browse the repository at this point in the history
[stable10] Check the return value of getUserSession before using it
  • Loading branch information
sharidas authored Feb 9, 2018
2 parents aefb9f7 + 4d9146a commit e28c31d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use InterfaSys\LogNormalizer\Normalizer;

use \OCP\ILogger;
use OCP\IUserSession;
use OCP\Util;

/**
Expand Down Expand Up @@ -292,12 +293,15 @@ public function log($level, $message, array $context = []) {

// check for user
if (!empty($logCondition['users'])) {
$user = \OC::$server->getUserSession()->getUser();

// if the user matches set the log condition to satisfied
if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
$this->logConditionSatisfied = true;
break;
$userSession = \OC::$server->getUserSession();
if ($userSession instanceof IUserSession) {
$user = $userSession->getUser();

// if the user matches set the log condition to satisfied
if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
$this->logConditionSatisfied = true;
break;
}
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\IUser;
use OCP\IConfig;
use OCP\IUserBackend;
use OCP\IUserSession;
use OCP\User\IChangePasswordBackend;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
Expand Down Expand Up @@ -331,11 +332,17 @@ public function canChangePassword() {
* @return bool
*/
public function canChangeDisplayName() {
if (($this->config->getSystemValue('allow_user_to_change_display_name') === false) &&
(!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) &&
(!$this->groupManager->getSubAdmin()->isSubAdmin($this->userSession->getUser()))) {
return false;
if ($this->userSession instanceof IUserSession) {
$user = $this->userSession->getUser();
if (
($this->config->getSystemValue('allow_user_to_change_display_name') === false) &&
(($user !== null) && (!$this->groupManager->isAdmin($user->getUID()))) &&
(($user !== null) && (!$this->groupManager->getSubAdmin()->isSubAdmin($user)))
) {
return false;
}
}

$backend = $this->account->getBackendInstance();
if (is_null($backend)) {
return false;
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OC\Log;
use OCP\IConfig;
use OCP\IUserSession;
use OCP\Util;

class LoggerTest extends TestCase {
Expand Down Expand Up @@ -77,6 +78,27 @@ public function testAppLogCondition() {
$this->assertEquals($expected, $this->getLogs());
}

public function testNullUserSession() {
$userSession = $this->createMock(IUserSession::class);
$userSession->expects($this->any())
->method('getUser')
->willReturn(null);
$this->config->expects($this->any())
->method('getValue')
->will(($this->returnValueMap([
['loglevel', Util::WARN, Util::WARN],
['log.conditions', [], [['users' => ['foo'], 'apps' => ['files'], 'logfile' => '/tmp/test.log']]]
])));
$logger = $this->logger;

$logger->warning('Don\'t display info messages');

$expected = [
'2 Don\'t display info messages',
];
$this->assertEquals($expected, $this->getLogs());
}

private function getLogs() {
return self::$logs;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,45 @@ public function testCanChangeDisplayName($expected, $implements) {
$this->assertEquals($expected, $user->setDisplayName('Foo'));
}

public function provideNullorFalseData() {
return [
[null, null, false],
[null, true, false],
[null, true, true]
];
}

/**
* @dataProvider provideNullorFalseData
* @param $user
* @param $backendinstance
* @param $setDisplayName
*/
public function testCanChangeDisplayNameWhenNullSession($getUser, $backendinstance, $setDisplayName) {
$this->sessionUser->method('getUser')
->willReturn($getUser);
$backend = $this->getMockBuilder(Database::class)
->setMethods(['implementsActions'])
->getMock();

/** @var Account | \PHPUnit_Framework_MockObject_MockObject $account */
$account = $this->getMockBuilder(Account::class)
->setMethods(['getBackendInstance', 'getDisplayName', 'setDisplayName'])
->getMock();
if ($backendinstance !== null) {
$backendinstance = $backend;
}
$account->expects($this->any())->method('getBackendInstance')->willReturn($backendinstance);
$account->expects($this->any())->method('getDisplayName')->willReturn('admin');
$account->expects($this->any())->method('setDisplayName')->willReturn($setDisplayName);
$user = new User($account, $this->accountMapper, null, $this->config, null, null, $this->groupManager, null);
if ($setDisplayName !== true) {
$this->assertEquals($setDisplayName, $user->canChangeDisplayName());
} else {
$this->assertEquals(null, $user->canChangeDisplayName());
}
}

/**
* don't allow display names containing whitespaces only
*/
Expand Down

0 comments on commit e28c31d

Please sign in to comment.