Skip to content

Commit 2d1b6a2

Browse files
committed
fix: Do not DI the database connection to prevent cyclic dependency
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 1547eab commit 2d1b6a2

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

lib/private/Files/FilenameValidator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class FilenameValidator implements IFilenameValidator {
2727

2828
private IL10N $l10n;
2929

30+
private ?IDBConnection $database;
31+
3032
/**
3133
* @var list<string>
3234
*/
@@ -48,7 +50,6 @@ class FilenameValidator implements IFilenameValidator {
4850

4951
public function __construct(
5052
IFactory $l10nFactory,
51-
private IDBConnection $database,
5253
private IConfig $config,
5354
private LoggerInterface $logger,
5455
) {
@@ -187,6 +188,11 @@ public function validateFilename(string $filename): void {
187188
throw new FileNameTooLongException();
188189
}
189190

191+
// We need to lazy load the database as otherwise there is a cyclic dependency
192+
if (!isset($this->database)) {
193+
$this->database = \OCP\Server::get(IDBConnection::class);
194+
}
195+
190196
if (!$this->database->supports4ByteText()) {
191197
// verify database - e.g. mysql only 3-byte chars
192198
if (preg_match('%(?:

tests/lib/Files/FilenameValidatorTest.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
use Psr\Log\LoggerInterface;
2525
use Test\TestCase;
2626

27+
/**
28+
* @group DB
29+
*/
2730
class FilenameValidatorTest extends TestCase {
2831

2932
protected IFactory&MockObject $l10n;
3033
protected IConfig&MockObject $config;
3134
protected IDBConnection&MockObject $database;
3235
protected LoggerInterface&MockObject $logger;
36+
protected bool $dbSupportsUtf8 = true;
3337

3438
protected function setUp(): void {
3539
parent::setUp();
@@ -45,7 +49,13 @@ protected function setUp(): void {
4549
$this->config = $this->createMock(IConfig::class);
4650
$this->logger = $this->createMock(LoggerInterface::class);
4751
$this->database = $this->createMock(IDBConnection::class);
48-
$this->database->method('supports4ByteText')->willReturn(true);
52+
$this->database->method('supports4ByteText')->willReturnCallback(fn () => $this->dbSupportsUtf8);
53+
$this->overwriteService(IDBConnection::class, $this->database);
54+
}
55+
56+
protected function tearDown(): void {
57+
$this->restoreAllServices();
58+
parent::tearDown();
4959
}
5060

5161
/**
@@ -67,7 +77,7 @@ public function testValidateFilename(
6777
'getForbiddenExtensions',
6878
'getForbiddenFilenames',
6979
])
70-
->setConstructorArgs([$this->l10n, $this->database, $this->config, $this->logger])
80+
->setConstructorArgs([$this->l10n, $this->config, $this->logger])
7181
->getMock();
7282

7383
$validator->method('getForbiddenBasenames')
@@ -106,7 +116,7 @@ public function testIsFilenameValid(
106116
'getForbiddenFilenames',
107117
'getForbiddenCharacters',
108118
])
109-
->setConstructorArgs([$this->l10n, $this->database, $this->config, $this->logger])
119+
->setConstructorArgs([$this->l10n, $this->config, $this->logger])
110120
->getMock();
111121

112122
$validator->method('getForbiddenBasenames')
@@ -186,20 +196,17 @@ public function dataValidateFilename(): array {
186196
* @dataProvider data4ByteUnicode
187197
*/
188198
public function testDatabaseDoesNotSupport4ByteText($filename): void {
189-
$database = $this->createMock(IDBConnection::class);
190-
$database->expects($this->once())
191-
->method('supports4ByteText')
192-
->willReturn(false);
199+
$this->dbSupportsUtf8 = false;
200+
193201
$this->expectException(InvalidCharacterInPathException::class);
194-
$validator = new FilenameValidator($this->l10n, $database, $this->config, $this->logger);
202+
$validator = new FilenameValidator($this->l10n, $this->config, $this->logger);
195203
$validator->validateFilename($filename);
196204
}
197205

198206
public function data4ByteUnicode(): array {
199207
return [
200208
['plane 1 𐪅'],
201209
['emoji 😶‍🌫️'],
202-
203210
];
204211
}
205212

@@ -208,7 +215,7 @@ public function data4ByteUnicode(): array {
208215
*/
209216
public function testInvalidAsciiCharactersAreAlwaysForbidden(string $filename): void {
210217
$this->expectException(InvalidPathException::class);
211-
$validator = new FilenameValidator($this->l10n, $this->database, $this->config, $this->logger);
218+
$validator = new FilenameValidator($this->l10n, $this->config, $this->logger);
212219
$validator->validateFilename($filename);
213220
}
214221

@@ -256,7 +263,7 @@ public function testIsForbidden(string $filename, array $forbiddenNames, bool $e
256263
/** @var FilenameValidator&MockObject */
257264
$validator = $this->getMockBuilder(FilenameValidator::class)
258265
->onlyMethods(['getForbiddenFilenames'])
259-
->setConstructorArgs([$this->l10n, $this->database, $this->config, $this->logger])
266+
->setConstructorArgs([$this->l10n, $this->config, $this->logger])
260267
->getMock();
261268

262269
$validator->method('getForbiddenFilenames')

tests/lib/TestCase.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,6 @@ protected function getGroupAnnotations(): array {
508508
}
509509

510510
protected function IsDatabaseAccessAllowed() {
511-
// on travis-ci.org we allow database access in any case - otherwise
512-
// this will break all apps right away
513-
if (getenv('TRAVIS') == true) {
514-
return true;
515-
}
516511
$annotations = $this->getGroupAnnotations();
517512
if (isset($annotations)) {
518513
if (in_array('DB', $annotations) || in_array('SLOWDB', $annotations)) {
@@ -548,7 +543,7 @@ protected function assertTemplate($expectedHtml, $template, $vars = []) {
548543
return vsprintf($text, $parameters);
549544
});
550545

551-
$t = new Base($template, $requestToken, $l10n, $theme);
546+
$t = new Base($template, $requestToken, $l10n, $theme, base64_encode('csp-nonce'));
552547
$buf = $t->fetchPage($vars);
553548
$this->assertHtmlStringEqualsHtmlString($expectedHtml, $buf);
554549
}

0 commit comments

Comments
 (0)