Comments

hswong3i’s picture

Title: system_cron(): incorrect query placeholder » incorrect use of %s
Component: system.module » database system
StatusFileSize
new2.06 KB

statistics_title_list():

  1. %s should use for user query argument filtering, not as normal string replacement.
  2. All 3 target fields: totalcount, daycount and timestamp are INT, so should use 0 (integer compare) instead of '0' (string compare).

system_cron():

  1. Both status and query argument are INT, so should use %d instead of %s.
hswong3i’s picture

Title: incorrect use of %s » Siren #12: incorrect use of %s
Assigned: Unassigned » hswong3i

I mark this into my personal research project issue.

hswong3i’s picture

StatusFileSize
new2.06 KB

Patch reroll via latest CVS HEAD. Test without ill effect.

hswong3i’s picture

Priority: Normal » Critical

Just hope to promote if we are able to fix this within D6 RC1.

gábor hojtsy’s picture

Priority: Critical » Normal
Status: Needs review » Needs work

Not critical. The system module hunk looks good, but the other one makes the dbfield unsanitized and therefore a source of possible SQL injection the value needs to be stanitized.

hswong3i’s picture

Title: Siren #12: incorrect use of %s » Siren #12: incorrect use of %s and db_escape_string
Status: Needs work » Needs review
StatusFileSize
new7.04 KB

(I merge http://drupal.org/node/199214 with this issue.)

I try to use db_escape_table(): correct in syntax and function, but not logically suitable (we hope to escape column, not table)...

I try to folk db_escape_table() as db_escape_column(): this seems too ugly and not elegant...

Finally, I try rename db_escape_table() as db_escape_constraint(). As term "constraint" is a superset of "table" and "column", the meaning should be more suitable and general. I dig into all core code, and found that db_escape_table() is now used as internal, only. Hope this change would be acceptable.

gábor hojtsy’s picture

Status: Needs review » Needs work

No, we are not doing API changes this late in the cycle. Contributed modules could just as well use this just as we do.

hswong3i’s picture

Actually... totally agree...

So may I ask for some suggestion? Should I reuse db_escape_table() for incorrect meaning, or folk it as db_escape_column() for a not elegant handling? Either one solution should function correctly.

P.S. maybe we can use embed checking within each function. This should be the least impact to API, but most ugly...

hswong3i’s picture

Status: Needs work » Needs review
StatusFileSize
new3.74 KB

Folk db_escape_table() as db_escape_column().

hswong3i’s picture

StatusFileSize
new3.67 KB

If a simple documentation can solve this logical conflict, it should be the most simple solution?

chx’s picture

Title: Siren #12: incorrect use of %s and db_escape_string » incorrect use of %s and db_escape_string

As Siren is not a project on Drupal.org but an unofficial fork, I am removing it from issue titles to avoid confusion.

hswong3i’s picture

Title: incorrect use of %s and db_escape_string » incorrect use of %s and db_escape_string()
StatusFileSize
new6.32 KB

Patch update and reroll via CVS HEAD. Bugfix with:

  1. Within tablesort_sql(), $sql may sometime contain . as alias.column syntax. It is a special case and so need special handling.
  2. Within tablesort_sql(), $sort is assume as asc or desc ONLY. As we are filtering a SQL reserved word, it is also a special case.
  3. Within database.mysql.inc, database.mysqli.inc and database.pgsql.inc, contain some improper use of %s. db_escape_table() should be a better choice.
hswong3i’s picture

StatusFileSize
new6.31 KB

Fix database.pgsql.inc miss handling.

gábor hojtsy’s picture

Status: Needs review » Needs work

- All this call() ? TRUE : FALSE jumbo could easily be done with (bool) call(); much more nicely.
- I don't understand how the implode() in the tablesort code is better
- Also, why/how are the patterns in the tablesort code are correct? You seem to keep realizing missed use cases, so it seems like a restrictive white list approach.

hswong3i’s picture

StatusFileSize
new6.76 KB

Some update:

  1. Use (bool) instead of TRUE: FALSE
  2. Remove the use of implode() (right, it is just something fancy...)
  3. Patterns in tablesort code should be correct, as it is functioning and logically correct. Right that seems I am now working for a white list approach, and I even trying t restrict $sort as ASC and DESC only (but too fancy, seems not such necessary...)
gábor hojtsy’s picture

OK, looked into the tablesort_sql() case:

- $ts['sql'] seems to be a column name... very bad variable name... why don't you use the usual column name escape then?
- why do you need to do this magic on $ts['sort']? IMHO we should ensure in tablesort_get_sort() that we only return 'asc' or 'desc', and then we can do the same check here as well, as we aim to protect the API from possible SQL injection here as well

hswong3i’s picture

Status: Needs work » Needs review
StatusFileSize
new8.52 KB

- I hate $ts['sql'] too... so rename it as $ts['field'] (I don't know if this will void the rule of not changing API...). It is used internally, and tested as function. On the other hand, as $ts['field'] may represent in "alias.field" format, it is different from normal column filtering.
- Now I give a more explicitly checking for $ts['sort']: only allow ASC or DESC.

gábor hojtsy’s picture

Status: Needs review » Needs work

Yes, changing the array key name counts as an API change, so we should not do it. But changing the internal variable name in tablesort_sql() is not an API change and should be done.

When you have two possible values, in_array($value, array('ASC'? 'DESC')) is much cleaner and quicker then preg-replacing whatever. Why complicate this?

hswong3i’s picture

Status: Needs work » Needs review
StatusFileSize
new8.49 KB

I am also finding for a better solution... Thanks for your suggestion :)

P.S. Should I update statistics_title_list() into in_array() handling? Seems we only allow 3 choices.

gábor hojtsy’s picture

Status: Needs review » Needs work

- How on earth is statistics_title_list() related? Keep the issue focused.
- I said renaming the array index is an API change, as it is used across the different tablesort functions.

hswong3i’s picture

StatusFileSize
new7.01 KB

- statistics_title_list() is related because it use %s for column name filtering, which is also included in previous patches. Since we only allow 'totalcount', 'daycount' and 'timestamp' as valid $dbfield input, I guess we should also use in_array() for a faster and explicitly checking.
- Sorry for misunderstanding, and array key is now keep as unchanged. Only fix function internal variable naming.

hswong3i’s picture

Status: Needs work » Needs review
StatusFileSize
new7.37 KB

Additional minor catch in database.pgsql.inc: db_last_insert_id() is in similar case and so change handle with db_escape_table().

gábor hojtsy’s picture

Status: Needs review » Fixed
StatusFileSize
new7.4 KB

OK, modified the docs included, and committed this.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.