函数文档

wp_is_authorize_application_redirect_url_valid()

💡 云策文档标注

概述

wp_is_authorize_application_redirect_url_valid() 函数用于验证授权应用重定向 URL 的协议方案,确保其格式正确且安全。它检查 URL 的协议是否有效,排除不安全协议如 javascript 和 data,并在非本地环境下要求使用安全连接。

关键要点

  • 验证重定向 URL 的协议方案,允许除 javascript 和 data 外的任何协议。
  • 使用正则表达式检查 URL 格式,确保符合 RFC 2396 标准。
  • 通过 wp_authorize_application_redirect_url_invalid_protocols 过滤器允许自定义无效协议列表。
  • 在非本地环境下,禁止使用 http 协议,强制要求安全连接。
  • 返回 true 表示 URL 有效,否则返回 WP_Error 对象。

代码示例

function wp_is_authorize_application_redirect_url_valid( $url ) {
    $bad_protocols = array( 'javascript', 'data' );
    if ( empty( $url ) ) {
        return true;
    }

    // Based on https://www.rfc-editor.org/rfc/rfc2396#section-3.1
    $valid_scheme_regex = '/^[a-zA-Z][a-zA-Z0-9+.-]*:/';
    if ( ! preg_match( $valid_scheme_regex, $url ) ) {
        return new WP_Error(
            'invalid_redirect_url_format',
            __( 'Invalid URL format.' )
        );
    }

    $invalid_protocols = apply_filters( 'wp_authorize_application_redirect_url_invalid_protocols', $bad_protocols, $url );
    $invalid_protocols = array_map( 'strtolower', $invalid_protocols );

    $scheme   = wp_parse_url( $url, PHP_URL_SCHEME );
    $host     = wp_parse_url( $url, PHP_URL_HOST );
    $is_local = 'local' === wp_get_environment_type();

    if ( empty( $host ) || empty( $scheme ) || in_array( strtolower( $scheme ), $invalid_protocols, true ) ) {
        return new WP_Error(
            'invalid_redirect_url_format',
            __( 'Invalid URL format.' )
        );
    }

    if ( 'http' === $scheme && ! $is_local ) {
        return new WP_Error(
            'invalid_redirect_scheme',
            __( 'The URL must be served over a secure connection.' )
        );
    }

    return true;
}

注意事项

  • 函数在 WordPress 6.3.2 版本中引入,用于增强应用授权安全性。
  • 开发者可以通过 wp_authorize_application_redirect_url_invalid_protocols 过滤器扩展或修改无效协议列表。
  • 验证依赖于 wp_parse_url() 和 wp_get_environment_type() 等辅助函数,确保跨 PHP 版本兼容性。

📄 原文内容

Validates the redirect URL protocol scheme. The protocol can be anything except http and javascript.

Parameters

$urlstringrequired
The redirect URL to be validated.

Return

true|WP_Error True if the redirect URL is valid, a WP_Error object otherwise.

Source

function wp_is_authorize_application_redirect_url_valid( $url ) {
	$bad_protocols = array( 'javascript', 'data' );
	if ( empty( $url ) ) {
		return true;
	}

	// Based on https://www.rfc-editor.org/rfc/rfc2396#section-3.1
	$valid_scheme_regex = '/^[a-zA-Z][a-zA-Z0-9+.-]*:/';
	if ( ! preg_match( $valid_scheme_regex, $url ) ) {
		return new WP_Error(
			'invalid_redirect_url_format',
			__( 'Invalid URL format.' )
		);
	}

	/**
	 * Filters the list of invalid protocols used in applications redirect URLs.
	 *
	 * @since 6.3.2
	 *
	 * @param string[] $bad_protocols Array of invalid protocols.
	 * @param string   $url The redirect URL to be validated.
	 */
	$invalid_protocols = apply_filters( 'wp_authorize_application_redirect_url_invalid_protocols', $bad_protocols, $url );
	$invalid_protocols = array_map( 'strtolower', $invalid_protocols );

	$scheme   = wp_parse_url( $url, PHP_URL_SCHEME );
	$host     = wp_parse_url( $url, PHP_URL_HOST );
	$is_local = 'local' === wp_get_environment_type();

	// Validates if the proper URI format is applied to the URL.
	if ( empty( $host ) || empty( $scheme ) || in_array( strtolower( $scheme ), $invalid_protocols, true ) ) {
		return new WP_Error(
			'invalid_redirect_url_format',
			__( 'Invalid URL format.' )
		);
	}

	if ( 'http' === $scheme && ! $is_local ) {
		return new WP_Error(
			'invalid_redirect_scheme',
			__( 'The URL must be served over a secure connection.' )
		);
	}

	return true;
}

Hooks

apply_filters( ‘wp_authorize_application_redirect_url_invalid_protocols’, string[] $bad_protocols, string $url )

Filters the list of invalid protocols used in applications redirect URLs.

Changelog

Version Description
6.3.2 Introduced.