download( $url, true ); if ( empty( $got['ok'] ) ) { return self::fail( (string) $got['code'], (string) $got['detail'] ); } return $this->store( (string) $got['body'] ); } /** * Download a URL with every protection (used for images and for scheduled import files). * * @param string $url URL. * @param bool $require_pin Refuse (image_blocked, no_pin) a request the address pin cannot apply to. * @param string $file Stream the body into this file instead of memory (scheduled feeds: the feed limit applies). * @return array {ok, code, detail, body} (body is empty when streamed to $file) */ public function download( string $url, bool $require_pin = false, string $file = '' ): array { $max = '' === $file ? self::MAX_BYTES : self::feedMaxBytes(); if ( ! has_action( 'http_api_curl', array( SsrfGuard::class, 'pinCurl' ) ) ) { add_action( 'http_api_curl', array( SsrfGuard::class, 'pinCurl' ), 10, 3 ); } $current = $url; for ( $hop = 0; $hop <= self::MAX_REDIRECTS; $hop++ ) { $check = self::validate( $current ); if ( '' !== $check['code'] ) { return self::miss( $check['code'], $check['detail'] ); } if ( $require_pin && ! self::pinAvailable( $current ) ) { return self::miss( 'image_blocked', 'no_pin' ); } $args = array( 'timeout' => self::TIMEOUT, 'redirection' => 0, // One byte over the limit: the transports cut the body at the limit and report success, so a body that // reaches limit + 1 is how an oversized response is recognised. 'limit_response_size' => $max + 1, 'reject_unsafe_urls' => true, 'user-agent' => 'CatalogRelay/' . ( defined( 'CATALOGRELAY_VERSION' ) ? CATALOGRELAY_VERSION : '1' ), 'rotosix_pin' => array( 'host' => $check['host'], 'port' => $check['port'], 'ip' => $check['ips'][0], ), ); if ( '' !== $file ) { $args['stream'] = true; $args['filename'] = $file; } $res = wp_safe_remote_get( $current, $args ); if ( is_wp_error( $res ) ) { // WordPress' own SSRF filter refused the URL: that is a block, not a network failure. Its message is // translated with the site's language. $msg = $res->get_error_message(); $refused = 'A valid URL was not provided.' === $msg || __( 'A valid URL was not provided.' ) === $msg; // phpcs:ignore WordPress.WP.I18n.MissingArgDomain -- WordPress' own string, in its own text domain. return $refused ? self::miss( 'image_blocked', 'url' ) : self::miss( 'image_failed', 'request' ); } $code = (int) wp_remote_retrieve_response_code( $res ); if ( $code >= 300 && $code < 400 ) { $loc = (string) wp_remote_retrieve_header( $res, 'location' ); if ( '' === $loc ) { return self::miss( 'image_failed', 'redirect' ); } $current = self::absolute( $current, $loc ); continue; } if ( 200 !== $code ) { return self::miss( 'image_failed', 'http_' . $code ); } $body = '' === $file ? (string) wp_remote_retrieve_body( $res ) : ''; clearstatcache( true, $file ); $size = '' === $file ? strlen( $body ) : (int) @filesize( $file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- a missing file is an empty response. if ( 0 === $size || $size > $max ) { return self::miss( 'image_failed', 'size' ); } return array( 'ok' => true, 'code' => '', 'detail' => '', 'body' => $body, ); } return self::miss( 'image_failed', 'too_many_redirects' ); } /** * Largest scheduled feed file (bytes). * * @return int */ public static function feedMaxBytes(): int { /** * Filters the largest file a scheduled import downloads (default 512 MB); a larger response fails the run. * * @param int $bytes Bytes. */ return max( 1, (int) apply_filters( 'catalogrelay_feed_max_bytes', self::FEED_MAX_BYTES ) ); } /** * Validate a URL without any request: scheme, port, user info and the addresses of the host. * * @param string $url URL. * @return array {code: ''|image_blocked|image_failed, detail, host, port, ips} */ public static function validate( string $url ): array { $out = array( 'code' => '', 'detail' => '', 'host' => '', 'port' => 0, 'ips' => array(), ); $parts = parse_url( trim( $url ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url if ( ! is_array( $parts ) || empty( $parts['host'] ) || empty( $parts['scheme'] ) ) { $out['code'] = 'image_blocked'; $out['detail'] = 'url'; return $out; } $scheme = strtolower( $parts['scheme'] ); if ( 'http' !== $scheme && 'https' !== $scheme ) { $out['code'] = 'image_blocked'; $out['detail'] = 'scheme'; return $out; } if ( isset( $parts['user'] ) || isset( $parts['pass'] ) ) { $out['code'] = 'image_blocked'; $out['detail'] = 'userinfo'; return $out; } $port = isset( $parts['port'] ) ? (int) $parts['port'] : ( 'https' === $scheme ? 443 : 80 ); if ( 80 !== $port && 443 !== $port ) { $out['code'] = 'image_blocked'; $out['detail'] = 'port'; return $out; } $host = strtolower( trim( $parts['host'], '[]' ) ); $ips = self::resolve( $host ); if ( ! $ips ) { $out['code'] = 'image_failed'; $out['detail'] = 'dns'; return $out; } foreach ( $ips as $ip ) { if ( ! IpGuard::isPublic( $ip ) ) { $out['code'] = 'image_blocked'; $out['detail'] = 'address'; return $out; } } $out['host'] = $host; $out['port'] = $port; $out['ips'] = $ips; return $out; } /** * Every A and AAAA address of a host (an IP literal resolves to itself): the library's resolver, A records first. * * @param string $host Host. * @return string[] */ public static function resolve( string $host ): array { if ( false !== filter_var( $host, FILTER_VALIDATE_IP ) ) { return array( $host ); } return SsrfGuard::resolve( Plugin::descriptor(), $host ); } /** * Whether the request to this URL can be pinned to the validated address: WordPress sends it with cURL (the * http_api_curl action only fires there; Requests falls back to its streams transport for https when libcurl has no * TLS), and WordPress' proxy setting does not send it through a proxy that resolves the host instead. * * @param string $url URL. * @param \WP_HTTP_Proxy|null $proxy Proxy settings (tests). * @return bool */ public static function pinAvailable( string $url, $proxy = null ): bool { if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) || ! defined( 'CURLOPT_RESOLVE' ) ) { return false; } if ( 0 === stripos( $url, 'https:' ) ) { $curl = function_exists( 'curl_version' ) ? curl_version() : false; if ( ! is_array( $curl ) || ! ( CURL_VERSION_SSL & (int) $curl['features'] ) ) { return false; } } $proxy = $proxy instanceof \WP_HTTP_Proxy ? $proxy : new \WP_HTTP_Proxy(); return ! ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ); } /** * Resolve a Location header against the current URL. * * @param string $base Current URL. * @param string $loc Location. * @return string */ public static function absolute( string $base, string $loc ): string { if ( 1 === preg_match( '#^[a-z][a-z0-9+.-]*://#i', $loc ) ) { return $loc; } $p = parse_url( $base ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url if ( ! is_array( $p ) || empty( $p['host'] ) ) { return $loc; } $origin = ( $p['scheme'] ?? 'http' ) . '://' . $p['host'] . ( isset( $p['port'] ) ? ':' . $p['port'] : '' ); if ( 0 === strpos( $loc, '//' ) ) { return ( $p['scheme'] ?? 'http' ) . ':' . $loc; } if ( '/' === substr( $loc, 0, 1 ) ) { return $origin . $loc; } $path = $p['path'] ?? '/'; if ( '#' === substr( $loc, 0, 1 ) ) { return $origin . $path . ( isset( $p['query'] ) ? '?' . $p['query'] : '' ); } if ( '?' === substr( $loc, 0, 1 ) ) { return $origin . $path . $loc; } $dir = isset( $p['path'] ) ? rtrim( substr( $p['path'], 0, (int) strrpos( $p['path'], '/' ) ), '/' ) : ''; return $origin . $dir . '/' . $loc; } /** * Check the content type and the pixel size by content and write the body to a temporary file. * * @param string $body Body. * @return array */ private function store( string $body ): array { $info = @getimagesizefromstring( $body ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- invalid data returns false. $mime = is_array( $info ) && ! empty( $info['mime'] ) ? (string) $info['mime'] : ''; $dims = is_array( $info ) ? array( (int) $info[0], (int) $info[1] ) : array( 0, 0 ); if ( '' === $mime && 0 === strncmp( $body, 'RIFF', 4 ) && 'WEBP' === substr( $body, 8, 4 ) ) { $mime = 'image/webp'; $dims = self::webpSize( $body ); } if ( '' === $mime && 'ftypavif' === substr( $body, 4, 8 ) ) { $mime = 'image/avif'; } if ( 'image/avif' === $mime && ( $dims[0] < 1 || $dims[1] < 1 ) ) { $dims = self::avifSize( $body ); // PHP before 8.2 does not report the size of an AVIF image. } if ( ! in_array( $mime, self::ALLOWED_MIMES, true ) ) { return self::fail( 'image_failed', 'not_an_image' ); } // The image editor decodes the whole bitmap (about 4 bytes a pixel) to make the sub-sizes: a small file of a huge // size would exhaust the memory and kill the worker. An unknown size is refused too. if ( $dims[0] < 1 || $dims[1] < 1 || $dims[0] * $dims[1] > self::maxPixels() ) { return self::fail( 'image_failed', 'dimensions' ); } if ( ! function_exists( 'wp_tempnam' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; // Not loaded in WP-Cron, where Action Scheduler runs. } $tmp = wp_tempnam( 'catalogrelay-img' ); if ( ! $tmp || false === file_put_contents( $tmp, $body ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents if ( $tmp && file_exists( $tmp ) ) { wp_delete_file( $tmp ); } return self::fail( 'image_failed', 'tempfile' ); } return array( 'ok' => true, 'code' => '', 'detail' => '', 'file' => $tmp, 'mime' => $mime, ); } /** * Largest image (width x height) that is kept. * * @return int */ private static function maxPixels(): int { /** * Filters the largest image, in pixels (width x height), that is downloaded (default 50 million). * * @param int $pixels Pixels. */ return (int) apply_filters( 'catalogrelay_image_max_pixels', self::MAX_PIXELS ); } /** * Width and height from a WebP header (VP8, VP8L or VP8X), 0 when unknown. * * @param string $b Body. * @return int[] */ private static function webpSize( string $b ): array { $chunk = substr( $b, 12, 4 ); if ( 'VP8X' === $chunk && strlen( $b ) >= 30 ) { $w = unpack( 'V', substr( $b, 24, 3 ) . "\0" ); $h = unpack( 'V', substr( $b, 27, 3 ) . "\0" ); return array( (int) $w[1] + 1, (int) $h[1] + 1 ); } if ( 'VP8L' === $chunk && strlen( $b ) >= 25 ) { $v = unpack( 'V', substr( $b, 21, 4 ) ); return array( ( (int) $v[1] & 0x3FFF ) + 1, ( ( (int) $v[1] >> 14 ) & 0x3FFF ) + 1 ); } if ( 'VP8 ' === $chunk && strlen( $b ) >= 30 ) { $d = unpack( 'v2', substr( $b, 26, 4 ) ); return array( (int) $d[1] & 0x3FFF, (int) $d[2] & 0x3FFF ); } return array( 0, 0 ); } /** * Width and height of an AVIF image: the largest "ispe" (image spatial extents) box of its header, 0 when none. * * @param string $b Body. * @return int[] */ private static function avifSize( string $b ): array { $head = substr( $b, 0, 65536 ); $w = 0; $h = 0; $len = strlen( $head ); $at = strpos( $head, 'ispe' ); while ( false !== $at && $len >= $at + 16 ) { $d = unpack( 'N2', substr( $head, $at + 8, 8 ) ); $w = max( $w, (int) $d[1] ); $h = max( $h, (int) $d[2] ); $at = strpos( $head, 'ispe', $at + 4 ); } return array( $w, $h ); } /** * Download failure. * * @param string $code Code. * @param string $detail Detail. * @return array */ private static function miss( string $code, string $detail ): array { return array( 'ok' => false, 'code' => $code, 'detail' => $detail, 'body' => '', ); } /** * Failure result. * * @param string $code Code. * @param string $detail Detail. * @return array */ private static function fail( string $code, string $detail ): array { return array( 'ok' => false, 'code' => $code, 'detail' => $detail, 'file' => '', 'mime' => '', ); } }