【发布时间】:2015-12-09 12:12:23
【问题描述】:
我正在使用 messageSource 访问服务层中的 i18n 属性,以便在发生错误时向用户提供反馈;
messageSource.getMessage('validation.my.code', args, defaultLocale).
结果被推入flash范围(信息或错误)并以json形式返回浏览器并在jQuery中处理如下;
$.ajax({
type: 'POST',
url: 'update',
data: formData,
success: function (data) {
// do stuff
showMessage({info: data.info, error: data.error});
}
})
我的问题是,当传递给 messageSource 的 arg 显示用户输入时,例如“'johndoe' 用户名已被占用”,就有可能发生 XSS 攻击。 我在 Config.groovy 中的编码设置如下,因为如果更严格的话,它会破坏应用程序的其他部分。
codecs {
expression = 'html' // escapes values inside ${}
scriptlet = 'none' // escapes output from scriptlets in GSPs
taglib = 'none' // escapes output from taglibs
staticparts = 'none' // escapes output from static template parts
}
我想知道是否有一种优雅的方式来处理转义返回的 json 而无需手动滚动我自己的解决方案。
编辑:更多细节
通过简单地将以下内容放入 UI 表单的相关输入字段中来攻击上述内容:
<script>alert('hello');</script>
通过以下方法在服务层进行验证;
private List<String> validate(def domainObject) {
def messages = []
if (!domainObject.validate()) {
messages = domainObject.errors.allErrors.collect {
// the error objects implement MessageSourceResolvable
simpleMessageSource.getMessage(it, args, defaultLocale)
}
messages
}
输入被传递到 args 方法中,成为返回给用户的错误字符串的一部分,并最终以 json 格式输出回浏览器。 当然,这最终会导致一个 JavaScript 弹出窗口说“你好”。
【问题讨论】:
-
能否请您补充一下,您是如何攻击这个的
标签: javascript grails xss