← Back
Editing: SecureCurlHttpClient.php
<?php namespace GoDaddy\Auth; /** * Secure CURL HTTP client that enforces same-origin redirect policy */ class SecureCurlHttpClient implements HttpClientInterface { private const MAX_RETRIES = 3; private const MAX_REDIRECTS = 3; private $agent = 'PHP Curl'; public function get(string $url): string { return $this->send($url); } public function post(string $url, array $data): string { return $this->send($url, $data); } public function setAgent(string $agent) { $this->agent = $agent; } /** * Checks if two URLs have the same origin (protocol, host, and port). */ private function sameOrigin(string $url1, string $url2): bool { $parsed1 = parse_url($url1); $parsed2 = parse_url($url2); if (!$parsed1 || !$parsed2) { return false; } $scheme1 = strtolower($parsed1['scheme'] ?? 'https'); $scheme2 = strtolower($parsed2['scheme'] ?? 'https'); $host1 = strtolower($parsed1['host'] ?? ''); $host2 = strtolower($parsed2['host'] ?? ''); $port1 = $parsed1['port'] ?? ($scheme1 === 'https' ? 443 : 80); $port2 = $parsed2['port'] ?? ($scheme2 === 'https' ? 443 : 80); return $scheme1 === $scheme2 && $host1 === $host2 && $port1 === $port2; } /** * Sends a GET or POST request with same-origin redirect protection. * * @param string $url * @param array|null $postData Array to POST, null to GET. * * @return string * @throws AuthException When a redirect is refused (cross-origin, missing Location, or too many redirects). * @throws \Exception On curl initialization/transport failure or an unexpected HTTP response. */ protected function send($url, ?array $postData = null): string { if (!function_exists('curl_init') || !function_exists('curl_exec')) { throw new \RuntimeException('The PHP curl extension must be enabled'); } $originalUrl = $url; $redirectCount = 0; $retries = 0; retry: error_clear_last(); $ch = @curl_init($url); if ($ch === false) { $error = error_get_last(); throw new \Exception(sprintf('curl init error: %s', $error['message'] ?? 'unknown error')); } if ($postData !== null) { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData, '', '&')); } // Set options with redirect following disabled for manual control curl_setopt_array($ch, [ CURLOPT_USERAGENT => $this->agent, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, CURLOPT_FOLLOWLOCATION => false, // Disable automatic redirect following CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HEADER => true, // Include headers in response for redirect handling ]); error_clear_last(); $response = @curl_exec($ch); $errorCode = @curl_errno($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); if ($response === false) { if ($retries < self::MAX_RETRIES) { $retries++; usleep(100000 * pow($retries, 2)); curl_close($ch); goto retry; } $curlError = curl_error($ch) ?: 'unknown error'; curl_close($ch); throw new \Exception(sprintf('curl exec error: %s', $curlError)); } // Extract headers and body $headers = substr($response, 0, $headerSize); $body = substr($response, $headerSize); curl_close($ch); // Handle redirects with same-origin policy if ($httpCode >= 300 && $httpCode < 400) { if ($redirectCount >= self::MAX_REDIRECTS) { throw new AuthException('Refused public key redirect - too many redirects'); } // Extract Location header preg_match('/Location:\s*([^\r\n]+)/i', $headers, $matches); if (empty($matches[1])) { throw new AuthException('Redirect response without Location header'); } $redirectUrl = trim($matches[1]); // Handle relative URLs if (!preg_match('/^https?:\/\//i', $redirectUrl)) { $parsedBase = parse_url($url); if ($redirectUrl[0] === '/') { // Absolute path $redirectUrl = $parsedBase['scheme'] . '://' . $parsedBase['host'] . (isset($parsedBase['port']) ? ':' . $parsedBase['port'] : '') . $redirectUrl; } else { // Relative path $basePath = dirname($parsedBase['path'] ?? '/'); if ($basePath === '.') $basePath = ''; $redirectUrl = $parsedBase['scheme'] . '://' . $parsedBase['host'] . (isset($parsedBase['port']) ? ':' . $parsedBase['port'] : '') . $basePath . '/' . $redirectUrl; } } // Check same-origin policy if (!$this->sameOrigin($originalUrl, $redirectUrl)) { throw new AuthException(sprintf( 'Refused cross-origin public key redirect from %s to %s', $originalUrl, $redirectUrl )); } $redirectCount++; $url = $redirectUrl; goto retry; } // Handle errors and retries if (($httpCode >= 500 || $errorCode > 0) && $retries < self::MAX_RETRIES) { $retries++; usleep(100000 * pow($retries, 2)); goto retry; } if ($httpCode >= 500 || $httpCode < 200 || $errorCode !== 0) { // Note: $body is deliberately omitted from the exception. // Recommend adding server-side logging if $body is needed for debugging. throw new \Exception(sprintf( 'unexpected response code %1$d (curl errno %2$d)', $httpCode, $errorCode )); } return $body; } }
Save File
Cancel