Magic quotes

Magic quotes

Magic quotes is a controversial feature of the PHP scripting language, which was introduced to help newcomers write functioning SQL commands without requiring manual escaping. It was later described and widely misinterpreted as help to prevent inexperienced developers from writing code which is vulnerable to SQL injection attacks. This feature is officially deprecated as of PHP 5.3.0, and removed in PHP 5.4 due to security concerns.[1]

Contents

Concept

The current revision of the PHP manual mentions the rationale behind magic quotes is to "help [prevent] code written by beginners from being dangerous."[2] It was however originally introduced in PHP2 as php.h compile-time setting for msql and only escaping single quotes "making it easier to pass form data directly to msql queries"[3]. It originally was intended as "convenience feature, not as security feature."[4].[5]

The use scope was expanded in PHP3. 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, $_REQUEST, $_POST and $_COOKIE global variables. Developers can then in theory use string concatenation safely to construct SQL queries with data provided by the user. (Which was true at best around PHP2 and PHP3, when the primarily supported databases only allowed 1-byte character sets.)

Criticism

Magic quotes were enabled by default in new installations of PHP3 and 4, 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 should be disabled.[6]

Problems with magic quotes include:

  • Not all data that are supplied by the user are intended for insertion into a database. They 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 into even widely used software.[7]
  • Not all data that are supplied by the user and used in a database query are 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.[8][9]
  • While many DBMS support escaping quotes with a backslash, the standard actually calls for using another quote. Magic quotes offer no protection for databases not set up to support escaping quotes with a backslash.
  • 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, or the other way round.
  • 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.[10]

Other approaches

  • Some languages such as Perl[11] and Ruby[12] opt for an approach involving data tainting, where data from untrusted sources, such as user input, are considered "tainted" and can not be used for dangerous operations until explicitly marked as trustworthy, usually after validation and/or encoding. Since the construction of SQL queries is considered "dangerous" in this context, this forces the programmer to address the problem. Tainting does not solve the problem, but it does highlight those instances where there is a problem so that the programmer is able to solve them appropriately.
  • Joel Spolsky has suggested using a form of Hungarian notation that indicates whether data are safe or unsafe.[13]
  • Modern database engines and libraries use parameterised queries to pass data to the database separately from SQL commands, greatly reducing the need to escape data before constructing the queries.

References

  1. ^ "PHP: Magic Quotes". http://php.net/manual/en/security.magicquotes.php. Retrieved 2009-05-02. 
  2. ^ "PHP:Why use magic quotes?". PHP documentation. http://uk.php.net/manual/en/security.magicquotes.why.php. Retrieved 2007-02-19. 
  3. ^ "If the MAGIC_QUOTES variable is defined in the php.h file then these quotes will be automatically escaped making it easier to pass form data directly to msql queries.". http://www.php.net/manual/phpfi2.php#msql_support. Retrieved 1997-04-23. 
  4. ^ "Magic Quotes is oft-understood, even by journeymen PHP programmers". http://www.gthelp.com/showpost.php?s=1f2dcfbf42564c12dcc84d35cbedfc4d&p=184969&postcount=3. 
  5. ^ "Re: [PHP3 what are magic_quotes?"]. PHP-dev mailing list. http://marc.info/?l=php-general&m=93575922621574&w=2. Retrieved 1999-08-27. 
  6. ^ "PHP:Why not to use magic quotes". PHP documentation. http://uk.php.net/manual/en/security.magicquotes.whynot.php. Retrieved 2007-02-19. 
  7. ^ "Quotation marks are double escaped when editing a comment". WordPress issue tracker. http://trac.wordpress.org/ticket/2768. Retrieved 2007-02-19. 
  8. ^ Chris Shiflett. "addslashes() versus mysql_real_escape_string()". http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string. Retrieved 2007-02-19. 
  9. ^ MySQL AB. "Changes in release 5.0.22 (24 May 2006)". MySQL 5.0 Reference Manual. http://dev.mysql.com/doc/refman/5.0/en/news-5-0-22.html. Retrieved 2007-02-19. 
  10. ^ PHP Group (2005-11-12). "Minutes PHP Developers Meeting". http://www.php.net/~derick/meeting-notes.html#magic-quotes. Retrieved 2007-02-19. 
  11. ^ Dan Ragle (2006-04-18). "Introduction to Perl's Taint Mode". webreference.com. http://www.webreference.com/programming/perl/taint/. Retrieved 2007-03-21. 
  12. ^ "Locking Ruby in the Safe". Programming Ruby. http://www.rubycentral.com/book/taint.html. Retrieved 2007-03-21. [dead link]
  13. ^ Joel Spolsky (2005-05-11). "Making Wrong Code Look Wrong". Joel on Software: Painless Software Management. http://www.joelonsoftware.com/articles/Wrong.html. Retrieved 2007-02-19. 

External links


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Magic — Contents 1 Computing 2 Gaming 3 Film 4 Music …   Wikipedia

  • Magic Babe Ning — Taken at TV Shoot, May 2011 Born Ning Cai 16 October 1982(1982 10 16) Singapore R …   Wikipedia

  • Magic Hollow (album) — Magic Hollow Box set by The Beau Brummels Released June 21, 2005 Recorded 1964 1968 …   Wikipedia

  • Magic Wand (software) — Magic Wand is the name of a piece of word processing software for CP/M based computers first released in 1979, written by Mike Griffin. Les Pinter was COO and Bill Radding was VP of Marketing. It was marketed in 1980 with such quotes as the most… …   Wikipedia

  • Magic (illusion) — For other varieties of magic, see Magic (disambiguation). Illusionist redirects here. For the artistic tradition, see Illusionism (art). For other uses, see The Illusionist (disambiguation). Magic The Conjurer (painting), 1475 1480, by Hieronymus …   Wikipedia

  • Magic Johnson — Earvin Johnson redirects here. For the NBA center, see Ervin Johnson. Earvin Magic Johnson No. 32 …   Wikipedia

  • Magic in Harry Potter — In the Harry Potter series created by J. K. Rowling, magic is depicted as a natural force that can be used to override the usual laws of nature. Many fictional magical creatures exist in the series, while ordinary creatures sometimes exhibit new… …   Wikipedia

  • Magic Johnson — Spielerinformationen Voller Name Earvin Johnson Jr …   Deutsch Wikipedia

  • Magic Mutt — Infobox Television episode Title = Magic Mutt Series = Garfield and Friends Caption = Season = 1 Episode = 28 Airdate = November 19, 1988 Production = G010 A Writer = Mark Evanier Director = John Walker Steve Clark John Sparey Guests = Episode… …   Wikipedia

  • Magic: The Gathering Pro Tour season 2001–02 — 2001–02 Pro Tour season Pro Player of the Year Kai Budde Rookie of the Year Farid Meraghni …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”