Skip to content

Commit

Permalink
Merge pull request #46762 from nextcloud/backport/46672/stable27
Browse files Browse the repository at this point in the history
[stable27] Avoid using partial file info as valid one
  • Loading branch information
juliusknorr authored Jul 29, 2024
2 parents 1e77c4a + e4e4e12 commit 0f48386
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions apps/files/lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public function getThumbnail($x, $y, $file) {
throw new NotFoundException();
}

if ($file->getId() <= 0) {
return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
}

/** @var File $file */
$preview = $this->previewManager->getPreview($file, $x, $y, true);

Expand Down
12 changes: 12 additions & 0 deletions apps/files/tests/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public function testGetThumbnailInvalidSize() {

public function testGetThumbnailInvalidImage() {
$file = $this->createMock(File::class);
$file->method('getId')->willReturn(123);
$this->userFolder->method('get')
->with($this->equalTo('unknown.jpg'))
->willReturn($file);
Expand All @@ -188,8 +189,19 @@ public function testGetThumbnailInvalidImage() {
$this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
}

public function testGetThumbnailInvalidPartFile() {
$file = $this->createMock(File::class);
$file->method('getId')->willReturn(0);
$this->userFolder->method('get')
->with($this->equalTo('unknown.jpg'))
->willReturn($file);
$expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
$this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
}

public function testGetThumbnail() {
$file = $this->createMock(File::class);
$file->method('getId')->willReturn(123);
$this->userFolder->method('get')
->with($this->equalTo('known.jpg'))
->willReturn($file);
Expand Down
4 changes: 4 additions & 0 deletions core/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ private function fetchPreview(
return new DataResponse([], Http::STATUS_FORBIDDEN);
}

if ($node->getId() <= 0) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

$storage = $node->getStorage();
if ($storage->instanceOfStorage(SharedStorage::class)) {
/** @var SharedStorage $storage */
Expand Down
7 changes: 4 additions & 3 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -1369,9 +1369,6 @@ public function getFileInfo($path, $includeMountPoints = true) {
if (!Filesystem::isValidPath($path)) {
return false;
}
if (Cache\Scanner::isPartialFile($path)) {
return $this->getPartFileInfo($path);
}
$relativePath = $path;
$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);

Expand All @@ -1382,6 +1379,10 @@ public function getFileInfo($path, $includeMountPoints = true) {
$data = $this->getCacheEntry($storage, $internalPath, $relativePath);

if (!$data instanceof ICacheEntry) {
if (Cache\Scanner::isPartialFile($relativePath)) {
return $this->getPartFileInfo($relativePath);
}

return false;
}

Expand Down
1 change: 1 addition & 0 deletions tests/Core/Controller/PreviewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public function testValidPreview() {
->willReturn($userFolder);

$file = $this->createMock(File::class);
$file->method('getId')->willReturn(123);
$userFolder->method('get')
->with($this->equalTo('file'))
->willReturn($file);
Expand Down

0 comments on commit 0f48386

Please sign in to comment.