symfony - Escape HTML code with TWIG Markdown -
I'm working on a blog comment bundle and I want to let the user post some code using Markdown.
I work for Symphony 2, TUIG and Parsing
{{post.content | markdown}} In fact, the content is well marked by markdown pers ( & lt; code & gt; ...) but if there is some HTML code in my content: some content `` `& lt; Script & gt; Alert ("Hello World"); & Lt; / Script & gt; `` The code has not been saved and I have a warning message. Could someone please tell me how can I deal with XSS issues? ( foo | raw and foo | escape is particularly broken)
I am just a victim of this problem, but since the strip_tags attributes are not enough to protect values in the tag, I will present my answer. Open a console console that I am using to remove all unwanted HTML elements and attributes and execute the following command to install it. Music requires $ ezyang / htmlpurifier "^ 4.6" Then you can create your own twig extension:
Namespace AcmeBundle \ Twig; Class HTMLPurifierExtension \ twig_Extension {public function getFilters () {returning array (new \ Twig_SimpleFilter ('html_purifier', array ($ this, 'net'), array ('is_safe' => array ('html')) ($ Text) {$ element = array ('P', 'BR', 'small', 'strong', 'b', 'm', 'i', ' Strike ',' Sub ',' Super ',' In ',' Dale ',' Oh ',' Ul ',' Lee ',' H1 ',' H2 ',' H3 ',' DL ' 'DD', 'DT', 'Pre', 'Code', 'Nam', 'KBD', 'Q', 'Blockkot', 'ABRR', 'Seat', 'Table', 'Thad', 'Tebs' , 'Th', 'tr' 'td', 'a | $ Config = \ HTMLPurifier_Config :: createDefault (); $ Config- & gt; Set ('HTML.Allowed' $ filtered (',', $ element); $ Purifier = new \ HTMLPurifier ($ config); net $ net-> pure ($ text);} public function getName () {return 'html_purifier';}} < / Code> Open services.yml and register extension as a service: Services: acme.html_purifier_extension: class: AcmeBundle \ Twig \ HTMLPurifierExtension Public: Incorrect Tags: - {name: twig.extension} You can now save it with
{{post.content} Sub Industry can | Markdown | Html_purifier}}
Comments
Post a Comment