Skip to content

Commit

Permalink
Fix IP detection in WP to sync with hCaptcha events info.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jan 29, 2025
1 parent fffe7f2 commit 9b5860d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions .tests/php/integration/WP/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ public function test_add_captcha_when_NOT_active(): void {
* @throws ReflectionException ReflectionException.
*/
public function test_verify(): void {
$commentdata = [ 'some comment data' ];
$commentdata = [
'some comment data',
'comment_author_IP' => '7.7.7.7',
];

$this->prepare_hcaptcha_get_verify_message_html( 'hcaptcha_comment_nonce', 'hcaptcha_comment' );

Expand Down Expand Up @@ -172,7 +175,10 @@ public function test_verify_in_admin(): void {
* @throws ReflectionException ReflectionException.
*/
public function test_verify_not_verified(): void {
$commentdata = [ 'some comment data' ];
$commentdata = [
'some comment data',
'comment_author_IP' => '7.7.7.7',
];
$expected = '<strong>hCaptcha error:</strong> The hCaptcha is invalid.';

$this->prepare_hcaptcha_get_verify_message_html( 'hcaptcha_comment_nonce', 'hcaptcha_comment', false );
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ Instructions for popular native integrations are below:
* Added compatibility with Cookies and Content Security Policy plugin.
* Added auto-verification of arbitrary forms in ajax.
* Improved error messaging for hCaptcha verification.
* Fixed IP detection in the WordPress core via filter, to sync with hCaptcha events information when the IP collection is activated.
* Fixed fatal error with the WPForms plugin in rare cases.
* Fixed error message at the first entry to the login page when Hide Login Errors in on.
* Fixed scrolling to the message on the General page.
Expand Down
3 changes: 3 additions & 0 deletions src/php/WP/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public function verify( $comment_data ): array {
return $comment_data;
}

// Override poor IP detection by WP Core and make sure that IP is the same in the 'comments' table and in the 'hcaptcha_events' table.
$comment_data['comment_author_IP'] = hcap_get_user_ip();

$this->result = hcaptcha_get_verify_message_html( self::NONCE, self::ACTION );

unset( $_POST['h-captcha-response'], $_POST['g-recaptcha-response'] );
Expand Down

0 comments on commit 9b5860d

Please sign in to comment.