Skip to content

增加vaptcha对忘记密码页面和注册界面的支持,修复ip因为获取到x_forwarded_for多个代理ip而导致vaptcha不可用#1332

Open
xuanmou wants to merge 4 commits intomirai-mamori:previewfrom
xuanmou:v3.1
Open

增加vaptcha对忘记密码页面和注册界面的支持,修复ip因为获取到x_forwarded_for多个代理ip而导致vaptcha不可用#1332
xuanmou wants to merge 4 commits intomirai-mamori:previewfrom
xuanmou:v3.1

Conversation

@xuanmou
Copy link

@xuanmou xuanmou commented Oct 12, 2025

No description provided.

@github-actions
Copy link

这个 PR 已经 45 天没有任何活动了,将被标记为过时 stale 。 删除 stale 的标签或评论,否则将在 10 天内关闭。

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends Vaptcha CAPTCHA support to the forgot password and registration pages, and fixes an issue where the get_the_user_ip() function was failing with multiple proxy IPs in the X-Forwarded-For header.

Key changes:

  • Added Vaptcha initialization hooks to registration and forgot password forms
  • Implemented validation functions for Vaptcha on registration and password reset flows
  • Modified IP extraction logic to handle comma-separated proxy IP lists

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

}
if ($response->success === 0) {
$errorcode = $response->msg;
return new WP_Error('prooffail', '<strong>错误</strong>:非法数据' . $errorcode);
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space before concatenation operator. The error message should be '<strong>错误</strong>:非法数据' . $errorcode with spaces around the . operator for better readability and consistency with PHP coding standards.

Copilot uses AI. Check for mistakes.
}
add_filter('authenticate', 'checkVaptchaAction', 20, 3);

function Vaptcha_lostpassword_CHECK($errors)
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name should follow the codebase naming convention. Change Vaptcha_lostpassword_CHECK to vaptcha_lostpassword_check to match the style used by similar functions like lostpassword_CHECK, turnstile_lostpassword_check, and other captcha verification functions in this file.

Suggested change
function Vaptcha_lostpassword_CHECK($errors)
function vaptcha_lostpassword_check($errors)

Copilot uses AI. Check for mistakes.
}
if (!(isset($_POST['vaptcha_server']) && isset($_POST['vaptcha_token'])))
{
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:请先进行人机验证');
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space after error code identifier 'invalid_vaptcha '. Remove the trailing space to make it 'invalid_vaptcha' for consistency.

Copilot uses AI. Check for mistakes.
}
if (!preg_match('/^https:\/\/([\w-]+\.)+[\w-]*([^<>=?\"\'])*$/', $_POST['vaptcha_server']) || !preg_match('/^[\w\-\$]+$/', $_POST['vaptcha_token']))
{
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:非法数据');
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space after error code identifier 'invalid_vaptcha '. Remove the trailing space to make it 'invalid_vaptcha' for consistency.

Copilot uses AI. Check for mistakes.
$errorcode = $response->msg;
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:非法数据' . $errorcode);
}
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:人机验证失败');
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space after error code identifier 'invalid_vaptcha '. Remove the trailing space to make it 'invalid_vaptcha' for consistency.

Copilot uses AI. Check for mistakes.
}
add_action('lostpassword_post', 'Vaptcha_lostpassword_CHECK');

function Vaptcha_registration_CHECK($errors, $sanitized_user_login, $user_email)
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name should follow the codebase naming convention. Change Vaptcha_registration_CHECK to vaptcha_registration_check to match the style used by similar functions like registration_CAPTCHA_CHECK, turnstile_registration_check, and other captcha verification functions in this file.

Suggested change
function Vaptcha_registration_CHECK($errors, $sanitized_user_login, $user_email)
function vaptcha_registration_check($errors, $sanitized_user_login, $user_email)

