Make WordPress Core

Opened 3 years ago

Last modified 2 days ago

#60505 new defect (bug)

Column names in wpdb->insert aren't sanitized.

Reported by: kazet Owned by:
Priority: normal Milestone: Awaiting Review
Component: Database Version: 6.4.3
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Column names in wpdb->insert aren't sanitized, although the documentation says: "Data to insert (in column => value pairs). Both $data columns and $data values should be “raw” (neither should be SQL escaped)."

Steps To Reproduce:

<?php
global $wpdb;
$wpdb->insert("wp_posts", array("id`) VALUES (sleep(1)); -- " => 0));

This will sleep one second.

The WordPress security team has reviewed this and asked to open a public ticket.

Attachments (1)

60505.diff​ (1.3 KB ) - added by peterwilsoncc 3 years ago.

Download all attachments as: .zip

Change History (4)

#1 @swissspidy
3 years ago

  • Component General → Database

#2 @peterwilsoncc
3 years ago

60505.diff​ is a proof of concept to use the ​identifier placeholders introduced in WordPress 6.2.

wpdb::update() is also affected and would need similar code.

This ticket was mentioned in ​PR #15802 on ​WordPress/wordpress-develop by ​@salvatorecorsi.


2 days ago
#3

  • Keywords has-patch has-unit-tests added

wpdb::insert(), replace(), update() and delete() accept raw column names, but interpolate them between backticks before preparing the query. A valid name containing a backtick breaks the SQL, and a name containing %s can be mistaken for a value placeholder.

Pass column names as %i arguments instead. INSERT/REPLACE place the identifier arguments before the value arguments; UPDATE and DELETE keep each identifier beside its optional value. NULL assignments and IS NULL conditions still omit the value argument. Empty INSERT/REPLACE data continues to return false rather than becoming a default-row insert.

This addresses column identifiers across the CRUD methods. It does not change table-name interpolation or introduce new identifier-validity rules. Related PR #9292 changes validation inside prepare(); it does not update these CRUD call sites. The linked public ticket records that the security team reviewed the report and requested a public ticket.

Testing

Use a temporary table with a valid column named backtick or percent%s`, then insert, replace, update and delete through the public CRUD methods. The operations should round-trip their values without SQL errors. The regression tests create and remove their own temporary tables and also cover NULL data/conditions, string/integer/float formats, ordinary column names and zero affected rows.

  • The unchanged source fails 12 of the 18 identifier cases; the six ordinary-name controls pass.
  • The fixed focused tests pass, including the empty-column-list control (19 tests).
  • The complete Tests_DB class passes in single-site and multisite: 531 tests each.
  • PHP syntax, coding standards, compatibility and whitespace checks pass.
  • Full PHPStan reports three Windows theme documentation-reference errors, reproduced unchanged on the exact base; no errors are reported in the changed production file.

Use of AI Tools

AI assistance: Yes
Tool(s): Codex
Used for: Investigating the ticket and related PRs, implementation, regression tests, validation, and independent automated code review. This contribution has not received a human review from the account owner.

Note: See TracTickets for help on using tickets.