워드프레스: Username에 특정 단어가 포함된 사용자의 회원등록 차단하기

Last Updated: 2023년 07월 11일 | | 댓글 남기기

워드프레스에서는 회원 가입 기능을 활성화하여 방문자들의 회원가입 가입을 허용할 수 있습니다. 정교한 멤버십 사이트를 운영하려는 경우에는 Ultimate Member나 WP-Members와 같은 멤버십 플러그인을 사용할 수 있고, 우커머스에서 멤버십 방식으로 상품이나 서비스를 판매하고 싶은 경우 단비스토어의 서비스 유료화 상품이나 YITH 플러그인을 이용할 수 있습니다(참고).

회원가입 기능을 활성화할 경우 방문자가 늘어나면서 봇에 의한 스팸 가입자도 증가합니다. 그런 경우 UM이나 WP-Members 플러그인을 사용하고 있다면 reCAPTCHA 기능을 활성화하여 스팸 사용자의 등록을 차단할 수 있습니다("Ultimate Member 및 WP-Members 플러그인에 reCAPTCHA 설정하기" 참고). 멤버십 플러그인이 설치되어 있지 않다면 CAPTCHA 4WP와 같은 무료 플러그인을 사용하는 것도 가능합니다.

워드프레스: Username에 특정 단어가 포함된 사용자의 회원등록 차단하기

워드프레스: Username에 특정 단어가 포함된 사용자의 회원등록 차단하기

네이버 카페에 아이디에 test가 포함된 사용자들이 하루에 30명(이후에는 40명) 정도 가입하다는 질문이 최근에 올라온 적이 있습니다.

봇인지 사람인지 하루에 30번 가입을 합니다. 어제 부터요.
아이디가 test03493094 이런 식입니다.
이멜주소가 test4029029@gmail.com304eoe.dfojeofj 대충 이런 식입니다.
발견 즉시 삭제하는데 계속 가입을 합니다. 아이디 차단이나 test ~ 로 시작하는 아이디 가입 자체를 차단할 수 없을까요 ?
공격으로 보이는데 …난감하네요

출처: 네이버 카페

CAPTCHA 4WP 플러그인을 추천해드렸지만 세팅이 잘못되었는지 아니면 본래 효과가 없는 것인지는 몰라도 효과가 없다고 하네요.

이와 관련하여 다음과 같은 코드를 테마의 함수 파일에 추가하면 사용자 이름(Username)에 "test"가 포함된 사용자들의 가입이 차단됩니다.

/**
 * This WordPress filter prevents user registration if the username contains the word 'test'.
 * It checks the provided username during registration for the occurrence of 'test'.
 * If it finds 'test' in the username, it adds an error that is displayed to the user,
 * effectively blocking the registration process.
 */

add_filter( 'registration_errors', 'disable_user_registration_for_username', 10, 3 );
function disable_user_registration_for_username ( $errors, $sanitized_user_login, $user_email ) {
    // Check if username contains "test"
    if ( strpos( $sanitized_user_login, 'test' ) !== false ) {
        // Throw registration error
        $errors->add( 'username_error', '<strong>ERROR</strong>: The word "test" is not allowed in usernames.' );
    }

    return $errors;
}

실제로 이 코드를 추가한 후에 스팸 가입자가 사라졌다고 하네요. 차일드 테마를 만들어서 차일드 테마 내의 함수 파일에 추가해야 추후 테마 업데이트 시 코드가 사라지지 않습니다.

만약 username에 test 또는 user 단어가 포함된 가입자의 가입을 차단하려는 경우에는 다음과 같이 응용할 수 있습니다.

add_filter( 'registration_errors', 'disable_user_registration_for_username', 10, 3 );
function disable_user_registration_for_username ( $errors, $sanitized_user_login, $user_email ) {
    // Check if username contains "test" or "user"
    if ( stripos( $sanitized_user_login, 'test' ) !== false || stripos( $sanitized_user_login, 'user' ) !== false ) {
        // Throw registration error
        $errors->add( 'username_error', '<strong>ERROR</strong>: The words "test" or "user" are not allowed in usernames.' );
    }

    return $errors;
}

상기 코드에 대하여 보안상 안전한지 챗GPT에 물어보니 보안 관점에서 비교적 안전하다고 합니다. 만약 상기 코드가 보안상 문제가 있다면 댓글을 통해 알려 주시면 감사하겠습니다.

The provided code is relatively safe from a security standpoint as it doesn't perform any unsafe operations such as executing raw user-provided data, writing data to the database without sanitization, or revealing sensitive information.

It simply checks the username during the registration process for a specific pattern, and if it finds it, it adds an error to the registration process, which prevents the form from being submitted.

참고


댓글 남기기

Leave a Comment