dsfvsdfv23599 2016-10-01 13:33
浏览 39
已采纳

如何从输入安全地存储代码

I am currently building an administration panel. There is a page where a file containing HTML is loaded into a html editor (TinyMCE) and is saved to the same file that was loaded (to save the changes).

The file is simply a page that has dynamic links that are to be interchanged from an administration panel. If all of these pages were the same, then I would simply save core values to a database and then load specific sections, however that is not the case as there are many variations of the templates, it's rare that any two are the same.

Therefore, I wanted an application that clients could use to edit these pages themselves. How would I go about saving these files in a secure way that strips any malicious behavior?

Thank you.

  • 写回答

1条回答 默认 最新

  • dongyupen6269 2016-10-01 14:01
    关注

    Sanitising html when storing it in the db is generally a bad idea, if you get it wrong and you corrupt the data there is no going back, you'd have to get your users to re-input the content once you'd fixed any mistakes. You might think you've covered all bases but there will always be that user that says "When I do this...."

    Instead just store the data in the database as it comes in (obviously filtering for sql injection) You don't want Mr O'reilly breaking your db.

    You need to filter the content on the way out of the db, how you do this depends on your circumstances.

    If you are just expecting your users to store text, then using htmlspecialchars is sufficient this will change < to &lt; and so on

    However if you want your users to be able to use tags then strip tags would be more appropriate

    eg

    $allowable_tags = array(
      '<p>',
      '<h1>',
      '<h2>',
      '<h3>',
      '<strong>',
      '<em>',
    );
    
    
    $content =  $db->get($page_id);
    
    echo strip_tags($content,implode('',$allowable_tags));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看