Skip to content
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
20 changes: 0 additions & 20 deletions cypress/e2e/share.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import { randUser } from '../utils/index.js'

const user = randUser()
const recipient = randUser()

describe('Open test.md in viewer', function () {
before(function () {
Expand All @@ -21,8 +20,6 @@ describe('Open test.md in viewer', function () {
cy.uploadFile('test.md', 'text/markdown', 'test3.md')
cy.uploadFile('test.md', 'text/markdown', 'test2.md')
cy.uploadFile('test.md', 'text/markdown')

cy.createUser(recipient)
})
beforeEach(function () {
cy.login(user)
Expand Down Expand Up @@ -129,23 +126,6 @@ describe('Open test.md in viewer', function () {
})
})

it('Share a file with download disabled shows an error', function () {
cy.shareFileToUser('test.md', recipient, {
attributes: '[{"scope":"permissions","key":"download","value":false}]',
}).then(() => {
cy.login(recipient)
cy.visit('/apps/files')
cy.openFile('test.md')
cy.getModal()
.find('.document-status')
.should(
'contain',
'This file cannot be displayed as download is disabled by the share',
)
cy.getModal().getContent().should('not.exist')
})
})

it('makes use of the file id', function () {
cy.intercept({
method: 'PUT',
Expand Down
12 changes: 11 additions & 1 deletion lib/DAV/WorkspacePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCA\Text\Service\WorkspaceService;
use OCP\Files\GenericFileException;
use OCP\Files\NotPermittedException;
use OCP\Files\Storage\ISharedStorage;
use OCP\ICacheFactory;
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -103,7 +104,7 @@ public function propFind(PropFind $propFind, INode $node) {
}
$workspaceContentCallback = function () use ($file) {
$cachedContent = '';
if ($file instanceof File) {
if ($file instanceof File && $this->canSeeContent($file)) {
$cache = $this->cacheFactory->createDistributed('text_workspace');
$cacheKey = $file->getFileInfo()->getId() . '_' . $file->getFileInfo()->getEtag();
if (($cachedContent = $cache->get($cacheKey)) !== null) {
Expand Down Expand Up @@ -139,4 +140,13 @@ public function propFind(PropFind $propFind, INode $node) {
$propFind->handle(self::WORKSPACE_FILE_PROPERTY, $workspaceFileCallback);
$propFind->handle(self::WORKSPACE_FILE_PROPERTY_FLAT, $workspaceFileCallback);
}

private function canSeeContent(File $file): bool {
$storage = $file->getStorage();
if (!$storage->instanceOfStorage(ISharedStorage::class)) {
return true;
}
/** @var ISharedStorage $storage */
return $storage->getShare()->canSeeContent();
}
}
3 changes: 1 addition & 2 deletions lib/Middleware/SessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ private function assertUserOrShareToken(ISessionAwareController $controller): vo
throw new InvalidSessionException();
}

$attributes = $share->getAttributes();
if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
if (!$share->canSeeContent()) {
throw new InvalidSessionException();
}

Expand Down
12 changes: 6 additions & 6 deletions lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Exception;
use InvalidArgumentException;
use OCA\Files_Sharing\SharedStorage;
use OCA\NotifyPush\Queue\IQueue;
use OCA\Text\AppInfo\Application;
use OCA\Text\Db\Document;
Expand All @@ -25,6 +24,7 @@
use OCP\Files\Lock\ILock;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\Storage\ISharedStorage;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Lock\LockedException;
Expand Down Expand Up @@ -56,10 +56,11 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b
*/
try {
$this->documentService->checkSharePermissions($token, Constants::PERMISSION_READ);
$this->documentService->checkShareCanSeeContent($token);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (NotPermittedException $e) {
return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], Http::STATUS_NOT_FOUND);
return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], Http::STATUS_FORBIDDEN);
}
} elseif ($fileId !== null) {
try {
Expand All @@ -76,12 +77,11 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b

$storage = $file->getStorage();

// Block using text for disabled download internal shares
if ($storage->instanceOfStorage(SharedStorage::class)) {
// Block using text when the share does not allow viewing the content
if ($storage->instanceOfStorage(ISharedStorage::class)) {
/** @var IShare $share */
$share = $storage->getShare();
$shareAttribtues = $share->getAttributes();
if ($shareAttribtues !== null && $shareAttribtues->getAttribute('permissions', 'download') === false) {
if (!$share->canSeeContent()) {
return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], Http::STATUS_FORBIDDEN);
}
}
Expand Down
52 changes: 41 additions & 11 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use OC\User\NoUserException;
use OCA\DAV\Connector\Sabre\PublicAuth;
use OCA\Files_Sharing\SharedStorage;
use OCA\Text\Controller\AttachmentController;
use OCA\Text\Db\Session;
use OCP\Constants;
Expand All @@ -24,6 +23,7 @@
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\Storage\ISharedStorage;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IPreview;
use OCP\ISession;
Expand Down Expand Up @@ -57,6 +57,7 @@ public function __construct(
*/
public function getImageFile(int $documentId, string $imageFileName, string $userId, bool $preferRawImage): File|ISimpleFile|null {
$textFile = $this->getTextFile($documentId, $userId);
$preferRawImage = $preferRawImage && !$this->isDownloadDisabled($textFile);
return $this->getImageFileContent($imageFileName, $textFile, $preferRawImage);
}

Expand All @@ -70,6 +71,7 @@ public function getImageFile(int $documentId, string $imageFileName, string $use
*/
public function getImageFilePublic(int $documentId, string $imageFileName, string $shareToken, bool $preferRawImage): File|ISimpleFile|null {
$textFile = $this->getTextFilePublic($documentId, $shareToken);
$preferRawImage = $preferRawImage && $this->canDownloadPublic($shareToken);
return $this->getImageFileContent($imageFileName, $textFile, $preferRawImage);
}

Expand Down Expand Up @@ -110,6 +112,9 @@ private function getImageFileContent(string $imageFileName, File $textFile, bool
*/
public function getMediaFile(int $documentId, string $mediaFileName, string $userId): ?File {
$textFile = $this->getTextFile($documentId, $userId);
if ($this->isDownloadDisabled($textFile)) {
return null;
}
return $this->getMediaFullFile($mediaFileName, $textFile);
}

Expand All @@ -123,6 +128,9 @@ public function getMediaFile(int $documentId, string $mediaFileName, string $use
*/
public function getMediaFilePublic(int $documentId, string $mediaFileName, string $shareToken): ?File {
$textFile = $this->getTextFilePublic($documentId, $shareToken);
if (!$this->canDownloadPublic($shareToken)) {
return null;
}
return $this->getMediaFullFile($mediaFileName, $textFile);
}

Expand All @@ -135,7 +143,7 @@ public function getMediaFilePublic(int $documentId, string $mediaFileName, strin
private function getMediaFullFile(string $mediaFileName, File $textFile): ?File {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$mediaFile = $attachmentFolder->get($mediaFileName);
if ($mediaFile instanceof File && !$this->isDownloadDisabled($mediaFile)) {
if ($mediaFile instanceof File) {
return $mediaFile;
}
return null;
Expand Down Expand Up @@ -174,7 +182,7 @@ public function getMediaFilePreviewPublic(int $documentId, string $mediaFileName
private function getMediaFilePreviewFile(string $mediaFileName, File $textFile): ?array {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$mediaFile = $attachmentFolder->get($mediaFileName);
if ($mediaFile instanceof File && !$this->isDownloadDisabled($mediaFile)) {
if ($mediaFile instanceof File) {
if ($this->previewManager->isMimeSupported($mediaFile->getMimeType())) {
try {
return [
Expand Down Expand Up @@ -223,6 +231,9 @@ public function getAttachmentList(int $documentId, ?string $userId = null, ?Sess
: '?documentId=' . $documentId . $shareTokenUrlString;

$attachments = [];
$canDownload = $shareToken !== null
? $this->canDownloadPublic($shareToken)
: !$this->isDownloadDisabled($textFile);

// Folder davPath need to be relative to.
$davFolder = $userId !== null
Expand Down Expand Up @@ -256,9 +267,11 @@ public function getAttachmentList(int $documentId, ?string $userId = null, ?Sess
'isImage' => $isImage,
'davPath' => $davFolder?->getRelativePath($node->getPath()),
'metadata' => $metadata,
'fullUrl' => $isImage
? $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getImageFile') . $urlParamsBase . '&imageFileName=' . rawurlencode($name) . '&preferRawImage=1'
: $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getMediaFile') . $urlParamsBase . '&mediaFileName=' . rawurlencode($name),
'fullUrl' => match (true) {
$isImage => $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getImageFile') . $urlParamsBase . '&imageFileName=' . rawurlencode($name) . ($canDownload ? '&preferRawImage=1' : ''),
$canDownload => $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getMediaFile') . $urlParamsBase . '&mediaFileName=' . rawurlencode($name),
default => null,
},
'previewUrl' => $isImage
? $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getImageFile') . $urlParamsBase . '&imageFileName=' . rawurlencode($name)
: $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getMediaFilePreview') . $urlParamsBase . '&mediaFileName=' . rawurlencode($name),
Expand Down Expand Up @@ -500,8 +513,8 @@ private function getFileFromPath(string $filePath, string $userId): File {
*/
private function isDownloadDisabled(File $file): bool {
$storage = $file->getStorage();
if ($storage->instanceOfStorage(SharedStorage::class)) {
/** @var SharedStorage $storage */
if ($storage->instanceOfStorage(ISharedStorage::class)) {
/** @var ISharedStorage $storage */
$share = $storage->getShare();
$attributes = $share->getAttributes();
if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
Expand All @@ -512,6 +525,23 @@ private function isDownloadDisabled(File $file): bool {
return false;
}

private function canSeeContent(File $file): bool {
$storage = $file->getStorage();
if (!$storage->instanceOfStorage(ISharedStorage::class)) {
return true;
}
/** @var ISharedStorage $storage */
return $storage->getShare()->canSeeContent();
}

private function canDownloadPublic(string $shareToken): bool {
try {
return $this->shareManager->getShareByToken($shareToken)->canDownload();
} catch (ShareNotFound) {
return false;
}
}

/**
* Get a user file from file ID
*
Expand All @@ -522,7 +552,7 @@ private function isDownloadDisabled(File $file): bool {
private function getTextFile(int $documentId, string $userId): File {
$userFolder = $this->rootFolder->getUserFolder($userId);
$file = $userFolder->getFirstNodeById($documentId);
if ($file instanceof File && !$this->isDownloadDisabled($file)) {
if ($file instanceof File && $this->canSeeContent($file)) {
return $file;
}
throw new NotFoundException('Text file with id=' . $documentId . ' was not found in storage of ' . $userId);
Expand All @@ -542,7 +572,7 @@ private function getTextFilePublic(?int $documentId, string $shareToken): File {
if ($share->getNodeType() === 'file') {
$textFile = $share->getNode();
if ($textFile instanceof File
&& !$this->isDownloadDisabled($textFile)
&& $this->canSeeContent($textFile)
&& $textFile->getId() === $documentId
) {
return $textFile;
Expand All @@ -551,7 +581,7 @@ private function getTextFilePublic(?int $documentId, string $shareToken): File {
$folder = $share->getNode();
if ($folder instanceof Folder) {
$textFile = $folder->getFirstNodeById($documentId);
if ($textFile instanceof File && !$this->isDownloadDisabled($textFile)) {
if ($textFile instanceof File && $this->canSeeContent($textFile)) {
return $textFile;
}
}
Expand Down
16 changes: 16 additions & 0 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,22 @@ public function checkSharePermissions(string $shareToken, int $permission = Cons
}
}

/**
* @throws NotFoundException
* @throws NotPermittedException
*/
public function checkShareCanSeeContent(string $shareToken): void {
try {
$share = $this->shareManager->getShareByToken($shareToken);
} catch (ShareNotFound $e) {
throw new NotFoundException();
}

if (!$share->canSeeContent()) {
throw new NotPermittedException();
}
}

public function hasUnsavedChanges(Document $document): bool {
$stepsVersion = $this->stepMapper->getLatestVersion($document->getId()) ?: 0;
$docVersion = $document->getLastSavedVersion();
Expand Down
56 changes: 55 additions & 1 deletion playwright/e2e/propfind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import { expect } from '@playwright/test'
import { createFolder, uploadFile } from '../support/fixtures/Node.ts'
import { test } from '../support/fixtures/random-user.ts'
import { setTextSetting } from '../support/fixtures/settings.ts'
import { setAppConfig, setTextSetting } from '../support/fixtures/settings.ts'
import { test as sharedFileTest } from '../support/fixtures/shared-file.ts'
import {
deleteWebDAVResource,
PROPERTY_WORKSPACE,
Expand Down Expand Up @@ -140,4 +141,57 @@ test.describe('Text PROPFIND extension', () => {
expect(folder).not.toHaveProperty(PROPERTY_WORKSPACE)
})
})

sharedFileTest.describe('in a folder shared with download disabled', () => {
sharedFileTest.beforeEach(async ({ owner, recipient }) => {
await setTextSetting(recipient, 'workspace_enabled', 1)
const folder = await createFolder({ name: 'shared', owner })
await owner.uploadFile({
name: 'shared/Readme.md',
content: '## Hello world\n',
})
await folder.shareWithUser(recipient.account.userId, {
attributes: [
{ scope: 'permissions', key: 'download', value: false },
],
})
})

sharedFileTest(
'returns the rich workspace content',
async ({ recipient }) => {
const [folder] = await propfindFolder(recipient, '/shared', 0)
expect(folder).toHaveProperty(PROPERTY_WORKSPACE, '## Hello world\n')
},
)

sharedFileTest.describe('with viewing without download disallowed', () => {
sharedFileTest.beforeEach(async ({ request }) => {
await setAppConfig(
request,
'core',
'shareapi_allow_view_without_download',
'no',
)
})

sharedFileTest.afterEach(async ({ request }) => {
await setAppConfig(
request,
'core',
'shareapi_allow_view_without_download',
null,
)
})

sharedFileTest(
'does not return the rich workspace content',
async ({ recipient }) => {
const [folder] = await propfindFolder(recipient, '/shared', 0)
expect(folder).toHaveProperty(PROPERTY_WORKSPACE, '')
expect(folder[PROPERTY_WORKSPACE_FILE]).not.toBe('')
},
)
})
})
})
Loading
Loading