10.02.2013 Views

PHP Programming Language - OpenLibra

PHP Programming Language - OpenLibra

PHP Programming Language - OpenLibra

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Magic quotes 113<br />

Magic quotes<br />

Magic quotes is a controversial feature of the <strong>PHP</strong> scripting language, intended to help prevent inexperienced<br />

developers from writing code which is vulnerable to SQL injection attacks. This feature is officially deprecated as of<br />

<strong>PHP</strong> 5.3.0, and removed in <strong>PHP</strong> 6 due to security concerns. [1]<br />

Concept<br />

The rationale behind magic quotes is to "help [prevent] code written by beginners from being dangerous." [2] Single<br />

quotes, double quotes, backslashes and null characters in all user-supplied data all have a backslash prepended to<br />

them before being passed to the script in the $_GET, $_REQUEST, $_POST and $_COOKIE global variables.<br />

Developers can then in theory use string concatenation safely to construct SQL queries with data provided by the<br />

user.<br />

Criticism<br />

Magic quotes were enabled by default in new installations of <strong>PHP</strong>3 and 4, and since their operation is behind the<br />

scenes and not immediately obvious, developers may be unaware of their existence and the potential problems that<br />

they can introduce. The <strong>PHP</strong> documentation points out several pitfalls and recommends that, despite being enabled<br />

by default, they should be disabled. [3]<br />

Problems with magic quotes include:<br />

• Not all data that are supplied by the user are intended for insertion into a database. They may be rendered directly<br />

to the screen, stored in a session, or previewed before saving. This can result in backslashes being added where<br />

they are not wanted and being shown to the end user. This bug often creeps into even widely used software. [4]<br />

• Not all data that are supplied by the user and used in a database query are obtained directly from sources protected<br />

by magic quotes. For instance, a user-supplied value might be inserted into a database — protected by magic<br />

quotes — and later retrieved from the database and used in a subsequent database operation. The latter use is not<br />

protected by magic quotes, and a naive programmer used to relying on them may be unaware of the need to<br />

protect it explicitly.<br />

• Magic quotes also use the generic functionality provided by <strong>PHP</strong>'s addslashes() function, which is not Unicode<br />

aware and still subject to SQL injection vulnerabilities in some multi-byte character encodings. Database-specific<br />

functions such as mysql_real_escape_string() or, where possible, prepared queries with bound parameters are<br />

[5] [6]<br />

preferred.<br />

• While many DBMS support escaping quotes with a backslash, the standard actually calls for using another quote.<br />

Magic quotes offer no protection for databases not set up to support escaping quotes with a backslash.<br />

• Portability is an issue if an application is coded with the assumption that magic quotes are enabled and is then<br />

moved to a server where they are disabled, or the other way round.<br />

• Adding magic quotes and subsequently removing them where appropriate incurs a small but unnecessary<br />

performance overhead.<br />

• Magic quotes do not protect against other common security vulnerabilities such as cross-site scripting attacks or<br />

SMTP header injection attacks.<br />

In November 2005 the core <strong>PHP</strong> developers decided on account of these problems that the magic quotes feature<br />

would be removed from <strong>PHP</strong> 6. [7]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!