====== PHP RFC: New operator (short tag) for context-dependent escaping ====== * Version: 1.0 * Date: 2016-07-14 * Author: Michael Vostrikov * Status: Declined * First Published at: http://wiki.php.net/rfc/escaping_operator ===== Introduction ===== This RFC proposes the addition of new operator for context-dependent escaping. This operator is intended mainly for HTML escaping but it allows to add handlers for other contexts. This operator requires new syntax, so it requires the changes in the language parser. ===== Problem description ===== Missing or wrong HTML escaping is the main reason of XSS vulnerabilities on many sites. Template engines solve this problem, but there are many applications where template engines are not used - which are written on custom engines, on CMSs, on frameworks without a template engine by default. These projects continue to develop and require to write code. In applications without template engines output a value from database is very frequent operation. Almost all cases of using operator must be with HTML-escaping, and only sometimes it is needed to output raw HTML. I suggest new operator, which make escaping operation more easier, safer and useful. Usually it is not very hard to move an application to new version of language, but it is almost impossible to rewrite all PHP-templates to a special template engine. Of course, we can make a function with short name like . But the problem is not that we don't have a function. The problem is that and both work good, and unsafe variant works exactly as safe one until we get unsafe data. There is no such problem with other contexts. If we don't call json_encode() when passing an array or object into javascript, this only will break the script, and this will be noticeable, there won't be a problem with security. Also, because we need to call escaping function everywhere, the problem is that we must copy-paste it, and there is a possibility to forget to do this sometime. Calling an escaping function manually on every output is the same as calling constructor manually after every 'new' statement: (new User)->__construct(...); (new Profile)->__construct(...); Main argument against such operator is that main problem is in specific context. There are various contexts and each one requires special escaping. But I think this is not required to support all of them. Because - who asks about it?) There are no requests about special operator for json_encode(), but there are many requests for htmlspecialchars(). You can not deny the problem with HTML escaping really exists. My first goal is to draw the attention on it. Exact implementation is a secondary thing. HTML context is very frequent case, and it can be used together with other contexts. Consider the example: It may seem that different escaping is required here. But it's not. The call of htmlspecialchars() is required in all 3 cases: 'Say "Hello")']; ?> Actually, on web page we have 3 external contexts - HTML,