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
3 changes: 3 additions & 0 deletions apps/files_sharing/lib/External/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ private function getDefaultRequestOptions(): array {
$options = [
'timeout' => 10,
'connect_timeout' => 10,
'nextcloud' => [
'allow_local_address' => $this->config->getSystemValueBool('davstorage_allow_local_address', false),
],
];
if ($this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates')) {
$options['verify'] = false;
Expand Down
25 changes: 25 additions & 0 deletions apps/files_sharing/tests/ExternalStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\ICertificateManager;
use OCP\IConfig;
use OCP\IUser;
use OCP\OCM\IOCMDiscoveryService;
use OCP\OCM\IOCMProvider;
Expand Down Expand Up @@ -111,6 +112,30 @@ public function testStorageMountOptions($inputUri, $baseUri): void {
$this->assertEquals($baseUri, $storage->getBaseUri());
}

public static function allowLocalAddressProvider(): array {
return [
'not configured' => [null, false],
'disabled' => [false, false],
'enabled' => [true, true],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'allowLocalAddressProvider')]
public function testDefaultRequestOptionsAllowLocalAddress(?bool $configValue, bool $expected): void {
$config = Server::get(IConfig::class);
if ($configValue !== null) {
$config->setSystemValue('davstorage_allow_local_address', $configValue);
}

try {
$options = self::invokePrivate($this->getTestStorage('https://remoteserver'), 'getDefaultRequestOptions');
} finally {
$config->deleteSystemValue('davstorage_allow_local_address');
}

$this->assertSame($expected, $options['nextcloud']['allow_local_address']);
}

public function testIfTestReturnsTheValue(): void {
$storage = $this->getTestStorage('https://remoteserver');
$result = $storage->test();
Expand Down
13 changes: 13 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,19 @@
*/
'davstorage.request_timeout' => 30,

/**
* Allow WebDAV based storages to connect to servers with local addresses
* (e.g., a NAS on the local network). This applies to WebDAV and Nextcloud
* external storages and to the file access of federated shares, without
* allowing local addresses globally as ``allow_local_remote_servers`` does.
*
* Users who can create personal external storages can then make the server
* send requests to hosts on the local network.
*
* Defaults to ``false``
*/
'davstorage_allow_local_address' => false,

/**
* The timeout in seconds for synchronizing address books, e.g., federated system address books (as run by ``occ federation:sync-addressbooks``).
*
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Files/Storage/DAV.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class DAV extends Common {

/** @var int */
private $timeout;
private bool $allowLocalAddress;

protected const PROPFIND_PROPS = [
'{DAV:}getlastmodified',
Expand Down Expand Up @@ -225,6 +226,7 @@ public function __construct(array $parameters) {
$this->eventLogger = Server::get(IEventLogger::class);
// This timeout value will be used for the download and upload of files
$this->timeout = Server::get(IConfig::class)->getSystemValueInt('davstorage.request_timeout', IClient::DEFAULT_REQUEST_TIMEOUT);
$this->allowLocalAddress = Server::get(IConfig::class)->getSystemValueBool('davstorage_allow_local_address', false);
$this->mimeTypeDetector = Server::get(IMimeTypeDetector::class);
$this->signatureManager = Server::get(ISignatureManager::class);
$this->signatoryManager = Server::get(OCMSignatoryManager::class);
Expand Down Expand Up @@ -340,6 +342,7 @@ protected function exchangeRefreshToken(): string {
],
'timeout' => 10,
'connect_timeout' => 10,
'nextcloud' => ['allow_local_address' => $this->allowLocalAddress],
];

try {
Expand Down Expand Up @@ -677,6 +680,7 @@ public function fopen(string $path, string $mode) {
// set download timeout for users with slow connections or large files
'timeout' => $this->timeout,
'verify' => $this->verify,
'nextcloud' => ['allow_local_address' => $this->allowLocalAddress],
]);
});
} catch (\GuzzleHttp\Exception\ClientException $e) {
Expand Down Expand Up @@ -839,6 +843,7 @@ protected function uploadFile(string $path, string $target): void {
// set upload timeout for users with slow connections or large files
'timeout' => $this->timeout,
'verify' => $this->verify,
'nextcloud' => ['allow_local_address' => $this->allowLocalAddress],
]);
});

Expand Down
106 changes: 106 additions & 0 deletions tests/lib/Files/Storage/DAVTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Test\Files\Storage;

use OC\Files\Storage\DAV;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IConfig;
use OCP\ITempManager;
use OCP\Server;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class DAVTest extends TestCase {
private IClient&MockObject $client;

protected function setUp(): void {
parent::setUp();

$this->client = $this->createMock(IClient::class);
$clientService = $this->createMock(IClientService::class);
$clientService->method('newClient')->willReturn($this->client);
$this->overwriteService(IClientService::class, $clientService);
}

protected function tearDown(): void {
Server::get(IConfig::class)->deleteSystemValue('davstorage_allow_local_address');
$this->restoreService(IClientService::class);
parent::tearDown();
}

private function createStorage(): TestDAVStorage {
return new TestDAVStorage([
'host' => 'nas.local',
'user' => 'user',
'password' => 'password',
'root' => '/',
'secure' => false,
]);
}

public static function allowLocalAddressProvider(): array {
return [
'not configured' => [null, false],
'disabled' => [false, false],
'enabled' => [true, true],
];
}

#[DataProvider(methodName: 'allowLocalAddressProvider')]
public function testFopenReadPassesAllowLocalAddress(?bool $configValue, bool $expected): void {
if ($configValue !== null) {
Server::get(IConfig::class)->setSystemValue('davstorage_allow_local_address', $configValue);
}

$response = $this->createMock(IResponse::class);
$response->method('getStatusCode')->willReturn(200);
$response->method('getBody')->willReturn(fopen('php://memory', 'r'));

$this->client->expects($this->once())
->method('get')
->with(
'http://nas.local/file.txt',
$this->callback(fn (array $options): bool => $options['nextcloud']['allow_local_address'] === $expected),
)
->willReturn($response);

$this->assertIsResource($this->createStorage()->fopen('file.txt', 'r'));
}

#[DataProvider(methodName: 'allowLocalAddressProvider')]
public function testUploadFilePassesAllowLocalAddress(?bool $configValue, bool $expected): void {
if ($configValue !== null) {
Server::get(IConfig::class)->setSystemValue('davstorage_allow_local_address', $configValue);
}

$this->client->expects($this->once())
->method('put')
->with(
'http://nas.local/file.txt',
$this->callback(fn (array $options): bool => $options['nextcloud']['allow_local_address'] === $expected),
);

$source = Server::get(ITempManager::class)->getTemporaryFile();
$this->createStorage()->callUploadFile($source, 'file.txt');
}
}

class TestDAVStorage extends DAV {
public function callUploadFile(string $path, string $target): void {
$this->uploadFile($path, $target);
}

#[\Override]
protected function removeCachedFile(string $path): void {
}
}
Loading