I need my users to enter the URI of their personal website in their profile so that other users can see and click on it. I am worried that this could lead to XSS attacks if the output is not sanitized properly.
Like in this very simplistic schema below:
I am using the full stack symfony2 framework, Doctrine as ORM and Twig as a template engine. I know that Symfony provides some amazing Validation tools, and that TWIG provides automatic output escaping (which is not necessary in this particular case) as well as some filters for output sanitizing.
I've read the following about how symfony2 and twig handle sanitization:
Doctrine comes with sanitization for database (SQL) injections. Apart from this, there is no recommended / provided input sanitization at controller level in Symfony2. However, using Twig in the view, output sanitization is available.
As an example, in CakePHP however:
Data sanitization is implemented as a Utility which can be accessed from anywhere (controller, component, model .. even view). It follows a sanitize-all-input approach with a fixed set of predefined sanitization filters. Sanitizing specific inputs with dedicated rules is possible, but seems not to be encouraged.The existing rules concentrate on SQL and HTML injections and filtering out general suspicious unicode characters.
1 How do symfony2 + twig
users handle input sanitization? Do they discard input sanitization totally and for example rely on validation only? Or do they write their own utility function to filter user inputs? or maybe use a library like owasp-esapi-php?
2 How do symfony2 + twig
users handle output sanitization? Do they rely on the filters provided by the twig engine only? For example, are there already any tools that one can use to sanitize a user-entered URI, something similar to this?
3 In this situarion, how would you handle database storage and display of a user-entered URI like in the example above, would you care about input sanitization at all? or would you use output sanitization only and store the URI as is?
For sanitization against XSS it’s generally better to save raw HTML in database without modification and sanitize at the time of output/display.
So you should save the input as it is. You can then use a twig filter to prevent XSS attacks. Id do believe{{ user.website|e('url') }}
would do the job. If not, it is always possible to create a new filter. – cheesemacflyhtmlentities
vs symfony/twig usinghtmlspecialchars
, so I am not sure if the CakePHP example was really relevant here. – cheesemacfly