-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
The script is currently checking if the `REQUEST_URI` is containing `wp-comments-post.php`, the default script to handle the submission of a comment. Some security plugins have options to rename this file to disguise that WordPress is used. With this fix, the `SCRIPT_NAME` is used instead. Since many security plugins do use rewrite rules, while the `REQUEST_URI` value is changed, the `SCRIPT_NAME` value stays the same. Therefor the condition would still recognize if a comment was submitted. Original fix by @2ndkauboy in #589, adapted to v3.
- Loading branch information
Showing
3 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters