Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use SCRIPT_NAME instead of REQUEST_URI to check path (#585) #593

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Rules/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function precheck(): void {
return;
}

$request_uri = Settings::get_key( $_SERVER, 'REQUEST_URI' );
$request_uri = Settings::get_key( $_SERVER, 'SCRIPT_NAME' );
$request_path = DataHelper::parse_url( $request_uri, 'path' );

if ( strpos( $request_path, 'wp-comments-post.php' ) === false ) {
Expand Down
71 changes: 71 additions & 0 deletions tests/Unit/Handlers/CommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace AntispamBee\Tests\Unit\Handlers;

use AntispamBee\Handlers\Comment;
use AntispamBee\Handlers\Reaction;
use Yoast\WPTestUtils\BrainMonkey\TestCase;

use function Brain\Monkey\Functions\stubs;

/**
* Unit tests for {@see Comment}.
*/
class CommentTest extends TestCase {

public function test_process() {
global $_POST;
global $_SERVER;

$_POST = null;
$_SERVER = [
'HTTP_CLIENT_IP' => '192.0.2.100',
'SCRIPT_NAME' => '/index.php'
];

stubs(
[
'esc_url_raw' => function (string $url) {
return $url;
},
'wp_parse_url' => 'parse_url',
'wp_unslash' => function ($value) {
return $value;
},
]
);

$processed = [];
mock('overload:' . Reaction::class )
->expects( 'process' )
->withArgs( function( $input ) use ( &$processed ) {
$processed[] = $input;
return true;
} );

$comment = [ 'comment_type' => 'comment' ];

$result = Comment::process( $comment );
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on index.php' );
self::assertEmpty( $processed, 'Comment should no have been processed on index.php' );

$_SERVER['SCRIPT_NAME'] = '';
$result = Comment::process( $comment );
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on invalid request' );
self::assertSame( 1, $result['ab_spam__invalid_request'], 'Invalid request not detected' );
self::assertEmpty( $processed, 'Comment should no have been processed on invalid request' );

$_SERVER['SCRIPT_NAME'] = '/wp-comments-post.php';
$result = Comment::process( $comment );
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on invalid request' );
self::assertArrayNotHasKey( 'processed', $result, 'Comment should no have been processed without POST data' );

$_POST = 'test me';
$result = Comment::process( $comment );
self::assertSame( [ $result ], $processed, 'Comment was not processed' );

$comment = [ 'comment_type' => 'linkback' ];
$result = Comment::process( $comment );
self::assertSame( $comment, $result, 'Linkback should not be modified by comment handler' );
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Rules/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use AntispamBee\Rules\Honeypot;

use function Brain\Monkey\Functions\stubs;

/**
* Unit tests for {@see Honeypot}.
*
Expand Down Expand Up @@ -35,4 +37,59 @@ public function test_init() {
'comment_form_field_comment filter was not added'
);
}

public function test_precheck() {
global $_POST;
global $_SERVER;

stubs(
[
'esc_url_raw' => function (string $url) {
return $url;
},
'is_feed' => false,
'is_trackback' => false,
'wp_parse_url' => 'parse_url',
'wp_unslash' => function ($value) {
return $value;
},
]
);
mock( 'overload:' . \AntispamBee\Helpers\Honeypot::class )
->expects( 'get_secret_name_for_post' )
->andReturns( 'my-secret' );

$_POST = [];
$_SERVER = [ 'SCRIPT_NAME' => '/index.php' ];

Honeypot::precheck();
self::assertEmpty( $_POST, 'Empty POST data modified unexpectedly' );

$_POST = [ 'foo' => 'bar' ];
Honeypot::precheck();
self::assertSame( ['foo' => 'bar' ], $_POST, 'POST data modified on index.php' );

$_SERVER = [ 'SCRIPT_NAME' => '/wp-comments-post.php' ];
Honeypot::precheck();
self::assertSame( 1, $_POST[ 'ab_spam__invalid_request' ], 'request without missing fields not detected' );

$_POST = [
'my-secret' => 'S3cr3t',
'my-hidden' => 'H1dd3n',
];
Honeypot::precheck();
self::assertSame( 1, $_POST[ 'ab_spam__hidden_field' ], 'non-empty hidden fiend not detected' );

$_POST = [
'my-secret' => 'S3cr3t',
'my-hidden' => '',
];
Honeypot::precheck();
self::assertSame(
[ 'my-hidden' => 'S3cr3t' ],
$_POST,
'secret was not moved to hidden field'
);

}
}