【问题标题】:Grails GSP expressions encoded only in production仅在生产环境中编码的 Grails GSP 表达式
【发布时间】:2021-07-18 12:44:46
【问题描述】:

我试图找出导致我在 Grails v4.0.6 中看到的行为的配置。

如果我有一个 GSP 表达式,例如 ${myMessage} 并且 myMessage 包含 html 标记,例如 <div class='some-class'>hello world!</div>,那么当我在本地以开发模式运行时,这将按预期呈现。

但是,如果我部署到生产模式,Grails 会编码 html 标记,因此它不会按预期呈现。我确信这是某处的配置,可能与 application.yml 中的编码或编解码器配置有关,但我没有看到任何具体区分开发模式和生产模式的内容。

我还要注意,我所看到的时间都是在从我的自定义标记库呈现的模板中使用表达式时,例如:

out << render(template:"bookTemplate",model:"[book: myBook]")

我有问题的表达式位于 _bookTemplate.gsp 中。我不能说这是发生这种情况的唯一场景。

【问题讨论】:

    标签: grails gsp


    【解决方案1】:

    https://docs.grails.org/4.0.6/guide/single.html#xssPrevention

    默认情况下,Grails 会安全处理并转义 ${} 中的所有内容 GSP 中的表达式。所有标准 GSP 标签也是安全的 默认,转义任何相关的属性值。

    那么当你想阻止 Grails 逃逸时会发生什么? 内容?有将 HTML 放入数据库的有效用例 并按原样呈现,只要该内容是可信的。在这样的 情况下,您可以告诉 Grails 内容是安全的 呈现原始,即没有任何转义:

    &lt;section&gt;${raw(page.content)}&lt;/section&gt;

    您在此处看到的 raw() 方法可从控制器、标记 库和 GSP 页面。

    及以后:

    GSP 具有自动对 GSP 表达式进行 HTML 编码的能力, 从 Grails 2.3 开始,这是默认配置。默认 新创建的 Grails 的配置(在 application.yml 中找到) 应用如下:

     grails:
        views:
            gsp:
                encoding: UTF-8
                htmlcodec: xml # use xml escaping instead of HTML4 escaping
                codecs:
                    expression: html # escapes values inside ${}
                    scriptlets: html # escapes output from scriptlets in GSPs
                    taglib: none # escapes output from taglibs
                    staticparts: none # escapes output from static template parts
    

    GSP 具有多种编解码器,在将页面写入 回复。编解码器在编解码器块中配置,并且是 如下所述:

    expression - The expression codec is used to encode any code found within ${..} expressions. The default for newly created application is html encoding.
    
    scriptlet - Used for output from GSP scriplets (<% %>, <%= %> blocks). The default for newly created applications is html encoding
    
    taglib - Used to encode output from GSP tag libraries. The default is none for new applications, as typically it is the responsibility of the tag author to define the encoding of a given tag and by specifying none Grails remains backwards compatible with older tag libraries.
    
    staticparts - Used to encode the raw markup output by a GSP page. The default is none.
    

    【讨论】:

    • 感谢@Michal_Szulc 的回复。实际上,我确实已经阅读了该文档,它确实解释了为什么我需要使用 raw() 来解决我的问题。我想弄清楚的是,为什么在开发模式下本地运行时,我不需要使用 raw() 但在生产模式下我需要使用?如果有的话,什么配置说“在生产中更严格”?
    猜你喜欢
    • 2013-08-24
    • 2014-01-13
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多