Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show HTML user input, security issue

On my website, users will be able to input html tags for the content so the text can be bold, italic or links and image. I plan to use ckeditor or tinymce which are really using HTML tags (not BBC code or wiki syntax) If I allow HTML, when the text will be shown it will be interpreted and it may contain some "hack" like javascript or XSS.... How can I do to avoid this security issue ? Do I have to list the wanted html tags and to delete all unwanted tags and content ? Can I use strip tags for this ?

How is it done on stackoverflow for example ?

Do you know some plugin php/jquery plugins who can safely save and safely interpret limited html tags ?

Thanks in advance for your help

like image 646
user1496486 Avatar asked Jul 23 '26 08:07

user1496486


1 Answers

You need to use both a server side HTML sanitizer, and a Content Security Policy preventing in-line scripts, eval and remotely hosted scripts

Depending on what language you are using server side, use HtmlSanitiser or python Bleach.

using either client side validation or naive filtering will not protect you at all:

  1. client side validation, as suggested by @Smamatti will not help you if a user submits the form manually.
  2. naive filtering such as str_replace('<script>', '', $str); suggested by @user1477388 will not protect you when someone uploads <script src="foo"> or <<script>script>alert('foo');</script> or <body onload="alert('foo')";</body>
like image 131
Thomas Grainger Avatar answered Jul 25 '26 00:07

Thomas Grainger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!