Magic quotes

From Wikipedia, the free encyclopedia

Magic quotes are a controversial feature of the PHP scripting language, intended to help prevent inexperienced developers from writing code which is vulnerable to SQL injection attacks.

Contents

[edit] Concept

The rationale behind magic quotes is to "help code written by beginners from being dangerous."[1] Single quotes, double quotes, backslashes and null characters in all user-supplied data all have a backslash prepended to them before being passed to the script in the $_GET, $_POST and $_COOKIE global variables. Developers can then in theory use string concatenation safely to construct SQL queries with data provided by the user.

[edit] Criticism

Magic quotes are enabled by default in new installations of PHP, and since their operation is behind the scenes and not immediately obvious, developers may be unaware of their existence and the potential problems that they can introduce. The PHP documentation points out several pitfalls and recommends that, despite being enabled by default, they be disabled.[2]

Problems with magic quotes include:

  • Not all data that is supplied by the user is intended for insertion into a database. It may be rendered directly to the screen, stored in a session, or previewed before saving. This can result in backslashes being added where they are not wanted and being shown to the end user. This bug often creeps in even in widely used software.[3]
  • Not all data that is supplied by the user and used in a database query is obtained directly from sources protected by magic quotes. For instance, a user-supplied value might be inserted into a database -- protected by magic quotes -- and later retrieved from the database and used in a subsequent database operation. The latter use is not protected by magic quotes, and a naive programmer used to relying on them may be unaware of the need to protect it explicitly.
  • Magic quotes also use the generic functionality provided by PHP's addslashes() function, which is not Unicode aware and still subject to SQL injection vulnerabilities in some multi-byte character encodings. Database-specific functions such as mysql_real_escape_string() or, where possible, prepared queries with bound parameters are preferred.[4][5]
  • Portability is an issue if an application is coded with the assumption that magic quotes are enabled and is then moved to a server where they are disabled.
  • Adding magic quotes and subsequently removing them where appropriate incurs a small but unnecessary performance overhead.
  • Magic quotes do not protect against other common security vulnerabilities such as cross-site scripting attacks or SMTP header injection attacks.

In November 2005 the core PHP developers decided on account of these problems that the magic quotes feature would be removed from PHP 6.[6]

[edit] Other approaches

  • Some languages such as Perl[7] and Ruby[8] opt for an approach involving data tainting, where user input is considered "tainted" and can not be used to construct SQL query strings until explicitly marked as trustworthy, usually after validation and/or encoding.
  • Joel Spolsky has suggested using a form of Hungarian notation that indicates whether data is safe or unsafe.[9]
  • Modern database engines and libraries use parametrised queries to pass data to the database separately from SQL commands, greatly reducing the need to escape data before constructing the queries.

[edit] See also

[edit] External links

[edit] References

  1. ^ PHP:Why use magic quotes?. PHP documentation. Retrieved on 2007-02-19.
  2. ^ PHP:Why not to use magic quotes. PHP documentation. Retrieved on 2007-02-19.
  3. ^ Quotation marks are double escaped when editing a comment. WordPress issue tracker. Retrieved on 2007-02-19.
  4. ^ Chris Shiflett. addslashes() versus mysql_real_escape_string(). Retrieved on 2007-02-19.
  5. ^ MySQL AB. Changes in release 5.0.22 (24 May 2006). MySQL 5.0 Reference Manual. Retrieved on 2007-02-19.
  6. ^ PHP Group (2005-11-12). Minutes PHP Developers Meeting. Retrieved on 2007-02-19.
  7. ^ Dan Ragle (2006-04-18). Introduction to Perl's Taint Mode. webreference.com. Retrieved on 2007-03-21.
  8. ^ Locking Ruby in the Safe. Programming Ruby. Retrieved on 2007-03-21.
  9. ^ Joel Spolsky (2005-05-11). Making Wrong Code Look Wrong. Joel on Software: Painless Software Management. Retrieved on 2007-02-19.