Make WordPress Core

Ticket #14601: 14601.3.patch

File 14601.3.patch, 5.8 KB (added by rachelbaker, 11 years ago)

Filters given comment_author_IP and comment_agent values and includes unit tests

Line 
1Index: src/wp-includes/comment.php
2===================================================================
3--- src/wp-includes/comment.php (revision 32691)
4+++ src/wp-includes/comment.php (working copy)
5@@ -2304,9 +2304,18 @@
6 $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
7 $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
8
9- $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
10- $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
11+ if ( isset( $commentdata['comment_author_IP'] ) ) {
12+ $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] );
13+ } else {
14+ $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
15+ }
16
17+ if ( isset( $commentdata['comment_agent'] ) ) {
18+ $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 );
19+ } else {
20+ $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
21+ }
22+
23 if ( empty( $commentdata['comment_date'] ) ) {
24 $commentdata['comment_date'] = current_time('mysql');
25 }
26Index: tests/phpunit/tests/comment.php
27===================================================================
28--- tests/phpunit/tests/comment.php (revision 32691)
29+++ tests/phpunit/tests/comment.php (working copy)
30@@ -113,6 +113,130 @@
31 }
32 }
33
34+ /**
35+ * @ticket 14601
36+ */
37+ public function test_wp_new_comment_respects_author_ip() {
38+ $u = $this->factory->user->create();
39+ $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
40+
41+ $data = array(
42+ 'comment_post_ID' => $post_id,
43+ 'comment_author' => rand_str(),
44+ 'comment_author_IP' => '192.168.1.1',
45+ 'comment_author_url' => '',
46+ 'comment_author_email' => '',
47+ 'comment_type' => '',
48+ 'comment_content' => rand_str(),
49+ );
50+
51+ $id = wp_new_comment( $data );
52+
53+ $comment = get_comment( $id );
54+
55+ $this->assertEquals( $data['comment_author_IP'], $comment->comment_author_IP );
56+ }
57+
58+ /**
59+ * @ticket 14601
60+ */
61+ public function test_wp_new_comment_respects_author_ip_empty_string() {
62+ $u = $this->factory->user->create();
63+ $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
64+
65+ $data = array(
66+ 'comment_post_ID' => $post_id,
67+ 'comment_author' => rand_str(),
68+ 'comment_author_IP' => '',
69+ 'comment_author_url' => '',
70+ 'comment_author_email' => '',
71+ 'comment_type' => '',
72+ 'comment_content' => rand_str(),
73+ );
74+
75+ $id = wp_new_comment( $data );
76+
77+ $comment = get_comment( $id );
78+
79+ $this->assertEquals( $data['comment_author_IP'], $comment->comment_author_IP );
80+ }
81+
82+ /**
83+ * @ticket 14601
84+ */
85+ public function test_wp_new_comment_respects_comment_agent() {
86+ $u = $this->factory->user->create();
87+ $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
88+
89+ $data = array(
90+ 'comment_post_ID' => $post_id,
91+ 'comment_author' => rand_str(),
92+ 'comment_author_IP' => '',
93+ 'comment_author_url' => '',
94+ 'comment_author_email' => '',
95+ 'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53',
96+ 'comment_type' => '',
97+ 'comment_content' => rand_str(),
98+ );
99+
100+ $id = wp_new_comment( $data );
101+
102+ $comment = get_comment( $id );
103+
104+ $this->assertEquals( $data['comment_agent'], $comment->comment_agent );
105+ }
106+
107+ /**
108+ * @ticket 14601
109+ */
110+ public function test_wp_new_comment_respects_comment_agent_length() {
111+ $u = $this->factory->user->create();
112+ $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
113+
114+ $data = array(
115+ 'comment_post_ID' => $post_id,
116+ 'comment_author' => rand_str(),
117+ 'comment_author_IP' => '',
118+ 'comment_author_url' => '',
119+ 'comment_author_email' => '',
120+ 'comment_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en; rv:1.8.1.4pre) Gecko/20070511 Camino/1.6pre',
121+ 'comment_type' => '',
122+ 'comment_content' => rand_str(),
123+ );
124+
125+ $id = wp_new_comment( $data );
126+
127+ $comment = get_comment( $id );
128+
129+ $this->assertEquals( 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16 Mozilla/5.0 (Macintosh; U; PPC Mac OS ', $comment->comment_agent );
130+ }
131+
132+ /**
133+ * @ticket 14601
134+ */
135+ public function test_wp_new_comment_respects_comment_agent_empty_string() {
136+ $u = $this->factory->user->create();
137+ $post_id = $this->factory->post->create( array( 'post_author' => $u ) );
138+
139+ $data = array(
140+ 'comment_post_ID' => $post_id,
141+ 'comment_author' => rand_str(),
142+ 'comment_author_IP' => '',
143+ 'comment_author_url' => '',
144+ 'comment_author_email' => '',
145+ 'comment_agent' => '',
146+ 'comment_type' => '',
147+ 'comment_content' => rand_str(),
148+ );
149+
150+ $id = wp_new_comment( $data );
151+
152+ $comment = get_comment( $id );
153+
154+ $this->assertEquals( $data['comment_agent'], $comment->comment_agent );
155+ }
156+
157+
158 public function test_comment_field_lengths() {
159 // `wp_new_comment()` checks REMOTE_ADDR, so we fake it to avoid PHP notices.
160 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {