$canonical_origin, 'callback_uri' => $canonical_origin . self::CALLBACK_SUFFIX, 'proof_uri' => $canonical_origin . self::PROOF_SUFFIX . '/' . $attempt_segment, ); } /** * Require submitted callback and proof destinations to match derivation. * * @param string $origin Canonical origin. * @param string $callback_uri Submitted callback URI. * @param string $proof_uri Submitted proof URI. * @param string $attempt_segment Unpadded base64url proof path segment. * @return void * @throws InvalidArgumentException For any destination mismatch. */ public static function assert_derived( $origin, $callback_uri, $proof_uri, $attempt_segment ) { $derived = self::derive( $origin, $attempt_segment ); if ( ! is_string( $callback_uri ) || ! hash_equals( $derived['callback_uri'], $callback_uri ) ) { throw new InvalidArgumentException( 'invalid_callback' ); } if ( ! is_string( $proof_uri ) || ! hash_equals( $derived['proof_uri'], $proof_uri ) ) { throw new InvalidArgumentException( 'invalid_proof_uri' ); } } /** * Require and return one byte-canonical HTTPS WordPress origin. * * @param string $origin Submitted origin. * @return string * @throws InvalidArgumentException For invalid or non-canonical input. */ public static function canonical_origin( $origin ) { if ( ! is_string( $origin ) || '' === $origin || 0 !== strpos( $origin, 'https://' ) ) { throw new InvalidArgumentException( 'invalid_origin' ); } if ( preg_match( '/[\x00-\x20\x7f\\\\]/', $origin ) ) { throw new InvalidArgumentException( 'invalid_origin' ); } $parts = wp_parse_url( $origin ); if ( false === $parts || 'https' !== $parts['scheme'] || empty( $parts['host'] ) ) { throw new InvalidArgumentException( 'invalid_origin' ); } foreach ( array( 'user', 'pass', 'query', 'fragment' ) as $forbidden ) { if ( array_key_exists( $forbidden, $parts ) ) { throw new InvalidArgumentException( 'invalid_origin' ); } } if ( isset( $parts['port'] ) && 443 !== $parts['port'] ) { throw new InvalidArgumentException( 'invalid_origin' ); } $host = self::canonical_host( $parts['host'] ); $path = self::canonical_path( isset( $parts['path'] ) ? $parts['path'] : '' ); $canonical = 'https://' . $host . $path; if ( ! hash_equals( $canonical, $origin ) ) { throw new InvalidArgumentException( 'noncanonical_origin' ); } return $canonical; } /** * Canonicalize and validate a URI host. * * @param string $host Parsed host. * @return string * @throws InvalidArgumentException For an invalid host. */ private static function canonical_host( $host ) { $host = rtrim( $host, '.' ); if ( '' === $host ) { throw new InvalidArgumentException( 'invalid_origin' ); } if ( preg_match( '/[^\x00-\x7f]/', $host ) ) { if ( ! function_exists( 'idn_to_ascii' ) ) { throw new InvalidArgumentException( 'invalid_origin' ); } $flags = defined( 'IDNA_DEFAULT' ) ? IDNA_DEFAULT : 0; $idna = defined( 'INTL_IDNA_VARIANT_UTS46' ) ? INTL_IDNA_VARIANT_UTS46 : 1; $host = idn_to_ascii( $host, $flags, $idna ); if ( false === $host ) { throw new InvalidArgumentException( 'invalid_origin' ); } } $host = strtolower( $host ); if ( filter_var( $host, FILTER_VALIDATE_IP ) ) { return $host; } if ( strlen( $host ) > 253 || ! preg_match( '/^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)(?:\.(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?))*$/D', $host ) ) { throw new InvalidArgumentException( 'invalid_origin' ); } return $host; } /** * Canonicalize a WordPress base path without resolving it. * * @param string $path Parsed path. * @return string * @throws InvalidArgumentException For an invalid path. */ private static function canonical_path( $path ) { if ( '' === $path || '/' === $path ) { return ''; } if ( '/' !== $path[0] || '/' === substr( $path, -1 ) || false !== strpos( $path, '//' ) ) { throw new InvalidArgumentException( 'invalid_origin_path' ); } if ( preg_match( '/%(?:2f|5c)/i', $path ) || preg_match( '/%(?![0-9a-fA-F]{2})/', $path ) ) { throw new InvalidArgumentException( 'invalid_origin_path' ); } $canonical_segments = array(); foreach ( explode( '/', substr( $path, 1 ) ) as $segment ) { if ( '' === $segment ) { throw new InvalidArgumentException( 'invalid_origin_path' ); } $decoded = preg_replace_callback( '/%([0-9a-fA-F]{2})/', function ( $match ) { return chr( hexdec( $match[1] ) ); }, $segment ); if ( 1 !== preg_match( '//u', $decoded ) || preg_match( '/[\x00-\x1f\x7f]/', $decoded ) ) { throw new InvalidArgumentException( 'invalid_origin_path' ); } if ( '.' === $decoded || '..' === $decoded ) { throw new InvalidArgumentException( 'invalid_origin_path' ); } $encoded = ''; for ( $index = 0, $length = strlen( $decoded ); $index < $length; ++$index ) { $byte = ord( $decoded[ $index ] ); if ( ( $byte >= 65 && $byte <= 90 ) || ( $byte >= 97 && $byte <= 122 ) || ( $byte >= 48 && $byte <= 57 ) || in_array( $decoded[ $index ], array( '-', '.', '_', '~' ), true ) ) { $encoded .= $decoded[ $index ]; } else { $encoded .= sprintf( '%%%02X', $byte ); } } $canonical_segments[] = $encoded; } return '/' . implode( '/', $canonical_segments ); } }