From 539dfac41c3cfd8add0720466140f6a5901b2c72 Mon Sep 17 00:00:00 2001 From: "vivi-the-going-merry[bot]" <308115520+vivi-the-going-merry[bot]@users.noreply.github.com> Date: Wed, 16 Sep 2026 20:04:03 -0600 Subject: [PATCH 1/5] Use %i identifier placeholder in db queries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Converts interpolated/concatenated table names in core and Stripe SQL queries to $wpdb->prepare()'s %i placeholder, adds a PHPCS sniff (Formidable.Security.PreferIdentifierPlaceholder) to catch regressions, and covers it with unit tests. Rebased onto current master (fresh-start: checked out origin/master and applied the PR's own diff via git apply --3way) — the branch was 405 commits behind and GitHub reported it as unmergeable. Several files in the original PR (FrmFormsListHelper.php's embed-post query and its test, FrmTransLiteListHelper.php's test file, and test_FrmStyle.php's numberposts bound) had already been fixed or superseded independently on master since this PR was opened, so those hunks were dropped as no-ops rather than reverting master's own since-changed code. Also fixes, per Franky's review round: - tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php: skip the class instead of fataling when the PHPCS dev dependency isn't installed (WP-test CI jobs don't have the lint job's vendor/ tree). - PreferIdentifierPlaceholderSniff.php: mark a ref unfixable when its opening backtick doesn't close in the same concatenation part, so apply_fix() can no longer rewrite it and leave a stray backtick (CodeRabbit); drop apply_fix()'s three unused parameters (CodeRabbit). Closes #3223. --- .../controllers/FrmWelcomeTourController.php | 2 +- classes/helpers/FrmEmailSummaryHelper.php | 8 +- classes/models/FrmDb.php | 3 +- classes/models/FrmEntry.php | 4 +- classes/models/FrmEntryMeta.php | 9 +- classes/models/FrmField.php | 8 +- classes/models/FrmForm.php | 9 +- classes/models/FrmMigrate.php | 26 +- classes/models/FrmStyle.php | 2 +- .../PreferIdentifierPlaceholderSniff.php | 997 ++++++++++++++++++ phpcs-sniffs/Formidable/ruleset.xml | 1 + .../FrmStrpLiteEventsController.php | 3 +- .../FrmTransLiteCRUDController.php | 14 +- .../FrmTransLiteSubscriptionsController.php | 2 +- stripe/helpers/FrmTransLiteListHelper.php | 46 +- stripe/models/FrmTransLiteDb.php | 50 +- stripe/models/FrmTransLiteSubscription.php | 3 +- .../prefer-identifier-placeholder-ruleset.xml | 5 + ...st_FrmPreferIdentifierPlaceholderSniff.php | 314 ++++++ 19 files changed, 1411 insertions(+), 95 deletions(-) create mode 100644 phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php create mode 100644 tests/phpunit/misc/fixtures/prefer-identifier-placeholder-ruleset.xml create mode 100644 tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php diff --git a/classes/controllers/FrmWelcomeTourController.php b/classes/controllers/FrmWelcomeTourController.php index 3c9e347063..2a92fb5eba 100644 --- a/classes/controllers/FrmWelcomeTourController.php +++ b/classes/controllers/FrmWelcomeTourController.php @@ -435,7 +435,7 @@ private static function more_than_the_default_form_exists() { */ private static function check_for_form_embeds() { global $wpdb; - $result = $wpdb->get_var( "SELECT 1 FROM {$wpdb->posts} WHERE post_content LIKE '%[formidable %' LIMIT 1" ); + $result = $wpdb->get_var( $wpdb->prepare( 'SELECT 1 FROM %i WHERE post_content LIKE %s LIMIT 1', $wpdb->posts, '%[formidable %' ) ); return '1' === $result; } diff --git a/classes/helpers/FrmEmailSummaryHelper.php b/classes/helpers/FrmEmailSummaryHelper.php index 40c7147960..ccd756aef5 100644 --- a/classes/helpers/FrmEmailSummaryHelper.php +++ b/classes/helpers/FrmEmailSummaryHelper.php @@ -330,10 +330,12 @@ public static function get_top_forms( $from_date, $to_date, $limit = 5 ) { $result = $wpdb->get_results( $wpdb->prepare( - "SELECT fr.id AS form_id, fr.name AS form_name, COUNT(*) as items_count - FROM {$wpdb->prefix}frm_items AS it INNER JOIN {$wpdb->prefix}frm_forms AS fr ON it.form_id = fr.id + 'SELECT fr.id AS form_id, fr.name AS form_name, COUNT(*) as items_count + FROM %i AS it INNER JOIN %i AS fr ON it.form_id = fr.id WHERE it.created_at BETWEEN %s AND %s AND it.is_draft = 0 AND parent_form_id = 0 - GROUP BY form_id ORDER BY items_count DESC LIMIT %d", + GROUP BY form_id ORDER BY items_count DESC LIMIT %d', + $wpdb->prefix . 'frm_items', + $wpdb->prefix . 'frm_forms', $from_date, $to_date . ' 23:59:59', intval( $limit ) diff --git a/classes/models/FrmDb.php b/classes/models/FrmDb.php index 72bb7a5903..72184c5043 100644 --- a/classes/models/FrmDb.php +++ b/classes/models/FrmDb.php @@ -823,8 +823,7 @@ public static function cache_delete_group( $group ) { public static function db_column_exists( $table, $column ) { global $wpdb; - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - $result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM ' . $wpdb->prefix . $table . ' LIKE %s', $column ) ); + $result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM %i LIKE %s', $wpdb->prefix . $table, $column ) ); return ! empty( $result ); } } diff --git a/classes/models/FrmEntry.php b/classes/models/FrmEntry.php index fcee5694d3..e576e85196 100644 --- a/classes/models/FrmEntry.php +++ b/classes/models/FrmEntry.php @@ -383,8 +383,8 @@ public static function destroy( $id ) { */ do_action( 'frm_before_destroy_entry', $id, $entry ); - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE item_id=%d', $id ) ); - $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) ); + $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE item_id=%d', $wpdb->prefix . 'frm_item_metas', $id ) ); + $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE id=%d', $wpdb->prefix . 'frm_items', $id ) ); self::clear_cache(); diff --git a/classes/models/FrmEntryMeta.php b/classes/models/FrmEntryMeta.php index c01c2267bc..95eb2346c8 100644 --- a/classes/models/FrmEntryMeta.php +++ b/classes/models/FrmEntryMeta.php @@ -198,9 +198,10 @@ public static function update_entry_metas( $entry_id, $values ) { 'field_id' => $field_ids_to_remove, ); FrmDb::get_where_clause_and_values( $where ); + array_unshift( $where['values'], $wpdb->prefix . 'frm_item_metas' ); // Delete any leftovers - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong + $wpdb->query( $wpdb->prepare( 'DELETE FROM %i ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared self::clear_cache(); } @@ -239,7 +240,7 @@ public static function delete_entry_meta( $entry_id, $field_id ) { global $wpdb; self::clear_cache(); - return $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_item_metas WHERE field_id=%d AND item_id=%d", $field_id, $entry_id ) ); + return $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE field_id=%d AND item_id=%d', $wpdb->prefix . 'frm_item_metas', $field_id, $entry_id ) ); } /** @@ -369,7 +370,7 @@ private static function meta_field_query( $field_id, $order, $limit, $args, arra if ( is_numeric( $field_id ) ) { $query[] = $wpdb->prepare( 'WHERE em.field_id=%d', $field_id ); } else { - $query[] = $wpdb->prepare( 'LEFT JOIN ' . $wpdb->prefix . 'frm_fields fi ON (em.field_id = fi.id) WHERE fi.field_key=%s', $field_id ); + $query[] = $wpdb->prepare( 'LEFT JOIN %i fi ON (em.field_id = fi.id) WHERE fi.field_key=%s', $wpdb->prefix . 'frm_fields', $field_id ); } if ( ! $args['is_draft'] ) { @@ -649,7 +650,7 @@ public static function search_entry_metas( $search, $field_id, $operator ) { $search = '%' . $search . '%'; } - $query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value {$operator} %s and field_id = %d", $search, $field_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong + $query = $wpdb->prepare( "SELECT DISTINCT item_id FROM %i WHERE meta_value {$operator} %s and field_id = %d", $wpdb->prefix . 'frm_item_metas', $search, $field_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong }//end if $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared diff --git a/classes/models/FrmField.php b/classes/models/FrmField.php index 9b583ce6e0..68f0e44a46 100644 --- a/classes/models/FrmField.php +++ b/classes/models/FrmField.php @@ -847,9 +847,9 @@ public static function destroy( $id ) { self::delete_form_transient( $field->form_id ); - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id=%d', $id ) ); + $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE field_id=%d', $wpdb->prefix . 'frm_item_metas', $id ) ); - return $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_fields WHERE id=%d', $id ) ); + return $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE id=%d', $wpdb->prefix . 'frm_fields', $id ) ); } /** @@ -865,7 +865,7 @@ public static function delete_form_transient( $form_id ) { delete_transient( 'frm_form_fields_' . $form_id . 'excludeexclude' ); global $wpdb; - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s', '_transient_timeout_frm_form_fields_' . $form_id . 'ex%', '_transient_frm_form_fields_' . $form_id . 'ex%', '_transient_timeout_frm_form_fields_' . $form_id . 'in%', '_transient_frm_form_fields_' . $form_id . 'in%' ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong + $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s', $wpdb->options, '_transient_timeout_frm_form_fields_' . $form_id . 'ex%', '_transient_frm_form_fields_' . $form_id . 'ex%', '_transient_timeout_frm_form_fields_' . $form_id . 'in%', '_transient_frm_form_fields_' . $form_id . 'in%' ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong FrmDb::cache_delete_group( 'frm_field' ); @@ -903,7 +903,7 @@ public static function getOne( $id, $filter = false ) { global $wpdb; $where = is_numeric( $id ) ? 'id=%d' : 'field_key=%s'; - $query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'frm_fields WHERE ' . $where, $id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + $query = $wpdb->prepare( 'SELECT * FROM %i WHERE ' . $where, $wpdb->prefix . 'frm_fields', $id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $results = FrmDb::check_cache( $id, 'frm_field', $query, 'get_row', 0 ); diff --git a/classes/models/FrmForm.php b/classes/models/FrmForm.php index dc65989d3a..ce37814e95 100644 --- a/classes/models/FrmForm.php +++ b/classes/models/FrmForm.php @@ -638,9 +638,10 @@ public static function set_status( $id, $status ) { 'or' => 1, ); FrmDb::get_where_clause_and_values( $where ); - array_unshift( $where['values'], $status ); + array_unshift( $where['values'], $wpdb->prefix . 'frm_forms', $status ); - $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET status = %s ' . $where['where'], $where['values'] ) ); } else { $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) ); $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) ); @@ -731,9 +732,9 @@ public static function destroy( $id ) { } // Disconnect the fields from this form - $wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong + $wpdb->query( $wpdb->prepare( 'DELETE fi FROM %i AS fi LEFT JOIN %i fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $wpdb->prefix . 'frm_fields', $wpdb->prefix . 'frm_forms', $id, $id ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong - $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); + $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE id=%d OR parent_form_id=%d', $wpdb->prefix . 'frm_forms', $id, $id ) ); if ( ! $query_results ) { return $query_results; diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index fb3bd75a40..d166a3ab17 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -302,28 +302,28 @@ private function add_composite_indexes_for_entries() { $index_name = 'idx_is_draft_created_at'; if ( ! self::index_exists( $table_name, $index_name ) ) { - $wpdb->query( "CREATE INDEX idx_is_draft_created_at ON `{$wpdb->prefix}frm_items` (is_draft, created_at)" ); + $wpdb->query( $wpdb->prepare( 'CREATE INDEX idx_is_draft_created_at ON %i (is_draft, created_at)', $table_name ) ); } $table_name = "{$wpdb->prefix}frm_item_metas"; $index_name = 'idx_field_id_item_id'; if ( ! self::index_exists( $table_name, $index_name ) ) { - $wpdb->query( "CREATE INDEX idx_field_id_item_id ON `{$wpdb->prefix}frm_item_metas` (field_id, item_id)" ); + $wpdb->query( $wpdb->prepare( 'CREATE INDEX idx_field_id_item_id ON %i (field_id, item_id)', $table_name ) ); } $table_name = "{$wpdb->prefix}frm_items"; $index_name = 'idx_form_id_is_draft'; if ( ! self::index_exists( $table_name, $index_name ) ) { - $wpdb->query( "CREATE INDEX idx_form_id_is_draft ON `{$wpdb->prefix}frm_items` (form_id, is_draft)" ); + $wpdb->query( $wpdb->prepare( 'CREATE INDEX idx_form_id_is_draft ON %i (form_id, is_draft)', $table_name ) ); } $table_name = "{$wpdb->prefix}frm_fields"; $index_name = 'idx_form_id_type'; if ( ! self::index_exists( $table_name, $index_name ) ) { - $wpdb->query( "CREATE INDEX idx_form_id_type ON `{$wpdb->prefix}frm_fields` (form_id, type(30))" ); + $wpdb->query( $wpdb->prepare( 'CREATE INDEX idx_form_id_type ON %i (form_id, type(30))', $table_name ) ); } } @@ -428,11 +428,11 @@ public function uninstall() { global $wpdb, $wp_roles; - $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->fields ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->forms ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->entries ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->entry_metas ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->gated_tokens ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $this->fields ) ); + $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $this->forms ) ); + $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $this->entries ) ); + $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $this->entry_metas ) ); + $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $this->gated_tokens ) ); delete_option( 'frm_options' ); delete_option( 'frm_db_version' ); @@ -463,7 +463,7 @@ public function uninstall() { remove_action( 'before_delete_post', 'FrmProDisplaysController::before_delete_post' ); remove_action( 'deleted_post', 'FrmProEntriesController::delete_entry' ); - $post_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type in (%s, %s, %s)', FrmFormActionsController::$action_post_type, FrmStylesController::$post_type, 'frm_display' ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong + $post_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM %i WHERE post_type in (%s, %s, %s)', $wpdb->posts, FrmFormActionsController::$action_post_type, FrmStylesController::$post_type, 'frm_display' ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong foreach ( $post_ids as $post_id ) { // Delete's each post. @@ -477,7 +477,7 @@ public function uninstall() { delete_transient( 'frmpro_options' ); delete_transient( FrmOnboardingWizardController::TRANSIENT_NAME ); - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE %s OR option_name LIKE %s', '_transient_timeout_frm_form_fields_%', '_transient_frm_form_fields_%' ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong + $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE option_name LIKE %s OR option_name LIKE %s', $wpdb->options, '_transient_timeout_frm_form_fields_%', '_transient_frm_form_fields_%' ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong do_action( 'frm_after_uninstall' ); @@ -759,10 +759,10 @@ private function migrate_to_25() { */ private function migrate_to_23() { global $wpdb; - $exists = $wpdb->get_row( 'SHOW COLUMNS FROM ' . $this->forms . ' LIKE "parent_form_id"' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + $exists = $wpdb->get_row( $wpdb->prepare( 'SHOW COLUMNS FROM %i LIKE %s', $this->forms, 'parent_form_id' ) ); if ( ! $exists ) { - $wpdb->query( 'ALTER TABLE ' . $this->forms . ' ADD parent_form_id int(11) default 0' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD parent_form_id int(11) default 0', $this->forms ) ); } } diff --git a/classes/models/FrmStyle.php b/classes/models/FrmStyle.php index 7cb8e3bb13..1ae46cc204 100644 --- a/classes/models/FrmStyle.php +++ b/classes/models/FrmStyle.php @@ -588,7 +588,7 @@ public function get_all( $orderby = 'title', $order = 'ASC', $limit = 99 ) { if ( ! $temp_styles ) { global $wpdb; // Make sure there wasn't a conflict with the query - $query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->posts . ' WHERE post_type=%s AND post_status=%s ORDER BY post_title ASC LIMIT 99', FrmStylesController::$post_type, 'publish' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong + $query = $wpdb->prepare( 'SELECT * FROM %i WHERE post_type=%s AND post_status=%s ORDER BY post_title ASC LIMIT 99', $wpdb->posts, FrmStylesController::$post_type, 'publish' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong $temp_styles = FrmDb::check_cache( 'frm_backup_style_check', 'frm_styles', $query, 'get_results' ); if ( ! $temp_styles ) { diff --git a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php new file mode 100644 index 0000000000..cdd4096720 --- /dev/null +++ b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php @@ -0,0 +1,997 @@ +query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_items WHERE id = %d", $id ) ); + * $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id = %d', $id ) ); + * $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->table ); + * + * Good: + * $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE id = %d', $wpdb->prefix . 'frm_items', $id ) ); + * $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $this->table ) ); + * + * CREATE TABLE statements are exempt because they are built for dbDelta(), which cannot use prepare(). + */ +class PreferIdentifierPlaceholderSniff implements Sniff { + + /** + * SQL keywords that are directly followed by a table identifier. + * + * A bare ON is deliberately excluded because join conditions follow it with column + * references. Only the CREATE INDEX ... ON form takes a table there. + * + * @var string + */ + const TABLE_KEYWORDS = 'FROM|JOIN|INTO|UPDATE|TABLE|EXISTS|INDEX\s+\S+\s+ON'; + + /** + * Regex fragment matching one interpolated segment inside a double quoted string. + * + * @var string + */ + const INTERP_SEGMENT = '(?:\{\$[^}]+\}|\$[A-Za-z_]\w*(?:->\w+)*)'; + + /** + * $wpdb methods that receive raw SQL as their first argument. + * + * @var array + */ + private $queryMethods = array( + 'query', + 'get_var', + 'get_row', + 'get_col', + 'get_results', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + return array( T_VARIABLE ); + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the stack passed in $tokens. + * + * @return void + */ + public function process( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + + if ( $tokens[ $stackPtr ]['content'] !== '$wpdb' ) { + return; + } + + $objectOp = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); + + if ( false === $objectOp || $tokens[ $objectOp ]['code'] !== T_OBJECT_OPERATOR ) { + return; + } + + $methodToken = $phpcsFile->findNext( T_WHITESPACE, $objectOp + 1, null, true ); + + if ( false === $methodToken || $tokens[ $methodToken ]['code'] !== T_STRING ) { + return; + } + + $methodName = $tokens[ $methodToken ]['content']; + $isPrepare = 'prepare' === $methodName; + + if ( ! $isPrepare && ! in_array( $methodName, $this->queryMethods, true ) ) { + return; + } + + $openParen = $phpcsFile->findNext( T_WHITESPACE, $methodToken + 1, null, true ); + + if ( false === $openParen || $tokens[ $openParen ]['code'] !== T_OPEN_PARENTHESIS || ! isset( $tokens[ $openParen ]['parenthesis_closer'] ) ) { + return; + } + + $closeParen = $tokens[ $openParen ]['parenthesis_closer']; + + if ( ! $isPrepare && $this->first_arg_is_wpdb_call( $phpcsFile, $openParen ) ) { + // The inner $wpdb->prepare() call is processed on its own token. + return; + } + + $argEnd = $this->find_first_arg_end( $phpcsFile, $openParen, $closeParen ); + $parts = $this->parse_concat_parts( $phpcsFile, $openParen + 1, $argEnd ); + + if ( ! $parts ) { + return; + } + + if ( $this->is_create_table_statement( $parts ) ) { + return; + } + + $refs = $this->find_table_refs( $parts ); + + if ( ! $refs ) { + return; + } + + if ( ! $isPrepare ) { + $phpcsFile->addError( + 'Table name built into SQL passed to $wpdb->%s() without prepare(). Wrap the query in $wpdb->prepare() and use the %%i placeholder for the identifier.', + $refs[0]['ptr'], + 'TableNotPrepared', + array( $methodName ) + ); + + return; + } + + $this->handle_prepare_refs( $phpcsFile, $parts, $refs, $openParen, $argEnd, $closeParen ); + } + + /** + * Checks whether the first argument of a call starts with another $wpdb method call. + * + * @param File $phpcsFile The file being scanned. + * @param int $openParen The opening parenthesis of the outer call. + * + * @return bool + */ + private function first_arg_is_wpdb_call( File $phpcsFile, $openParen ) { + $tokens = $phpcsFile->getTokens(); + $firstArg = $phpcsFile->findNext( T_WHITESPACE, $openParen + 1, null, true ); + + return false !== $firstArg && $tokens[ $firstArg ]['code'] === T_VARIABLE && $tokens[ $firstArg ]['content'] === '$wpdb'; + } + + /** + * Finds the token position that ends the first argument (the first top level comma, or the closing parenthesis). + * + * @param File $phpcsFile The file being scanned. + * @param int $openParen The opening parenthesis of the call. + * @param int $closeParen The closing parenthesis of the call. + * + * @return int + */ + private function find_first_arg_end( File $phpcsFile, $openParen, $closeParen ) { + $tokens = $phpcsFile->getTokens(); + + for ( $i = $openParen + 1; $i < $closeParen; $i++ ) { + $i = $this->skip_nested( $tokens, $i ); + + if ( $i >= $closeParen ) { + break; + } + + if ( $tokens[ $i ]['code'] === T_COMMA ) { + return $i; + } + } + + return $closeParen; + } + + /** + * Skips over nested parentheses and bracket structures. + * + * @param array $tokens The token stack. + * @param int $i The current token position. + * + * @return int The position to continue from (the closer when a nested structure starts at $i). + */ + private function skip_nested( $tokens, $i ) { + if ( $tokens[ $i ]['code'] === T_OPEN_PARENTHESIS && isset( $tokens[ $i ]['parenthesis_closer'] ) ) { + return $tokens[ $i ]['parenthesis_closer']; + } + + if ( isset( $tokens[ $i ]['bracket_closer'] ) && in_array( $tokens[ $i ]['code'], array( T_OPEN_SQUARE_BRACKET, T_OPEN_SHORT_ARRAY, T_OPEN_CURLY_BRACKET ), true ) ) { + return $tokens[ $i ]['bracket_closer']; + } + + if ( $tokens[ $i ]['code'] === T_ARRAY && isset( $tokens[ $i ]['parenthesis_closer'] ) ) { + return $tokens[ $i ]['parenthesis_closer']; + } + + return $i; + } + + /** + * Parses an expression token range into concatenation parts. + * + * Each part is array( 'type' => 'sq'|'dq'|'expr', 'start' => int, 'end' => int, 'content' => string ). + * For string parts, content is the inner string without the surrounding quotes. + * For expr parts, content is the raw PHP code. + * + * @param File $phpcsFile The file being scanned. + * @param int $start The first token of the expression. + * @param int $end The token after the last token of the expression. + * + * @return array|false + */ + private function parse_concat_parts( File $phpcsFile, $start, $end ) { + $tokens = $phpcsFile->getTokens(); + $parts = array(); + $run = array(); + + for ( $i = $start; $i < $end; $i++ ) { + $code = $tokens[ $i ]['code']; + + if ( T_WHITESPACE === $code || T_COMMENT === $code ) { + continue; + } + + if ( T_STRING_CONCAT === $code ) { + $part = $this->close_part( $phpcsFile, $run ); + + if ( ! $part ) { + return false; + } + + $parts[] = $part; + $run = array(); + continue; + } + + $next = $this->skip_nested( $tokens, $i ); + + for ( $j = $i; $j <= $next; $j++ ) { + $run[] = $j; + } + + $i = $next; + } + + $part = $this->close_part( $phpcsFile, $run ); + + if ( ! $part ) { + return false; + } + + $parts[] = $part; + + return $parts; + } + + /** + * Converts a token run into a single concatenation part. + * + * @param File $phpcsFile The file being scanned. + * @param array $run Token positions collected for this part. + * + * @return array|false + */ + private function close_part( File $phpcsFile, $run ) { + if ( ! $run ) { + return false; + } + + $tokens = $phpcsFile->getTokens(); + $start = $run[0]; + $end = $run[ count( $run ) - 1 ]; + + // PHPCS splits multiline strings into one token per line, so a string part + // is any run made up entirely of tokens of the same string type. + $isSingleQuoted = true; + $isDoubleQuoted = true; + $raw = ''; + + foreach ( $run as $ptr ) { + if ( $tokens[ $ptr ]['code'] !== T_CONSTANT_ENCAPSED_STRING ) { + $isSingleQuoted = false; + } + + if ( $tokens[ $ptr ]['code'] !== T_DOUBLE_QUOTED_STRING ) { + $isDoubleQuoted = false; + } + + $raw .= $tokens[ $ptr ]['content']; + } + + if ( $isSingleQuoted || $isDoubleQuoted ) { + $type = "'" === $raw[0] ? 'sq' : 'dq'; + + return array( + 'type' => $type, + 'start' => $start, + 'end' => $end, + 'content' => substr( $raw, 1, -1 ), + ); + } + + return array( + 'type' => 'expr', + 'start' => $start, + 'end' => $end, + 'content' => trim( $phpcsFile->getTokensAsString( $start, $end - $start + 1 ) ), + ); + } + + /** + * Checks whether the SQL starts with CREATE TABLE, which is exempt (dbDelta schema DDL). + * + * @param array $parts The concatenation parts. + * + * @return bool + */ + private function is_create_table_statement( $parts ) { + if ( 'expr' === $parts[0]['type'] ) { + return false; + } + + return (bool) preg_match( '/^\s*CREATE\s+TABLE/i', $parts[0]['content'] ); + } + + /** + * Finds table identifier references in the parsed parts. + * + * Each ref is array( + * 'type' => 'in_string'|'cross_part', + * 'ptr' => int (token to report on), + * 'part' => int (part index for in_string refs, first part index for cross_part refs), + * 'offset' => int (byte offset of the ref inside the string, in_string only), + * 'length' => int (byte length including surrounding backticks, in_string only), + * 'text' => string (the matched identifier text, in_string only), + * 'expr' => string (PHP expression to move into the prepare arguments), + * 'parts' => array (part indexes consumed, cross_part only), + * 'lead_word' => string (leading identifier characters absorbed from the following literal, cross_part only), + * ). + * + * @param array $parts The concatenation parts. + * + * @return array + */ + private function find_table_refs( $parts ) { + $refs = array(); + $count = count( $parts ); + + for ( $p = 0; $p < $count; $p++ ) { + $part = $parts[ $p ]; + + if ( 'dq' === $part['type'] ) { + $refs = array_merge( $refs, $this->find_in_string_refs( $part, $p ) ); + } + + if ( 'expr' === $part['type'] || $p + 1 >= $count || 'expr' !== $parts[ $p + 1 ]['type'] ) { + continue; + } + + if ( ! preg_match( '/\b(?:' . self::TABLE_KEYWORDS . ')\s+`?\s*$/i', $part['content'] ) ) { + continue; + } + + $ref = $this->collect_cross_part_ref( $parts, $p + 1 ); + + if ( $ref ) { + $refs[] = $ref; + } + } + + return $refs; + } + + /** + * Finds table references fully contained inside a double quoted string part. + * + * @param array $part The dq part. + * @param int $p The part index. + * + * @return array + */ + private function find_in_string_refs( $part, $p ) { + $interp = self::INTERP_SEGMENT; + $refRun = '(?:[A-Za-z0-9_]+)?(?:' . $interp . '(?:[A-Za-z0-9_]+)?)+'; + + if ( ! preg_match_all( '/\b(?:' . self::TABLE_KEYWORDS . ')\s+(`?)(' . $refRun . ')(`?)/i', $part['content'], $matches, PREG_OFFSET_CAPTURE ) ) { + return array(); + } + + $refs = array(); + + foreach ( $matches[2] as $index => $match ) { + $text = $match[0]; + $tickBefore = $matches[1][ $index ][0]; + $tickAfter = $matches[3][ $index ][0]; + $startOffset = $matches[1][ $index ][1]; + + if ( '' !== $tickBefore && '' === $tickAfter ) { + // Unbalanced backtick, the identifier continues in another part. Not safely fixable. + $tickAfter = ''; + } + + $refs[] = array( + 'type' => 'in_string', + 'ptr' => $part['start'], + 'part' => $p, + 'offset' => $startOffset, + 'length' => strlen( $tickBefore ) + strlen( $text ) + strlen( $tickAfter ), + 'text' => $text, + 'expr' => $this->segments_to_expression( $text ), + ); + } + + return $refs; + } + + /** + * Collects a table reference that spans concatenation parts, starting at an expr part. + * + * @param array $parts The concatenation parts. + * @param int $start The index of the first expr part of the reference. + * + * @return array|false + */ + private function collect_cross_part_ref( $parts, $start ) { + $count = count( $parts ); + $exprBits = array(); + $consumed = array(); + $leadWord = ''; + $p = $start; + + while ( $p < $count ) { + $part = $parts[ $p ]; + + if ( 'expr' === $part['type'] ) { + $exprBits[] = $part['content']; + $consumed[] = $p; + $p++; + continue; + } + + if ( 'sq' !== $part['type'] ) { + break; + } + + if ( ! preg_match( '/^([A-Za-z0-9_]+)/', $part['content'], $match ) ) { + break; + } + + if ( $match[1] === $part['content'] && $p + 1 < $count && 'expr' === $parts[ $p + 1 ]['type'] ) { + // The whole literal is part of the identifier and it continues with another expression. + $exprBits[] = "'" . $match[1] . "'"; + $consumed[] = $p; + $p++; + continue; + } + + $exprBits[] = "'" . $match[1] . "'"; + $leadWord = $match[1]; + break; + } + + if ( ! $exprBits ) { + return false; + } + + return array( + 'type' => 'cross_part', + 'ptr' => $parts[ $start ]['start'], + 'part' => $start, + 'parts' => $consumed, + 'lead_word' => $leadWord, + 'expr' => implode( ' . ', $exprBits ), + ); + } + + /** + * Converts an interpolated identifier text into an equivalent PHP expression. + * + * @param string $text The identifier text, e.g. "{$wpdb->prefix}frm_items". + * + * @return string + */ + private function segments_to_expression( $text ) { + preg_match_all( '/\{\$([^}]+)\}|\$[A-Za-z_]\w*(?:->\w+)*|[A-Za-z0-9_]+/', $text, $matches, PREG_SET_ORDER ); + + $bits = array(); + + foreach ( $matches as $match ) { + if ( isset( $match[1] ) && '' !== $match[1] ) { + $bits[] = '$' . $match[1]; + } elseif ( '$' === $match[0][0] ) { + $bits[] = $match[0]; + } else { + $bits[] = "'" . $match[0] . "'"; + } + } + + return implode( ' . ', $bits ); + } + + /** + * Reports and, when safe, fixes table references inside a $wpdb->prepare() call. + * + * @param File $phpcsFile The file being scanned. + * @param array $parts The concatenation parts of the first argument. + * @param array $refs The detected table references. + * @param int $openParen The opening parenthesis of the prepare() call. + * @param int $argEnd The end of the first argument (comma or closing parenthesis). + * @param int $closeParen The closing parenthesis of the prepare() call. + * + * @return void + */ + private function handle_prepare_refs( File $phpcsFile, $parts, $refs, $openParen, $argEnd, $closeParen ) { + $fixable = $this->refs_are_fixable( $parts, $refs ); + $args = $fixable ? $this->parse_args( $phpcsFile, $argEnd, $closeParen ) : false; + + if ( $fixable && false !== $args ) { + $placeholderTotal = $this->count_placeholders_in_literals( $parts ); + $fixable = count( $args ) === $placeholderTotal; + } + + if ( ! $fixable || false === $args ) { + foreach ( $refs as $ref ) { + $phpcsFile->addError( + 'Table name built into $wpdb->prepare() SQL. Use the %i placeholder and pass the identifier as a prepare() argument.', + $ref['ptr'], + 'TableInPrepare' + ); + } + + return; + } + + $fix = false; + + foreach ( $refs as $ref ) { + $fix = $phpcsFile->addFixableError( + 'Table name built into $wpdb->prepare() SQL. Use the %i placeholder and pass the identifier as a prepare() argument.', + $ref['ptr'], + 'TableInPrepare' + ) || $fix; + } + + if ( ! $fix ) { + return; + } + + $this->apply_fix( $phpcsFile, $parts, $refs, $openParen, $argEnd, $closeParen, $args ); + } + + /** + * Determines whether the detected references can be fixed automatically. + * + * Fixing is only safe when the whole first argument is made of literals plus the + * reference expressions themselves, so every placeholder can be counted and every + * prepare() argument maps positionally. + * + * @param array $parts The concatenation parts. + * @param array $refs The detected table references. + * + * @return bool + */ + private function refs_are_fixable( $parts, $refs ) { + $refPartIndexes = array(); + + foreach ( $refs as $ref ) { + if ( 'cross_part' === $ref['type'] ) { + foreach ( $ref['parts'] as $p ) { + $refPartIndexes[ $p ] = true; + } + } + } + + foreach ( $parts as $p => $part ) { + if ( 'expr' === $part['type'] && ! isset( $refPartIndexes[ $p ] ) ) { + return false; + } + + if ( 'expr' !== $part['type'] && preg_match( '/%\d+\$/', $part['content'] ) ) { + // Numbered placeholders change argument mapping. Not safely fixable. + return false; + } + + if ( 'dq' === $part['type'] && $this->dq_has_unhandled_interpolation( $parts, $refs, $p ) ) { + return false; + } + } + + return true; + } + + /** + * Checks whether a double quoted part contains interpolation that is not part of a detected reference. + * + * Leftover interpolation before a reference makes placeholder counting unreliable. + * Leftover interpolation after every reference is harmless for argument mapping only + * when it cannot contain placeholders, which cannot be known, so any leftover + * interpolation before the last reference blocks fixing. Leftover interpolation + * after the last reference in the same string is allowed because the placeholder + * positions for every reference are already determined by the literal text before them. + * + * @param array $parts The concatenation parts. + * @param array $refs The detected table references. + * @param int $p The part index to inspect. + * + * @return bool + */ + private function dq_has_unhandled_interpolation( $parts, $refs, $p ) { + $content = $parts[ $p ]['content']; + + if ( ! preg_match_all( '/' . self::INTERP_SEGMENT . '/', $content, $matches, PREG_OFFSET_CAPTURE ) ) { + return false; + } + + $lastRefEnd = -1; + + foreach ( $refs as $ref ) { + if ( 'in_string' === $ref['type'] && $ref['part'] === $p ) { + $lastRefEnd = max( $lastRefEnd, $ref['offset'] + $ref['length'] ); + } + } + + foreach ( $matches[0] as $match ) { + $offset = $match[1]; + $covered = false; + + foreach ( $refs as $ref ) { + if ( 'in_string' === $ref['type'] && $ref['part'] === $p && $offset >= $ref['offset'] && $offset < $ref['offset'] + $ref['length'] ) { + $covered = true; + break; + } + } + + if ( ! $covered && $offset < $lastRefEnd ) { + return true; + } + + if ( ! $covered && -1 === $lastRefEnd ) { + return true; + } + } + + return false; + } + + /** + * Counts value placeholders in all literal parts. + * + * @param array $parts The concatenation parts. + * + * @return int + */ + private function count_placeholders_in_literals( $parts ) { + $count = 0; + + foreach ( $parts as $part ) { + if ( 'expr' !== $part['type'] ) { + $count += $this->count_placeholders( $part['content'] ); + } + } + + return $count; + } + + /** + * Counts value placeholders in a piece of SQL text. + * + * @param string $text The SQL text. + * + * @return int + */ + private function count_placeholders( $text ) { + $text = str_replace( '%%', '', $text ); + + return preg_match_all( '/%[dfFsi]/', $text ); + } + + /** + * Splits the remaining prepare() arguments into token ranges. + * + * @param File $phpcsFile The file being scanned. + * @param int $argEnd The end of the first argument (comma or closing parenthesis). + * @param int $closeParen The closing parenthesis of the call. + * + * @return array|false Array of array( 'start' => int, 'end' => int ), or false when the arguments cannot be parsed. + */ + private function parse_args( File $phpcsFile, $argEnd, $closeParen ) { + $tokens = $phpcsFile->getTokens(); + + if ( $argEnd >= $closeParen ) { + return array(); + } + + $args = array(); + $argStart = false; + $lastReal = false; + + for ( $i = $argEnd + 1; $i < $closeParen; $i++ ) { + $code = $tokens[ $i ]['code']; + + if ( T_WHITESPACE === $code || T_COMMENT === $code ) { + continue; + } + + $next = $this->skip_nested( $tokens, $i ); + + if ( $next !== $i ) { + if ( false === $argStart ) { + $argStart = $i; + } + + $lastReal = $next; + $i = $next; + continue; + } + + if ( T_COMMA === $code ) { + if ( false === $argStart ) { + return false; + } + + $args[] = array( + 'start' => $argStart, + 'end' => $lastReal, + ); + $argStart = false; + $lastReal = false; + continue; + } + + if ( false === $argStart ) { + $argStart = $i; + } + + $lastReal = $i; + } + + if ( false !== $argStart ) { + $args[] = array( + 'start' => $argStart, + 'end' => $lastReal, + ); + } + + return $args; + } + + /** + * Applies the %i fix: rewrites the SQL argument and inserts identifier expressions into the argument list. + * + * @param File $phpcsFile The file being scanned. + * @param array $parts The concatenation parts. + * @param array $refs The detected table references. + * @param int $openParen The opening parenthesis of the call. + * @param int $argEnd The end of the first argument. + * @param int $closeParen The closing parenthesis of the call. + * @param array $args The existing argument token ranges. + * + * @return void + */ + private function apply_fix( File $phpcsFile, $parts, $refs, $openParen, $argEnd, $closeParen, $args ) { + $newParts = $this->build_new_parts( $parts, $refs ); + $insertions = $this->calculate_insertions( $parts, $refs ); + $newSql = $this->render_parts( $newParts ); + + $phpcsFile->fixer->beginChangeset(); + + $sqlStart = $parts[0]['start']; + $sqlEnd = $parts[ count( $parts ) - 1 ]['end']; + + $phpcsFile->fixer->replaceToken( $sqlStart, $newSql ); + + for ( $i = $sqlStart + 1; $i <= $sqlEnd; $i++ ) { + $phpcsFile->fixer->replaceToken( $i, '' ); + } + + // Group insertions by argument slot, keeping reference order. + $bySlot = array(); + + foreach ( $insertions as $insertion ) { + $bySlot[ $insertion['slot'] ][] = $insertion['expr']; + } + + foreach ( $bySlot as $slot => $exprs ) { + $code = implode( ', ', $exprs ); + + if ( isset( $args[ $slot ] ) ) { + $phpcsFile->fixer->addContentBefore( $args[ $slot ]['start'], $code . ', ' ); + } elseif ( $args ) { + $lastArg = $args[ count( $args ) - 1 ]; + $phpcsFile->fixer->addContent( $lastArg['end'], ', ' . $code ); + } else { + $phpcsFile->fixer->addContent( $sqlEnd, ', ' . $code ); + } + } + + $phpcsFile->fixer->endChangeset(); + } + + /** + * Builds the substituted part contents with %i in place of each reference. + * + * @param array $parts The concatenation parts. + * @param array $refs The detected table references. + * + * @return array Array of array( 'type' => 'sq'|'dq', 'content' => string ), expr parts consumed by refs are dropped. + */ + private function build_new_parts( $parts, $refs ) { + $contents = array(); + $types = array(); + $dropped = array(); + $leadTrim = array(); + + foreach ( $parts as $p => $part ) { + $contents[ $p ] = $part['content']; + $types[ $p ] = $part['type']; + } + + foreach ( $refs as $ref ) { + if ( 'in_string' === $ref['type'] ) { + continue; + } + + $firstPart = $ref['parts'][0]; + $prevPart = $firstPart - 1; + + // Replace the trailing whitespace/backtick after the keyword with a single space and %i. + $hadBacktick = (bool) preg_match( '/`\s*$/', $contents[ $prevPart ] ); + $contents[ $prevPart ] = rtrim( $contents[ $prevPart ], " \t\n`" ) . ' %i'; + + foreach ( $ref['parts'] as $p ) { + $dropped[ $p ] = true; + } + + // Strip the absorbed leading word (and a possible closing backtick) from the following literal. + $afterPart = $ref['parts'][ count( $ref['parts'] ) - 1 ] + 1; + + if ( isset( $contents[ $afterPart ] ) && ( '' !== $ref['lead_word'] || $hadBacktick ) ) { + $leadTrim[ $afterPart ] = $ref['lead_word']; + } + } + + foreach ( $leadTrim as $p => $word ) { + if ( '' === $word || 0 === strpos( $contents[ $p ], $word ) ) { + $contents[ $p ] = ltrim( substr( $contents[ $p ], strlen( $word ) ), '`' ); + } + + // The literal was fully absorbed into the identifier expression. + if ( '' === $contents[ $p ] ) { + $dropped[ $p ] = true; + } + } + + // In-string substitutions, applied right to left so offsets stay valid. + $byPart = array(); + + foreach ( $refs as $ref ) { + if ( 'in_string' === $ref['type'] ) { + $byPart[ $ref['part'] ][] = $ref; + } + } + + foreach ( $byPart as $p => $partRefs ) { + usort( + $partRefs, + function ( $a, $b ) { + return $b['offset'] - $a['offset']; + } + ); + + foreach ( $partRefs as $ref ) { + $contents[ $p ] = substr_replace( $contents[ $p ], '%i', $ref['offset'], $ref['length'] ); + } + } + + $newParts = array(); + + foreach ( $contents as $p => $content ) { + if ( isset( $dropped[ $p ] ) ) { + continue; + } + + $type = $types[ $p ]; + + if ( 'dq' === $type && ! preg_match( '/[\$\\\\\']/', $content ) ) { + $type = 'sq'; + } + + $newParts[] = array( + 'type' => $type, + 'content' => $content, + ); + } + + return $newParts; + } + + /** + * Calculates where each reference expression must be inserted in the argument list. + * + * The slot is the number of original value placeholders that appear before the + * reference in the SQL, which equals the index of the original argument the + * expression must be inserted before. + * + * @param array $parts The concatenation parts. + * @param array $refs The detected table references. + * + * @return array Array of array( 'slot' => int, 'expr' => string ) in reference order. + */ + private function calculate_insertions( $parts, $refs ) { + $insertions = array(); + + foreach ( $refs as $ref ) { + $slot = 0; + + if ( 'in_string' === $ref['type'] ) { + $limitPart = $ref['part']; + $limitOffset = $ref['offset']; + } else { + $limitPart = $ref['parts'][0]; + $limitOffset = null; + } + + foreach ( $parts as $p => $part ) { + if ( 'expr' === $part['type'] ) { + continue; + } + + if ( $p > $limitPart ) { + break; + } + + if ( $p === $limitPart ) { + if ( null !== $limitOffset ) { + $slot += $this->count_placeholders( substr( $part['content'], 0, $limitOffset ) ); + } + + break; + } + + $slot += $this->count_placeholders( $part['content'] ); + } + + $insertions[] = array( + 'slot' => $slot, + 'expr' => $ref['expr'], + ); + } + + return $insertions; + } + + /** + * Renders substituted parts back into a single PHP concatenation expression. + * + * @param array $newParts The substituted parts. + * + * @return string + */ + private function render_parts( $newParts ) { + $rendered = array(); + + foreach ( $newParts as $part ) { + if ( 'sq' === $part['type'] ) { + $code = "'" . $part['content'] . "'"; + } else { + $code = '"' . $part['content'] . '"'; + } + + $last = count( $rendered ) - 1; + + if ( $last >= 0 && 'sq' === $part['type'] && "'" === substr( $rendered[ $last ], -1 ) && "'" === $rendered[ $last ][0] ) { + $rendered[ $last ] = substr( $rendered[ $last ], 0, -1 ) . $part['content'] . "'"; + continue; + } + + $rendered[] = $code; + } + + return implode( ' . ', $rendered ); + } +} diff --git a/phpcs-sniffs/Formidable/ruleset.xml b/phpcs-sniffs/Formidable/ruleset.xml index 2b9c8bf6fe..596491c6b2 100644 --- a/phpcs-sniffs/Formidable/ruleset.xml +++ b/phpcs-sniffs/Formidable/ruleset.xml @@ -96,6 +96,7 @@ + diff --git a/stripe/controllers/FrmStrpLiteEventsController.php b/stripe/controllers/FrmStrpLiteEventsController.php index c9921e77cb..baca1a060e 100644 --- a/stripe/controllers/FrmStrpLiteEventsController.php +++ b/stripe/controllers/FrmStrpLiteEventsController.php @@ -196,7 +196,8 @@ private function reset_customer() { } $wpdb->query( $wpdb->prepare( - "DELETE FROM $wpdb->usermeta WHERE meta_value = %s AND meta_key LIKE %s", + 'DELETE FROM %i WHERE meta_value = %s AND meta_key LIKE %s', + $wpdb->usermeta, $customer_id, '_frmstrp_customer_id%' ) diff --git a/stripe/controllers/FrmTransLiteCRUDController.php b/stripe/controllers/FrmTransLiteCRUDController.php index ab066b2276..80a583b2ae 100644 --- a/stripe/controllers/FrmTransLiteCRUDController.php +++ b/stripe/controllers/FrmTransLiteCRUDController.php @@ -65,20 +65,18 @@ public static function show( $id = 0 ) { private static function get_payment_row( $id ) { global $wpdb; - $table_name = self::table_name(); - - // @codingStandardsIgnoreStart return $wpdb->get_row( $wpdb->prepare( - "SELECT + 'SELECT p.*, e.user_id - FROM `{$wpdb->prefix}frm_{$table_name}` p - LEFT JOIN `{$wpdb->prefix}frm_items` e ON p.item_id = e.id - WHERE p.id=%d", + FROM %i p + LEFT JOIN %i e ON p.item_id = e.id + WHERE p.id=%d', + $wpdb->prefix . 'frm_' . self::table_name(), + $wpdb->prefix . 'frm_items', $id ) ); - // @codingStandardsIgnoreEnd } /** diff --git a/stripe/controllers/FrmTransLiteSubscriptionsController.php b/stripe/controllers/FrmTransLiteSubscriptionsController.php index e67ec7e6ed..be29d20fb1 100755 --- a/stripe/controllers/FrmTransLiteSubscriptionsController.php +++ b/stripe/controllers/FrmTransLiteSubscriptionsController.php @@ -55,7 +55,7 @@ public static function show_receipt_link( $subscription ) { public static function show_cancel_link( $sub, $atts = array() ) { if ( ! isset( $sub->user_id ) ) { global $wpdb; - $sub->user_id = $wpdb->get_var( $wpdb->prepare( 'SELECT user_id FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $sub->item_id ) ); + $sub->user_id = $wpdb->get_var( $wpdb->prepare( 'SELECT user_id FROM %i WHERE id=%d', $wpdb->prefix . 'frm_items', $sub->item_id ) ); } $link = self::cancel_link( $sub, $atts ); diff --git a/stripe/helpers/FrmTransLiteListHelper.php b/stripe/helpers/FrmTransLiteListHelper.php index b94fefa8b8..8137838087 100755 --- a/stripe/helpers/FrmTransLiteListHelper.php +++ b/stripe/helpers/FrmTransLiteListHelper.php @@ -63,7 +63,8 @@ public function prepare_items() { $query = $this->get_table_query(); $order_query = FrmDb::esc_order( "ORDER BY p.{$orderby} $order" ); - // @codingStandardsIgnoreStart + // The table query and order query are prepared and escaped where they are built. + // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared $this->items = $wpdb->get_results( $wpdb->prepare( 'SELECT p.* ' . $query . $order_query . ' LIMIT %d, %d', @@ -72,7 +73,7 @@ public function prepare_items() { ) ); $total_items = $wpdb->get_var( 'SELECT COUNT(*) ' . $query ); - // @codingStandardsIgnoreEnd + // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared $this->set_pagination_args( array( @@ -92,17 +93,17 @@ private function get_table_query() { $form_id = FrmAppHelper::get_param( 'form', 0, 'get', 'absint' ); if ( ! $form_id ) { - return "FROM `{$wpdb->prefix}{$table_name}` p"; + return $wpdb->prepare( 'FROM %i p', $wpdb->prefix . $table_name ); } - // @codingStandardsIgnoreStart - return $wpdb->prepare( - "FROM `{$wpdb->prefix}{$table_name}` p - JOIN `{$wpdb->prefix}frm_items` i ON p.item_id = i.id - WHERE i.form_id = %d", - $form_id - ); - // @codingStandardsIgnoreEnd + return $wpdb->prepare( + 'FROM %i p + JOIN %i i ON p.item_id = i.id + WHERE i.form_id = %d', + $wpdb->prefix . $table_name, + $wpdb->prefix . 'frm_items', + $form_id + ); } /** @@ -335,17 +336,20 @@ private function get_form_ids() { } global $wpdb; - // @codingStandardsIgnoreStart - $forms = $wpdb->get_results( - "SELECT - fo.id as form_id, - fo.name, - e.id - FROM {$wpdb->prefix}frm_items e - LEFT JOIN {$wpdb->prefix}frm_forms fo ON e.form_id = fo.id - WHERE e.id in (" . implode( ',', $entry_ids ) . ')' + + $ids_placeholders = implode( ',', array_fill( 0, count( $entry_ids ), '%d' ) ); + $forms = $wpdb->get_results( + $wpdb->prepare( + 'SELECT + fo.id as form_id, + fo.name, + e.id + FROM %i e + LEFT JOIN %i fo ON e.form_id = fo.id + WHERE e.id in (' . $ids_placeholders . ')', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + array_merge( array( $wpdb->prefix . 'frm_items', $wpdb->prefix . 'frm_forms' ), $entry_ids ) + ) ); - // @codingStandardsIgnoreEnd unset( $entry_ids ); $form_ids = array(); diff --git a/stripe/models/FrmTransLiteDb.php b/stripe/models/FrmTransLiteDb.php index f2b6e2a869..a3500d2907 100755 --- a/stripe/models/FrmTransLiteDb.php +++ b/stripe/models/FrmTransLiteDb.php @@ -147,9 +147,7 @@ public function destroy( $id ) { */ do_action( 'frm_before_destroy_' . $this->singular, $id ); - // @codingStandardsIgnoreStart - return $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . $this->table_name . ' WHERE id=%d', $id ) ); - // @codingStandardsIgnoreEnd + return $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE id=%d', $wpdb->prefix . $this->table_name, $id ) ); } /** @@ -160,14 +158,13 @@ public function destroy( $id ) { public function get_one( $id ) { global $wpdb; - // @codingStandardsIgnoreStart return $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM ' . $wpdb->prefix . $this->table_name . ' WHERE id=%d', + 'SELECT * FROM %i WHERE id=%d', + $wpdb->prefix . $this->table_name, $id ) ); - // @codingStandardsIgnoreEnd } /** @@ -183,17 +180,15 @@ public function get_one_by( $id, $field = 'receipt_id' ) { } global $wpdb; - // Can this be exploited? - $field = sanitize_text_field( $field ); - // @codingStandardsIgnoreStart + return $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM ' . $wpdb->prefix . $this->table_name - . ' WHERE ' . $field . ' = %s ORDER BY created_at DESC', + 'SELECT * FROM %i WHERE %i = %s ORDER BY created_at DESC', + $wpdb->prefix . $this->table_name, + $field, $id ) ); - // @codingStandardsIgnoreEnd } /** @@ -216,15 +211,15 @@ public function get_all_by( $value, $field = 'item_id' ) { } global $wpdb; - // @codingStandardsIgnoreStart + return $wpdb->get_results( $wpdb->prepare( - 'SELECT * FROM ' . $wpdb->prefix . $this->table_name - . ' WHERE ' . $field . ' = %s ORDER BY created_at DESC', + 'SELECT * FROM %i WHERE %i = %s ORDER BY created_at DESC', + $wpdb->prefix . $this->table_name, + $field, $value ) ); - // @codingStandardsIgnoreEnd } /** @@ -234,21 +229,22 @@ public function get_all_by( $value, $field = 'item_id' ) { */ public function get_all_for_user( $user_id ) { global $wpdb; - // @codingStandardsIgnoreStart + return $wpdb->get_results( $wpdb->prepare( 'SELECT *, e.id as entry_id, p.id as id - FROM ' . $wpdb->prefix . $this->table_name . ' p ' - . 'LEFT JOIN ' . $wpdb->prefix . 'frm_items e ON e.id = p.item_id ' - . 'WHERE e.user_id = %d ' - . 'ORDER BY p.created_at DESC', + FROM %i p + LEFT JOIN %i e ON e.id = p.item_id + WHERE e.user_id = %d + ORDER BY p.created_at DESC', + $wpdb->prefix . $this->table_name, + $wpdb->prefix . 'frm_items', $user_id ) ); - // @codingStandardsIgnoreEnd } /** @@ -265,11 +261,7 @@ public function get_all_for_entry( $id ) { */ public function get_count() { global $wpdb; - // @codingStandardsIgnoreStart - return $wpdb->get_var( - 'SELECT COUNT(*) FROM ' . $wpdb->prefix . $this->table_name - ); - // @codingStandardsIgnoreEnd + return $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $wpdb->prefix . $this->table_name ) ); } /** @@ -326,14 +318,14 @@ private function migrate_data( $old_db_version ) { */ private function migrate_to_4() { global $wpdb; - $result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM ' . $wpdb->prefix . 'frm_payments LIKE %s', 'completed' ) ); + $result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM %i LIKE %s', $wpdb->prefix . 'frm_payments', 'completed' ) ); if ( ! $result ) { return; } $payments = $wpdb->get_results( - "SELECT * FROM {$wpdb->prefix}frm_payments WHERE completed is NOT NULL AND status is NULL" + $wpdb->prepare( 'SELECT * FROM %i WHERE completed is NOT NULL AND status is NULL', $wpdb->prefix . 'frm_payments' ) ); foreach ( $payments as $payment ) { diff --git a/stripe/models/FrmTransLiteSubscription.php b/stripe/models/FrmTransLiteSubscription.php index 88e7b01366..8cd142508d 100755 --- a/stripe/models/FrmTransLiteSubscription.php +++ b/stripe/models/FrmTransLiteSubscription.php @@ -83,10 +83,11 @@ public function get_overdue_subscriptions() { global $wpdb; return $wpdb->get_results( $wpdb->prepare( - "SELECT * FROM `{$wpdb->prefix}frm_subscriptions` + "SELECT * FROM %i WHERE fail_count < %d AND next_bill_date < %s AND (status = 'active' OR status = 'future_cancel')", + $wpdb->prefix . 'frm_subscriptions', 3, gmdate( 'Y-m-d' ) ) diff --git a/tests/phpunit/misc/fixtures/prefer-identifier-placeholder-ruleset.xml b/tests/phpunit/misc/fixtures/prefer-identifier-placeholder-ruleset.xml new file mode 100644 index 0000000000..5f12cf7ea5 --- /dev/null +++ b/tests/phpunit/misc/fixtures/prefer-identifier-placeholder-ruleset.xml @@ -0,0 +1,5 @@ + + + Runs only the PreferIdentifierPlaceholder sniff for its unit tests. + + diff --git a/tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php b/tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php new file mode 100644 index 0000000000..25e17f9f7d --- /dev/null +++ b/tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php @@ -0,0 +1,314 @@ +process(); + + return $file; + } + + /** + * Collects error sources and fixable flags from a processed file. + * + * @param \PHP_CodeSniffer\Files\DummyFile $file The processed file. + * + * @return array Array of array( 'source' => string, 'fixable' => bool ). + */ + private function get_error_list( $file ) { + $list = array(); + foreach ( $file->getErrors() as $line => $cols ) { + foreach ( $cols as $col => $errors ) { + foreach ( $errors as $error ) { + $list[] = array( + 'source' => $error['source'], + 'fixable' => $error['fixable'], + ); + } + } + } + + return $list; + } + + /** + * Asserts the sniff fixes $code into $expected. + * + * @param string $code The offending code. + * @param string $expected The expected fixed code. + * + * @return void + */ + private function assert_fixed( $code, $expected ) { + $file = $this->process_code( $code ); + $errors = $this->get_error_list( $file ); + + $this->assertNotEmpty( $errors, 'Expected the sniff to flag: ' . $code ); + + foreach ( $errors as $error ) { + $this->assertSame( 'Formidable.Security.PreferIdentifierPlaceholder.TableInPrepare', $error['source'] ); + $this->assertTrue( $error['fixable'], 'Expected a fixable error for: ' . $code ); + } + + $file->fixer->fixFile(); + $this->assertSame( "fixer->getContents() ); + } + + /** + * Asserts the sniff flags $code with $source and cannot auto-fix it. + * + * @param string $code The offending code. + * @param string $source The expected error source. + * + * @return void + */ + private function assert_flagged_only( $code, $source ) { + $file = $this->process_code( $code ); + $errors = $this->get_error_list( $file ); + + $this->assertNotEmpty( $errors, 'Expected the sniff to flag: ' . $code ); + + foreach ( $errors as $error ) { + $this->assertSame( $source, $error['source'] ); + $this->assertFalse( $error['fixable'], 'Expected a non-fixable error for: ' . $code ); + } + } + + /** + * Asserts the sniff stays silent for $code. + * + * @param string $code The clean code. + * + * @return void + */ + private function assert_clean( $code ) { + $file = $this->process_code( $code ); + $this->assertSame( 0, $file->getErrorCount(), 'Expected no errors for: ' . $code ); + } + + public function test_fixes_interpolated_prefix_table() { + $this->assert_fixed( + '$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_item_metas WHERE field_id=%d AND item_id=%d", $field_id, $entry_id ) );', + "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM %i WHERE field_id=%d AND item_id=%d', \$wpdb->prefix . 'frm_item_metas', \$field_id, \$entry_id ) );" + ); + } + + public function test_fixes_concatenated_prefix_table() { + $this->assert_fixed( + "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM ' . \$wpdb->prefix . 'frm_item_metas WHERE field_id=%d', \$id ) );", + "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM %i WHERE field_id=%d', \$wpdb->prefix . 'frm_item_metas', \$id ) );" + ); + } + + public function test_fixes_wpdb_property_table() { + $this->assert_fixed( + "\$ids = \$wpdb->get_col( \$wpdb->prepare( 'SELECT ID FROM ' . \$wpdb->posts . ' WHERE post_type in (%s, %s)', \$a, \$b ) );", + "\$ids = \$wpdb->get_col( \$wpdb->prepare( 'SELECT ID FROM %i WHERE post_type in (%s, %s)', \$wpdb->posts, \$a, \$b ) );" + ); + } + + public function test_fixes_dynamic_table_variable() { + $this->assert_fixed( + "\$result = \$wpdb->get_results( \$wpdb->prepare( 'SHOW COLUMNS FROM ' . \$wpdb->prefix . \$table . ' LIKE %s', \$column ) );", + "\$result = \$wpdb->get_results( \$wpdb->prepare( 'SHOW COLUMNS FROM %i LIKE %s', \$wpdb->prefix . \$table, \$column ) );" + ); + } + + public function test_fixes_two_tables_in_one_query() { + $this->assert_fixed( + "\$wpdb->query( \$wpdb->prepare( 'DELETE fi FROM ' . \$wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . \$wpdb->prefix" + . " . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', \$id, \$id ) );", + "\$wpdb->query( \$wpdb->prepare( 'DELETE fi FROM %i AS fi LEFT JOIN %i fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d'," + . " \$wpdb->prefix . 'frm_fields', \$wpdb->prefix . 'frm_forms', \$id, \$id ) );" + ); + } + + public function test_fixes_table_and_keeps_other_interpolation() { + $this->assert_fixed( + '$query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value' + . ' {$operator} %s and field_id = %d", $search, $field_id );', + '$query = $wpdb->prepare( "SELECT DISTINCT item_id FROM %i WHERE meta_value {$operator} %s and field_id = %d",' + . ' $wpdb->prefix . \'frm_item_metas\', $search, $field_id );' + ); + } + + public function test_fixes_backticked_interpolated_table() { + $this->assert_fixed( + '$sub = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}frm_subscriptions` WHERE id = %d", $id ) );', + "\$sub = \$wpdb->get_row( \$wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', \$wpdb->prefix . 'frm_subscriptions', \$id ) );" + ); + } + + public function test_fixes_prepare_without_existing_args() { + $this->assert_fixed( + '$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}frm_payments WHERE completed is NOT NULL" ) );', + "\$rows = \$wpdb->get_results( \$wpdb->prepare( 'SELECT * FROM %i WHERE completed is NOT NULL', \$wpdb->prefix . 'frm_payments' ) );" + ); + } + + public function test_fixes_double_interpolated_table_name() { + $this->assert_fixed( + '$results = $wpdb->get_results( $wpdb->prepare( "SELECT p.* FROM `{$wpdb->prefix}frm_{$table_name}` p' + . ' LEFT JOIN `{$wpdb->prefix}frm_items` e ON p.item_id = e.id WHERE e.id = %d", $id ) );', + "\$results = \$wpdb->get_results( \$wpdb->prepare( 'SELECT p.* FROM %i p LEFT JOIN %i e ON p.item_id = e.id WHERE e.id = %d'," + . " \$wpdb->prefix . 'frm_' . \$table_name, \$wpdb->prefix . 'frm_items', \$id ) );" + ); + } + + public function test_fixes_create_index_on_table() { + $this->assert_fixed( + '$wpdb->query( $wpdb->prepare( "CREATE INDEX idx_is_draft_created_at ON {$wpdb->prefix}frm_items (is_draft, created_at)" ) );', + "\$wpdb->query( \$wpdb->prepare( 'CREATE INDEX idx_is_draft_created_at ON %i (is_draft, created_at)', \$wpdb->prefix . 'frm_items' ) );" + ); + } + + public function test_fixes_multiline_interpolated_tables() { + $code = implode( + "\n", + array( + '$result = $wpdb->get_results(', + "\t\$wpdb->prepare(", + "\t\t\"SELECT COUNT(*) as items_count", + "\t\t\tFROM {\$wpdb->prefix}frm_items AS it INNER JOIN {\$wpdb->prefix}frm_forms AS fr ON it.form_id = fr.id", + "\t\t\tWHERE it.created_at BETWEEN %s AND %s\",", + "\t\t\$from_date,", + "\t\t\$to_date", + "\t)", + ');', + ) + ); + + $expected = implode( + "\n", + array( + '$result = $wpdb->get_results(', + "\t\$wpdb->prepare(", + "\t\t'SELECT COUNT(*) as items_count", + "\t\t\tFROM %i AS it INNER JOIN %i AS fr ON it.form_id = fr.id", + "\t\t\tWHERE it.created_at BETWEEN %s AND %s',", + "\t\t\$wpdb->prefix . 'frm_items', \$wpdb->prefix . 'frm_forms', \$from_date,", + "\t\t\$to_date", + "\t)", + ');', + ) + ); + + $this->assert_fixed( $code, $expected ); + } + + public function test_flags_unknown_sql_fragment_without_fixing() { + $this->assert_flagged_only( + "\$wpdb->query( \$wpdb->prepare( 'UPDATE ' . \$wpdb->prefix . 'frm_forms SET status = %s ' . \$where['where'], \$where['values'] ) );", + 'Formidable.Security.PreferIdentifierPlaceholder.TableInPrepare' + ); + } + + public function test_flags_variable_alias_without_fixing() { + $this->assert_flagged_only( + "\$sql = \$wpdb->prepare( ' LEFT JOIN ' . \$wpdb->prefix . 'frm_item_metas em' . \$o_key . ' ON em' . \$o_key . '.field_id=%d ', \$o_field->id );", + 'Formidable.Security.PreferIdentifierPlaceholder.TableInPrepare' + ); + } + + public function test_flags_unprepared_query() { + $this->assert_flagged_only( + "\$wpdb->query( 'DROP TABLE IF EXISTS ' . \$this->fields );", + 'Formidable.Security.PreferIdentifierPlaceholder.TableNotPrepared' + ); + } + + public function test_flags_unprepared_interpolated_query() { + $this->assert_flagged_only( + '$found = $wpdb->get_var( "SELECT 1 FROM {$wpdb->posts} WHERE post_content LIKE \'%[formidable %\' LIMIT 1" );', + 'Formidable.Security.PreferIdentifierPlaceholder.TableNotPrepared' + ); + } + + public function test_ignores_create_table_ddl() { + $this->assert_clean( '$wpdb->query( "CREATE TABLE {$wpdb->prefix}frm_payments ( id BIGINT )" );' ); + } + + public function test_ignores_existing_identifier_placeholder() { + $this->assert_clean( "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM %i WHERE id = %d', \$wpdb->prefix . 'frm_items', \$id ) );" ); + } + + public function test_ignores_literal_table_name() { + $this->assert_clean( "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM wp_frm_items WHERE id = %d', \$id ) );" ); + } + + public function test_ignores_frmdb_helper_table_argument() { + $this->assert_clean( "\$count = FrmDb::get_var( 'frm_forms', array( 'id' => \$id ), 'COUNT(*)' );" ); + } + + public function test_ignores_interpolated_alias_after_on() { + $this->assert_clean( '$x = $wpdb->prepare( "SELECT * FROM %i em{$o_key} WHERE em{$o_key}.field_id = %d", $wpdb->prefix . \'frm_item_metas\', $o_field->id );' ); + } + + public function test_ignores_interpolated_alias_in_join_condition() { + $this->assert_clean( + '$y = $wpdb->prepare( "SELECT * FROM %i pm{$o_key} INNER JOIN %i it ON pm{$o_key}.post_id = it.post_id' + . ' WHERE it.id = %d", $wpdb->postmeta, $wpdb->prefix . \'frm_items\', $id );' + ); + } +} From ae26cacf60df9d38932158421f04b00ee23d98d2 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Thu, 19 Nov 2026 14:26:45 -0400 Subject: [PATCH 2/5] Run the identifier placeholder sniff through the fixture harness The PHPUnit test for the new sniff loaded PHP_CodeSniffer from a vendor path that does not exist in the WordPress test environment, so every PHPUnit job failed, and the same file broke PHP CS Fixer and Mago. Move its cases to the standard sniff fixtures, which run in the PHPCS job, and let the fixture runner find sniffs in any category. Make the sniff report every violation as fixable and auto wrap unprepared queries in prepare(). Skip cases it cannot fix safely. Convert the three CREATE INDEX queries in FrmMigrate that the rule now flags. Co-Authored-By: Claude Opus 5.5 --- classes/models/FrmMigrate.php | 6 +- .../PreferIdentifierPlaceholderSniff.php | 151 +++++++-- phpcs-sniffs/tests/README.md | 1 + .../PreferIdentifierPlaceholder.fixed | 53 +++ .../fixtures/PreferIdentifierPlaceholder.inc | 50 +++ phpcs-sniffs/tests/fixtures/expected.json | 22 ++ phpcs-sniffs/tests/run.php | 22 +- .../prefer-identifier-placeholder-ruleset.xml | 5 - ...st_FrmPreferIdentifierPlaceholderSniff.php | 314 ------------------ 9 files changed, 277 insertions(+), 347 deletions(-) create mode 100644 phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed create mode 100644 phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc delete mode 100644 tests/phpunit/misc/fixtures/prefer-identifier-placeholder-ruleset.xml delete mode 100644 tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index e26ee1fe3a..3b7635c405 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -340,7 +340,7 @@ private function add_composite_indexes() { $index_name = 'idx_form_id_created_at'; if ( ! self::index_exists( $table_name, $index_name ) ) { - $wpdb->query( "CREATE INDEX idx_form_id_created_at ON `{$wpdb->prefix}frm_items` (form_id, created_at)" ); + $wpdb->query( $wpdb->prepare( 'CREATE INDEX idx_form_id_created_at ON %i (form_id, created_at)', $table_name ) ); } /** @@ -351,14 +351,14 @@ private function add_composite_indexes() { $index_name = 'idx_parent_form_id'; if ( ! self::index_exists( $table_name, $index_name ) ) { - $wpdb->query( "CREATE INDEX idx_parent_form_id ON `{$wpdb->prefix}frm_forms` (parent_form_id)" ); + $wpdb->query( $wpdb->prepare( 'CREATE INDEX idx_parent_form_id ON %i (parent_form_id)', $table_name ) ); } $table_name = "{$wpdb->prefix}frm_forms"; $index_name = 'frm_forms_template_status'; if ( ! self::index_exists( $table_name, $index_name ) ) { - $wpdb->query( "CREATE INDEX frm_forms_template_status ON `{$wpdb->prefix}frm_forms` (is_template, status, parent_form_id)" ); + $wpdb->query( $wpdb->prepare( 'CREATE INDEX frm_forms_template_status ON %i (is_template, status, parent_form_id)', $table_name ) ); } } diff --git a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php index cdd4096720..3a5cae5336 100644 --- a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php +++ b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php @@ -25,6 +25,10 @@ * $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $this->table ) ); * * CREATE TABLE statements are exempt because they are built for dbDelta(), which cannot use prepare(). + * + * Only queries that can be rewritten from what is already in the source are reported. + * A query with another dynamic SQL fragment, numbered placeholders, an array of + * prepare() arguments, or a percent sign outside prepare() is left alone. */ class PreferIdentifierPlaceholderSniff implements Sniff { @@ -132,12 +136,7 @@ public function process( File $phpcsFile, $stackPtr ) { } if ( ! $isPrepare ) { - $phpcsFile->addError( - 'Table name built into SQL passed to $wpdb->%s() without prepare(). Wrap the query in $wpdb->prepare() and use the %%i placeholder for the identifier.', - $refs[0]['ptr'], - 'TableNotPrepared', - array( $methodName ) - ); + $this->handle_unprepared_refs( $phpcsFile, $parts, $refs, $methodName ); return; } @@ -514,7 +513,7 @@ private function segments_to_expression( $text ) { } /** - * Reports and, when safe, fixes table references inside a $wpdb->prepare() call. + * Reports and fixes table references inside a $wpdb->prepare() call when the rewrite is safe. * * @param File $phpcsFile The file being scanned. * @param array $parts The concatenation parts of the first argument. @@ -531,18 +530,11 @@ private function handle_prepare_refs( File $phpcsFile, $parts, $refs, $openParen if ( $fixable && false !== $args ) { $placeholderTotal = $this->count_placeholders_in_literals( $parts ); - $fixable = count( $args ) === $placeholderTotal; + $fixable = count( $args ) === $placeholderTotal && ! $this->has_array_arg( $phpcsFile, $args ); } if ( ! $fixable || false === $args ) { - foreach ( $refs as $ref ) { - $phpcsFile->addError( - 'Table name built into $wpdb->prepare() SQL. Use the %i placeholder and pass the identifier as a prepare() argument.', - $ref['ptr'], - 'TableInPrepare' - ); - } - + // The rewrite cannot be derived from the source, so the call is left for a manual change. return; } @@ -563,6 +555,92 @@ private function handle_prepare_refs( File $phpcsFile, $parts, $refs, $openParen $this->apply_fix( $phpcsFile, $parts, $refs, $openParen, $argEnd, $closeParen, $args ); } + /** + * Checks whether a prepare() argument is an array literal. + * + * prepare() only unpacks an array when it is the sole argument, so an identifier + * cannot be inserted next to it. + * + * @param File $phpcsFile The file being scanned. + * @param array $args The existing argument token ranges. + * + * @return bool + */ + private function has_array_arg( File $phpcsFile, $args ) { + $tokens = $phpcsFile->getTokens(); + + foreach ( $args as $arg ) { + if ( in_array( $tokens[ $arg['start'] ]['code'], array( T_ARRAY, T_OPEN_SHORT_ARRAY ), true ) ) { + return true; + } + } + + return false; + } + + /** + * Reports and fixes table references in SQL passed to a $wpdb query method without prepare(). + * + * The query is only reported when it can be wrapped in prepare() without changing + * its meaning. A percent sign or a leftover interpolated value would be read as a + * placeholder by prepare(), so those queries are left alone. + * + * @param File $phpcsFile The file being scanned. + * @param array $parts The concatenation parts of the first argument. + * @param array $refs The detected table references. + * @param string $methodName The $wpdb method that receives the SQL. + * + * @return void + */ + private function handle_unprepared_refs( File $phpcsFile, $parts, $refs, $methodName ) { + if ( ! $this->refs_are_fixable( $parts, $refs ) ) { + return; + } + + foreach ( $parts as $part ) { + if ( 'expr' !== $part['type'] && false !== strpos( $part['content'], '%' ) ) { + return; + } + } + + $newParts = $this->build_new_parts( $parts, $refs ); + + foreach ( $newParts as $newPart ) { + if ( 'dq' === $newPart['type'] && preg_match( '/' . self::INTERP_SEGMENT . '/', $newPart['content'] ) ) { + return; + } + } + + $fix = $phpcsFile->addFixableError( + 'Table name built into SQL passed to $wpdb->%s() without prepare(). Wrap the query in $wpdb->prepare() and use the %%i placeholder for the identifier.', + $refs[0]['ptr'], + 'TableNotPrepared', + array( $methodName ) + ); + + if ( true !== $fix ) { + return; + } + + $exprs = array(); + + foreach ( $refs as $ref ) { + $exprs[] = $ref['expr']; + } + + $sqlStart = $parts[0]['start']; + $sqlEnd = $parts[ count( $parts ) - 1 ]['end']; + + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->replaceToken( $sqlStart, '$wpdb->prepare( ' . $this->render_parts( $newParts ) . ', ' . implode( ', ', $exprs ) . ' )' ); + + for ( $i = $sqlStart + 1; $i <= $sqlEnd; $i++ ) { + $phpcsFile->fixer->replaceToken( $i, '' ); + } + + $phpcsFile->fixer->endChangeset(); + } + /** * Determines whether the detected references can be fixed automatically. * @@ -797,21 +875,50 @@ private function apply_fix( File $phpcsFile, $parts, $refs, $openParen, $argEnd, } foreach ( $bySlot as $slot => $exprs ) { - $code = implode( ', ', $exprs ); - if ( isset( $args[ $slot ] ) ) { - $phpcsFile->fixer->addContentBefore( $args[ $slot ]['start'], $code . ', ' ); + $separator = $this->get_arg_separator( $phpcsFile, $args[ $slot ]['start'] ); + $phpcsFile->fixer->addContentBefore( $args[ $slot ]['start'], implode( $separator, $exprs ) . $separator ); } elseif ( $args ) { - $lastArg = $args[ count( $args ) - 1 ]; - $phpcsFile->fixer->addContent( $lastArg['end'], ', ' . $code ); + $lastArg = $args[ count( $args ) - 1 ]; + $separator = $this->get_arg_separator( $phpcsFile, $lastArg['start'] ); + $phpcsFile->fixer->addContent( $lastArg['end'], $separator . implode( $separator, $exprs ) ); } else { - $phpcsFile->fixer->addContent( $sqlEnd, ', ' . $code ); + $separator = $this->get_arg_separator( $phpcsFile, $sqlStart ); + $phpcsFile->fixer->addContent( $sqlEnd, $separator . implode( $separator, $exprs ) ); } } $phpcsFile->fixer->endChangeset(); } + /** + * Gets the separator to put between an inserted argument and its neighbor. + * + * An argument that starts its own line gets inserted arguments on their own lines + * with the same indentation, so a multiline call stays one argument per line. + * + * @param File $phpcsFile The file being scanned. + * @param int $argStart The first token of the neighboring argument. + * + * @return string + */ + private function get_arg_separator( File $phpcsFile, $argStart ) { + $tokens = $phpcsFile->getTokens(); + $prev = $phpcsFile->findPrevious( T_WHITESPACE, $argStart - 1, null, true ); + + if ( false === $prev || $tokens[ $prev ]['line'] === $tokens[ $argStart ]['line'] ) { + return ', '; + } + + $indent = ''; + + if ( T_WHITESPACE === $tokens[ $argStart - 1 ]['code'] && $tokens[ $argStart - 1 ]['line'] === $tokens[ $argStart ]['line'] ) { + $indent = $tokens[ $argStart - 1 ]['content']; + } + + return ',' . $phpcsFile->eolChar . $indent; + } + /** * Builds the substituted part contents with %i in place of each reference. * diff --git a/phpcs-sniffs/tests/README.md b/phpcs-sniffs/tests/README.md index e7382c0ad3..777e2ed81c 100644 --- a/phpcs-sniffs/tests/README.md +++ b/phpcs-sniffs/tests/README.md @@ -22,3 +22,4 @@ Detection is deliberately conservative: - SQL placeholder lists must match the helper's comma and space delimiter exactly. Different delimiters and expressions with side effects are left alone. - Column queries require explicit column mode, or a direct `wp_list_pluck()` of the single named column selected by `FrmDb::get_results()`. Indexed extraction and multi-column queries are left alone. - WordPress attribute helpers replace only standalone conditional attribute output inside an HTML tag. The original condition is evaluated as a boolean, preserving strict and loose comparisons. Static attributes, ARIA values, and output containing additional attributes are left alone. +- Identifier placeholders apply only when the whole query is literals plus the table expressions. Another dynamic SQL fragment, numbered placeholders, an array of `prepare()` arguments, and an unprepared query holding a percent sign or an interpolated value are left alone, because the rewrite would have to guess the argument order or change how the query is read. diff --git a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed new file mode 100644 index 0000000000..a80f6a73e7 --- /dev/null +++ b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed @@ -0,0 +1,53 @@ +query( $wpdb->prepare( 'DELETE FROM %i WHERE field_id=%d AND item_id=%d', $wpdb->prefix . 'frm_item_metas', $field_id, $entry_id ) ); +$wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE field_id=%d', $wpdb->prefix . 'frm_item_metas', $id ) ); +$ids = $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM %i WHERE post_type in (%s, %s)', $wpdb->posts, $a, $b ) ); +$result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM %i LIKE %s', $wpdb->prefix . $table, $column ) ); +$wpdb->query( $wpdb->prepare( 'DELETE fi FROM %i AS fi LEFT JOIN %i fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $wpdb->prefix . 'frm_fields', $wpdb->prefix . 'frm_forms', $id, $id ) ); +$query = $wpdb->prepare( "SELECT DISTINCT item_id FROM %i WHERE meta_value {$operator} %s and field_id = %d", $wpdb->prefix . 'frm_item_metas', $search, $field_id ); +$sub = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $wpdb->prefix . 'frm_subscriptions', $id ) ); +$rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE completed is NOT NULL', $wpdb->prefix . 'frm_payments' ) ); +$results = $wpdb->get_results( $wpdb->prepare( 'SELECT p.* FROM %i p LEFT JOIN %i e ON p.item_id = e.id WHERE e.id = %d', $wpdb->prefix . 'frm_' . $table_name, $wpdb->prefix . 'frm_items', $id ) ); +$wpdb->query( $wpdb->prepare( 'CREATE INDEX idx_is_draft_created_at ON %i (is_draft, created_at)', $wpdb->prefix . 'frm_items' ) ); +$result = $wpdb->get_results( + $wpdb->prepare( + 'SELECT COUNT(*) as items_count + FROM %i AS it INNER JOIN %i AS fr ON it.form_id = fr.id + WHERE it.created_at BETWEEN %s AND %s', + $wpdb->prefix . 'frm_items', + $wpdb->prefix . 'frm_forms', + $from_date, + $to_date + ) +); +$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $this->fields ) ); +$wpdb->query( $wpdb->prepare( 'CREATE INDEX idx_form_id_type ON %i (form_id, type(30))', $wpdb->prefix . 'frm_fields' ) ); +$count = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $wpdb->prefix . $this->table_name ) ); +$late = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE name = %s AND form_id IN ( SELECT id FROM %i WHERE status = %s )', $wpdb->prefix . 'frm_items', $name, $wpdb->prefix . 'frm_forms', $status ) ); +// A second dynamic SQL fragment makes the argument order unknowable. +$wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); +$sql = $wpdb->prepare( ' LEFT JOIN ' . $wpdb->prefix . 'frm_item_metas em' . $o_key . ' ON em' . $o_key . '.field_id=%d ', $o_field->id ); +// prepare() only unpacks an array when it is the sole argument. +$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id = %d', array( $id ) ) ); +$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id IN (%d, %d)', $ids ) ); +// Numbered placeholders change the argument mapping. +$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id = %1$d OR parent_item_id = %1$d', $id ) ); +// A percent sign or an interpolated value would be read as a placeholder once wrapped in prepare(). +$found = $wpdb->get_var( "SELECT 1 FROM {$wpdb->posts} WHERE post_content LIKE '%[formidable %' LIMIT 1" ); +$match = $wpdb->get_var( "SELECT 1 FROM {$wpdb->posts} WHERE post_title = '{$title}'" ); +$other = $wpdb->get_var( 'SELECT 1 FROM ' . $wpdb->posts . ' WHERE ' . $where ); +// Already correct, or not a table built into the SQL. +$wpdb->query( "CREATE TABLE {$wpdb->prefix}frm_payments ( id BIGINT )" ); +$wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE id = %d', $wpdb->prefix . 'frm_items', $id ) ); +$wpdb->query( $wpdb->prepare( 'DELETE FROM wp_frm_items WHERE id = %d', $id ) ); +$count = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'COUNT(*)' ); +$x = $wpdb->prepare( "SELECT * FROM %i em{$o_key} WHERE em{$o_key}.field_id = %d", $wpdb->prefix . 'frm_item_metas', $o_field->id ); +$y = $wpdb->prepare( "SELECT * FROM %i pm{$o_key} INNER JOIN %i it ON pm{$o_key}.post_id = it.post_id WHERE it.id = %d", $wpdb->postmeta, $wpdb->prefix . 'frm_items', $id ); +$wpdb->query( $sql ); +$other_db->query( 'DROP TABLE IF EXISTS ' . $this->fields ); +$payments = $wpdb->get_results( + $wpdb->prepare( + 'SELECT * FROM %i WHERE completed is NOT NULL', + $wpdb->prefix . 'frm_payments' + ) +); diff --git a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc new file mode 100644 index 0000000000..e3cb0c13f6 --- /dev/null +++ b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc @@ -0,0 +1,50 @@ +query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_item_metas WHERE field_id=%d AND item_id=%d", $field_id, $entry_id ) ); +$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id=%d', $id ) ); +$ids = $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type in (%s, %s)', $a, $b ) ); +$result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM ' . $wpdb->prefix . $table . ' LIKE %s', $column ) ); +$wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) ); +$query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value {$operator} %s and field_id = %d", $search, $field_id ); +$sub = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}frm_subscriptions` WHERE id = %d", $id ) ); +$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}frm_payments WHERE completed is NOT NULL" ) ); +$results = $wpdb->get_results( $wpdb->prepare( "SELECT p.* FROM `{$wpdb->prefix}frm_{$table_name}` p LEFT JOIN `{$wpdb->prefix}frm_items` e ON p.item_id = e.id WHERE e.id = %d", $id ) ); +$wpdb->query( $wpdb->prepare( "CREATE INDEX idx_is_draft_created_at ON {$wpdb->prefix}frm_items (is_draft, created_at)" ) ); +$result = $wpdb->get_results( + $wpdb->prepare( + "SELECT COUNT(*) as items_count + FROM {$wpdb->prefix}frm_items AS it INNER JOIN {$wpdb->prefix}frm_forms AS fr ON it.form_id = fr.id + WHERE it.created_at BETWEEN %s AND %s", + $from_date, + $to_date + ) +); +$wpdb->query( 'DROP TABLE IF EXISTS ' . $this->fields ); +$wpdb->query( "CREATE INDEX idx_form_id_type ON `{$wpdb->prefix}frm_fields` (form_id, type(30))" ); +$count = $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . $this->table_name ); +$late = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'frm_items WHERE name = %s AND form_id IN ( SELECT id FROM ' . $wpdb->prefix . 'frm_forms WHERE status = %s )', $name, $status ) ); +// A second dynamic SQL fragment makes the argument order unknowable. +$wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); +$sql = $wpdb->prepare( ' LEFT JOIN ' . $wpdb->prefix . 'frm_item_metas em' . $o_key . ' ON em' . $o_key . '.field_id=%d ', $o_field->id ); +// prepare() only unpacks an array when it is the sole argument. +$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id = %d', array( $id ) ) ); +$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id IN (%d, %d)', $ids ) ); +// Numbered placeholders change the argument mapping. +$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id = %1$d OR parent_item_id = %1$d', $id ) ); +// A percent sign or an interpolated value would be read as a placeholder once wrapped in prepare(). +$found = $wpdb->get_var( "SELECT 1 FROM {$wpdb->posts} WHERE post_content LIKE '%[formidable %' LIMIT 1" ); +$match = $wpdb->get_var( "SELECT 1 FROM {$wpdb->posts} WHERE post_title = '{$title}'" ); +$other = $wpdb->get_var( 'SELECT 1 FROM ' . $wpdb->posts . ' WHERE ' . $where ); +// Already correct, or not a table built into the SQL. +$wpdb->query( "CREATE TABLE {$wpdb->prefix}frm_payments ( id BIGINT )" ); +$wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE id = %d', $wpdb->prefix . 'frm_items', $id ) ); +$wpdb->query( $wpdb->prepare( 'DELETE FROM wp_frm_items WHERE id = %d', $id ) ); +$count = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'COUNT(*)' ); +$x = $wpdb->prepare( "SELECT * FROM %i em{$o_key} WHERE em{$o_key}.field_id = %d", $wpdb->prefix . 'frm_item_metas', $o_field->id ); +$y = $wpdb->prepare( "SELECT * FROM %i pm{$o_key} INNER JOIN %i it ON pm{$o_key}.post_id = it.post_id WHERE it.id = %d", $wpdb->postmeta, $wpdb->prefix . 'frm_items', $id ); +$wpdb->query( $sql ); +$other_db->query( 'DROP TABLE IF EXISTS ' . $this->fields ); +$payments = $wpdb->get_results( + $wpdb->prepare( + "SELECT * FROM {$wpdb->prefix}frm_payments WHERE completed is NOT NULL" + ) +); diff --git a/phpcs-sniffs/tests/fixtures/expected.json b/phpcs-sniffs/tests/fixtures/expected.json index 5520bcab3e..f845bf0d3b 100644 --- a/phpcs-sniffs/tests/fixtures/expected.json +++ b/phpcs-sniffs/tests/fixtures/expected.json @@ -31,5 +31,27 @@ ], "CheckFunctionCallable": [ 2 + ], + "PreferIdentifierPlaceholder": [ + 2, + 3, + 4, + 5, + 6, + 6, + 7, + 8, + 9, + 10, + 10, + 11, + 14, + 14, + 21, + 22, + 23, + 24, + 24, + 48 ] } diff --git a/phpcs-sniffs/tests/run.php b/phpcs-sniffs/tests/run.php index 736a222244..1f9f2a255c 100644 --- a/phpcs-sniffs/tests/run.php +++ b/phpcs-sniffs/tests/run.php @@ -39,6 +39,23 @@ function run_sniff_command( $command ) { return array( proc_close( $process ), $output, $error ); } +/** + * Find the sniff file for a fixture in any category of the custom sniff library. + * + * @param string $root Lite checkout path. + * @param string $name Sniff name without the Sniff suffix. + * + * @return string Sniff file path. + */ +function find_sniff_path( $root, $name ) { + $paths = glob( $root . '/phpcs-sniffs/Formidable/Sniffs/*/' . $name . 'Sniff.php' ); + + if ( ! $paths || 1 !== count( $paths ) ) { + throw new RuntimeException( $name . ': cannot find exactly one sniff file.' ); + } + return $paths[0]; +} + /** * Count the violations the full Lite standard reports for a file, grouped by sniff source. * @@ -68,8 +85,7 @@ function count_standard_violations( $root, $path ) { $rules = ''; foreach ( $expected as $name => $lines ) { - $fixturePath = $root . '/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/' . $name . 'Sniff.php'; - $rules .= ''; + $rules .= ''; } $rules .= ''; file_put_contents( $scratch . '/ruleset.xml', $rules ); @@ -177,7 +193,7 @@ function count_standard_violations( $root, $path ) { foreach ( $report['files'][ $fixturePath ]['messages'] as $message ) { $sourceParts = explode( '.', $message['source'] ); - if ( array_slice( $sourceParts, 0, 3 ) !== array( 'Formidable', 'CodeAnalysis', (string) $name ) ) { + if ( array_slice( $sourceParts, 0, 3 ) !== array( 'Formidable', basename( dirname( find_sniff_path( $root, $name ) ) ), (string) $name ) ) { continue; } diff --git a/tests/phpunit/misc/fixtures/prefer-identifier-placeholder-ruleset.xml b/tests/phpunit/misc/fixtures/prefer-identifier-placeholder-ruleset.xml deleted file mode 100644 index 5f12cf7ea5..0000000000 --- a/tests/phpunit/misc/fixtures/prefer-identifier-placeholder-ruleset.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - Runs only the PreferIdentifierPlaceholder sniff for its unit tests. - - diff --git a/tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php b/tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php deleted file mode 100644 index 25e17f9f7d..0000000000 --- a/tests/phpunit/misc/test_FrmPreferIdentifierPlaceholderSniff.php +++ /dev/null @@ -1,314 +0,0 @@ -process(); - - return $file; - } - - /** - * Collects error sources and fixable flags from a processed file. - * - * @param \PHP_CodeSniffer\Files\DummyFile $file The processed file. - * - * @return array Array of array( 'source' => string, 'fixable' => bool ). - */ - private function get_error_list( $file ) { - $list = array(); - foreach ( $file->getErrors() as $line => $cols ) { - foreach ( $cols as $col => $errors ) { - foreach ( $errors as $error ) { - $list[] = array( - 'source' => $error['source'], - 'fixable' => $error['fixable'], - ); - } - } - } - - return $list; - } - - /** - * Asserts the sniff fixes $code into $expected. - * - * @param string $code The offending code. - * @param string $expected The expected fixed code. - * - * @return void - */ - private function assert_fixed( $code, $expected ) { - $file = $this->process_code( $code ); - $errors = $this->get_error_list( $file ); - - $this->assertNotEmpty( $errors, 'Expected the sniff to flag: ' . $code ); - - foreach ( $errors as $error ) { - $this->assertSame( 'Formidable.Security.PreferIdentifierPlaceholder.TableInPrepare', $error['source'] ); - $this->assertTrue( $error['fixable'], 'Expected a fixable error for: ' . $code ); - } - - $file->fixer->fixFile(); - $this->assertSame( "fixer->getContents() ); - } - - /** - * Asserts the sniff flags $code with $source and cannot auto-fix it. - * - * @param string $code The offending code. - * @param string $source The expected error source. - * - * @return void - */ - private function assert_flagged_only( $code, $source ) { - $file = $this->process_code( $code ); - $errors = $this->get_error_list( $file ); - - $this->assertNotEmpty( $errors, 'Expected the sniff to flag: ' . $code ); - - foreach ( $errors as $error ) { - $this->assertSame( $source, $error['source'] ); - $this->assertFalse( $error['fixable'], 'Expected a non-fixable error for: ' . $code ); - } - } - - /** - * Asserts the sniff stays silent for $code. - * - * @param string $code The clean code. - * - * @return void - */ - private function assert_clean( $code ) { - $file = $this->process_code( $code ); - $this->assertSame( 0, $file->getErrorCount(), 'Expected no errors for: ' . $code ); - } - - public function test_fixes_interpolated_prefix_table() { - $this->assert_fixed( - '$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_item_metas WHERE field_id=%d AND item_id=%d", $field_id, $entry_id ) );', - "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM %i WHERE field_id=%d AND item_id=%d', \$wpdb->prefix . 'frm_item_metas', \$field_id, \$entry_id ) );" - ); - } - - public function test_fixes_concatenated_prefix_table() { - $this->assert_fixed( - "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM ' . \$wpdb->prefix . 'frm_item_metas WHERE field_id=%d', \$id ) );", - "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM %i WHERE field_id=%d', \$wpdb->prefix . 'frm_item_metas', \$id ) );" - ); - } - - public function test_fixes_wpdb_property_table() { - $this->assert_fixed( - "\$ids = \$wpdb->get_col( \$wpdb->prepare( 'SELECT ID FROM ' . \$wpdb->posts . ' WHERE post_type in (%s, %s)', \$a, \$b ) );", - "\$ids = \$wpdb->get_col( \$wpdb->prepare( 'SELECT ID FROM %i WHERE post_type in (%s, %s)', \$wpdb->posts, \$a, \$b ) );" - ); - } - - public function test_fixes_dynamic_table_variable() { - $this->assert_fixed( - "\$result = \$wpdb->get_results( \$wpdb->prepare( 'SHOW COLUMNS FROM ' . \$wpdb->prefix . \$table . ' LIKE %s', \$column ) );", - "\$result = \$wpdb->get_results( \$wpdb->prepare( 'SHOW COLUMNS FROM %i LIKE %s', \$wpdb->prefix . \$table, \$column ) );" - ); - } - - public function test_fixes_two_tables_in_one_query() { - $this->assert_fixed( - "\$wpdb->query( \$wpdb->prepare( 'DELETE fi FROM ' . \$wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . \$wpdb->prefix" - . " . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', \$id, \$id ) );", - "\$wpdb->query( \$wpdb->prepare( 'DELETE fi FROM %i AS fi LEFT JOIN %i fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d'," - . " \$wpdb->prefix . 'frm_fields', \$wpdb->prefix . 'frm_forms', \$id, \$id ) );" - ); - } - - public function test_fixes_table_and_keeps_other_interpolation() { - $this->assert_fixed( - '$query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value' - . ' {$operator} %s and field_id = %d", $search, $field_id );', - '$query = $wpdb->prepare( "SELECT DISTINCT item_id FROM %i WHERE meta_value {$operator} %s and field_id = %d",' - . ' $wpdb->prefix . \'frm_item_metas\', $search, $field_id );' - ); - } - - public function test_fixes_backticked_interpolated_table() { - $this->assert_fixed( - '$sub = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}frm_subscriptions` WHERE id = %d", $id ) );', - "\$sub = \$wpdb->get_row( \$wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', \$wpdb->prefix . 'frm_subscriptions', \$id ) );" - ); - } - - public function test_fixes_prepare_without_existing_args() { - $this->assert_fixed( - '$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}frm_payments WHERE completed is NOT NULL" ) );', - "\$rows = \$wpdb->get_results( \$wpdb->prepare( 'SELECT * FROM %i WHERE completed is NOT NULL', \$wpdb->prefix . 'frm_payments' ) );" - ); - } - - public function test_fixes_double_interpolated_table_name() { - $this->assert_fixed( - '$results = $wpdb->get_results( $wpdb->prepare( "SELECT p.* FROM `{$wpdb->prefix}frm_{$table_name}` p' - . ' LEFT JOIN `{$wpdb->prefix}frm_items` e ON p.item_id = e.id WHERE e.id = %d", $id ) );', - "\$results = \$wpdb->get_results( \$wpdb->prepare( 'SELECT p.* FROM %i p LEFT JOIN %i e ON p.item_id = e.id WHERE e.id = %d'," - . " \$wpdb->prefix . 'frm_' . \$table_name, \$wpdb->prefix . 'frm_items', \$id ) );" - ); - } - - public function test_fixes_create_index_on_table() { - $this->assert_fixed( - '$wpdb->query( $wpdb->prepare( "CREATE INDEX idx_is_draft_created_at ON {$wpdb->prefix}frm_items (is_draft, created_at)" ) );', - "\$wpdb->query( \$wpdb->prepare( 'CREATE INDEX idx_is_draft_created_at ON %i (is_draft, created_at)', \$wpdb->prefix . 'frm_items' ) );" - ); - } - - public function test_fixes_multiline_interpolated_tables() { - $code = implode( - "\n", - array( - '$result = $wpdb->get_results(', - "\t\$wpdb->prepare(", - "\t\t\"SELECT COUNT(*) as items_count", - "\t\t\tFROM {\$wpdb->prefix}frm_items AS it INNER JOIN {\$wpdb->prefix}frm_forms AS fr ON it.form_id = fr.id", - "\t\t\tWHERE it.created_at BETWEEN %s AND %s\",", - "\t\t\$from_date,", - "\t\t\$to_date", - "\t)", - ');', - ) - ); - - $expected = implode( - "\n", - array( - '$result = $wpdb->get_results(', - "\t\$wpdb->prepare(", - "\t\t'SELECT COUNT(*) as items_count", - "\t\t\tFROM %i AS it INNER JOIN %i AS fr ON it.form_id = fr.id", - "\t\t\tWHERE it.created_at BETWEEN %s AND %s',", - "\t\t\$wpdb->prefix . 'frm_items', \$wpdb->prefix . 'frm_forms', \$from_date,", - "\t\t\$to_date", - "\t)", - ');', - ) - ); - - $this->assert_fixed( $code, $expected ); - } - - public function test_flags_unknown_sql_fragment_without_fixing() { - $this->assert_flagged_only( - "\$wpdb->query( \$wpdb->prepare( 'UPDATE ' . \$wpdb->prefix . 'frm_forms SET status = %s ' . \$where['where'], \$where['values'] ) );", - 'Formidable.Security.PreferIdentifierPlaceholder.TableInPrepare' - ); - } - - public function test_flags_variable_alias_without_fixing() { - $this->assert_flagged_only( - "\$sql = \$wpdb->prepare( ' LEFT JOIN ' . \$wpdb->prefix . 'frm_item_metas em' . \$o_key . ' ON em' . \$o_key . '.field_id=%d ', \$o_field->id );", - 'Formidable.Security.PreferIdentifierPlaceholder.TableInPrepare' - ); - } - - public function test_flags_unprepared_query() { - $this->assert_flagged_only( - "\$wpdb->query( 'DROP TABLE IF EXISTS ' . \$this->fields );", - 'Formidable.Security.PreferIdentifierPlaceholder.TableNotPrepared' - ); - } - - public function test_flags_unprepared_interpolated_query() { - $this->assert_flagged_only( - '$found = $wpdb->get_var( "SELECT 1 FROM {$wpdb->posts} WHERE post_content LIKE \'%[formidable %\' LIMIT 1" );', - 'Formidable.Security.PreferIdentifierPlaceholder.TableNotPrepared' - ); - } - - public function test_ignores_create_table_ddl() { - $this->assert_clean( '$wpdb->query( "CREATE TABLE {$wpdb->prefix}frm_payments ( id BIGINT )" );' ); - } - - public function test_ignores_existing_identifier_placeholder() { - $this->assert_clean( "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM %i WHERE id = %d', \$wpdb->prefix . 'frm_items', \$id ) );" ); - } - - public function test_ignores_literal_table_name() { - $this->assert_clean( "\$wpdb->query( \$wpdb->prepare( 'DELETE FROM wp_frm_items WHERE id = %d', \$id ) );" ); - } - - public function test_ignores_frmdb_helper_table_argument() { - $this->assert_clean( "\$count = FrmDb::get_var( 'frm_forms', array( 'id' => \$id ), 'COUNT(*)' );" ); - } - - public function test_ignores_interpolated_alias_after_on() { - $this->assert_clean( '$x = $wpdb->prepare( "SELECT * FROM %i em{$o_key} WHERE em{$o_key}.field_id = %d", $wpdb->prefix . \'frm_item_metas\', $o_field->id );' ); - } - - public function test_ignores_interpolated_alias_in_join_condition() { - $this->assert_clean( - '$y = $wpdb->prepare( "SELECT * FROM %i pm{$o_key} INNER JOIN %i it ON pm{$o_key}.post_id = it.post_id' - . ' WHERE it.id = %d", $wpdb->postmeta, $wpdb->prefix . \'frm_items\', $id );' - ); - } -} From 505f6487bf98525b92fd01fb70e9e251da52d92f Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Mon, 23 Nov 2026 09:34:41 -0400 Subject: [PATCH 3/5] fix(sniffs): preserve quoted table suffixes --- .../Sniffs/Security/PreferIdentifierPlaceholderSniff.php | 5 +++-- phpcs-sniffs/tests/README.md | 2 +- .../tests/fixtures/PreferIdentifierPlaceholder.fixed | 4 ++++ phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc | 4 ++++ phpcs-sniffs/tests/fixtures/expected.json | 5 ++++- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php index 3a5cae5336..2952a24075 100644 --- a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php +++ b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php @@ -452,8 +452,9 @@ private function collect_cross_part_ref( $parts, $start ) { continue; } - if ( 'sq' !== $part['type'] ) { - break; + if ( 'dq' === $part['type'] && false !== strpbrk( $part['content'], '$\\' ) ) { + // Escapes or interpolation can continue the identifier at runtime. + return false; } if ( ! preg_match( '/^([A-Za-z0-9_]+)/', $part['content'], $match ) ) { diff --git a/phpcs-sniffs/tests/README.md b/phpcs-sniffs/tests/README.md index 777e2ed81c..9d61c380c0 100644 --- a/phpcs-sniffs/tests/README.md +++ b/phpcs-sniffs/tests/README.md @@ -22,4 +22,4 @@ Detection is deliberately conservative: - SQL placeholder lists must match the helper's comma and space delimiter exactly. Different delimiters and expressions with side effects are left alone. - Column queries require explicit column mode, or a direct `wp_list_pluck()` of the single named column selected by `FrmDb::get_results()`. Indexed extraction and multi-column queries are left alone. - WordPress attribute helpers replace only standalone conditional attribute output inside an HTML tag. The original condition is evaluated as a boolean, preserving strict and loose comparisons. Static attributes, ARIA values, and output containing additional attributes are left alone. -- Identifier placeholders apply only when the whole query is literals plus the table expressions. Another dynamic SQL fragment, numbered placeholders, an array of `prepare()` arguments, and an unprepared query holding a percent sign or an interpolated value are left alone, because the rewrite would have to guess the argument order or change how the query is read. +- Identifier placeholders apply only when the whole query is literals plus the table expressions. Another dynamic SQL fragment, numbered placeholders, heredoc SQL, an array of `prepare()` arguments, and an unprepared query holding a percent sign or an interpolated value are left alone, because the rewrite would have to guess the argument order or change how the query is read. diff --git a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed index a80f6a73e7..de4007cfb0 100644 --- a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed +++ b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed @@ -51,3 +51,7 @@ $payments = $wpdb->get_results( $wpdb->prefix . 'frm_payments' ) ); +$wpdb->prepare( 'SELECT id FROM %i WHERE id=%d', $wpdb->prefix . 'frm_items', $id ); +$wpdb->prepare( 'SELECT id FROM %i WHERE id=%d', $wpdb->prefix . 'frm_items', $id ); +$wpdb->prepare( 'SELECT id FROM %i WHERE id=%d', $wpdb->prefix . 'frm_items', $id ); +$wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items\nWHERE id=%d", $id ); diff --git a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc index e3cb0c13f6..2f94aaebfc 100644 --- a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc +++ b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc @@ -48,3 +48,7 @@ $payments = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}frm_payments WHERE completed is NOT NULL" ) ); +$wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items WHERE id=%d", $id ); +$wpdb->prepare( "SELECT id FROM " . $wpdb->prefix . "frm_items WHERE id=%d", $id ); +$wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items" . ' WHERE id=%d', $id ); +$wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items\nWHERE id=%d", $id ); diff --git a/phpcs-sniffs/tests/fixtures/expected.json b/phpcs-sniffs/tests/fixtures/expected.json index f845bf0d3b..77adc6cc65 100644 --- a/phpcs-sniffs/tests/fixtures/expected.json +++ b/phpcs-sniffs/tests/fixtures/expected.json @@ -52,6 +52,9 @@ 23, 24, 24, - 48 + 48, + 51, + 52, + 53 ] } From e2442a84c517953208e0175fae3354d96e63c298 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 25 Nov 2026 09:35:57 -0400 Subject: [PATCH 4/5] fix(sniffs): skip identifiers split across string parts PreferIdentifierPlaceholder rewrote two shapes into broken SQL. A table name cut by an unbalanced backtick bound only its first half and left a stray backtick, and a name continued by a second literal left the suffix glued to %i. Both are now left alone, and the fixtures cover them. Co-Authored-By: Claude Opus 5.5 --- .../PreferIdentifierPlaceholderSniff.php | 31 ++++++++++++------- phpcs-sniffs/tests/README.md | 2 +- .../PreferIdentifierPlaceholder.fixed | 3 ++ .../fixtures/PreferIdentifierPlaceholder.inc | 3 ++ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php index 2952a24075..159f6a9932 100644 --- a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php +++ b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php @@ -349,6 +349,7 @@ private function is_create_table_statement( $parts ) { * 'expr' => string (PHP expression to move into the prepare arguments), * 'parts' => array (part indexes consumed, cross_part only), * 'lead_word' => string (leading identifier characters absorbed from the following literal, cross_part only), + * 'unfixable' => bool (true when the identifier is cut off by an unbalanced backtick, in_string only), * ). * * @param array $parts The concatenation parts. @@ -408,19 +409,16 @@ private function find_in_string_refs( $part, $p ) { $tickAfter = $matches[3][ $index ][0]; $startOffset = $matches[1][ $index ][1]; - if ( '' !== $tickBefore && '' === $tickAfter ) { - // Unbalanced backtick, the identifier continues in another part. Not safely fixable. - $tickAfter = ''; - } - $refs[] = array( - 'type' => 'in_string', - 'ptr' => $part['start'], - 'part' => $p, - 'offset' => $startOffset, - 'length' => strlen( $tickBefore ) + strlen( $text ) + strlen( $tickAfter ), - 'text' => $text, - 'expr' => $this->segments_to_expression( $text ), + 'type' => 'in_string', + 'ptr' => $part['start'], + 'part' => $p, + 'offset' => $startOffset, + 'length' => strlen( $tickBefore ) + strlen( $text ) + strlen( $tickAfter ), + 'text' => $text, + 'expr' => $this->segments_to_expression( $text ), + // An unbalanced backtick means the identifier continues in another part. Not safely fixable. + 'unfixable' => '' !== $tickBefore && '' === $tickAfter, ); } @@ -469,6 +467,11 @@ private function collect_cross_part_ref( $parts, $start ) { continue; } + if ( $match[1] === $part['content'] && $p + 1 < $count && preg_match( '/^[A-Za-z0-9_]/', $parts[ $p + 1 ]['content'] ) ) { + // The identifier continues in the next literal, so the whole name cannot be bound. Not safely fixable. + return false; + } + $exprBits[] = "'" . $match[1] . "'"; $leadWord = $match[1]; break; @@ -658,6 +661,10 @@ private function refs_are_fixable( $parts, $refs ) { $refPartIndexes = array(); foreach ( $refs as $ref ) { + if ( ! empty( $ref['unfixable'] ) ) { + return false; + } + if ( 'cross_part' === $ref['type'] ) { foreach ( $ref['parts'] as $p ) { $refPartIndexes[ $p ] = true; diff --git a/phpcs-sniffs/tests/README.md b/phpcs-sniffs/tests/README.md index 9d61c380c0..71d38fb1db 100644 --- a/phpcs-sniffs/tests/README.md +++ b/phpcs-sniffs/tests/README.md @@ -22,4 +22,4 @@ Detection is deliberately conservative: - SQL placeholder lists must match the helper's comma and space delimiter exactly. Different delimiters and expressions with side effects are left alone. - Column queries require explicit column mode, or a direct `wp_list_pluck()` of the single named column selected by `FrmDb::get_results()`. Indexed extraction and multi-column queries are left alone. - WordPress attribute helpers replace only standalone conditional attribute output inside an HTML tag. The original condition is evaluated as a boolean, preserving strict and loose comparisons. Static attributes, ARIA values, and output containing additional attributes are left alone. -- Identifier placeholders apply only when the whole query is literals plus the table expressions. Another dynamic SQL fragment, numbered placeholders, heredoc SQL, an array of `prepare()` arguments, and an unprepared query holding a percent sign or an interpolated value are left alone, because the rewrite would have to guess the argument order or change how the query is read. +- Identifier placeholders apply only when the whole query is literals plus the table expressions. Another dynamic SQL fragment, numbered placeholders, heredoc SQL, a table name split across two literals or cut by an unbalanced backtick, an array of `prepare()` arguments, and an unprepared query holding a percent sign or an interpolated value are left alone, because the rewrite would have to guess the argument order or change how the query is read. diff --git a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed index de4007cfb0..d1f6840761 100644 --- a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed +++ b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed @@ -55,3 +55,6 @@ $wpdb->prepare( 'SELECT id FROM %i WHERE id=%d', $wpdb->prefix . 'frm_items', $i $wpdb->prepare( 'SELECT id FROM %i WHERE id=%d', $wpdb->prefix . 'frm_items', $id ); $wpdb->prepare( 'SELECT id FROM %i WHERE id=%d', $wpdb->prefix . 'frm_items', $id ); $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items\nWHERE id=%d", $id ); +$wpdb->prepare( "SELECT id FROM `{$wpdb->prefix}frm_" . "items` WHERE id=%d", $id ); +$wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . 'frm_items' . "_old WHERE id=%d", $id ); +$wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . 'frm_items' . '_old WHERE id=%d', $id ); diff --git a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc index 2f94aaebfc..73fb156597 100644 --- a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc +++ b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc @@ -52,3 +52,6 @@ $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items WHERE id=%d", $id $wpdb->prepare( "SELECT id FROM " . $wpdb->prefix . "frm_items WHERE id=%d", $id ); $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items" . ' WHERE id=%d', $id ); $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items\nWHERE id=%d", $id ); +$wpdb->prepare( "SELECT id FROM `{$wpdb->prefix}frm_" . "items` WHERE id=%d", $id ); +$wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . 'frm_items' . "_old WHERE id=%d", $id ); +$wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . 'frm_items' . '_old WHERE id=%d', $id ); From cd008a80414db682b06bc7dc728be990950991d8 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 25 Nov 2026 15:11:37 -0400 Subject: [PATCH 5/5] Skip identifier placeholder fixes when the table name does not end cleanly The sniff now checks the character after a table identifier, in the same literal or the next one, before binding it to %i. Only whitespace, a comma, a semicolon, a parenthesis or the end of the query are accepted. This stops phpcbf from writing broken SQL for an interpolated prefix split across two literals and for a closing backtick held in a later literal. Co-Authored-By: Claude Opus 5.5 --- .../PreferIdentifierPlaceholderSniff.php | 68 +++++++++++++++---- phpcs-sniffs/tests/README.md | 2 +- .../PreferIdentifierPlaceholder.fixed | 3 + .../fixtures/PreferIdentifierPlaceholder.inc | 3 + 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php index 159f6a9932..b232777734 100644 --- a/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php +++ b/phpcs-sniffs/Formidable/Sniffs/Security/PreferIdentifierPlaceholderSniff.php @@ -364,7 +364,7 @@ private function find_table_refs( $parts ) { $part = $parts[ $p ]; if ( 'dq' === $part['type'] ) { - $refs = array_merge( $refs, $this->find_in_string_refs( $part, $p ) ); + $refs = array_merge( $refs, $this->find_in_string_refs( $parts, $p ) ); } if ( 'expr' === $part['type'] || $p + 1 >= $count || 'expr' !== $parts[ $p + 1 ]['type'] ) { @@ -388,12 +388,13 @@ private function find_table_refs( $parts ) { /** * Finds table references fully contained inside a double quoted string part. * - * @param array $part The dq part. - * @param int $p The part index. + * @param array $parts The concatenation parts. + * @param int $p The index of the dq part. * * @return array */ - private function find_in_string_refs( $part, $p ) { + private function find_in_string_refs( $parts, $p ) { + $part = $parts[ $p ]; $interp = self::INTERP_SEGMENT; $refRun = '(?:[A-Za-z0-9_]+)?(?:' . $interp . '(?:[A-Za-z0-9_]+)?)+'; @@ -408,17 +409,19 @@ private function find_in_string_refs( $part, $p ) { $tickBefore = $matches[1][ $index ][0]; $tickAfter = $matches[3][ $index ][0]; $startOffset = $matches[1][ $index ][1]; + $length = strlen( $tickBefore ) + strlen( $text ) + strlen( $tickAfter ); + $rest = substr( $part['content'], $startOffset + $length ); $refs[] = array( 'type' => 'in_string', 'ptr' => $part['start'], 'part' => $p, 'offset' => $startOffset, - 'length' => strlen( $tickBefore ) + strlen( $text ) + strlen( $tickAfter ), + 'length' => $length, 'text' => $text, 'expr' => $this->segments_to_expression( $text ), - // An unbalanced backtick means the identifier continues in another part. Not safely fixable. - 'unfixable' => '' !== $tickBefore && '' === $tickAfter, + // An unbalanced backtick, or SQL that continues the name in another part, cannot be bound safely. + 'unfixable' => ( '' !== $tickBefore && '' === $tickAfter ) || ! $this->identifier_ends_cleanly( $parts, $p, $rest ), ); } @@ -438,6 +441,8 @@ private function collect_cross_part_ref( $parts, $start ) { $exprBits = array(); $consumed = array(); $leadWord = ''; + $rest = ''; + $restPart = $count - 1; $p = $start; while ( $p < $count ) { @@ -456,6 +461,8 @@ private function collect_cross_part_ref( $parts, $start ) { } if ( ! preg_match( '/^([A-Za-z0-9_]+)/', $part['content'], $match ) ) { + $rest = $part['content']; + $restPart = $p; break; } @@ -467,13 +474,10 @@ private function collect_cross_part_ref( $parts, $start ) { continue; } - if ( $match[1] === $part['content'] && $p + 1 < $count && preg_match( '/^[A-Za-z0-9_]/', $parts[ $p + 1 ]['content'] ) ) { - // The identifier continues in the next literal, so the whole name cannot be bound. Not safely fixable. - return false; - } - $exprBits[] = "'" . $match[1] . "'"; $leadWord = $match[1]; + $rest = substr( $part['content'], strlen( $match[1] ) ); + $restPart = $p; break; } @@ -481,6 +485,16 @@ private function collect_cross_part_ref( $parts, $start ) { return false; } + if ( '`' === substr( $rest, 0, 1 ) && preg_match( '/`\s*$/', $parts[ $start - 1 ]['content'] ) ) { + // The closing backtick sits in the same literal as the end of the name, so the fixer removes it. + $rest = substr( $rest, 1 ); + } + + if ( ! $this->identifier_ends_cleanly( $parts, $restPart, $rest ) ) { + // The identifier continues past what can be bound, or a backtick is left behind. Not safely fixable. + return false; + } + return array( 'type' => 'cross_part', 'ptr' => $parts[ $start ]['start'], @@ -491,6 +505,36 @@ private function collect_cross_part_ref( $parts, $start ) { ); } + /** + * Checks whether the SQL right after a table identifier ends the identifier. + * + * The next character, in the same literal or the next one, must be whitespace, + * a comma, a semicolon, a parenthesis, or the end of the query. Anything else, + * such as identifier characters, a backtick or interpolation, would continue the + * name or leave a stray character next to the %i placeholder. + * + * @param array $parts The concatenation parts. + * @param int $p The index of the part holding the end of the identifier. + * @param string $rest The text of that part after the identifier. + * + * @return bool + */ + private function identifier_ends_cleanly( $parts, $p, $rest ) { + if ( '' === $rest ) { + if ( ! isset( $parts[ $p + 1 ] ) ) { + return true; + } + + if ( 'expr' === $parts[ $p + 1 ]['type'] ) { + return false; + } + + $rest = $parts[ $p + 1 ]['content']; + } + + return (bool) preg_match( '/^[\s,;()]/', $rest ); + } + /** * Converts an interpolated identifier text into an equivalent PHP expression. * diff --git a/phpcs-sniffs/tests/README.md b/phpcs-sniffs/tests/README.md index 71d38fb1db..74f04faf10 100644 --- a/phpcs-sniffs/tests/README.md +++ b/phpcs-sniffs/tests/README.md @@ -22,4 +22,4 @@ Detection is deliberately conservative: - SQL placeholder lists must match the helper's comma and space delimiter exactly. Different delimiters and expressions with side effects are left alone. - Column queries require explicit column mode, or a direct `wp_list_pluck()` of the single named column selected by `FrmDb::get_results()`. Indexed extraction and multi-column queries are left alone. - WordPress attribute helpers replace only standalone conditional attribute output inside an HTML tag. The original condition is evaluated as a boolean, preserving strict and loose comparisons. Static attributes, ARIA values, and output containing additional attributes are left alone. -- Identifier placeholders apply only when the whole query is literals plus the table expressions. Another dynamic SQL fragment, numbered placeholders, heredoc SQL, a table name split across two literals or cut by an unbalanced backtick, an array of `prepare()` arguments, and an unprepared query holding a percent sign or an interpolated value are left alone, because the rewrite would have to guess the argument order or change how the query is read. +- Identifier placeholders apply only when the whole query is literals plus the table expressions. Another dynamic SQL fragment, numbered placeholders, heredoc SQL, a table name split across two literals, cut by an unbalanced backtick, or followed by anything other than whitespace, a comma, a semicolon, a parenthesis or the end of the query, an array of `prepare()` arguments, and an unprepared query holding a percent sign or an interpolated value are left alone, because the rewrite would have to guess the argument order or change how the query is read. diff --git a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed index d1f6840761..ff9a2915f3 100644 --- a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed +++ b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.fixed @@ -58,3 +58,6 @@ $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items\nWHERE id=%d", $i $wpdb->prepare( "SELECT id FROM `{$wpdb->prefix}frm_" . "items` WHERE id=%d", $id ); $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . 'frm_items' . "_old WHERE id=%d", $id ); $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . 'frm_items' . '_old WHERE id=%d', $id ); +$wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_" . "items WHERE id=%d", $id ); +$wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_items" . "_old WHERE id=%d", $id ); +$wpdb->prepare( 'SELECT id FROM `' . $wpdb->prefix . 'frm_items' . '` WHERE id=%d', $id ); diff --git a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc index 73fb156597..5bd15927bd 100644 --- a/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc +++ b/phpcs-sniffs/tests/fixtures/PreferIdentifierPlaceholder.inc @@ -55,3 +55,6 @@ $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . "frm_items\nWHERE id=%d", $i $wpdb->prepare( "SELECT id FROM `{$wpdb->prefix}frm_" . "items` WHERE id=%d", $id ); $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . 'frm_items' . "_old WHERE id=%d", $id ); $wpdb->prepare( 'SELECT id FROM ' . $wpdb->prefix . 'frm_items' . '_old WHERE id=%d', $id ); +$wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_" . "items WHERE id=%d", $id ); +$wpdb->prepare( "SELECT id FROM {$wpdb->prefix}frm_items" . "_old WHERE id=%d", $id ); +$wpdb->prepare( 'SELECT id FROM `' . $wpdb->prefix . 'frm_items' . '` WHERE id=%d', $id );