Grails GSP表达式仅在生产中编码



我正试图弄清楚是哪种配置导致了我在Grails v4.0.6中看到的行为。

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

然而,如果我部署到生产模式,Grails正在对html标记进行编码,因此它不会按预期呈现。我确信这是某个地方的配置,可能与application.yml中的编码或编解码器配置有关,但我看不出开发模式和生产模式之间有什么特别的区别。

我还将注意到,我看到的这些都是在从我的自定义标记库中呈现的模板中使用表达式的时候,比如这样的东西:

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

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

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

默认情况下,Grails保护它,并转义${}中的所有内容GSP中的表达式。所有标准GSP标签也通过默认情况下,转义任何相关的属性值。

那么,当你想阻止Grails逃离一些所容纳之物存在将HTML放入数据库的有效用例并按原样呈现,只要该内容是可信的。在这种情况下在这种情况下,您可以告诉Grails内容是安全的呈现为原始,即没有任何转义:

<section>${raw(page.content)}</section>

您在这里看到的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.

最新更新