Magic quotes

Uses your system voice; voice and rate in settings.

Snapshot

≈ 4 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

Magic quotes was a feature of the PHP scripting language, wherein strings are automatically escaped—special characters are prefixed with a backslash—before being passed on. It was introduced to help newcomers write functioning SQL commands without requiring manual escaping. It was later described as intended to prevent inexperienced developers from writing code that was vulnerable to SQL injection attacks. This feature was officially deprecated as of PHP 5.3.0 and removed in PHP 5.4, due to security concerns.

Magic quotes was a feature of the PHP scripting language, wherein strings are automatically escaped—special characters are prefixed with a backslash—before being passed on. It was introduced to help newcomers write functioning SQL commands without requiring manual escaping. It was later described as intended to prevent inexperienced developers from writing code that was vulnerable to SQL injection attacks. This feature was officially deprecated as of PHP 5.3.0 and removed in PHP 5.4, due to security concerns.

Criticism

Magic quotes were enabled by default in new installations of PHP 3 and 4, but could be disabled through the magic_quotes_gpc configuration directive. Since the operation of magic quotes was behind the scenes and not immediately obvious, developers may have been unaware of their existence and the potential problems that they could introduce. The PHP documentation pointed out several pitfalls and recommended that, despite being enabled by default, they should be disabled.

Problems with magic quotes included: Not all user supplied data 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 creeped into even widely used software. Not all user supplied data used in database queries 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. Whatever protection magic quotes offer, they only work for quoted strings, leaving other query parts (such as numbers or column names) entirely unprotected, and giving the developer a false feeling of security.

Magic quotes also use the generic functionality provided by PHP's addslashes() function, which is not Unicode-aware and is 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. While many database management systems support escaping quotes with a backslash, the standard actually calls for using another quote.

Thus, magic quotes can only offer protection for databases configured to support escaping quotes with a backslash. Code written assuming magic quotes are disabled will not work in an environment where they are enabled or vice versa. This can be worked around using the ini_get() or ini_set() functions but requires forethought by programmers , and usage of ini_set() is generally seen as bad practice . Adding magic quotes and subsequently removing them where appropriate incurs a small but unnecessary amount of performance overhead.

Magic quotes do not protect against other common, related, security vulnerabilities such as Cross-site scripting (XSS) or SMTP header injection attacks, that a beginner might assume are prevented by magic quotes. In November 2005, the core PHP developers decided that because of these problems, the magic quotes feature would be removed from PHP 6. When development of PHP 6 stalled and development continued on the 5.x branch instead, the feature was deprecated in PHP 5.3.0 and removed in 5.4.

Text derived from https://en.wikipedia.org/w/index.php?oldid=1377588943 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

13 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.