Closed (fixed)
Project:
Drupal core
Version:
5.x-dev
Component:
database system
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
16 Apr 2007 at 22:00 UTC
Updated:
11 Jul 2007 at 03:47 UTC
Jump to comment: Most recent file
Comments
Comment #1
meba commentedThis appears in both 6.x and 5.x, altough little bit differently. Submitting a patch against 6.x.
Background:
db_query("SELECT ... FROM {$table}") doesn't work because PHP itself does variable replacement using {$table}, see:
http://www.php.net/manual/en/language.types.string.php#language.types.st...
Comment #2
gábor hojtsyLooks good, committed! Needs to be ported back to Drupal 5.x.
Comment #3
meba commented5.x is using:
db_query("SELECT data, created, headers, expire FROM {%s} WHERE cid = '%s'", $table, $key) instead of 6.x direct "{$table}" which is also considered bad.
This patch is porting 6.x syntax to 5.x
Comment #4
gábor hojtsyFor 5.x.
Comment #5
gábor hojtsyJust for the records. {%s}, $table is bad because the prefixing is done *before* the placeholders are replaced, so cache tables can only have the default prefix, and no table dependent prefixing. It might be good to sanitize the table name (which did not seem to be required in Drupal 6 before this patch).
Comment #6
ChrisKennedy commentedThe same patch (without the string concatenation style errors) was already sitting in the queue in http://drupal.org/node/126867 btw...
Comment #7
gábor hojtsyDoh, fixed coding style issues, thanks for noting! The 5.x patch still needs review from Niel.
Comment #8
gábor hojtsyMeba, although I fixed the coding style problem introduced earlier to Drupal 6 with your patch, the Drupal 5.x backport has that still. ".$table." should be ". $table ." (note the extra spaces around $table, one on each side.
Comment #9
meba commentedFixing code style.
Comment #10
Fetch commentedIs it absolutely desired that db_query be ignorant of table prefix expansion when used with positional variables (%s, etc.)? If so, the quick modification to cache.inc:cache_set() to include the table in the query string is the best fix.
The code comment for database.inc:db_query() does not indicate this is the case (although it does not talk about handing off to db_prefix_tables for the substitution in its description text at all). If the case is that db_query should do prefix substitutions after variable substitutions, then http://drupal.org/node/152747 has a more robust table prefix expansion (necessary for when serialized data gets inserted in after variable expansion).
Honestly, I don't know why I spent time doing this the hard way, but if the simple way is canonical then arguably a note should be added to the function descriptions of db_query and db_prefix_tables to that effect.
Comment #11
Steven commentedThe goal of using
'%s'placeholder tokens is to ensure there are no SQL injection exploits using strings. Using%s(without quotes) in a query offers no security at all, and sets a bad example.Comment #12
pwolanin commentedlooks fine - applies cleanly - no errors/problems. However, the patch was made from /includes rather than the root directory. Patch attached differs only in this path for the patch.
Comment #13
drumm@Steven- this is how HEAD does it and the cache table name will hopefully never be user input.
Tested and committed to 5.x.
Comment #14
(not verified) commented