Skip to content

Commit ee78276

Browse files
committed
fix(appstore): catch GenericFileException when reading cache file in Fetcher
When the appstore cache file exists but file_get_contents returns false (e.g. empty or corrupted file), a GenericFileException is thrown but was not caught, crashing the apps settings page. Recreate the file and proceed to fetch fresh data, same as when the file is absent. Signed-off-by: Anna Larch <anna@nextcloud.com> AI-Assisted-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f16c6c9 commit ee78276

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

lib/private/App/AppStore/Fetcher/Fetcher.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OC\Files\AppData\Factory;
1313
use OCP\AppFramework\Http;
1414
use OCP\AppFramework\Utility\ITimeFactory;
15+
use OCP\Files\GenericFileException;
1516
use OCP\Files\IAppData;
1617
use OCP\Files\NotFoundException;
1718
use OCP\Http\Client\IClientService;
@@ -167,7 +168,11 @@ public function get($allowUnstable = false): array {
167168
}
168169
}
169170
} catch (NotFoundException $e) {
170-
// File does not already exists
171+
// File does not already exist
172+
$file = $rootFolder->newFile($this->fileName);
173+
} catch (GenericFileException $e) {
174+
// Cache file exists but is unreadable (e.g. empty or corrupted) — recreate it
175+
$this->logger->warning('Could not read appstore cache file, recreating it', ['app' => 'appstoreFetcher', 'exception' => $e]);
171176
$file = $rootFolder->newFile($this->fileName);
172177
}
173178

tests/lib/App/AppStore/Fetcher/FetcherBase.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OC\Files\AppData\AppData;
1414
use OC\Files\AppData\Factory;
1515
use OCP\AppFramework\Utility\ITimeFactory;
16+
use OCP\Files\GenericFileException;
1617
use OCP\Files\IAppData;
1718
use OCP\Files\NotFoundException;
1819
use OCP\Files\SimpleFS\ISimpleFile;
@@ -684,4 +685,84 @@ public function testFetchAfterUpgradeNoETag(): void {
684685
];
685686
$this->assertSame($expected, $this->fetcher->get());
686687
}
688+
689+
public function testGetWithUnreadableCacheFileRecreatesAndFetches(): void {
690+
$this->config
691+
->method('getSystemValueString')
692+
->willReturnCallback(function ($var, $default) {
693+
if ($var === 'appstoreurl') {
694+
return 'https://apps.nextcloud.com/api/v1';
695+
} elseif ($var === 'version') {
696+
return '11.0.0.2';
697+
}
698+
return $default;
699+
});
700+
$this->config->method('getSystemValueBool')
701+
->willReturnArgument(1);
702+
703+
$folder = $this->createMock(ISimpleFolder::class);
704+
$corruptedFile = $this->createMock(ISimpleFile::class);
705+
$freshFile = $this->createMock(ISimpleFile::class);
706+
$this->appData
707+
->expects($this->once())
708+
->method('getFolder')
709+
->with('/')
710+
->willReturn($folder);
711+
$folder
712+
->expects($this->once())
713+
->method('getFile')
714+
->with($this->fileName)
715+
->willReturn($corruptedFile);
716+
$corruptedFile
717+
->expects($this->once())
718+
->method('getContent')
719+
->willThrowException(new GenericFileException());
720+
$folder
721+
->expects($this->once())
722+
->method('newFile')
723+
->with($this->fileName)
724+
->willReturn($freshFile);
725+
$client = $this->createMock(IClient::class);
726+
$this->clientService
727+
->expects($this->once())
728+
->method('newClient')
729+
->willReturn($client);
730+
$response = $this->createMock(IResponse::class);
731+
$client
732+
->expects($this->once())
733+
->method('get')
734+
->with($this->endpoint)
735+
->willReturn($response);
736+
$response
737+
->expects($this->once())
738+
->method('getBody')
739+
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
740+
$response->method('getHeader')
741+
->with($this->equalTo('ETag'))
742+
->willReturn('"myETag"');
743+
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
744+
$freshFile
745+
->expects($this->once())
746+
->method('putContent')
747+
->with($fileData);
748+
$freshFile
749+
->expects($this->once())
750+
->method('getContent')
751+
->willReturn($fileData);
752+
$this->timeFactory
753+
->expects($this->once())
754+
->method('getTime')
755+
->willReturn(1502);
756+
757+
$expected = [
758+
[
759+
'id' => 'MyNewApp',
760+
'foo' => 'foo',
761+
],
762+
[
763+
'id' => 'bar',
764+
],
765+
];
766+
$this->assertSame($expected, $this->fetcher->get());
767+
}
687768
}

0 commit comments

Comments
 (0)