Copilot uses AI. Check for mistakes.
{
if (empty($_POST))
{
return false;
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function returns false when $_POST is empty, but this is inconsistent with the expected return type. The lostpassword_post action expects an $errors object to be returned, not false. This should return $errors directly to maintain consistency with the Turnstile implementation (line 3524) which returns $errors for similar conditions.

Suggested change
return false;
return $errors;

Copilot uses AI. Check for mistakes.
}
if ($response->success === 0) {
$errorcode = $response->msg;
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:非法数据' . $errorcode);
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space after error code identifier 'invalid_vaptcha '. Remove the trailing space to make it 'invalid_vaptcha' for consistency.

Copilot uses AI. Check for mistakes.
Comment on lines +3679 to +3680
$ip2 = explode('|', str_replace(',', '|', $ip));
return apply_filters('wpb_get_ip', trim($ip2[0])); //解决HTTP_X_FORWARDED_FOR获取到代理地址而导致人机验证不通过
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The delimiter handling uses pipe character | but HTTP_X_FORWARDED_FOR typically uses commas (,) as separators, not pipes. The str_replace(',', '|', $ip) converts commas to pipes and then explodes on pipes, which is unnecessarily convoluted. Consider simplifying to: $ip_array = explode(',', $ip); return apply_filters('wpb_get_ip', trim($ip_array[0]));

Suggested change
$ip2 = explode('|', str_replace(',', '|', $ip));
return apply_filters('wpb_get_ip', trim($ip2[0])); //解决HTTP_X_FORWARDED_FOR获取到代理地址而导致人机验证不通过
$ip_array = explode(',', $ip);
return apply_filters('wpb_get_ip', trim($ip_array[0])); //解决HTTP_X_FORWARDED_FOR获取到代理地址而导致人机验证不通过

Copilot uses AI. Check for mistakes.
$ip = $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
return apply_filters('wpb_get_ip', $ip);
$ip2 = explode('|', str_replace(',', '|', $ip));
return apply_filters('wpb_get_ip', trim($ip2[0])); //解决HTTP_X_FORWARDED_FOR获取到代理地址而导致人机验证不通过
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function assumes HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR always contain valid IP addresses, but these headers can be spoofed by clients. While this PR fixes the issue of multiple proxy IPs, consider adding validation to ensure the extracted IP is actually a valid IP address using filter_var($ip, FILTER_VALIDATE_IP) before returning it.

Suggested change
return apply_filters('wpb_get_ip', trim($ip2[0])); //解决HTTP_X_FORWARDED_FOR获取到代理地址而导致人机验证不通过
$user_ip = trim($ip2[0]);
if (!filter_var($user_ip, FILTER_VALIDATE_IP)) {
$user_ip = '';
}
return apply_filters('wpb_get_ip', $user_ip); //解决HTTP_X_FORWARDED_FOR获取到代理地址而导致人机验证不通过

Copilot uses AI. Check for mistakes.
@github-actions github-actions bot removed the Stale label Nov 28, 2025
@Shiroiame-Kusu Shiroiame-Kusu changed the base branch from v3.1 to preview January 5, 2026 14:41
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 21 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +3415 to +3416
if (!preg_match('/^https:\/\/([\w-]+\.)+[\w-]*([^<>=?\"\'])*$/', $_POST['vaptcha_server']) || !preg_match('/^[\w\-\$]+$/', $_POST['vaptcha_token']))
{
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent brace style: The opening brace is on a new line, which is inconsistent with the coding style used elsewhere in this file. The brace should be on the same line as the if statement for consistency.

Copilot uses AI. Check for mistakes.
Comment on lines +3452 to +3453
if (!preg_match('/^https:\/\/([\w-]+\.)+[\w-]*([^<>=?\"\'])*$/', $_POST['vaptcha_server']) || !preg_match('/^[\w\-\$]+$/', $_POST['vaptcha_token']))
{
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent brace style: The opening brace is on a new line, which is inconsistent with the coding style used elsewhere in this file. The brace should be on the same line as the if statement for consistency.

Copilot uses AI. Check for mistakes.
}
if ($response->success === 0) {
$errorcode = $response->msg;
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:非法数据' . $errorcode);
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after error message concatenation. There should be a space between '非法数据' and the error code to improve readability. Consider changing this to '错误:非法数据: ' . $errorcode to add proper spacing.

Suggested change
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:非法数据' . $errorcode);
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:非法数据: ' . $errorcode);

Copilot uses AI. Check for mistakes.
}
if (!(isset($_POST['vaptcha_server']) && isset($_POST['vaptcha_token'])))
{
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:请先进行人机验证');
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code key has a trailing space ('invalid_vaptcha ') which is inconsistent with WordPress conventions and the pattern used in other parts of the code. This should be 'invalid_vaptcha' without the trailing space for consistency.

Copilot uses AI. Check for mistakes.
$errorcode = $response->msg;
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:非法数据' . $errorcode);
}
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:人机验证失败');
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code key has a trailing space ('invalid_vaptcha ') which is inconsistent with WordPress conventions. This should be 'invalid_vaptcha' without the trailing space for consistency.

Copilot uses AI. Check for mistakes.
Comment on lines +3405 to +3410
function Vaptcha_lostpassword_CHECK($errors)
{
if (empty($_POST))
{
return false;
}
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing REQUEST_METHOD check. The turnstile implementation checks if $_SERVER['REQUEST_METHOD'] !== 'POST' and returns early. This function should follow the same pattern to avoid processing on non-POST requests.

Copilot uses AI. Check for mistakes.
Comment on lines +3679 to +3680
$ip2 = explode('|', str_replace(',', '|', $ip));
return apply_filters('wpb_get_ip', trim($ip2[0])); //解决HTTP_X_FORWARDED_FOR获取到代理地址而导致人机验证不通过
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential undefined array index: The code accesses $ip2[0] without checking if the array is empty. If $ip is an empty string or contains only delimiters, $ip2[0] might not exist, resulting in a PHP warning. Add a check to ensure the array is not empty before accessing the first element.

Copilot uses AI. Check for mistakes.
Comment on lines +3444 to +3445
if (empty($_POST))
{
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent brace style: The opening brace is on a new line, which is inconsistent with the coding style used elsewhere in this file. The brace should be on the same line as the if statement for consistency.

Copilot uses AI. Check for mistakes.
} else if (is_string($response)) {
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:' . $response);
}
return $errors->add('invalid_vaptcha ', '<strong>错误</strong>:未知错误');
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code key has a trailing space ('invalid_vaptcha ') which is inconsistent with WordPress conventions. This should be 'invalid_vaptcha' without the trailing space for consistency.

Copilot uses AI. Check for mistakes.
Comment on lines +3442 to +3447
function Vaptcha_registration_CHECK($errors, $sanitized_user_login, $user_email)
{
if (empty($_POST))
{
return new WP_Error();
}
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing REQUEST_METHOD check. The turnstile implementation checks if $_SERVER['REQUEST_METHOD'] !== 'POST' and returns early. This function should follow the same pattern to avoid processing on non-POST requests.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants