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

Fix file permissions at read time when wrong in DB #26542

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 12 additions & 1 deletion lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,20 @@ public function getShareByToken($token) {
*/
private function createShare($data) {
$share = new Share($this->rootFolder, $this->userManager);

$permissions = (int)$data['permissions'];
// mask file share permissions in case of invalid entries in the database
if ($data['item_type'] === 'file') {
$permissions = $permissions & (
\OCP\Constants::PERMISSION_READ |
\OCP\Constants::PERMISSION_UPDATE |
\OCP\Constants::PERMISSION_SHARE
);
}

$share->setId((int)$data['id'])
->setShareType((int)$data['share_type'])
->setPermissions((int)$data['permissions'])
->setPermissions($permissions)
->setTarget($data['file_target'])
->setMailSend((bool)$data['mail_send']);

Expand Down
100 changes: 74 additions & 26 deletions tests/lib/Share20/DefaultShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testGetShareByIdUserShare() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$qb->execute();

Expand Down Expand Up @@ -167,7 +167,7 @@ public function testGetShareByIdUserShare() {
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals($ownerPath, $share->getNode());
$this->assertEquals(13, $share->getPermissions());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals(null, $share->getToken());
$this->assertEquals(null, $share->getExpirationDate());
$this->assertEquals('myTarget', $share->getTarget());
Expand All @@ -185,7 +185,7 @@ public function testGetShareByIdLazy() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$qb->execute();

Expand All @@ -202,7 +202,7 @@ public function testGetShareByIdLazy() {
$this->assertEquals('sharedWith', $share->getSharedWith());
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals(13, $share->getPermissions());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals(null, $share->getToken());
$this->assertEquals(null, $share->getExpirationDate());
$this->assertEquals('myTarget', $share->getTarget());
Expand All @@ -220,7 +220,7 @@ public function testGetShareByIdLazy2() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$qb->execute();

Expand All @@ -246,7 +246,7 @@ public function testGetShareByIdLazy2() {
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals($ownerPath, $share->getNode());
$this->assertEquals(13, $share->getPermissions());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals(null, $share->getToken());
$this->assertEquals(null, $share->getExpirationDate());
$this->assertEquals('myTarget', $share->getTarget());
Expand All @@ -264,7 +264,7 @@ public function testGetShareByIdGroupShare() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());

Expand All @@ -289,7 +289,7 @@ public function testGetShareByIdGroupShare() {
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals($ownerPath, $share->getNode());
$this->assertEquals(13, $share->getPermissions());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals(null, $share->getToken());
$this->assertEquals(null, $share->getExpirationDate());
$this->assertEquals('myTarget', $share->getTarget());
Expand Down Expand Up @@ -345,7 +345,7 @@ public function testGetShareByIdLinkShare() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
'token' => $qb->expr()->literal('token'),
'expiration' => $qb->expr()->literal('2000-01-02 00:00:00'),
]);
Expand All @@ -371,7 +371,7 @@ public function testGetShareByIdLinkShare() {
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals($ownerPath, $share->getNode());
$this->assertEquals(13, $share->getPermissions());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals('token', $share->getToken());
$this->assertEquals(\DateTime::createFromFormat('Y-m-d H:i:s', '2000-01-02 00:00:00'), $share->getExpirationDate());
$this->assertEquals('myTarget', $share->getTarget());
Expand All @@ -387,7 +387,7 @@ public function testDeleteSingleShare() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());

Expand Down Expand Up @@ -431,7 +431,7 @@ public function testDeleteSingleShareLazy() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());

Expand Down Expand Up @@ -463,7 +463,7 @@ public function testDeleteGroupShareWithUserGroupShares() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());
$id = $qb->getLastInsertId();
Expand All @@ -477,7 +477,7 @@ public function testDeleteGroupShareWithUserGroupShares() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
'parent' => $qb->expr()->literal($id),
]);
$this->assertEquals(1, $qb->execute());
Expand Down Expand Up @@ -521,7 +521,7 @@ public function testGetChildren() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$qb->execute();

