Skip to content

Commit

Permalink
Make sure that the dav propfind plugins always use the proper user id
Browse files Browse the repository at this point in the history
For old android versions it could happen that the requests are performed
with a login name instead of the actual user id, so before this change
the property methods used the wrong value for fetching their information

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr authored and backportbot[bot] committed Jul 24, 2021
1 parent add5919 commit eae6ab2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
26 changes: 23 additions & 3 deletions apps/dav/lib/Connector/Sabre/FilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCP\IConfig;
use OCP\IPreview;
use OCP\IRequest;
use OCP\IUserSession;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\IFile;
Expand Down Expand Up @@ -88,6 +89,11 @@ class FilesPlugin extends ServerPlugin {
*/
private $tree;

/**
* @var IUserSession
*/
private $userSession;

/**
* Whether this is public webdav.
* If true, some returned information will be stripped off.
Expand Down Expand Up @@ -128,11 +134,13 @@ public function __construct(Tree $tree,
IConfig $config,
IRequest $request,
IPreview $previewManager,
IUserSession $userSession,
$isPublic = false,
$downloadAttachment = true) {
$this->tree = $tree;
$this->config = $config;
$this->request = $request;
$this->userSession = $userSession;
$this->isPublic = $isPublic;
$this->downloadAttachment = $downloadAttachment;
$this->previewManager = $previewManager;
Expand Down Expand Up @@ -322,14 +330,22 @@ public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node)
});

$propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) {
$user = $this->userSession->getUser();
if ($user === null) {
return null;
}
return $node->getSharePermissions(
$httpRequest->getRawServerValue('PHP_AUTH_USER')
$user->getUID()
);
});

$propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) {
$user = $this->userSession->getUser();
if ($user === null) {
return null;
}
$ncPermissions = $node->getSharePermissions(
$httpRequest->getRawServerValue('PHP_AUTH_USER')
$user->getUID()
);
$ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions);
return json_encode($ocmPermissions);
Expand Down Expand Up @@ -367,8 +383,12 @@ public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node)
});

$propFind->handle(self::SHARE_NOTE, function () use ($node, $httpRequest) {
$user = $this->userSession->getUser();
if ($user === null) {
return null;
}
return $node->getNoteFromShare(
$httpRequest->getRawServerValue('PHP_AUTH_USER')
$user->getUID()
);
});
}
Expand Down
1 change: 1 addition & 0 deletions apps/dav/lib/Connector/Sabre/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function createServer($baseUri,
$this->config,
$this->request,
$this->previewManager,
$this->userSession,
false,
!$this->config->getSystemValue('debug', false)
)
Expand Down
1 change: 1 addition & 0 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public function __construct(IRequest $request, $baseUri) {
\OC::$server->getConfig(),
$this->request,
\OC::$server->getPreviewManager(),
\OC::$server->getUserSession(),
false,
!\OC::$server->getConfig()->getSystemValue('debug', false)
)
Expand Down
26 changes: 13 additions & 13 deletions apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
use OCP\IConfig;
use OCP\IPreview;
use OCP\IRequest;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
use Sabre\DAV\Server;
Expand Down Expand Up @@ -99,30 +101,27 @@ class FilesPluginTest extends TestCase {
*/
private $previewManager;

/** @var IUserSession|MockObject */
private $userSession;

protected function setUp(): void {
parent::setUp();
$this->server = $this->getMockBuilder(Server::class)
->disableOriginalConstructor()
->getMock();
$this->tree = $this->getMockBuilder(Tree::class)
->disableOriginalConstructor()
->getMock();
$this->server = $this->createMock(Server::class);
$this->tree = $this->createMock(Tree::class);
$this->config = $this->createMock(IConfig::class);
$this->config->expects($this->any())->method('getSystemValue')
->with($this->equalTo('data-fingerprint'), $this->equalTo(''))
->willReturn('my_fingerprint');
$this->request = $this->getMockBuilder(IRequest::class)
->disableOriginalConstructor()
->getMock();
$this->previewManager = $this->getMockBuilder(IPreview::class)
->disableOriginalConstructor()
->getMock();
$this->request = $this->createMock(IRequest::class);
$this->previewManager = $this->createMock(IPreview::class);
$this->userSession = $this->createMock(IUserSession::class);

$this->plugin = new FilesPlugin(
$this->tree,
$this->config,
$this->request,
$this->previewManager
$this->previewManager,
$this->userSession
);

$response = $this->getMockBuilder(ResponseInterface::class)
Expand Down Expand Up @@ -264,6 +263,7 @@ public function testGetPublicPermissions() {
->disableOriginalConstructor()
->getMock(),
$this->previewManager,
$this->userSession,
true);
$this->plugin->initialize($this->server);

Expand Down
7 changes: 3 additions & 4 deletions apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,9 @@ public function testPrepareResponses() {
new \OCA\DAV\Connector\Sabre\FilesPlugin(
$this->tree,
$config,
$this->getMockBuilder(IRequest::class)
->disableOriginalConstructor()
->getMock(),
$this->previewManager
$this->createMock(IRequest::class),
$this->previewManager,
$this->createMock(IUserSession::class)
)
);
$this->plugin->initialize($this->server);
Expand Down

0 comments on commit eae6ab2

Please sign in to comment.