web-dev-qa-db-ja.com

'wordpress_logged_in'クッキーからユーザー名を削除する

私はいくつかの厳格なセキュリティ対策でクライアントと働いています。セキュリティレビューを受けた後、ログインCookieに保存されているユーザー名(例:.

wordpress_logged_in[username]|[hash]

削除する必要があるものです。これはログインシステムに欠くことのできない部分なので、それを削除してセッションを維持する方法がわからないのですが。

9
phatskat

簡単な紹介

WPソースコードを簡単に見てみると、解決策が見つかったと思います...

WordPressは認証クッキーを設定および解析するために2つの関数を使用します。

  • wp_generate_auth_cookie
  • wp_parse_auth_cookie

wp_generate_auth_cookieにはauth_cookieという名前のフィルタがあります。これはおそらくクッキーの内容を変更するために使用できますが、wp_parse_auth_cookieの中にフィルタはありませんが、...

これらの関数は両方ともpluggable.phpで定義されています。つまり、あなたはそれらのためにあなた自身の実装を書いてデフォルトのものを上書きすることができます。

溶液

  1. あなた自身のプラグインを書いてください(それをBetter Auth Cookieと呼びましょう)。
  2. このプラグインの中にあなた自身のwp_generate_auth_cookiewp_parse_auth_cookie関数を実装してください。
  3. プラグインを有効にしてください。

これらの関数の私の実装例(オリジナルバージョンに強く基づいています)を以下に見つけることができます:

if ( !function_exists('wp_generate_auth_cookie') ) :
/**
 * Generate authentication cookie contents.
 *
 * @since 2.5.0
 *
 * @param int $user_id User ID
 * @param int $expiration Cookie expiration in seconds
 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
 * @param string $token User's session token to use for this cookie
 * @return string Authentication cookie contents. Empty string if user does not exist.
 */
function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {
    $user = get_userdata($user_id);
    if ( ! $user ) {
        return '';
    }

    if ( ! $token ) {
        $manager = WP_Session_Tokens::get_instance( $user_id );
        $token = $manager->create( $expiration );
    }

    $pass_frag = substr($user->user_pass, 8, 4);

    $key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );

    // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
    $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
    $hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key );

    $cookie = $user_id . '|' . $expiration . '|' . $token . '|' . $hash;

    /**
     * Filter the authentication cookie.
     *
     * @since 2.5.0
     *
     * @param string $cookie     Authentication cookie.
     * @param int    $user_id    User ID.
     * @param int    $expiration Authentication cookie expiration in seconds.
     * @param string $scheme     Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.
     * @param string $token      User's session token used.
     */
    return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );
}
endif;


if ( !function_exists('wp_parse_auth_cookie') ) :
/**
 * Parse a cookie into its components
 *
 * @since 2.7.0
 *
 * @param string $cookie
 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
 * @return array Authentication cookie components
 */
function wp_parse_auth_cookie($cookie = '', $scheme = '') {
    if ( empty($cookie) ) {
        switch ($scheme){
            case 'auth':
                $cookie_name = AUTH_COOKIE;
                break;
            case 'secure_auth':
                $cookie_name = SECURE_AUTH_COOKIE;
                break;
            case "logged_in":
                $cookie_name = LOGGED_IN_COOKIE;
                break;
            default:
                if ( is_ssl() ) {
                    $cookie_name = SECURE_AUTH_COOKIE;
                    $scheme = 'secure_auth';
                } else {
                    $cookie_name = AUTH_COOKIE;
                    $scheme = 'auth';
                }
        }

        if ( empty($_COOKIE[$cookie_name]) )
            return false;
        $cookie = $_COOKIE[$cookie_name];
    }

    $cookie_elements = explode('|', $cookie);
    if ( count( $cookie_elements ) !== 4 ) {
        return false;
    }

    list( $user_id, $expiration, $token, $hmac ) = $cookie_elements;

    $user = get_userdata($user_id);
    $username = ( ! $user ) ? '' : $user->user_login;

    return compact( 'username', 'expiration', 'token', 'hmac', 'scheme' );
}
endif;

これらの関数の私のバージョンはuser_loginuser_idに置き換えます。しかし、それをさらに複雑なもの(つまり、ユーザー固有のハッシュ、またはこのようなもの)に変更するための良いスタートとなるはずです。

10