From cdfffe7bda61847f39cf956e00b69d657570e69a Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 16 Jan 2018 20:01:38 +0100 Subject: [PATCH 1/2] Strict DiscoveryService * Made strict * Type hints * Return types Signed-off-by: Roeland Jago Douma --- lib/private/OCS/DiscoveryService.php | 19 ++++++++++--------- lib/public/OCS/IDiscoveryService.php | 3 ++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/private/OCS/DiscoveryService.php b/lib/private/OCS/DiscoveryService.php index 4425947c55..53b4d13e79 100644 --- a/lib/private/OCS/DiscoveryService.php +++ b/lib/private/OCS/DiscoveryService.php @@ -1,4 +1,5 @@ * @@ -60,7 +61,7 @@ class DiscoveryService implements IDiscoveryService { * @param string $service the service you want to discover * @return array */ - public function discover($remote, $service) { + public function discover(string $remote, string $service): array { // Check the cache first $cacheData = $this->cache->get($remote . '#' . $service); if($cacheData) { @@ -77,7 +78,9 @@ class DiscoveryService implements IDiscoveryService { ]); if($response->getStatusCode() === Http::STATUS_OK) { $decodedServices = json_decode($response->getBody(), true); - $discoveredServices = $this->getEndpoints($decodedServices, $service); + if (\is_array($decodedServices)) { + $discoveredServices = $this->getEndpoints($decodedServices, $service); + } } } catch (\Exception $e) { // if we couldn't discover the service or any end-points we return a empty array @@ -91,17 +94,15 @@ class DiscoveryService implements IDiscoveryService { /** * get requested end-points from the requested service * - * @param $decodedServices - * @param $service + * @param array $decodedServices + * @param string $service * @return array */ - protected function getEndpoints($decodedServices, $service) { + protected function getEndpoints(array $decodedServices, string $service): array { $discoveredServices = []; - if(is_array($decodedServices) && - isset($decodedServices['services'][$service]['endpoints']) - ) { + if(isset($decodedServices['services'][$service]['endpoints'])) { foreach ($decodedServices['services'][$service]['endpoints'] as $endpoint => $url) { if($this->isSafeUrl($url)) { $discoveredServices[$endpoint] = $url; @@ -119,7 +120,7 @@ class DiscoveryService implements IDiscoveryService { * @param string $url * @return bool */ - protected function isSafeUrl($url) { + protected function isSafeUrl(string $url): bool { return (bool)preg_match('/^[\/\.\-A-Za-z0-9]+$/', $url); } diff --git a/lib/public/OCS/IDiscoveryService.php b/lib/public/OCS/IDiscoveryService.php index c9e67c3aca..9a86e2a441 100644 --- a/lib/public/OCS/IDiscoveryService.php +++ b/lib/public/OCS/IDiscoveryService.php @@ -1,4 +1,5 @@ * @@ -45,6 +46,6 @@ interface IDiscoveryService { * @param string $service the service you want to discover * @return array */ - public function discover($remote, $service); + public function discover(string $remote, string $service): array; } From 569b8413d46e7f4132793f4bc34485233b2cf60f Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 16 Jan 2018 22:06:57 +0100 Subject: [PATCH 2/2] Add extra check so we don't error out on type json_decode can return false if we have invalid data. In that case just assume there was nothing cached Signed-off-by: Roeland Jago Douma --- lib/private/OCS/DiscoveryService.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/private/OCS/DiscoveryService.php b/lib/private/OCS/DiscoveryService.php index 53b4d13e79..016331e908 100644 --- a/lib/private/OCS/DiscoveryService.php +++ b/lib/private/OCS/DiscoveryService.php @@ -65,7 +65,10 @@ class DiscoveryService implements IDiscoveryService { // Check the cache first $cacheData = $this->cache->get($remote . '#' . $service); if($cacheData) { - return json_decode($cacheData, true); + $data = json_decode($cacheData, true); + if (\is_array($data)) { + return $data; + } } $discoveredServices = [];