Skip to content

Commit 9550885

Browse files
committed
fix(http): keep redirect validation when callers pass allow_redirects
Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Daniel Kesselberg <3902676+kesselb@users.noreply.github.com>
1 parent c5c9b14 commit 9550885

2 files changed

Lines changed: 80 additions & 13 deletions

File tree

‎lib/private/Http/Client/Client.php‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,6 @@ private function buildRequestOptions(array $options): array {
5353
$defaults['curl'][\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_2TLS;
5454

5555
$options['nextcloud']['allow_local_address'] = $this->isLocalAddressAllowed($options);
56-
if ($options['nextcloud']['allow_local_address'] === false) {
57-
$onRedirectFunction = function (
58-
\Psr\Http\Message\RequestInterface $request,
59-
\Psr\Http\Message\ResponseInterface $response,
60-
\Psr\Http\Message\UriInterface $uri,
61-
) use ($options): void {
62-
$this->preventLocalAddress($uri->__toString(), $options);
63-
};
64-
65-
$defaults[RequestOptions::ALLOW_REDIRECTS] = [
66-
'on_redirect' => $onRedirectFunction
67-
];
68-
}
6956

7057
// Only add RequestOptions::PROXY if Nextcloud is explicitly
7158
// configured to use a proxy. This is needed in order not to override
@@ -76,6 +63,10 @@ private function buildRequestOptions(array $options): array {
7663

7764
$options = array_merge($defaults, $options);
7865

66+
if ($options['nextcloud']['allow_local_address'] === false) {
67+
$options = $this->addRedirectHostCheck($options);
68+
}
69+
7970
if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) {
8071
$userAgent = 'Nextcloud-Server-Crawler/' . $this->serverVersion->getVersionString();
8172
$overwriteCliUrl = $this->config->getSystemValueString('overwrite.cli.url');
@@ -180,6 +171,32 @@ protected function preventLocalAddress(string $uri, array $options): void {
180171
}
181172
}
182173

174+
private function addRedirectHostCheck(array $options): array {
175+
$allowRedirects = array_key_exists(RequestOptions::ALLOW_REDIRECTS, $options)
176+
? $options[RequestOptions::ALLOW_REDIRECTS]
177+
: true;
178+
if ($allowRedirects === true) {
179+
$allowRedirects = [];
180+
} elseif (!is_array($allowRedirects) || $allowRedirects === []) {
181+
return $options;
182+
}
183+
184+
$callerOnRedirect = $allowRedirects['on_redirect'] ?? null;
185+
$allowRedirects['on_redirect'] = function (
186+
\Psr\Http\Message\RequestInterface $request,
187+
\Psr\Http\Message\ResponseInterface $response,
188+
\Psr\Http\Message\UriInterface $uri,
189+
) use ($options, $callerOnRedirect): void {
190+
$this->preventLocalAddress($uri->__toString(), $options);
191+
if ($callerOnRedirect !== null) {
192+
$callerOnRedirect($request, $response, $uri);
193+
}
194+
};
195+
196+
$options[RequestOptions::ALLOW_REDIRECTS] = $allowRedirects;
197+
return $options;
198+
}
199+
183200
/**
184201
* Sends a GET request
185202
*

‎tests/lib/Http/Client/ClientTest.php‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
namespace Test\Http\Client;
1212

13+
use GuzzleHttp\Psr7\Request;
1314
use GuzzleHttp\Psr7\Response;
15+
use GuzzleHttp\Psr7\Uri;
1416
use OC\Http\Client\Client;
1517
use OCP\Http\Client\LocalServerException;
1618
use OCP\ICertificateManager;
@@ -677,4 +679,52 @@ public function testSetServerUrlInUserAgent(string $url, string $userAgent): voi
677679
],
678680
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
679681
}
682+
683+
public static function dataAllowRedirectsKeepsRedirectCheck(): array {
684+
return [
685+
[['max' => 1], ['max' => 1]],
686+
[true, []],
687+
];
688+
}
689+
690+
#[DataProvider('dataAllowRedirectsKeepsRedirectCheck')]
691+
public function testAllowRedirectsKeepsRedirectCheck(array|bool $allowRedirects, array $expected): void {
692+
$options = self::invokePrivate($this->client, 'buildRequestOptions', [['allow_redirects' => $allowRedirects]]);
693+
$onRedirect = $options['allow_redirects']['on_redirect'];
694+
unset($options['allow_redirects']['on_redirect']);
695+
$this->assertSame($expected, $options['allow_redirects']);
696+
697+
$this->expectException(LocalServerException::class);
698+
$onRedirect(new Request('GET', 'https://example.com'), new Response(302), new Uri('http://127.0.0.1/'));
699+
}
700+
701+
public function testAllowRedirectsCallsCallerOnRedirect(): void {
702+
$this->getAutoMock(IRemoteHostValidator::class)->method('isValid')->willReturn(true);
703+
$called = false;
704+
$options = self::invokePrivate($this->client, 'buildRequestOptions', [['allow_redirects' => [
705+
'on_redirect' => function () use (&$called): void {
706+
$called = true;
707+
},
708+
]]]);
709+
710+
$options['allow_redirects']['on_redirect'](new Request('GET', 'https://example.com'), new Response(302), new Uri('https://example.org/'));
711+
$this->assertTrue($called);
712+
}
713+
714+
public static function dataAllowRedirectsUnchanged(): array {
715+
return [
716+
[false],
717+
[null],
718+
[[]],
719+
[0],
720+
[''],
721+
['yes'],
722+
];
723+
}
724+
725+
#[DataProvider('dataAllowRedirectsUnchanged')]
726+
public function testAllowRedirectsUnchanged(mixed $allowRedirects): void {
727+
$options = self::invokePrivate($this->client, 'buildRequestOptions', [['allow_redirects' => $allowRedirects]]);
728+
$this->assertSame($allowRedirects, $options['allow_redirects']);
729+
}
680730
}

0 commit comments

Comments
 (0)