Expand Down Expand Up @@ -759,7 +759,7 @@ public function testGetShareByToken() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
'token' => $qb->expr()->literal('secrettoken'),
]);
$qb->execute();
Expand Down Expand Up @@ -837,7 +837,7 @@ public function testGetSharedWithUser($storageStringId, $fileName1, $fileName2)
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal($fileId),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());
$id = $qb->getLastInsertId();
Expand Down Expand Up @@ -902,7 +902,7 @@ public function testGetSharedWithGroup($storageStringId, $fileName1, $fileName2)
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal($fileId),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());
$id = $qb->getLastInsertId();
Expand Down Expand Up @@ -964,7 +964,7 @@ public function testGetSharedWithGroupUserModified($storageStringId, $fileName1,
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal($fileId),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());
$id = $qb->getLastInsertId();
Expand Down Expand Up @@ -1156,7 +1156,7 @@ public function testGetSharedWithWithDeletedFile($shareType, $trashed) {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal($deletedFileId),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());

Expand Down Expand Up @@ -1205,7 +1205,7 @@ public function testGetSharesBy() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());
$id = $qb->getLastInsertId();
Expand Down Expand Up @@ -1239,7 +1239,7 @@ public function testGetSharesBy() {
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals(Share::SHARE_TYPE_USER, $share->getShareType());
$this->assertEquals(13, $share->getPermissions());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals('myTarget', $share->getTarget());
}

Expand All @@ -1254,7 +1254,7 @@ public function testGetSharesNode() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());
$id = $qb->getLastInsertId();
Expand Down Expand Up @@ -1289,7 +1289,7 @@ public function testGetSharesNode() {
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals(Share::SHARE_TYPE_USER, $share->getShareType());
$this->assertEquals(13, $share->getPermissions());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals('myTarget', $share->getTarget());
}

Expand All @@ -1304,7 +1304,7 @@ public function testGetSharesReshare() {
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'permissions' => $qb->expr()->literal(19),
]);
$this->assertEquals(1, $qb->execute());
$id1 = $qb->getLastInsertId();
Expand Down Expand Up @@ -1339,7 +1339,7 @@ public function testGetSharesReshare() {
$this->assertSame('shareOwner', $share->getShareOwner());
$this->assertSame('shareOwner', $share->getSharedBy());
$this->assertEquals(Share::SHARE_TYPE_USER, $share->getShareType());
$this->assertEquals(13, $share->getPermissions());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals('myTarget', $share->getTarget());

$share = $shares[1];
Expand All @@ -1352,6 +1352,54 @@ public function testGetSharesReshare() {
$this->assertEquals('userTarget', $share->getTarget());
}

public function permissionMaskProvider() {
return [
['file', 23, 19],
['folder', 23, 23],
];
}

/**
* @dataProvider permissionMaskProvider
*/
public function testGetShareMasksInvalidFileSharePermissions($itemType, $dbPerms, $returnedPerms) {
$qb = $this->dbConn->getQueryBuilder();

$qb->insert('share')
->values([
'share_type' => $qb->expr()->literal(Share::SHARE_TYPE_USER),
'share_with' => $qb->expr()->literal('sharedWith'),
'uid_owner' => $qb->expr()->literal('shareOwner'),
'uid_initiator' => $qb->expr()->literal('sharedBy'),
'item_type' => $qb->expr()->literal($itemType),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal($dbPerms),
]);
$qb->execute();

$id = $qb->getLastInsertId();

$sharedBy = $this->createMock(IUser::class);
$sharedBy->method('getUID')->willReturn('sharedBy');
$shareOwner = $this->createMock(IUser::class);
$shareOwner->method('getUID')->willReturn('shareOwner');

$ownerPath = $this->createMock(File::class);
$shareOwnerFolder = $this->createMock(Folder::class);
$shareOwnerFolder->method('getById')->with(42)->willReturn([$ownerPath]);

$this->rootFolder
->method('getUserFolder')
->will($this->returnValueMap([
['shareOwner', $shareOwnerFolder],
]));

$share = $this->provider->getShareById($id);

$this->assertEquals($returnedPerms, $share->getPermissions());
}

public function testDeleteFromSelfGroupNoCustomShare() {
$qb = $this->dbConn->getQueryBuilder();
$stmt = $qb->insert('share')
Expand Down