Skip to content

Commit 53e0de9

Browse files
committed
fix: use SCRIPT_NAME instead of REQUEST_URI to check path (#585) (#593)
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.
1 parent e413b93 commit 53e0de9

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

src/Rules/Honeypot.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function precheck(): void {
8181
return;
8282
}
8383

84-
$request_uri = Settings::get_key( $_SERVER, 'REQUEST_URI' );
84+
$request_uri = Settings::get_key( $_SERVER, 'SCRIPT_NAME' );
8585
$request_path = DataHelper::parse_url( $request_uri, 'path' );
8686

8787
if ( strpos( $request_path, 'wp-comments-post.php' ) === false ) {

tests/Unit/Handlers/CommentTest.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace AntispamBee\Tests\Unit\Handlers;
4+
5+
use AntispamBee\Handlers\Comment;
6+
use AntispamBee\Handlers\Reaction;
7+
use Yoast\WPTestUtils\BrainMonkey\TestCase;
8+
9+
use function Brain\Monkey\Functions\stubs;
10+
11+
/**
12+
* Unit tests for {@see Comment}.
13+
*/
14+
class CommentTest extends TestCase {
15+
16+
public function test_process() {
17+
global $_POST;
18+
global $_SERVER;
19+
20+
$_POST = null;
21+
$_SERVER = [
22+
'HTTP_CLIENT_IP' => '192.0.2.100',
23+
'SCRIPT_NAME' => '/index.php'
24+
];
25+
26+
stubs(
27+
[
28+
'esc_url_raw' => function (string $url) {
29+
return $url;
30+
},
31+
'wp_parse_url' => 'parse_url',
32+
'wp_unslash' => function ($value) {
33+
return $value;
34+
},
35+
]
36+
);
37+
38+
$processed = [];
39+
mock('overload:' . Reaction::class )
40+
->expects( 'process' )
41+
->withArgs( function( $input ) use ( &$processed ) {
42+
$processed[] = $input;
43+
return true;
44+
} );
45+
46+
$comment = [ 'comment_type' => 'comment' ];
47+
48+
$result = Comment::process( $comment );
49+
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on index.php' );
50+
self::assertEmpty( $processed, 'Comment should no have been processed on index.php' );
51+
52+
$_SERVER['SCRIPT_NAME'] = '';
53+
$result = Comment::process( $comment );
54+
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on invalid request' );
55+
self::assertSame( 1, $result['ab_spam__invalid_request'], 'Invalid request not detected' );
56+
self::assertEmpty( $processed, 'Comment should no have been processed on invalid request' );
57+
58+
$_SERVER['SCRIPT_NAME'] = '/wp-comments-post.php';
59+
$result = Comment::process( $comment );
60+
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on invalid request' );
61+
self::assertArrayNotHasKey( 'processed', $result, 'Comment should no have been processed without POST data' );
62+
63+
$_POST = 'test me';
64+
$result = Comment::process( $comment );
65+
self::assertSame( [ $result ], $processed, 'Comment was not processed' );
66+
67+
$comment = [ 'comment_type' => 'linkback' ];
68+
$result = Comment::process( $comment );
69+
self::assertSame( $comment, $result, 'Linkback should not be modified by comment handler' );
70+
}
71+
}

tests/Unit/Rules/HoneypotTest.php

+57
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use AntispamBee\Rules\Honeypot;
66

7+
use function Brain\Monkey\Functions\stubs;
8+
79
/**
810
* Unit tests for {@see Honeypot}.
911
*
@@ -35,4 +37,59 @@ public function test_init() {
3537
'comment_form_field_comment filter was not added'
3638
);
3739
}
40+
41+
public function test_precheck() {
42+
global $_POST;
43+
global $_SERVER;
44+
45+
stubs(
46+
[
47+
'esc_url_raw' => function (string $url) {
48+
return $url;
49+
},
50+
'is_feed' => false,
51+
'is_trackback' => false,
52+
'wp_parse_url' => 'parse_url',
53+
'wp_unslash' => function ($value) {
54+
return $value;
55+
},
56+
]
57+
);
58+
mock( 'overload:' . \AntispamBee\Helpers\Honeypot::class )
59+
->expects( 'get_secret_name_for_post' )
60+
->andReturns( 'my-secret' );
61+
62+
$_POST = [];
63+
$_SERVER = [ 'SCRIPT_NAME' => '/index.php' ];
64+
65+
Honeypot::precheck();
66+
self::assertEmpty( $_POST, 'Empty POST data modified unexpectedly' );
67+
68+
$_POST = [ 'foo' => 'bar' ];
69+
Honeypot::precheck();
70+
self::assertSame( ['foo' => 'bar' ], $_POST, 'POST data modified on index.php' );
71+
72+
$_SERVER = [ 'SCRIPT_NAME' => '/wp-comments-post.php' ];
73+
Honeypot::precheck();
74+
self::assertSame( 1, $_POST[ 'ab_spam__invalid_request' ], 'request without missing fields not detected' );
75+
76+
$_POST = [
77+
'my-secret' => 'S3cr3t',
78+
'my-hidden' => 'H1dd3n',
79+
];
80+
Honeypot::precheck();
81+
self::assertSame( 1, $_POST[ 'ab_spam__hidden_field' ], 'non-empty hidden fiend not detected' );
82+
83+
$_POST = [
84+
'my-secret' => 'S3cr3t',
85+
'my-hidden' => '',
86+
];
87+
Honeypot::precheck();
88+
self::assertSame(
89+
[ 'my-hidden' => 'S3cr3t' ],
90+
$_POST,
91+
'secret was not moved to hidden field'
92+
);
93+
94+
}
3895
}

0 commit comments

Comments
 (0)