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
160 changes: 0 additions & 160 deletions cypress/e2e/share.spec.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/Context/FileContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public function buildDocument(): Document {
$this->logger->warning('No permission to access this file', ['exception' => $e]);
throw new NotFoundException($this->l10n->t('File not found'), $e->getCode(), $e);
}
// Block using text for disabled download internal shares
if ($this->fileService->isDownloadDisabled($file)) {
// Block using text when the share does not allow viewing the content
if (!$this->fileService->canSeeContent($file)) {
throw new NotPermittedException(
$this->l10n->t('This file cannot be displayed as download is disabled by the share')
);
Expand Down
6 changes: 6 additions & 0 deletions lib/Context/FileContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\Text\Service\LockService;
use OCP\Files\File;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
use OCP\IUser;
use OCP\Share\IShare;
Expand Down Expand Up @@ -39,6 +40,11 @@ public function build(
* If not then well 404 it is.
*/
$this->fileService->checkSharePermissions($auth);
if (!$auth->canSeeContent()) {
throw new NotPermittedException(
$this->l10n->t('This file cannot be displayed as download is disabled by the share')
);
}
}
return new FileContext(
$this->fileService,
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/AttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function getImageFile(
try {
$auth = $this->getAuth($shareToken, false);
$context = $this->getContext($auth, $fileId);
$imageFile = $this->attachmentService->getImageFile($context, $imageFileName, $preferRawImage === 1);
$imageFile = $this->attachmentService->getImageFile($context, $imageFileName, $preferRawImage === 1, $auth);
if ($imageFile !== null) {
$response = new DataDownloadResponse(
$imageFile->getContent(),
Expand Down Expand Up @@ -252,7 +252,7 @@ public function getMediaFile(
try {
$auth = $this->getAuth($shareToken, false);
$context = $this->getContext($auth, $fileId);
$mediaFile = $this->attachmentService->getMediaFile($context, $mediaFileName);
$mediaFile = $this->attachmentService->getMediaFile($context, $mediaFileName, $auth);
return $mediaFile !== null
? new DataDownloadResponse(
$mediaFile->getContent(),
Expand Down
3 changes: 3 additions & 0 deletions lib/Controller/PublicSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\PublicShareController;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IRequest;
use OCP\ISession;
use OCP\Share\Exceptions\ShareNotFound;
Expand Down Expand Up @@ -79,6 +80,8 @@ public function create(string $token, ?string $filePath = null, ?string $baseVer
return $this->apiService->create($context, $baseVersionEtag, $guestName);
} catch (NotFoundException|\InvalidArgumentException) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (NotPermittedException $e) {
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_FORBIDDEN);
}
}

Expand Down
8 changes: 6 additions & 2 deletions lib/DAV/WorkspacePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Files\FilesHome;
use OCA\Text\Service\ConfigService;
use OCA\Text\Service\EncodingService;
use OCA\Text\Service\FileService;
use OCA\Text\Service\WorkspaceService;
use OCP\Files\GenericFileException;
use OCP\Files\NotPermittedException;
Expand All @@ -34,8 +36,10 @@ class WorkspacePlugin extends ServerPlugin {

public function __construct(
private readonly WorkspaceService $workspaceService,
private readonly FileService $fileService,
private readonly ICacheFactory $cacheFactory,
private readonly ConfigService $configService,
private readonly EncodingService $encodingService,
private readonly LoggerInterface $logger,
private readonly ?string $userId,
) {
Expand Down Expand Up @@ -102,15 +106,15 @@ public function propFind(PropFind $propFind, INode $node) {
}
$workspaceContentCallback = function () use ($file) {
$cachedContent = '';
if ($file instanceof File) {
if ($file instanceof File && $this->fileService->canSeeContent($file)) {
$cache = $this->cacheFactory->createDistributed('text_workspace');
$cacheKey = $file->getFileInfo()->getId() . '_' . $file->getFileInfo()->getEtag();
if (($cachedContent = $cache->get($cacheKey)) !== null) {
return $cachedContent;
}

try {
$cachedContent = $file->getContent();
$cachedContent = $this->encodingService->encodeToXmlSafeUtf8($file->getContent());
$cache->set($cacheKey, $cachedContent, 3600);
} catch (GenericFileException|NotPermittedException|LockedException $e) {
// Ignore but log when debugging
Expand Down
31 changes: 22 additions & 9 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public function __construct(
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getImageFile(IContext $context, string $imageFileName, bool $preferRawImage): File|ISimpleFile|null {
public function getImageFile(IContext $context, string $imageFileName, bool $preferRawImage, IShare|IUser $auth): File|ISimpleFile|null {
$textFile = $this->getTextFile($context);
$preferRawImage = $preferRawImage && $this->canDownload($textFile, $auth);
return $this->getImageFileContent($imageFileName, $textFile, $preferRawImage);
}

Expand Down Expand Up @@ -96,11 +97,21 @@ private function getImageFileContent(string $imageFileName, File $textFile, bool
* @throws NotPermittedException
* @throws NoUserException
*/
public function getMediaFile(IContext $context, string $mediaFileName): ?File {
public function getMediaFile(IContext $context, string $mediaFileName, IShare|IUser $auth): ?File {
$textFile = $this->getTextFile($context);
if (!$this->canDownload($textFile, $auth)) {
return null;
}
return $this->getMediaFullFile($mediaFileName, $textFile);
}

private function canDownload(File $textFile, IShare|IUser $auth): bool {
if ($auth instanceof IShare) {
return $auth->canDownload();
}
return !$this->fileService->isDownloadDisabled($textFile);
}

/**
* @throws NotFoundException
* @throws NotPermittedException
Expand All @@ -110,7 +121,7 @@ public function getMediaFile(IContext $context, string $mediaFileName): ?File {
private function getMediaFullFile(string $mediaFileName, File $textFile): ?File {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$mediaFile = $attachmentFolder->get($mediaFileName);
if ($mediaFile instanceof File && !$this->fileService->isDownloadDisabled($mediaFile)) {
if ($mediaFile instanceof File) {
return $mediaFile;
}
return null;
Expand Down Expand Up @@ -138,7 +149,7 @@ public function getMediaFilePreview(IContext $context, string $mediaFileName): ?
private function getMediaFilePreviewFile(string $mediaFileName, File $textFile): ?array {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$mediaFile = $attachmentFolder->get($mediaFileName);
if ($mediaFile instanceof File && !$this->fileService->isDownloadDisabled($mediaFile)) {
if ($mediaFile instanceof File) {
if ($this->previewManager->isMimeSupported($mediaFile->getMimeType())) {
try {
return [
Expand Down Expand Up @@ -167,7 +178,6 @@ private function getMediaFilePreviewFile(string $mediaFileName, File $textFile):
public function getAttachmentList(IContext $context, IShare|IUser $auth, ?Session $session = null): array {
$textFile = $this->getTextFile($context);
try {

$attachmentDir = $this->getAttachmentDirectoryForFile($textFile);
} catch (NotFoundException) {
return [];
Expand All @@ -181,6 +191,7 @@ public function getAttachmentList(IContext $context, IShare|IUser $auth, ?Sessio
: '?fileId=' . $context->getId() . $shareTokenUrlString;

$attachments = [];
$canDownload = $this->canDownload($textFile, $auth);

// Folder davPath need to be relative to.
$davFolder = ($auth instanceof IUser)
Expand Down Expand Up @@ -214,9 +225,11 @@ public function getAttachmentList(IContext $context, IShare|IUser $auth, ?Sessio
'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 @@ -403,7 +416,7 @@ private function getFileFromPath(string $filePath, string $userId): File {
*/
private function getTextFile(IContext $context): File {
$file = $context->getFile();
if ($file instanceof File && !$this->fileService->isDownloadDisabled($file)) {
if ($file instanceof File && $this->fileService->canSeeContent($file)) {
return $file;
}
throw new NotFoundException('Text file for '
Expand Down
20 changes: 20 additions & 0 deletions lib/Service/EncodingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class EncodingService {
'UTF-8' => "\xef\xbb\xbf"
];

/**
* Matches characters outside the XML 1.0 Char production, which cannot be
* part of an XML document, not even escaped.
*
* @see https://www.w3.org/TR/xml/#charsets
*/
private const string XML_INVALID_CHARS = '/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u';

public function encodeToUtf8(string $string): ?string {
$encoding = $this->detectEncoding($string);
if ($encoding === null) {
Expand All @@ -30,6 +38,18 @@ public function encodeToUtf8(string $string): ?string {
return is_string($encoded) ? $encoded : null;
}

/**
* Encode to UTF-8 and replace characters that are not allowed in XML with U+FFFD
*/
public function encodeToXmlSafeUtf8(string $string): ?string {
$encoded = $this->encodeToUtf8($string);
if ($encoded === null) {
return null;
}

return preg_replace(self::XML_INVALID_CHARS, "\u{FFFD}", $encoded);
}

public function detectEncoding(string $string): ?string {
$bomDetect = $this->detectUtfBom($string);
if ($bomDetect !== null) {
Expand Down
Loading
Loading