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

PreparedSQLSniff: Trigger on variables in sql in heredocs and ignore nowdoc syntax #880

Merged
merged 1 commit into from
Mar 21, 2017
Merged
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
PreparedSQLSniff: Trigger on variables in sql in heredocs and ignore …
…nowdoc syntax

Includes unit tests.

Also:
* Adds unit tests for multi-line `T_DOUBLE_QUOTED_STRING` and `T_CONSTANT_ENCAPSED_STRING` which were missing
jrfnl committed Mar 21, 2017
commit ee038fe826225f01b86bcae104f0dbe001c81cbb
9 changes: 8 additions & 1 deletion WordPress/Sniffs/WP/PreparedSQLSniff.php
Original file line number Diff line number Diff line change
@@ -55,6 +55,11 @@ class WordPress_Sniffs_WP_PreparedSQLSniff extends WordPress_Sniff {
T_CLOSE_SQUARE_BRACKET => true,
T_COMMA => true,
T_LNUMBER => true,
T_START_HEREDOC => true,
T_END_HEREDOC => true,
T_START_NOWDOC => true,
T_NOWDOC => true,
T_END_NOWDOC => true,
);

/**
@@ -123,7 +128,9 @@ public function process_token( $stackPtr ) {
continue;
}

if ( T_DOUBLE_QUOTED_STRING === $this->tokens[ $this->i ]['code'] ) {
if ( T_DOUBLE_QUOTED_STRING === $this->tokens[ $this->i ]['code']
|| T_HEREDOC === $this->tokens[ $this->i ]['code']
) {

$bad_variables = array_filter(
$this->get_interpolated_variables( $this->tokens[ $this->i ]['content'] ),
50 changes: 50 additions & 0 deletions WordPress/Tests/WP/PreparedSQLUnitTest.inc
Original file line number Diff line number Diff line change
@@ -31,3 +31,53 @@ $all_post_meta = $wpdb->get_results( $wpdb->prepare( sprintf(

$wpdb->query( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . esc_sql( $foo ) . "';" ); // Ok.
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . absint( $foo ) . ";" ); // Ok.

// Test multi-line strings.
$all_post_meta = $wpdb->get_results( $wpdb->prepare( sprintf(
'SELECT `post_id`, `meta_value`
FROM `%s`
WHERE `meta_key` = "sort_order"
AND `post_id` IN (%s)',
$wpdb->postmeta,
implode( ',', array_fill( 0, count( $post_ids ), '%d' ) )
), $post_ids ) ); // Ok.

$wpdb->query( "
SELECT *
FROM $wpdb->posts
WHERE post_title LIKE '" . esc_sql( $foo ) . "';"
); // Ok.

$wpdb->query( $wpdb->prepare( "
SELECT *
FROM $wpdb->posts
WHERE post_title = 'The \\$_GET[foo]// var is evil again.'
AND ID = %s",
array( 123 )
) ); // Bad.


// Test heredoc & nowdoc for query.
$wpdb->query( <<<EOT
SELECT *
FROM {$wpdb->posts}
WHERE ID = {$foo};
EOT
); // Bad.

$wpdb->query( <<<"HD"
SELECT *
FROM {$wpdb->posts}
WHERE post_title LIKE '{$var}';
HD
); // Bad.

$all_post_meta = $wpdb->get_results( $wpdb->prepare( sprintf( <<<'ND'
SELECT `post_id`, `meta_value`
FROM `%s`
WHERE `meta_key` = "sort_order"
AND `post_id` IN (%s)
ND
, $wpdb->postmeta,
implode( ',', array_fill( 0, count( $post_ids ), '%d' ) )
), $post_ids ) ); // OK.
22 changes: 21 additions & 1 deletion WordPress/Tests/WP/PreparedSQLUnitTest.php
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ class WordPress_Tests_WP_PreparedSQLUnitTest extends AbstractSniffUnitTest {
* @return array <int line number> => <int number of errors>
*/
public function getErrorList() {
return array(
$errors = array(
3 => 1,
4 => 1,
5 => 1,
@@ -34,7 +34,27 @@ public function getErrorList() {
18 => 1,
20 => 1,
21 => 1,
54 => 1,
64 => 1,
71 => 1,
);

// Deal with PHP 5.2 not recognizing quoted heredoc openers, nor nowdoc syntax.
// These are all false positives!
if ( PHP_VERSION_ID < 50300 ) {
$errors[68] = 2;
$errors[69] = 2;
$errors[70] = 2;
$errors[71] = 4;
$errors[75] = 2;
$errors[76] = 7;
$errors[77] = 4;
$errors[78] = 5;
$errors[79] = 7;
$errors[80] = 1;
}

return $errors;
}

/**