@@ -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 *
0 commit comments