How can I allow my user to insert HTML code, without risks? (not only technical risks)

后端 未结 10 804
一生所求
一生所求 2020-12-09 05:41

I developed a web application, that permits my users to manage some aspects of a web site dynamically (yes, some kind of cms) in LAMP environment (debian, apache, php, mysql

相关标签:
10条回答
  • 2020-12-09 06:01

    I use this php strip_tags function because i want user can post safely and i allow just few tags which can be used in post in this way nobody can hack your website through script injection so i think strip_tags is best option

    Clich here for code for this php function

    0 讨论(0)
  • 2020-12-09 06:06

    Kohana's security helper is pretty good. From what I remember, it was taken from a different project.

    However I tested out

    <IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>
    

    From LFSR Consulting's answer, and it escaped it correctly.

    0 讨论(0)
  • 2020-12-09 06:07

    The general best strategy here is to whitelist specific tags and attributes that you deem safe, and escape/remove everything else. For example, a sensible whitelist might be <p>, <ul>, <ol>, <li>, <strong>, <em>, <pre>, <code>, <blockquote>, <cite>. Alternatively, consider human-friendly markup like Textile or Markdown that can be easily converted into safe HTML.

    0 讨论(0)
  • 2020-12-09 06:09

    If it is too difficult removing the tags you could reject the whole html-data until the user enters a valid one. I would reject html if it contains the following tags:

    frameset,frame,iframe,script,object,embed,applet.

    Also tags which you want to disallow are: head (and sub-tags),body,html because you want to provide them by yourself and you do not want the user to manipulate your metadata.

    But generally speaking, allowing the user to provide his own html code always imposes some security issues.

    0 讨论(0)
  • 2020-12-09 06:09

    code that I should have just copy/pasted instead of screenshotting

    It is very good function in php you can use it

    $string = strip_tags($_POST['comment'], "<b>");
    
    0 讨论(0)
  • 2020-12-09 06:11

    It doesn't really matter what you're looking to remove, someone will always find a way to get around it. As a reference take a look at this XSS Cheat Sheet.

    As an example, how are you ever going to remove this valid XSS attack:

    <IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>
    

    Your best option is only allow a subset of acceptable tags and remove anything else. This practice is know as White Listing and is the best method for preventing XSS (besides disallowing HTML.)

    Also use the cheat sheet in your testing; fire as much as you can at your website and try to find some ways to perform XSS.

    0 讨论(0)
提交回复
热议问题