Skip to content

Commit

Permalink
Revert the token scope to not end up with storing the user used in th…
Browse files Browse the repository at this point in the history
…e session

Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Feb 3, 2023
1 parent 1fed799 commit fef7fd0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/files/lib/Controller/DirectEditingViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct($appName, IRequest $request, IEventDispatcher $event
/**
* @PublicPage
* @NoCSRFRequired
* @UseSession
*
* @param string $token
* @return Response
Expand Down
15 changes: 14 additions & 1 deletion lib/private/DirectEditing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Manager implements IManager {
private $editors = [];
/** @var IDBConnection */
private $connection;
/** @var IUserSession */
private $userSession;
/** @var ISecureRandom */
private $random;
/** @var string|null */
Expand All @@ -80,6 +82,7 @@ public function __construct(
) {
$this->random = $random;
$this->connection = $connection;
$this->userSession = $userSession;
$this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
$this->rootFolder = $rootFolder;
$this->l10n = $l10nFactory->get('lib');
Expand Down Expand Up @@ -185,7 +188,13 @@ public function edit(string $token): Response {
$this->invalidateToken($token);
return new NotFoundResponse();
}
return $editor->open($tokenObject);

try {
$this->invokeTokenScope($tokenObject->getUser());
return $editor->open($tokenObject);
} finally {
$this->revertTokenScope();
}
}

public function editSecure(File $file, string $editorId): TemplateResponse {
Expand Down Expand Up @@ -250,6 +259,10 @@ public function invokeTokenScope($userId): void {
\OC_User::setUserId($userId);
}

public function revertTokenScope(): void {
$this->userSession->setUser(null);
}

public function createToken($editorId, File $file, string $filePath, IShare $share = null): string {
$token = $this->random->generate(64, ISecureRandom::CHAR_HUMAN_READABLE);
$query = $this->connection->getQueryBuilder();
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/DirectEditing/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\Files\IRootFolder;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Security\ISecureRandom;
Expand Down Expand Up @@ -137,6 +138,14 @@ protected function setUp(): void {
->method('getUserFolder')
->willReturn($this->userFolder);

$user = $this->createMock(IUser::class);
$user->expects(self::any())
->method('getUID')
->willReturn('admin');
$this->userSession->expects(self::any())
->method('getUser')
->willReturn($user);

$this->manager = new Manager(
$this->random, $this->connection, $this->userSession, $this->rootFolder, $l10nFactory, $this->encryptionManager
);
Expand Down

0 comments on commit fef7fd0

Please sign in to comment.