• Flask09 原始输出、转义、XSS攻击、过滤器???


     

    1 怎么在jinja模板中原始输出模板语法

      1.1 用双引号引起来后放到 {{ }} 中

        例如  

          {{ "{{}}" }}

        输出

          

      1.2 利用raw

        例如   

    {% raw %}
            {% for i in range(11) %}
                <li>{{ i }}</li>
            {% endfor %}
        {% endraw %}

        输出

          

    2 模板中遇到带有script标签怎么办

      如果在模板遇到  >、<、&、” 等字符时Jinja2模板会自动进行转义,即利用其它的字符来代替这些模板,例如:利用 &lt; 代替<

        

          

      2.1 Jinja2模板如何判断需要渲染的字符串是否需要转义

        如果渲染的字符串有 __html__ 属性就认为是安全的,不需要进行转义;否则就会自动进行转义

        

        

        注意:也可以利用  {{ to_escape is escaped }} 去判断 to_escape 变量是否拥有 __html__ 属性,如果返回false就没有

          

          

      2.2 如何取消转义

        让需要进行渲染的模板拥有 __html__ 属性就会自动取消转义

        {{待渲染字符串|safe}}  这样写的话待渲染字符串就会拥有 __html__ 属性,从而会取消转义

          

          

      2.3 如何判断转义

         {{待转义字符串 is escaped}} 返回结果为False就表示需要进行转义,为True就表示不需要进行转义

        

        

      2.4 如何自定义是否需要进行转义

        利用 autoescape,设置为false表示不会进行转义,设置为true表示会进行转义

          

          

          

    3 XSS攻击

      例如:用户评论的内容是一段 html 代码,如果这段代码不经过转义处理,那么其他用户在加载页面的时候机会执行这段代码

      3.1 模拟一个用户评论模块

        3.1.1 编写评论页面

    {% from 'my_macros.html' import form, input, textarea %}
    
    {% autoescape true %}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>测试XSS</title>
    </head>
    <body>
        <h4>测试XSS相关知识</h4>
        <hr />
        {% call form('xss.test_xss') %}
            {{ textarea('comment') }}
            {{ input('button', type='submit', value='发表') }}
        {% endcall %}
        <hr />
        {% if comments %}
            <ul>
                {% for comment in comments %}
                    <li>{{ comment }}</li>
                {% endfor %}
            </ul>
    
        {% endif %}
    </body>he
    </html>
    
    {% endautoescape %}
    View Code
    {% macro url(filename) %}
        {{ url_for('static', filename=filename) }}
    {% endmacro %}
    {#宏:相当于在模板中定义函数,但是这个函数时使用的模板语法#}
    
    {% macro link_to(endpoint, text) %}
        <a href="{{ url_for(endpoint, **kwargs) }}">{{ text }}</a>
    {% endmacro %}
    
    
    {% macro form(endpoint, method='post') %}
        <form action="{{ url_for(endpoint, **kwargs) }}" method="{{ method }}">
            {{ caller() }} {# caller() 获取 调用者 的 内容 #}
        </form>
    {% endmacro %}
    
    {% macro input(name, value='', type='text') %}
        <input type="{{ type }}" value="{{ value }}" name="{{ name }}" />
    {% endmacro %}
    
    {% macro textarea(name, value='', rows=10, cols=40) %}
        <textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{ value }}</textarea>
    {% endmacro %}
    工具

        3.1.2 编写视图函数

        

        3.1.3 将HTML代码作为评论内容

          

          注意:会显示出来,因为flask进行了转义处理

            

            

        3.1.4 如果取消自动转义的效果

          将 autoescape 设置为 false 就可以取消转义啦

            

          

          注意:如果评论内容是HTML代码,那么chrome浏览器会进行拦截

    4 本博客源代码

      点击前往

          

    5 过滤器

      待更新...

      2017年10月7日23:03:56

  • 相关阅读:
    Vue GET xxxx/sockjs-node/info?t=1573626343344 net::ERR_CONNECTION
    NavigationDuplicated Navigating to current location (“/XXX”) is not allowed
    node-sass报错(Node Sass could not find a binding for your current environment)
    DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead
    VSCODE 中.art文件识别为html文件
    gulp4.0构建任务
    gulp报错The following tasks did not complete
    setTimeout()
    格式化日期
    作业1.3
  • 原文地址:https://www.cnblogs.com/NeverCtrl-C/p/7598789.html
Copyright © 2020-2023  润新知