doupeng8419 2015-03-11 07:44 采纳率: 0%
浏览 44
已采纳

转义的快捷方式以防止XSS

I've just discovered that my website (html/php) is vulnerable to XSS attacks.
Is there any way to sanitize my data besides manually adding htmlspecialchars to each individual variable that I send to the webpage (and proably missing a few thereby leaving it still open to attack)?

  • 写回答

1条回答 默认 最新

  • douganmo1121 2015-03-11 08:25
    关注

    No, there is no shortcut. Data escaping always needs to happen on a case by case basis; not only with regards to HTML, but to any other textual format as well (SQL, JSON, CSV, whathaveyou). The "trick" is use tools which do not require you to think about this much and hence may allow you to "miss" something. If you're just echoing strings into other strings, you're working at the bare metal level and you do need a lot of conscious effort to escape everything. The generally accepted alternative is to use a templating language which implicitly escapes everything.

    For example, Twig:

    The PHP language is verbose and becomes ridiculously verbose when it comes to output escaping:

    <?php echo $var ?>
    <?php echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8') ?>
    

    In comparison, Twig has a very concise syntax, which make templates more readable:

    {{ var }}
    {{ var|escape }}
    {{ var|e }}         {# shortcut to escape a variable #}
    

    To be on the safe side, you can enable automatic output escaping globally or for a block of code:

    {% autoescape true %}
        {{ var }}
        {{ var|raw }}     {# var won't be escaped #}
        {{ var|escape }}  {# var won't be doubled-escaped #}
    {% endautoescape %}
    

    This still lets you shoot yourself in the foot, but is a lot better.

    One step up still is PHPTAL:

    <div class="item" tal:repeat="value values">
      <div class="title">
        <span tal:condition="value/hasDate" tal:replace="value/getDate"/>
        <a tal:attributes="href value/getUrl" tal:content="value/getTitle"/>
      </div>
      <div id="content" tal:content="value/getContent"/>
    </div>
    

    It requires you to write valid HTML simply to compile the template, and the template engine is fully aware of HTML-syntax and will process all user data at the level of a DOM, instead of a string soup. This relegates HTML to a pure serialisation format (which it should be anyway) which is produced by a serialiser whose only job it is to turn an object oriented data structure into text. There's no way to mess up that syntax through bad escaping.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上