【问题标题】:Escaping scriptlet output in Grails 2.3.0在 Grails 2.3.0 中转义 scriptlet 输出
【发布时间】:2013-10-11 19:11:26
【问题描述】:

我在 GSP 中有以下内容:

<%=model.something%>

在 Config.groovy 中,我有:

grails {
    views {
        gsp {
            encoding = 'UTF-8'
            htmlcodec = 'xml' // use xml escaping instead of HTML4 escaping
            codecs {
                expression = 'html' // escapes values inside null
                scriptlet = 'html' // escapes output from scriptlets in GSPs
                taglib = 'html' // escapes output from taglibs
                staticparts = 'none' // escapes output from static template parts
            }
        }
        // escapes all not-encoded output at final stage of outputting
        filteringCodecForContentType {
            //'text/html' = 'html'
        }
    }
}

但是当我在控制器中设置 model.something = "" 并渲染视图时,我得到了警告框。

如果我将其更改为使用 ${model.something} ,它似乎可以正确转义。但为了安全起见,我想确保 scriptlet 输出也被编码。我是否需要配置中的其他内容才能做到这一点?

【问题讨论】:

    标签: grails gsp


    【解决方案1】:

    &lt;%=%&gt; 形式并不意味着任何转义。只需使用${}

    ${..} 块中的变量默认不会转义,因此变量字符串中的任何 HTML 都会直接呈现到页面。为了降低跨站点脚本 (XSS) 攻击的风险,您可以使用 grails.views.default.codec 设置启用自动 HTML 转义。

    注意,&lt;%=%&gt; 没有任何说法。根据我自己的实践,我确认该设置不会影响 JSP 标记(您可能需要渲染一些未转义的内容)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      相关资源
      最近更新 更多