From 51753e403fa69c0ce124ede27d300477e3e799ca Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Mon, 3 Dec 2018 00:34:53 +0100 Subject: namespacing: move HTTP utilities along \Shaarli\Http\ classes Signed-off-by: VirtualTam --- application/HttpUtils.php | 479 ---------------------- application/Url.php | 88 ---- application/http/HttpUtils.php | 479 ++++++++++++++++++++++ application/http/Url.php | 2 +- application/http/UrlUtils.php | 88 ++++ index.php | 4 +- tests/HttpUtils/ClientIpIdTest.php | 52 --- tests/HttpUtils/GetHttpUrlTest.php | 65 --- tests/HttpUtils/GetIpAdressFromProxyTest.php | 59 --- tests/HttpUtils/IndexUrlTest.php | 72 ---- tests/HttpUtils/IsHttpsTest.php | 36 -- tests/HttpUtils/PageUrlTest.php | 76 ---- tests/HttpUtils/ServerUrlTest.php | 221 ---------- tests/Url/CleanupUrlTest.php | 109 ----- tests/Url/GetUrlSchemeTest.php | 30 -- tests/Url/UnparseUrlTest.php | 30 -- tests/Url/WhitelistProtocolsTest.php | 63 --- tests/http/HttpUtils/ClientIpIdTest.php | 54 +++ tests/http/HttpUtils/GetHttpUrlTest.php | 67 +++ tests/http/HttpUtils/GetIpAdressFromProxyTest.php | 61 +++ tests/http/HttpUtils/IndexUrlTest.php | 74 ++++ tests/http/HttpUtils/IsHttpsTest.php | 39 ++ tests/http/HttpUtils/PageUrlTest.php | 78 ++++ tests/http/HttpUtils/ServerUrlTest.php | 223 ++++++++++ tests/http/UrlTest.php | 2 +- tests/http/UrlUtils/CleanupUrlTest.php | 111 +++++ tests/http/UrlUtils/GetUrlSchemeTest.php | 32 ++ tests/http/UrlUtils/UnparseUrlTest.php | 32 ++ tests/http/UrlUtils/WhitelistProtocolsTest.php | 63 +++ 29 files changed, 1405 insertions(+), 1384 deletions(-) delete mode 100644 application/HttpUtils.php delete mode 100644 application/Url.php create mode 100644 application/http/HttpUtils.php create mode 100644 application/http/UrlUtils.php delete mode 100644 tests/HttpUtils/ClientIpIdTest.php delete mode 100644 tests/HttpUtils/GetHttpUrlTest.php delete mode 100644 tests/HttpUtils/GetIpAdressFromProxyTest.php delete mode 100644 tests/HttpUtils/IndexUrlTest.php delete mode 100644 tests/HttpUtils/IsHttpsTest.php delete mode 100644 tests/HttpUtils/PageUrlTest.php delete mode 100644 tests/HttpUtils/ServerUrlTest.php delete mode 100644 tests/Url/CleanupUrlTest.php delete mode 100644 tests/Url/GetUrlSchemeTest.php delete mode 100644 tests/Url/UnparseUrlTest.php delete mode 100644 tests/Url/WhitelistProtocolsTest.php create mode 100644 tests/http/HttpUtils/ClientIpIdTest.php create mode 100644 tests/http/HttpUtils/GetHttpUrlTest.php create mode 100644 tests/http/HttpUtils/GetIpAdressFromProxyTest.php create mode 100644 tests/http/HttpUtils/IndexUrlTest.php create mode 100644 tests/http/HttpUtils/IsHttpsTest.php create mode 100644 tests/http/HttpUtils/PageUrlTest.php create mode 100644 tests/http/HttpUtils/ServerUrlTest.php create mode 100644 tests/http/UrlUtils/CleanupUrlTest.php create mode 100644 tests/http/UrlUtils/GetUrlSchemeTest.php create mode 100644 tests/http/UrlUtils/UnparseUrlTest.php create mode 100644 tests/http/UrlUtils/WhitelistProtocolsTest.php diff --git a/application/HttpUtils.php b/application/HttpUtils.php deleted file mode 100644 index 51af5d0d..00000000 --- a/application/HttpUtils.php +++ /dev/null @@ -1,479 +0,0 @@ -idnToAscii(); - - if (!filter_var($cleanUrl, FILTER_VALIDATE_URL) || !$urlObj->isHttp()) { - return array(array(0 => 'Invalid HTTP Url'), false); - } - - $userAgent = - 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:45.0)' - . ' Gecko/20100101 Firefox/45.0'; - $acceptLanguage = - substr(setlocale(LC_COLLATE, 0), 0, 2) . ',en-US;q=0.7,en;q=0.3'; - $maxRedirs = 3; - - if (!function_exists('curl_init')) { - return get_http_response_fallback( - $cleanUrl, - $timeout, - $maxBytes, - $userAgent, - $acceptLanguage, - $maxRedirs - ); - } - - $ch = curl_init($cleanUrl); - if ($ch === false) { - return array(array(0 => 'curl_init() error'), false); - } - - // General cURL settings - curl_setopt($ch, CURLOPT_AUTOREFERER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt( - $ch, - CURLOPT_HTTPHEADER, - array('Accept-Language: ' . $acceptLanguage) - ); - curl_setopt($ch, CURLOPT_MAXREDIRS, $maxRedirs); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); - curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); - - if (is_callable($curlWriteFunction)) { - curl_setopt($ch, CURLOPT_WRITEFUNCTION, $curlWriteFunction); - } - - // Max download size management - curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024*16); - curl_setopt($ch, CURLOPT_NOPROGRESS, false); - curl_setopt( - $ch, - CURLOPT_PROGRESSFUNCTION, - function ($arg0, $arg1, $arg2, $arg3, $arg4 = 0) use ($maxBytes) { - if (version_compare(phpversion(), '5.5', '<')) { - // PHP version lower than 5.5 - // Callback has 4 arguments - $downloaded = $arg1; - } else { - // Callback has 5 arguments - $downloaded = $arg2; - } - // Non-zero return stops downloading - return ($downloaded > $maxBytes) ? 1 : 0; - } - ); - - $response = curl_exec($ch); - $errorNo = curl_errno($ch); - $errorStr = curl_error($ch); - $headSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - curl_close($ch); - - if ($response === false) { - if ($errorNo == CURLE_COULDNT_RESOLVE_HOST) { - /* - * Workaround to match fallback method behaviour - * Removing this would require updating - * GetHttpUrlTest::testGetInvalidRemoteUrl() - */ - return array(false, false); - } - return array(array(0 => 'curl_exec() error: ' . $errorStr), false); - } - - // Formatting output like the fallback method - $rawHeaders = substr($response, 0, $headSize); - - // Keep only headers from latest redirection - $rawHeadersArrayRedirs = explode("\r\n\r\n", trim($rawHeaders)); - $rawHeadersLastRedir = end($rawHeadersArrayRedirs); - - $content = substr($response, $headSize); - $headers = array(); - foreach (preg_split('~[\r\n]+~', $rawHeadersLastRedir) as $line) { - if (empty($line) || ctype_space($line)) { - continue; - } - $splitLine = explode(': ', $line, 2); - if (count($splitLine) > 1) { - $key = $splitLine[0]; - $value = $splitLine[1]; - if (array_key_exists($key, $headers)) { - if (!is_array($headers[$key])) { - $headers[$key] = array(0 => $headers[$key]); - } - $headers[$key][] = $value; - } else { - $headers[$key] = $value; - } - } else { - $headers[] = $splitLine[0]; - } - } - - return array($headers, $content); -} - -/** - * GET an HTTP URL to retrieve its content (fallback method) - * - * @param string $cleanUrl URL to get (http://... valid and in ASCII form) - * @param int $timeout network timeout (in seconds) - * @param int $maxBytes maximum downloaded bytes - * @param string $userAgent "User-Agent" header - * @param string $acceptLanguage "Accept-Language" header - * @param int $maxRedr maximum amount of redirections followed - * - * @return array HTTP response headers, downloaded content - * - * Output format: - * [0] = associative array containing HTTP response headers - * [1] = URL content (downloaded data) - * - * @see http://php.net/manual/en/function.file-get-contents.php - * @see http://php.net/manual/en/function.stream-context-create.php - * @see http://php.net/manual/en/function.get-headers.php - */ -function get_http_response_fallback( - $cleanUrl, - $timeout, - $maxBytes, - $userAgent, - $acceptLanguage, - $maxRedr -) { - $options = array( - 'http' => array( - 'method' => 'GET', - 'timeout' => $timeout, - 'user_agent' => $userAgent, - 'header' => "Accept: */*\r\n" - . 'Accept-Language: ' . $acceptLanguage - ) - ); - - stream_context_set_default($options); - list($headers, $finalUrl) = get_redirected_headers($cleanUrl, $maxRedr); - if (! $headers || strpos($headers[0], '200 OK') === false) { - $options['http']['request_fulluri'] = true; - stream_context_set_default($options); - list($headers, $finalUrl) = get_redirected_headers($cleanUrl, $maxRedr); - } - - if (! $headers) { - return array($headers, false); - } - - try { - // TODO: catch Exception in calling code (thumbnailer) - $context = stream_context_create($options); - $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes); - } catch (Exception $exc) { - return array(array(0 => 'HTTP Error'), $exc->getMessage()); - } - - return array($headers, $content); -} - -/** - * Retrieve HTTP headers, following n redirections (temporary and permanent ones). - * - * @param string $url initial URL to reach. - * @param int $redirectionLimit max redirection follow. - * - * @return array HTTP headers, or false if it failed. - */ -function get_redirected_headers($url, $redirectionLimit = 3) -{ - $headers = get_headers($url, 1); - if (!empty($headers['location']) && empty($headers['Location'])) { - $headers['Location'] = $headers['location']; - } - - // Headers found, redirection found, and limit not reached. - if ($redirectionLimit-- > 0 - && !empty($headers) - && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false) - && !empty($headers['Location'])) { - $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location']; - if ($redirection != $url) { - $redirection = getAbsoluteUrl($url, $redirection); - return get_redirected_headers($redirection, $redirectionLimit); - } - } - - return array($headers, $url); -} - -/** - * Get an absolute URL from a complete one, and another absolute/relative URL. - * - * @param string $originalUrl The original complete URL. - * @param string $newUrl The new one, absolute or relative. - * - * @return string Final URL: - * - $newUrl if it was already an absolute URL. - * - if it was relative, absolute URL from $originalUrl path. - */ -function getAbsoluteUrl($originalUrl, $newUrl) -{ - $newScheme = parse_url($newUrl, PHP_URL_SCHEME); - // Already an absolute URL. - if (!empty($newScheme)) { - return $newUrl; - } - - $parts = parse_url($originalUrl); - $final = $parts['scheme'] .'://'. $parts['host']; - $final .= (!empty($parts['port'])) ? $parts['port'] : ''; - $final .= '/'; - if ($newUrl[0] != '/') { - $final .= substr(ltrim($parts['path'], '/'), 0, strrpos($parts['path'], '/')); - } - $final .= ltrim($newUrl, '/'); - return $final; -} - -/** - * Returns the server's base URL: scheme://domain.tld[:port] - * - * @param array $server the $_SERVER array - * - * @return string the server's base URL - * - * @see http://www.ietf.org/rfc/rfc7239.txt - * @see http://www.ietf.org/rfc/rfc6648.txt - * @see http://stackoverflow.com/a/3561399 - * @see http://stackoverflow.com/q/452375 - */ -function server_url($server) -{ - $scheme = 'http'; - $port = ''; - - // Shaarli is served behind a proxy - if (isset($server['HTTP_X_FORWARDED_PROTO'])) { - // Keep forwarded scheme - if (strpos($server['HTTP_X_FORWARDED_PROTO'], ',') !== false) { - $schemes = explode(',', $server['HTTP_X_FORWARDED_PROTO']); - $scheme = trim($schemes[0]); - } else { - $scheme = $server['HTTP_X_FORWARDED_PROTO']; - } - - if (isset($server['HTTP_X_FORWARDED_PORT'])) { - // Keep forwarded port - if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) { - $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']); - $port = trim($ports[0]); - } else { - $port = $server['HTTP_X_FORWARDED_PORT']; - } - - // This is a workaround for proxies that don't forward the scheme properly. - // Connecting over port 443 has to be in HTTPS. - // See https://github.com/shaarli/Shaarli/issues/1022 - if ($port == '443') { - $scheme = 'https'; - } - - if (($scheme == 'http' && $port != '80') - || ($scheme == 'https' && $port != '443') - ) { - $port = ':' . $port; - } else { - $port = ''; - } - } - - if (isset($server['HTTP_X_FORWARDED_HOST'])) { - // Keep forwarded host - if (strpos($server['HTTP_X_FORWARDED_HOST'], ',') !== false) { - $hosts = explode(',', $server['HTTP_X_FORWARDED_HOST']); - $host = trim($hosts[0]); - } else { - $host = $server['HTTP_X_FORWARDED_HOST']; - } - } else { - $host = $server['SERVER_NAME']; - } - - return $scheme.'://'.$host.$port; - } - - // SSL detection - if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on') - || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) { - $scheme = 'https'; - } - - // Do not append standard port values - if (($scheme == 'http' && $server['SERVER_PORT'] != '80') - || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) { - $port = ':'.$server['SERVER_PORT']; - } - - return $scheme.'://'.$server['SERVER_NAME'].$port; -} - -/** - * Returns the absolute URL of the current script, without the query - * - * If the resource is "index.php", then it is removed (for better-looking URLs) - * - * @param array $server the $_SERVER array - * - * @return string the absolute URL of the current script, without the query - */ -function index_url($server) -{ - $scriptname = $server['SCRIPT_NAME']; - if (endsWith($scriptname, 'index.php')) { - $scriptname = substr($scriptname, 0, -9); - } - return server_url($server) . $scriptname; -} - -/** - * Returns the absolute URL of the current script, with the query - * - * If the resource is "index.php", then it is removed (for better-looking URLs) - * - * @param array $server the $_SERVER array - * - * @return string the absolute URL of the current script, with the query - */ -function page_url($server) -{ - if (! empty($server['QUERY_STRING'])) { - return index_url($server).'?'.$server['QUERY_STRING']; - } - return index_url($server); -} - -/** - * Retrieve the initial IP forwarded by the reverse proxy. - * - * Inspired from: https://github.com/zendframework/zend-http/blob/master/src/PhpEnvironment/RemoteAddress.php - * - * @param array $server $_SERVER array which contains HTTP headers. - * @param array $trustedIps List of trusted IP from the configuration. - * - * @return string|bool The forwarded IP, or false if none could be extracted. - */ -function getIpAddressFromProxy($server, $trustedIps) -{ - $forwardedIpHeader = 'HTTP_X_FORWARDED_FOR'; - if (empty($server[$forwardedIpHeader])) { - return false; - } - - $ips = preg_split('/\s*,\s*/', $server[$forwardedIpHeader]); - $ips = array_diff($ips, $trustedIps); - if (empty($ips)) { - return false; - } - - return array_pop($ips); -} - - -/** - * Return an identifier based on the advertised client IP address(es) - * - * This aims at preventing session hijacking from users behind the same proxy - * by relying on HTTP headers. - * - * See: - * - https://secure.php.net/manual/en/reserved.variables.server.php - * - https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php - * - https://stackoverflow.com/questions/12233406/preventing-session-hijacking - * - https://stackoverflow.com/questions/21354859/trusting-x-forwarded-for-to-identify-a-visitor - * - * @param array $server The $_SERVER array - * - * @return string An identifier based on client IP address information - */ -function client_ip_id($server) -{ - $ip = $server['REMOTE_ADDR']; - - if (isset($server['HTTP_X_FORWARDED_FOR'])) { - $ip = $ip . '_' . $server['HTTP_X_FORWARDED_FOR']; - } - if (isset($server['HTTP_CLIENT_IP'])) { - $ip = $ip . '_' . $server['HTTP_CLIENT_IP']; - } - return $ip; -} - - -/** - * Returns true if Shaarli's currently browsed in HTTPS. - * Supports reverse proxies (if the headers are correctly set). - * - * @param array $server $_SERVER. - * - * @return bool true if HTTPS, false otherwise. - */ -function is_https($server) -{ - - if (isset($server['HTTP_X_FORWARDED_PORT'])) { - // Keep forwarded port - if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) { - $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']); - $port = trim($ports[0]); - } else { - $port = $server['HTTP_X_FORWARDED_PORT']; - } - - if ($port == '443') { - return true; - } - } - - return ! empty($server['HTTPS']); -} diff --git a/application/Url.php b/application/Url.php deleted file mode 100644 index 81f72fb0..00000000 --- a/application/Url.php +++ /dev/null @@ -1,88 +0,0 @@ -cleanup(); -} - -/** - * Get URL scheme. - * - * @param string url Url for which the scheme is requested - * - * @return mixed the URL scheme or false if none is provided. - */ -function get_url_scheme($url) -{ - $obj_url = new \Shaarli\Http\Url($url); - return $obj_url->getScheme(); -} - -/** - * Adds a trailing slash at the end of URL if necessary. - * - * @param string $url URL to check/edit. - * - * @return string $url URL with a end trailing slash. - */ -function add_trailing_slash($url) -{ - return $url . (!endsWith($url, '/') ? '/' : ''); -} - -/** - * Replace not whitelisted protocols by 'http://' from given URL. - * - * @param string $url URL to clean - * @param array $protocols List of allowed protocols (aside from http(s)). - * - * @return string URL with allowed protocol - */ -function whitelist_protocols($url, $protocols) -{ - if (startsWith($url, '?') || startsWith($url, '/')) { - return $url; - } - $protocols = array_merge(['http', 'https'], $protocols); - $protocol = preg_match('#^(\w+):/?/?#', $url, $match); - // Protocol not allowed: we remove it and replace it with http - if ($protocol === 1 && ! in_array($match[1], $protocols)) { - $url = str_replace($match[0], 'http://', $url); - } elseif ($protocol !== 1) { - $url = 'http://' . $url; - } - return $url; -} diff --git a/application/http/HttpUtils.php b/application/http/HttpUtils.php new file mode 100644 index 00000000..2ea9195d --- /dev/null +++ b/application/http/HttpUtils.php @@ -0,0 +1,479 @@ +idnToAscii(); + + if (!filter_var($cleanUrl, FILTER_VALIDATE_URL) || !$urlObj->isHttp()) { + return array(array(0 => 'Invalid HTTP UrlUtils'), false); + } + + $userAgent = + 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:45.0)' + . ' Gecko/20100101 Firefox/45.0'; + $acceptLanguage = + substr(setlocale(LC_COLLATE, 0), 0, 2) . ',en-US;q=0.7,en;q=0.3'; + $maxRedirs = 3; + + if (!function_exists('curl_init')) { + return get_http_response_fallback( + $cleanUrl, + $timeout, + $maxBytes, + $userAgent, + $acceptLanguage, + $maxRedirs + ); + } + + $ch = curl_init($cleanUrl); + if ($ch === false) { + return array(array(0 => 'curl_init() error'), false); + } + + // General cURL settings + curl_setopt($ch, CURLOPT_AUTOREFERER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_HEADER, true); + curl_setopt( + $ch, + CURLOPT_HTTPHEADER, + array('Accept-Language: ' . $acceptLanguage) + ); + curl_setopt($ch, CURLOPT_MAXREDIRS, $maxRedirs); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); + + if (is_callable($curlWriteFunction)) { + curl_setopt($ch, CURLOPT_WRITEFUNCTION, $curlWriteFunction); + } + + // Max download size management + curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024*16); + curl_setopt($ch, CURLOPT_NOPROGRESS, false); + curl_setopt( + $ch, + CURLOPT_PROGRESSFUNCTION, + function ($arg0, $arg1, $arg2, $arg3, $arg4 = 0) use ($maxBytes) { + if (version_compare(phpversion(), '5.5', '<')) { + // PHP version lower than 5.5 + // Callback has 4 arguments + $downloaded = $arg1; + } else { + // Callback has 5 arguments + $downloaded = $arg2; + } + // Non-zero return stops downloading + return ($downloaded > $maxBytes) ? 1 : 0; + } + ); + + $response = curl_exec($ch); + $errorNo = curl_errno($ch); + $errorStr = curl_error($ch); + $headSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + curl_close($ch); + + if ($response === false) { + if ($errorNo == CURLE_COULDNT_RESOLVE_HOST) { + /* + * Workaround to match fallback method behaviour + * Removing this would require updating + * GetHttpUrlTest::testGetInvalidRemoteUrl() + */ + return array(false, false); + } + return array(array(0 => 'curl_exec() error: ' . $errorStr), false); + } + + // Formatting output like the fallback method + $rawHeaders = substr($response, 0, $headSize); + + // Keep only headers from latest redirection + $rawHeadersArrayRedirs = explode("\r\n\r\n", trim($rawHeaders)); + $rawHeadersLastRedir = end($rawHeadersArrayRedirs); + + $content = substr($response, $headSize); + $headers = array(); + foreach (preg_split('~[\r\n]+~', $rawHeadersLastRedir) as $line) { + if (empty($line) || ctype_space($line)) { + continue; + } + $splitLine = explode(': ', $line, 2); + if (count($splitLine) > 1) { + $key = $splitLine[0]; + $value = $splitLine[1]; + if (array_key_exists($key, $headers)) { + if (!is_array($headers[$key])) { + $headers[$key] = array(0 => $headers[$key]); + } + $headers[$key][] = $value; + } else { + $headers[$key] = $value; + } + } else { + $headers[] = $splitLine[0]; + } + } + + return array($headers, $content); +} + +/** + * GET an HTTP URL to retrieve its content (fallback method) + * + * @param string $cleanUrl URL to get (http://... valid and in ASCII form) + * @param int $timeout network timeout (in seconds) + * @param int $maxBytes maximum downloaded bytes + * @param string $userAgent "User-Agent" header + * @param string $acceptLanguage "Accept-Language" header + * @param int $maxRedr maximum amount of redirections followed + * + * @return array HTTP response headers, downloaded content + * + * Output format: + * [0] = associative array containing HTTP response headers + * [1] = URL content (downloaded data) + * + * @see http://php.net/manual/en/function.file-get-contents.php + * @see http://php.net/manual/en/function.stream-context-create.php + * @see http://php.net/manual/en/function.get-headers.php + */ +function get_http_response_fallback( + $cleanUrl, + $timeout, + $maxBytes, + $userAgent, + $acceptLanguage, + $maxRedr +) { + $options = array( + 'http' => array( + 'method' => 'GET', + 'timeout' => $timeout, + 'user_agent' => $userAgent, + 'header' => "Accept: */*\r\n" + . 'Accept-Language: ' . $acceptLanguage + ) + ); + + stream_context_set_default($options); + list($headers, $finalUrl) = get_redirected_headers($cleanUrl, $maxRedr); + if (! $headers || strpos($headers[0], '200 OK') === false) { + $options['http']['request_fulluri'] = true; + stream_context_set_default($options); + list($headers, $finalUrl) = get_redirected_headers($cleanUrl, $maxRedr); + } + + if (! $headers) { + return array($headers, false); + } + + try { + // TODO: catch Exception in calling code (thumbnailer) + $context = stream_context_create($options); + $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes); + } catch (Exception $exc) { + return array(array(0 => 'HTTP Error'), $exc->getMessage()); + } + + return array($headers, $content); +} + +/** + * Retrieve HTTP headers, following n redirections (temporary and permanent ones). + * + * @param string $url initial URL to reach. + * @param int $redirectionLimit max redirection follow. + * + * @return array HTTP headers, or false if it failed. + */ +function get_redirected_headers($url, $redirectionLimit = 3) +{ + $headers = get_headers($url, 1); + if (!empty($headers['location']) && empty($headers['Location'])) { + $headers['Location'] = $headers['location']; + } + + // Headers found, redirection found, and limit not reached. + if ($redirectionLimit-- > 0 + && !empty($headers) + && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false) + && !empty($headers['Location'])) { + $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location']; + if ($redirection != $url) { + $redirection = getAbsoluteUrl($url, $redirection); + return get_redirected_headers($redirection, $redirectionLimit); + } + } + + return array($headers, $url); +} + +/** + * Get an absolute URL from a complete one, and another absolute/relative URL. + * + * @param string $originalUrl The original complete URL. + * @param string $newUrl The new one, absolute or relative. + * + * @return string Final URL: + * - $newUrl if it was already an absolute URL. + * - if it was relative, absolute URL from $originalUrl path. + */ +function getAbsoluteUrl($originalUrl, $newUrl) +{ + $newScheme = parse_url($newUrl, PHP_URL_SCHEME); + // Already an absolute URL. + if (!empty($newScheme)) { + return $newUrl; + } + + $parts = parse_url($originalUrl); + $final = $parts['scheme'] .'://'. $parts['host']; + $final .= (!empty($parts['port'])) ? $parts['port'] : ''; + $final .= '/'; + if ($newUrl[0] != '/') { + $final .= substr(ltrim($parts['path'], '/'), 0, strrpos($parts['path'], '/')); + } + $final .= ltrim($newUrl, '/'); + return $final; +} + +/** + * Returns the server's base URL: scheme://domain.tld[:port] + * + * @param array $server the $_SERVER array + * + * @return string the server's base URL + * + * @see http://www.ietf.org/rfc/rfc7239.txt + * @see http://www.ietf.org/rfc/rfc6648.txt + * @see http://stackoverflow.com/a/3561399 + * @see http://stackoverflow.com/q/452375 + */ +function server_url($server) +{ + $scheme = 'http'; + $port = ''; + + // Shaarli is served behind a proxy + if (isset($server['HTTP_X_FORWARDED_PROTO'])) { + // Keep forwarded scheme + if (strpos($server['HTTP_X_FORWARDED_PROTO'], ',') !== false) { + $schemes = explode(',', $server['HTTP_X_FORWARDED_PROTO']); + $scheme = trim($schemes[0]); + } else { + $scheme = $server['HTTP_X_FORWARDED_PROTO']; + } + + if (isset($server['HTTP_X_FORWARDED_PORT'])) { + // Keep forwarded port + if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) { + $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']); + $port = trim($ports[0]); + } else { + $port = $server['HTTP_X_FORWARDED_PORT']; + } + + // This is a workaround for proxies that don't forward the scheme properly. + // Connecting over port 443 has to be in HTTPS. + // See https://github.com/shaarli/Shaarli/issues/1022 + if ($port == '443') { + $scheme = 'https'; + } + + if (($scheme == 'http' && $port != '80') + || ($scheme == 'https' && $port != '443') + ) { + $port = ':' . $port; + } else { + $port = ''; + } + } + + if (isset($server['HTTP_X_FORWARDED_HOST'])) { + // Keep forwarded host + if (strpos($server['HTTP_X_FORWARDED_HOST'], ',') !== false) { + $hosts = explode(',', $server['HTTP_X_FORWARDED_HOST']); + $host = trim($hosts[0]); + } else { + $host = $server['HTTP_X_FORWARDED_HOST']; + } + } else { + $host = $server['SERVER_NAME']; + } + + return $scheme.'://'.$host.$port; + } + + // SSL detection + if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on') + || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) { + $scheme = 'https'; + } + + // Do not append standard port values + if (($scheme == 'http' && $server['SERVER_PORT'] != '80') + || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) { + $port = ':'.$server['SERVER_PORT']; + } + + return $scheme.'://'.$server['SERVER_NAME'].$port; +} + +/** + * Returns the absolute URL of the current script, without the query + * + * If the resource is "index.php", then it is removed (for better-looking URLs) + * + * @param array $server the $_SERVER array + * + * @return string the absolute URL of the current script, without the query + */ +function index_url($server) +{ + $scriptname = $server['SCRIPT_NAME']; + if (endsWith($scriptname, 'index.php')) { + $scriptname = substr($scriptname, 0, -9); + } + return server_url($server) . $scriptname; +} + +/** + * Returns the absolute URL of the current script, with the query + * + * If the resource is "index.php", then it is removed (for better-looking URLs) + * + * @param array $server the $_SERVER array + * + * @return string the absolute URL of the current script, with the query + */ +function page_url($server) +{ + if (! empty($server['QUERY_STRING'])) { + return index_url($server).'?'.$server['QUERY_STRING']; + } + return index_url($server); +} + +/** + * Retrieve the initial IP forwarded by the reverse proxy. + * + * Inspired from: https://github.com/zendframework/zend-http/blob/master/src/PhpEnvironment/RemoteAddress.php + * + * @param array $server $_SERVER array which contains HTTP headers. + * @param array $trustedIps List of trusted IP from the configuration. + * + * @return string|bool The forwarded IP, or false if none could be extracted. + */ +function getIpAddressFromProxy($server, $trustedIps) +{ + $forwardedIpHeader = 'HTTP_X_FORWARDED_FOR'; + if (empty($server[$forwardedIpHeader])) { + return false; + } + + $ips = preg_split('/\s*,\s*/', $server[$forwardedIpHeader]); + $ips = array_diff($ips, $trustedIps); + if (empty($ips)) { + return false; + } + + return array_pop($ips); +} + + +/** + * Return an identifier based on the advertised client IP address(es) + * + * This aims at preventing session hijacking from users behind the same proxy + * by relying on HTTP headers. + * + * See: + * - https://secure.php.net/manual/en/reserved.variables.server.php + * - https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php + * - https://stackoverflow.com/questions/12233406/preventing-session-hijacking + * - https://stackoverflow.com/questions/21354859/trusting-x-forwarded-for-to-identify-a-visitor + * + * @param array $server The $_SERVER array + * + * @return string An identifier based on client IP address information + */ +function client_ip_id($server) +{ + $ip = $server['REMOTE_ADDR']; + + if (isset($server['HTTP_X_FORWARDED_FOR'])) { + $ip = $ip . '_' . $server['HTTP_X_FORWARDED_FOR']; + } + if (isset($server['HTTP_CLIENT_IP'])) { + $ip = $ip . '_' . $server['HTTP_CLIENT_IP']; + } + return $ip; +} + + +/** + * Returns true if Shaarli's currently browsed in HTTPS. + * Supports reverse proxies (if the headers are correctly set). + * + * @param array $server $_SERVER. + * + * @return bool true if HTTPS, false otherwise. + */ +function is_https($server) +{ + + if (isset($server['HTTP_X_FORWARDED_PORT'])) { + // Keep forwarded port + if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) { + $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']); + $port = trim($ports[0]); + } else { + $port = $server['HTTP_X_FORWARDED_PORT']; + } + + if ($port == '443') { + return true; + } + } + + return ! empty($server['HTTPS']); +} diff --git a/application/http/Url.php b/application/http/Url.php index 260231c6..90444a2f 100644 --- a/application/http/Url.php +++ b/application/http/Url.php @@ -206,7 +206,7 @@ class Url } /** - * Test if the Url is an HTTP one. + * Test if the UrlUtils is an HTTP one. * * @return true is HTTP, false otherwise. */ diff --git a/application/http/UrlUtils.php b/application/http/UrlUtils.php new file mode 100644 index 00000000..4bc84b82 --- /dev/null +++ b/application/http/UrlUtils.php @@ -0,0 +1,88 @@ +cleanup(); +} + +/** + * Get URL scheme. + * + * @param string url UrlUtils for which the scheme is requested + * + * @return mixed the URL scheme or false if none is provided. + */ +function get_url_scheme($url) +{ + $obj_url = new \Shaarli\Http\Url($url); + return $obj_url->getScheme(); +} + +/** + * Adds a trailing slash at the end of URL if necessary. + * + * @param string $url URL to check/edit. + * + * @return string $url URL with a end trailing slash. + */ +function add_trailing_slash($url) +{ + return $url . (!endsWith($url, '/') ? '/' : ''); +} + +/** + * Replace not whitelisted protocols by 'http://' from given URL. + * + * @param string $url URL to clean + * @param array $protocols List of allowed protocols (aside from http(s)). + * + * @return string URL with allowed protocol + */ +function whitelist_protocols($url, $protocols) +{ + if (startsWith($url, '?') || startsWith($url, '/')) { + return $url; + } + $protocols = array_merge(['http', 'https'], $protocols); + $protocol = preg_match('#^(\w+):/?/?#', $url, $match); + // Protocol not allowed: we remove it and replace it with http + if ($protocol === 1 && ! in_array($match[1], $protocols)) { + $url = str_replace($match[0], 'http://', $url); + } elseif ($protocol !== 1) { + $url = 'http://' . $url; + } + return $url; +} diff --git a/index.php b/index.php index 6d1ae3fc..66fe30f1 100644 --- a/index.php +++ b/index.php @@ -59,16 +59,16 @@ require_once __DIR__ . '/vendor/autoload.php'; require_once 'application/ApplicationUtils.php'; require_once 'application/config/ConfigPlugin.php'; require_once 'application/feed/Cache.php'; +require_once 'application/http/HttpUtils.php'; +require_once 'application/http/UrlUtils.php'; require_once 'application/FileUtils.php'; require_once 'application/History.php'; -require_once 'application/HttpUtils.php'; require_once 'application/LinkDB.php'; require_once 'application/LinkFilter.php'; require_once 'application/LinkUtils.php'; require_once 'application/NetscapeBookmarkUtils.php'; require_once 'application/PageBuilder.php'; require_once 'application/TimeZone.php'; -require_once 'application/Url.php'; require_once 'application/Utils.php'; require_once 'application/PluginManager.php'; require_once 'application/Router.php'; diff --git a/tests/HttpUtils/ClientIpIdTest.php b/tests/HttpUtils/ClientIpIdTest.php deleted file mode 100644 index c15ac5cc..00000000 --- a/tests/HttpUtils/ClientIpIdTest.php +++ /dev/null @@ -1,52 +0,0 @@ -assertEquals( - '10.1.167.42', - client_ip_id(['REMOTE_ADDR' => '10.1.167.42']) - ); - } - - /** - * Get a remote client ID based on its IP and proxy information (1) - */ - public function testClientIpIdRemoteForwarded() - { - $this->assertEquals( - '10.1.167.42_127.0.1.47', - client_ip_id([ - 'REMOTE_ADDR' => '10.1.167.42', - 'HTTP_X_FORWARDED_FOR' => '127.0.1.47' - ]) - ); - } - - /** - * Get a remote client ID based on its IP and proxy information (2) - */ - public function testClientIpIdRemoteForwardedClient() - { - $this->assertEquals( - '10.1.167.42_10.1.167.56_127.0.1.47', - client_ip_id([ - 'REMOTE_ADDR' => '10.1.167.42', - 'HTTP_X_FORWARDED_FOR' => '10.1.167.56', - 'HTTP_CLIENT_IP' => '127.0.1.47' - ]) - ); - } -} diff --git a/tests/HttpUtils/GetHttpUrlTest.php b/tests/HttpUtils/GetHttpUrlTest.php deleted file mode 100644 index ea53de5f..00000000 --- a/tests/HttpUtils/GetHttpUrlTest.php +++ /dev/null @@ -1,65 +0,0 @@ -assertEquals('Invalid HTTP Url', $headers[0]); - $this->assertFalse($content); - - // Non HTTP - list($headers, $content) = get_http_response('ftp://save.tld/mysave', 1); - $this->assertEquals('Invalid HTTP Url', $headers[0]); - $this->assertFalse($content); - } - - /** - * Get an invalid remote URL - */ - public function testGetInvalidRemoteUrl() - { - list($headers, $content) = @get_http_response('http://non.existent', 1); - $this->assertFalse($headers); - $this->assertFalse($content); - } - - /** - * Test getAbsoluteUrl with relative target URL. - */ - public function testGetAbsoluteUrlWithRelative() - { - $origin = 'http://non.existent/blabla/?test'; - $target = '/stuff.php'; - - $expected = 'http://non.existent/stuff.php'; - $this->assertEquals($expected, getAbsoluteUrl($origin, $target)); - - $target = 'stuff.php'; - $expected = 'http://non.existent/blabla/stuff.php'; - $this->assertEquals($expected, getAbsoluteUrl($origin, $target)); - } - - /** - * Test getAbsoluteUrl with absolute target URL. - */ - public function testGetAbsoluteUrlWithAbsolute() - { - $origin = 'http://non.existent/blabla/?test'; - $target = 'http://other.url/stuff.php'; - - $this->assertEquals($target, getAbsoluteUrl($origin, $target)); - } -} diff --git a/tests/HttpUtils/GetIpAdressFromProxyTest.php b/tests/HttpUtils/GetIpAdressFromProxyTest.php deleted file mode 100644 index 7af5bd9d..00000000 --- a/tests/HttpUtils/GetIpAdressFromProxyTest.php +++ /dev/null @@ -1,59 +0,0 @@ -assertFalse(getIpAddressFromProxy(array(), array())); - } - - /** - * Test with a single IP in proxy header. - */ - public function testWithOneForwardedIp() - { - $ip = '1.1.1.1'; - $server = array('HTTP_X_FORWARDED_FOR' => $ip); - $this->assertEquals($ip, getIpAddressFromProxy($server, array())); - } - - /** - * Test with a multiple IPs in proxy header. - */ - public function testWithMultipleForwardedIp() - { - $ip = '1.1.1.1'; - $ip2 = '2.2.2.2'; - - $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); - $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); - - $server = array('HTTP_X_FORWARDED_FOR' => $ip .' , '. $ip2); - $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); - } - - /** - * Test with a trusted IP address. - */ - public function testWithTrustedIp() - { - $ip = '1.1.1.1'; - $ip2 = '2.2.2.2'; - - $server = array('HTTP_X_FORWARDED_FOR' => $ip); - $this->assertFalse(getIpAddressFromProxy($server, array($ip))); - - $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); - $this->assertEquals($ip2, getIpAddressFromProxy($server, array($ip))); - $this->assertFalse(getIpAddressFromProxy($server, array($ip, $ip2))); - } -} diff --git a/tests/HttpUtils/IndexUrlTest.php b/tests/HttpUtils/IndexUrlTest.php deleted file mode 100644 index 337dcab0..00000000 --- a/tests/HttpUtils/IndexUrlTest.php +++ /dev/null @@ -1,72 +0,0 @@ -assertEquals( - 'http://host.tld/', - index_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/index.php' - ) - ) - ); - - $this->assertEquals( - 'http://host.tld/admin/', - index_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/admin/index.php' - ) - ) - ); - } - - /** - * The resource is != "index.php" - */ - public function testOtherResource() - { - $this->assertEquals( - 'http://host.tld/page.php', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/page.php' - ) - ) - ); - - $this->assertEquals( - 'http://host.tld/admin/page.php', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/admin/page.php' - ) - ) - ); - } -} diff --git a/tests/HttpUtils/IsHttpsTest.php b/tests/HttpUtils/IsHttpsTest.php deleted file mode 100644 index 097f2bcf..00000000 --- a/tests/HttpUtils/IsHttpsTest.php +++ /dev/null @@ -1,36 +0,0 @@ -assertTrue(is_https(['HTTPS' => true])); - $this->assertTrue(is_https(['HTTPS' => '1'])); - $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 443])); - $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443'])); - $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443,123,456,'])); - } - - /** - * Test is_https with HTTP values. - */ - public function testIsHttpsFalse() - { - $this->assertFalse(is_https([])); - $this->assertFalse(is_https(['HTTPS' => false])); - $this->assertFalse(is_https(['HTTPS' => '0'])); - $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 123])); - $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '123'])); - $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => ',123,456,'])); - } -} diff --git a/tests/HttpUtils/PageUrlTest.php b/tests/HttpUtils/PageUrlTest.php deleted file mode 100644 index 4dbbe9cf..00000000 --- a/tests/HttpUtils/PageUrlTest.php +++ /dev/null @@ -1,76 +0,0 @@ -assertEquals( - 'http://host.tld/?p1=v1&p2=v2', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/index.php', - 'QUERY_STRING' => 'p1=v1&p2=v2' - ) - ) - ); - - $this->assertEquals( - 'http://host.tld/admin/?action=edit_tag', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/admin/index.php', - 'QUERY_STRING' => 'action=edit_tag' - ) - ) - ); - } - - /** - * The resource is != "index.php" - */ - public function testOtherResource() - { - $this->assertEquals( - 'http://host.tld/page.php?p1=v1&p2=v2', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/page.php', - 'QUERY_STRING' => 'p1=v1&p2=v2' - ) - ) - ); - - $this->assertEquals( - 'http://host.tld/admin/page.php?action=edit_tag', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/admin/page.php', - 'QUERY_STRING' => 'action=edit_tag' - ) - ) - ); - } -} diff --git a/tests/HttpUtils/ServerUrlTest.php b/tests/HttpUtils/ServerUrlTest.php deleted file mode 100644 index 324b827a..00000000 --- a/tests/HttpUtils/ServerUrlTest.php +++ /dev/null @@ -1,221 +0,0 @@ -assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'ON', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '443' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld:8080', - server_url( - array( - 'HTTPS' => 'ON', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '8080' - ) - ) - ); - } - - /** - * Detect a Proxy that sets Forwarded-Host - */ - public function testHttpsProxyForwardedHost() - { - $this->assertEquals( - 'https://host.tld:8080', - server_url( - array( - 'HTTP_X_FORWARDED_PROTO' => 'https', - 'HTTP_X_FORWARDED_PORT' => '8080', - 'HTTP_X_FORWARDED_HOST' => 'host.tld' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld:4974', - server_url( - array( - 'HTTP_X_FORWARDED_PROTO' => 'https, https', - 'HTTP_X_FORWARDED_PORT' => '4974, 80', - 'HTTP_X_FORWARDED_HOST' => 'host.tld, example.com' - ) - ) - ); - } - - /** - * Detect a Proxy with SSL enabled - */ - public function testHttpsProxyForward() - { - $this->assertEquals( - 'https://host.tld:8080', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https', - 'HTTP_X_FORWARDED_PORT' => '8080' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https', - 'HTTP_X_FORWARDED_PORT' => '443' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld:4974', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https, https', - 'HTTP_X_FORWARDED_PORT' => '4974, 80' - ) - ) - ); - } - - /** - * Detect if the server uses a specific port (!= 80) - */ - public function testPort() - { - // HTTP - $this->assertEquals( - 'http://host.tld:8080', - server_url( - array( - 'HTTPS' => 'OFF', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '8080' - ) - ) - ); - - // HTTPS - $this->assertEquals( - 'https://host.tld:8080', - server_url( - array( - 'HTTPS' => 'ON', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '8080' - ) - ) - ); - } - - /** - * HTTP server on port 80 - */ - public function testStandardHttpPort() - { - $this->assertEquals( - 'http://host.tld', - server_url( - array( - 'HTTPS' => 'OFF', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80' - ) - ) - ); - } - - /** - * HTTPS server on port 443 - */ - public function testStandardHttpsPort() - { - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'ON', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '443' - ) - ) - ); - } - - /** - * Misconfigured server (see #1022): Proxy HTTP but 443 - */ - public function testHttpWithPort433() - { - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'http', - 'HTTP_X_FORWARDED_PORT' => '443' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https, http', - 'HTTP_X_FORWARDED_PORT' => '443, 80' - ) - ) - ); - } -} diff --git a/tests/Url/CleanupUrlTest.php b/tests/Url/CleanupUrlTest.php deleted file mode 100644 index 24791948..00000000 --- a/tests/Url/CleanupUrlTest.php +++ /dev/null @@ -1,109 +0,0 @@ -assertEquals('', cleanup_url('')); - } - - /** - * Clean an already cleaned URL - */ - public function testCleanupUrlAlreadyClean() - { - $this->assertEquals($this->ref, cleanup_url($this->ref)); - $this->ref2 = $this->ref.'/path/to/dir/'; - $this->assertEquals($this->ref2, cleanup_url($this->ref2)); - } - - /** - * Clean URL fragments - */ - public function testCleanupUrlFragment() - { - $this->assertEquals($this->ref, cleanup_url($this->ref.'#tk.rss_all')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-U3ht0tkc4b')); - } - - /** - * Clean URL query - single annoying parameter - */ - public function testCleanupUrlQuerySingle() - { - $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_object_map=junk')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_ref_map=Cr4p!')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_type_map=g4R84g3')); - - $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb_stuff=v41u3')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb=71m3w4573')); - - $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_campaign=zomg')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_medium=numnum')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_source=c0d3')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_term=1n4l')); - - $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url')); - - $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_name=junk')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_start=junk')); - $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_item_index=junk')); - } - - /** - * Clean URL query - multiple annoying parameters - */ - public function testCleanupUrlQueryMultiple() - { - $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url&fb=som3th1ng')); - - $this->assertEquals($this->ref, cleanup_url( - $this->ref.'?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3' - )); - - $this->assertEquals($this->ref, cleanup_url( - $this->ref.'?campaign_start=zomg&campaign_name=numnum' - )); - } - - /** - * Clean URL query - multiple annoying parameters and fragment - */ - public function testCleanupUrlQueryFragment() - { - $this->assertEquals($this->ref, cleanup_url( - $this->ref.'?xtor=some-url&fb=som3th1ng#tk.rss_all' - )); - - // ditch annoying query params and fragment, keep useful params - $this->assertEquals( - $this->ref.'?my=stuff&is=kept', - cleanup_url( - $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all' - ) - ); - - // ditch annoying query params, keep useful params and fragment - $this->assertEquals( - $this->ref.'?my=stuff&is=kept#again', - cleanup_url( - $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again' - ) - ); - } -} diff --git a/tests/Url/GetUrlSchemeTest.php b/tests/Url/GetUrlSchemeTest.php deleted file mode 100644 index 18b932d6..00000000 --- a/tests/Url/GetUrlSchemeTest.php +++ /dev/null @@ -1,30 +0,0 @@ -assertEquals('', get_url_scheme('')); - } - - /** - * Get normal scheme of Url - */ - public function testGetUrlScheme() - { - $this->assertEquals('http', get_url_scheme('http://domain.tld:3000')); - $this->assertEquals('https', get_url_scheme('https://domain.tld:3000')); - $this->assertEquals('http', get_url_scheme('domain.tld')); - $this->assertEquals('ssh', get_url_scheme('ssh://domain.tld')); - $this->assertEquals('ftp', get_url_scheme('ftp://domain.tld')); - $this->assertEquals('git', get_url_scheme('git://domain.tld/push?pull=clone#checkout')); - } -} diff --git a/tests/Url/UnparseUrlTest.php b/tests/Url/UnparseUrlTest.php deleted file mode 100644 index e314b484..00000000 --- a/tests/Url/UnparseUrlTest.php +++ /dev/null @@ -1,30 +0,0 @@ -assertEquals('', unparse_url(array())); - } - - /** - * Rebuild a full-featured URL - */ - public function testUnparseFull() - { - $ref = 'http://username:password@hostname:9090/path' - .'?arg1=value1&arg2=value2#anchor'; - $this->assertEquals($ref, unparse_url(parse_url($ref))); - } -} diff --git a/tests/Url/WhitelistProtocolsTest.php b/tests/Url/WhitelistProtocolsTest.php deleted file mode 100644 index a3156804..00000000 --- a/tests/Url/WhitelistProtocolsTest.php +++ /dev/null @@ -1,63 +0,0 @@ -assertEquals($url, whitelist_protocols($url, $whitelist)); - $url = '/path.jpg'; - $this->assertEquals($url, whitelist_protocols($url, $whitelist)); - } - - /** - * Test whitelist_protocols() on a note (relative URL). - */ - public function testWhitelistProtocolMissing() - { - $whitelist = ['ftp', 'magnet']; - $url = 'test.tld/path/?query=value#hash'; - $this->assertEquals('http://'. $url, whitelist_protocols($url, $whitelist)); - } - - /** - * Test whitelist_protocols() with allowed protocols. - */ - public function testWhitelistAllowedProtocol() - { - $whitelist = ['ftp', 'magnet']; - $url = 'http://test.tld/path/?query=value#hash'; - $this->assertEquals($url, whitelist_protocols($url, $whitelist)); - $url = 'https://test.tld/path/?query=value#hash'; - $this->assertEquals($url, whitelist_protocols($url, $whitelist)); - $url = 'ftp://test.tld/path/?query=value#hash'; - $this->assertEquals($url, whitelist_protocols($url, $whitelist)); - $url = 'magnet:test.tld/path/?query=value#hash'; - $this->assertEquals($url, whitelist_protocols($url, $whitelist)); - } - - /** - * Test whitelist_protocols() with allowed protocols. - */ - public function testWhitelistDisallowedProtocol() - { - $whitelist = ['ftp', 'magnet']; - $url = 'javascript:alert("xss");'; - $this->assertEquals('http://alert("xss");', whitelist_protocols($url, $whitelist)); - $url = 'other://test.tld/path/?query=value#hash'; - $this->assertEquals('http://test.tld/path/?query=value#hash', whitelist_protocols($url, $whitelist)); - } -} diff --git a/tests/http/HttpUtils/ClientIpIdTest.php b/tests/http/HttpUtils/ClientIpIdTest.php new file mode 100644 index 00000000..982e57e0 --- /dev/null +++ b/tests/http/HttpUtils/ClientIpIdTest.php @@ -0,0 +1,54 @@ +assertEquals( + '10.1.167.42', + client_ip_id(['REMOTE_ADDR' => '10.1.167.42']) + ); + } + + /** + * Get a remote client ID based on its IP and proxy information (1) + */ + public function testClientIpIdRemoteForwarded() + { + $this->assertEquals( + '10.1.167.42_127.0.1.47', + client_ip_id([ + 'REMOTE_ADDR' => '10.1.167.42', + 'HTTP_X_FORWARDED_FOR' => '127.0.1.47' + ]) + ); + } + + /** + * Get a remote client ID based on its IP and proxy information (2) + */ + public function testClientIpIdRemoteForwardedClient() + { + $this->assertEquals( + '10.1.167.42_10.1.167.56_127.0.1.47', + client_ip_id([ + 'REMOTE_ADDR' => '10.1.167.42', + 'HTTP_X_FORWARDED_FOR' => '10.1.167.56', + 'HTTP_CLIENT_IP' => '127.0.1.47' + ]) + ); + } +} diff --git a/tests/http/HttpUtils/GetHttpUrlTest.php b/tests/http/HttpUtils/GetHttpUrlTest.php new file mode 100644 index 00000000..3dc5bc9b --- /dev/null +++ b/tests/http/HttpUtils/GetHttpUrlTest.php @@ -0,0 +1,67 @@ +assertEquals('Invalid HTTP UrlUtils', $headers[0]); + $this->assertFalse($content); + + // Non HTTP + list($headers, $content) = get_http_response('ftp://save.tld/mysave', 1); + $this->assertEquals('Invalid HTTP UrlUtils', $headers[0]); + $this->assertFalse($content); + } + + /** + * Get an invalid remote URL + */ + public function testGetInvalidRemoteUrl() + { + list($headers, $content) = @get_http_response('http://non.existent', 1); + $this->assertFalse($headers); + $this->assertFalse($content); + } + + /** + * Test getAbsoluteUrl with relative target URL. + */ + public function testGetAbsoluteUrlWithRelative() + { + $origin = 'http://non.existent/blabla/?test'; + $target = '/stuff.php'; + + $expected = 'http://non.existent/stuff.php'; + $this->assertEquals($expected, getAbsoluteUrl($origin, $target)); + + $target = 'stuff.php'; + $expected = 'http://non.existent/blabla/stuff.php'; + $this->assertEquals($expected, getAbsoluteUrl($origin, $target)); + } + + /** + * Test getAbsoluteUrl with absolute target URL. + */ + public function testGetAbsoluteUrlWithAbsolute() + { + $origin = 'http://non.existent/blabla/?test'; + $target = 'http://other.url/stuff.php'; + + $this->assertEquals($target, getAbsoluteUrl($origin, $target)); + } +} diff --git a/tests/http/HttpUtils/GetIpAdressFromProxyTest.php b/tests/http/HttpUtils/GetIpAdressFromProxyTest.php new file mode 100644 index 00000000..fe3a639e --- /dev/null +++ b/tests/http/HttpUtils/GetIpAdressFromProxyTest.php @@ -0,0 +1,61 @@ +assertFalse(getIpAddressFromProxy(array(), array())); + } + + /** + * Test with a single IP in proxy header. + */ + public function testWithOneForwardedIp() + { + $ip = '1.1.1.1'; + $server = array('HTTP_X_FORWARDED_FOR' => $ip); + $this->assertEquals($ip, getIpAddressFromProxy($server, array())); + } + + /** + * Test with a multiple IPs in proxy header. + */ + public function testWithMultipleForwardedIp() + { + $ip = '1.1.1.1'; + $ip2 = '2.2.2.2'; + + $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); + $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); + + $server = array('HTTP_X_FORWARDED_FOR' => $ip .' , '. $ip2); + $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); + } + + /** + * Test with a trusted IP address. + */ + public function testWithTrustedIp() + { + $ip = '1.1.1.1'; + $ip2 = '2.2.2.2'; + + $server = array('HTTP_X_FORWARDED_FOR' => $ip); + $this->assertFalse(getIpAddressFromProxy($server, array($ip))); + + $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); + $this->assertEquals($ip2, getIpAddressFromProxy($server, array($ip))); + $this->assertFalse(getIpAddressFromProxy($server, array($ip, $ip2))); + } +} diff --git a/tests/http/HttpUtils/IndexUrlTest.php b/tests/http/HttpUtils/IndexUrlTest.php new file mode 100644 index 00000000..bcbe59cb --- /dev/null +++ b/tests/http/HttpUtils/IndexUrlTest.php @@ -0,0 +1,74 @@ +assertEquals( + 'http://host.tld/', + index_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/index.php' + ) + ) + ); + + $this->assertEquals( + 'http://host.tld/admin/', + index_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/admin/index.php' + ) + ) + ); + } + + /** + * The resource is != "index.php" + */ + public function testOtherResource() + { + $this->assertEquals( + 'http://host.tld/page.php', + page_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/page.php' + ) + ) + ); + + $this->assertEquals( + 'http://host.tld/admin/page.php', + page_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/admin/page.php' + ) + ) + ); + } +} diff --git a/tests/http/HttpUtils/IsHttpsTest.php b/tests/http/HttpUtils/IsHttpsTest.php new file mode 100644 index 00000000..348956c6 --- /dev/null +++ b/tests/http/HttpUtils/IsHttpsTest.php @@ -0,0 +1,39 @@ +assertTrue(is_https(['HTTPS' => true])); + $this->assertTrue(is_https(['HTTPS' => '1'])); + $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 443])); + $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443'])); + $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443,123,456,'])); + } + + /** + * Test is_https with HTTP values. + */ + public function testIsHttpsFalse() + { + $this->assertFalse(is_https([])); + $this->assertFalse(is_https(['HTTPS' => false])); + $this->assertFalse(is_https(['HTTPS' => '0'])); + $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 123])); + $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '123'])); + $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => ',123,456,'])); + } +} diff --git a/tests/http/HttpUtils/PageUrlTest.php b/tests/http/HttpUtils/PageUrlTest.php new file mode 100644 index 00000000..f1991716 --- /dev/null +++ b/tests/http/HttpUtils/PageUrlTest.php @@ -0,0 +1,78 @@ +assertEquals( + 'http://host.tld/?p1=v1&p2=v2', + page_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/index.php', + 'QUERY_STRING' => 'p1=v1&p2=v2' + ) + ) + ); + + $this->assertEquals( + 'http://host.tld/admin/?action=edit_tag', + page_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/admin/index.php', + 'QUERY_STRING' => 'action=edit_tag' + ) + ) + ); + } + + /** + * The resource is != "index.php" + */ + public function testOtherResource() + { + $this->assertEquals( + 'http://host.tld/page.php?p1=v1&p2=v2', + page_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/page.php', + 'QUERY_STRING' => 'p1=v1&p2=v2' + ) + ) + ); + + $this->assertEquals( + 'http://host.tld/admin/page.php?action=edit_tag', + page_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/admin/page.php', + 'QUERY_STRING' => 'action=edit_tag' + ) + ) + ); + } +} diff --git a/tests/http/HttpUtils/ServerUrlTest.php b/tests/http/HttpUtils/ServerUrlTest.php new file mode 100644 index 00000000..9caf1049 --- /dev/null +++ b/tests/http/HttpUtils/ServerUrlTest.php @@ -0,0 +1,223 @@ +assertEquals( + 'https://host.tld', + server_url( + array( + 'HTTPS' => 'ON', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '443' + ) + ) + ); + + $this->assertEquals( + 'https://host.tld:8080', + server_url( + array( + 'HTTPS' => 'ON', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '8080' + ) + ) + ); + } + + /** + * Detect a Proxy that sets Forwarded-Host + */ + public function testHttpsProxyForwardedHost() + { + $this->assertEquals( + 'https://host.tld:8080', + server_url( + array( + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'HTTP_X_FORWARDED_PORT' => '8080', + 'HTTP_X_FORWARDED_HOST' => 'host.tld' + ) + ) + ); + + $this->assertEquals( + 'https://host.tld:4974', + server_url( + array( + 'HTTP_X_FORWARDED_PROTO' => 'https, https', + 'HTTP_X_FORWARDED_PORT' => '4974, 80', + 'HTTP_X_FORWARDED_HOST' => 'host.tld, example.com' + ) + ) + ); + } + + /** + * Detect a Proxy with SSL enabled + */ + public function testHttpsProxyForward() + { + $this->assertEquals( + 'https://host.tld:8080', + server_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'HTTP_X_FORWARDED_PORT' => '8080' + ) + ) + ); + + $this->assertEquals( + 'https://host.tld', + server_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'HTTP_X_FORWARDED_PROTO' => 'https' + ) + ) + ); + + $this->assertEquals( + 'https://host.tld', + server_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'HTTP_X_FORWARDED_PORT' => '443' + ) + ) + ); + + $this->assertEquals( + 'https://host.tld:4974', + server_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'HTTP_X_FORWARDED_PROTO' => 'https, https', + 'HTTP_X_FORWARDED_PORT' => '4974, 80' + ) + ) + ); + } + + /** + * Detect if the server uses a specific port (!= 80) + */ + public function testPort() + { + // HTTP + $this->assertEquals( + 'http://host.tld:8080', + server_url( + array( + 'HTTPS' => 'OFF', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '8080' + ) + ) + ); + + // HTTPS + $this->assertEquals( + 'https://host.tld:8080', + server_url( + array( + 'HTTPS' => 'ON', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '8080' + ) + ) + ); + } + + /** + * HTTP server on port 80 + */ + public function testStandardHttpPort() + { + $this->assertEquals( + 'http://host.tld', + server_url( + array( + 'HTTPS' => 'OFF', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80' + ) + ) + ); + } + + /** + * HTTPS server on port 443 + */ + public function testStandardHttpsPort() + { + $this->assertEquals( + 'https://host.tld', + server_url( + array( + 'HTTPS' => 'ON', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '443' + ) + ) + ); + } + + /** + * Misconfigured server (see #1022): Proxy HTTP but 443 + */ + public function testHttpWithPort433() + { + $this->assertEquals( + 'https://host.tld', + server_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'HTTP_X_FORWARDED_PROTO' => 'http', + 'HTTP_X_FORWARDED_PORT' => '443' + ) + ) + ); + + $this->assertEquals( + 'https://host.tld', + server_url( + array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'HTTP_X_FORWARDED_PROTO' => 'https, http', + 'HTTP_X_FORWARDED_PORT' => '443, 80' + ) + ) + ); + } +} diff --git a/tests/http/UrlTest.php b/tests/http/UrlTest.php index 011b416d..342b78a4 100644 --- a/tests/http/UrlTest.php +++ b/tests/http/UrlTest.php @@ -1,6 +1,6 @@ assertEquals('', cleanup_url('')); + } + + /** + * Clean an already cleaned URL + */ + public function testCleanupUrlAlreadyClean() + { + $this->assertEquals($this->ref, cleanup_url($this->ref)); + $this->ref2 = $this->ref.'/path/to/dir/'; + $this->assertEquals($this->ref2, cleanup_url($this->ref2)); + } + + /** + * Clean URL fragments + */ + public function testCleanupUrlFragment() + { + $this->assertEquals($this->ref, cleanup_url($this->ref.'#tk.rss_all')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-U3ht0tkc4b')); + } + + /** + * Clean URL query - single annoying parameter + */ + public function testCleanupUrlQuerySingle() + { + $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_object_map=junk')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_ref_map=Cr4p!')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_type_map=g4R84g3')); + + $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb_stuff=v41u3')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb=71m3w4573')); + + $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_campaign=zomg')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_medium=numnum')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_source=c0d3')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_term=1n4l')); + + $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url')); + + $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_name=junk')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_start=junk')); + $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_item_index=junk')); + } + + /** + * Clean URL query - multiple annoying parameters + */ + public function testCleanupUrlQueryMultiple() + { + $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url&fb=som3th1ng')); + + $this->assertEquals($this->ref, cleanup_url( + $this->ref.'?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3' + )); + + $this->assertEquals($this->ref, cleanup_url( + $this->ref.'?campaign_start=zomg&campaign_name=numnum' + )); + } + + /** + * Clean URL query - multiple annoying parameters and fragment + */ + public function testCleanupUrlQueryFragment() + { + $this->assertEquals($this->ref, cleanup_url( + $this->ref.'?xtor=some-url&fb=som3th1ng#tk.rss_all' + )); + + // ditch annoying query params and fragment, keep useful params + $this->assertEquals( + $this->ref.'?my=stuff&is=kept', + cleanup_url( + $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all' + ) + ); + + // ditch annoying query params, keep useful params and fragment + $this->assertEquals( + $this->ref.'?my=stuff&is=kept#again', + cleanup_url( + $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again' + ) + ); + } +} diff --git a/tests/http/UrlUtils/GetUrlSchemeTest.php b/tests/http/UrlUtils/GetUrlSchemeTest.php new file mode 100644 index 00000000..2b97f7be --- /dev/null +++ b/tests/http/UrlUtils/GetUrlSchemeTest.php @@ -0,0 +1,32 @@ +assertEquals('', get_url_scheme('')); + } + + /** + * Get normal scheme of UrlUtils + */ + public function testGetUrlScheme() + { + $this->assertEquals('http', get_url_scheme('http://domain.tld:3000')); + $this->assertEquals('https', get_url_scheme('https://domain.tld:3000')); + $this->assertEquals('http', get_url_scheme('domain.tld')); + $this->assertEquals('ssh', get_url_scheme('ssh://domain.tld')); + $this->assertEquals('ftp', get_url_scheme('ftp://domain.tld')); + $this->assertEquals('git', get_url_scheme('git://domain.tld/push?pull=clone#checkout')); + } +} diff --git a/tests/http/UrlUtils/UnparseUrlTest.php b/tests/http/UrlUtils/UnparseUrlTest.php new file mode 100644 index 00000000..040d8c54 --- /dev/null +++ b/tests/http/UrlUtils/UnparseUrlTest.php @@ -0,0 +1,32 @@ +assertEquals('', unparse_url(array())); + } + + /** + * Rebuild a full-featured URL + */ + public function testUnparseFull() + { + $ref = 'http://username:password@hostname:9090/path' + .'?arg1=value1&arg2=value2#anchor'; + $this->assertEquals($ref, unparse_url(parse_url($ref))); + } +} diff --git a/tests/http/UrlUtils/WhitelistProtocolsTest.php b/tests/http/UrlUtils/WhitelistProtocolsTest.php new file mode 100644 index 00000000..69512dbd --- /dev/null +++ b/tests/http/UrlUtils/WhitelistProtocolsTest.php @@ -0,0 +1,63 @@ +assertEquals($url, whitelist_protocols($url, $whitelist)); + $url = '/path.jpg'; + $this->assertEquals($url, whitelist_protocols($url, $whitelist)); + } + + /** + * Test whitelist_protocols() on a note (relative URL). + */ + public function testWhitelistProtocolMissing() + { + $whitelist = ['ftp', 'magnet']; + $url = 'test.tld/path/?query=value#hash'; + $this->assertEquals('http://'. $url, whitelist_protocols($url, $whitelist)); + } + + /** + * Test whitelist_protocols() with allowed protocols. + */ + public function testWhitelistAllowedProtocol() + { + $whitelist = ['ftp', 'magnet']; + $url = 'http://test.tld/path/?query=value#hash'; + $this->assertEquals($url, whitelist_protocols($url, $whitelist)); + $url = 'https://test.tld/path/?query=value#hash'; + $this->assertEquals($url, whitelist_protocols($url, $whitelist)); + $url = 'ftp://test.tld/path/?query=value#hash'; + $this->assertEquals($url, whitelist_protocols($url, $whitelist)); + $url = 'magnet:test.tld/path/?query=value#hash'; + $this->assertEquals($url, whitelist_protocols($url, $whitelist)); + } + + /** + * Test whitelist_protocols() with allowed protocols. + */ + public function testWhitelistDisallowedProtocol() + { + $whitelist = ['ftp', 'magnet']; + $url = 'javascript:alert("xss");'; + $this->assertEquals('http://alert("xss");', whitelist_protocols($url, $whitelist)); + $url = 'other://test.tld/path/?query=value#hash'; + $this->assertEquals('http://test.tld/path/?query=value#hash', whitelist_protocols($url, $whitelist)); + } +} -- cgit v1.2.3