|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-08-04 14:13 UTC] kjelkenes at gmail dot com
Description:
------------
This is a feature request regarding the "echo" and "print" functions used in PHP. The echo/print statement is used for output. As of today there are no way of extending these statements, leading to potential security risks.
If we could extend the echo function at a given time with a handler/closure this could really improve the security of PHP.
Say we have the following security risk (XSS injection):
$data = "<h1><script>alert('hi');</script></h1>"; // From db.
echo $data;
Today we need custom functions to escape this such as:
function escape($data){
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}
echo escape($data);
What if we could implement a handler for the echo/print statements such as this:
// Define a handler:
$outputHandler = function escape($data){
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
};
/**
* Sets a output handler for php, it's used in echo and print statements.
* @param string $name The identity of this handler ( Unique )
* @param mixed $outputHandler function name or callback closure to use.
* @param mixed $flags What type of satements to use this function.
*/
add_output_handler('xss_filter',$outputHandler, OutputHandler::F_ECHO | OutputHandler::F_PRINT);
/**
* Removes a given output handler by it's name.
*/
remove_output_handler('xss_filter');
Then we could use normal statements:
echo '<script>alert('This will never be exected.')</script>';
And when we don't need it anymore:
remove_output_handler('xss_filter');
This way, one can be sure that the output of ANY kind is actually stripped, without implementing a whole new templating system for PHP.
Also this does not break any kind of PHP applications running, it just adds new functionality that is (let's face it) really needed for PHP.
This is also great for MVC frameworks, just apply it before executing a view file and remove it after!
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 21:00:01 2025 UTC |
You can already accomplish this using output buffering if so desired. I don't see a need to add anything else into the mix. For example: ------------------------------------------ <?php $outputHandler = function($data){ return htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); }; ob_start($outputHandler); echo '<script>alert(\'This will never be executed.\')</script>'; ------------------------------------------ outputs: <script>alert('This will never be executed.')</script>From my point of view, keithm provides a solution that does exactly the thing you have asked for. Output buffering wasn't created for this purpose, but it can be easily used for it without breaking anything. If this is not what you wanted, maybe your description is not clear enough? However this is a solution for a problem that doesn't exist in reality. 1. Most of the data output by scripts should never be escaped. Yet your idea causes ALL data to be escaped, producing garbage. 2. In few specific cases there are small portions of data from untrusted sources that should be escaped. In such cases a single call is enough. What you want to be introduced requires 3 lines of code (enable escaping, echo, disable escaping) just to make same thing a single function call could do. 3. Even worse: the concept is perpendicular to echo by design, but not perpendicular to echo by behaviour. Hence it's a design error.Ignorance, gotta love it. There is really a difference of output buffering functions and this, I do of course know of the output buffering functions, this is NOT RELATED. Read on to really see what I mean about this. This is how people in most cases write their VIEW logic. Meaning ending and starting <?php echo ..?> every time they echo stuff.. That also means you should ESCAPE ALL data that comes to echo, else you are just not SAFE. index.php: <?php // Web page title. $title = 'My website'; // A item from the database. $item = array('title' => '<script>alert("Hi!")</script>'); ?> <?php ob_start() ?> <html> <head> <title><?php echo $title?></title> </head> <body> <div><p><?php echo $item['title']?></p></div> </body> </html> <?php $content = htmlspecialchars(ob_get_flush(),ENT_QUOTES,'UTF-8'); echo $content; /* Returns: <html> <head> <title>My website</title> </head> <body> <div><p><script>alert("Hi!")</script></p></div> </body> </html> FAIL?! We NEED a echo handler.... */ ?> Obviously this won't work if people wrote their template like this (but that's up to them...): index.php echo " <html> <head> <title>{$title}</title> </head> <body> <div><p>{$item['title']}</p></div> </body> </html> "; This is wanted behaviour (The use of htmlspecialchars wouldn't be necessary if we had a handler that intercepted the echo statement.): <?php // Web page title. $title = 'My website'; // A item from the database. $item = array('title' => '<script>alert("Hi!")</script>'); ?> <html> <head> <title><?php echo htmlspecialchars($title,ENT_QUOTES,'UTF-8')?></title> </head> <body> <div><p><?php echo htmlspecialchars($item['title'],ENT_QUOTES,'UTF-8')?></p></div> </body> </html> <?php /* Returns: <html> <head> <title>My website</title> </head> <body> <div><p><script>alert("Hi!")</script></p></div> </body> </html> Perfect! */ ?>Just stating the obvious. OOP, MVC, Templating engine, you sir have greatly missed the point of this feature request. Are you talking about OOP? OOP has _nothing_ to do with this question, of course you would use OOP: classes, namespaces and traits.. But this is not the place to talk about OOP design. Are you talking about MVC? Because this addition will make every currently existing MVC frameworks such as Symfony 2 and Codeigniter more secure. You separate controller / business logic / presentation logic, but you are now starting a discussion of using a 3rdparty library against php itself as a templating-engine. Does not really make sense at all regarding this feature request. You don't USE a templating framework if you implement this change, this change will make it easier then never to CREATE the fastest templating engine on the market without having to parse code to php code and then use the php code to make php safe. Note, php is a language, you can also template with it because of it's easy syntax HTML<?php echo $var?>MORE_HTML. In Symfony 2 you have to either use - TWIG syntax (Templating engine) - echo $this->escape("<script></script"); ( Symfony way .. ) Now, this didn't have to be needed if this feature request was implemented. - echo "<script>.."; ( This is not possible at this time )