How to use Buefy#39;s Dialog component with user provided content and be safe in terms of XSS(如何将Buefy的对话框组件与用户提供的内容一起使用,并确保XSS的安全性)
问题描述
Buefy的对话框组件需要message
属性字符串。根据一份文档,该字符串可以包含HTML。我希望在字符串中使用模板值,但当然这应该是XSS安全的。
当前不安全示例
这是不安全的,因为this.name
不安全。我可以使用NPM包对名称进行html编码,但我真的更喜欢使用Vue。
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
props: {
name: { type: String, required: false },
},
methods: {
showModal() {
this.$buefy.dialog.confirm({
title: 'myTitle',
message: `<p>Hello ${this.name}</p>`, // unsafe - possible XSS!
cancelText: 'Cancel',
confirmText: 'OK',
type: 'is-success',
onConfirm: async () => {
// something
},
});
},
},
});
</script>
这是已使用的Buefy组件的问题,文档here:
所需设置
我已经创建了一个新组件,在本例中我将其命名为ModalMessage.Vue
<template>
<p>Hello {{name}}</p>
</template>
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
props: {
name: { type: String, required: true },
},
});
</script>
然后,我希望将ModalMessage.Vue呈现为string
在TypeScript:
<script lang="ts">
import Vue from 'vue';
import ModalMessage from 'ModalMessage.vue';
export default Vue.extend({
props: {
name: { type: String, required: false },
},
methods: {
showModal() {
this.$buefy.dialog.confirm({
title: 'myTitle',
message:, // todo render ModalMessage and pass name prop
cancelText: 'Cancel',
confirmText: 'OK',
type: 'is-success',
onConfirm: async () => {
// something
},
});
},
},
});
</script>
问题
如何将ModalMessage.Vue呈现并将名称prop传递给string
?
我非常肯定这是可能的--我过去就见过。不幸的是,我在网络或StackOverflow上找不到它。我只能找到从到字符串呈现模板的问题,但我不需要它-它需要从到字符串。
推荐答案
我的真正问题是
如何将Buefy的对话框组件与用户提供的内容一起使用并确保安全(&P> 因此,您需要创建一些HTML,在该HTML内容中包含一些用户提供内容(this.name
),并在一个对话框中显示它。您说得对,将未过滤的用户输入放入Dialog
的message
参数是不安全的(如Buefy docs中明确指出的)
但您所需的设置似乎不必要地复杂。最简单的方法是使用(文档很少)这样一个事实,即BuefyDialog
配置对象的message
参数可以是VNode的Array
而不是字符串。它的文档很少,但从源代码here和here可以清楚地看出,如果您传递一个VNode数组,Buefy会将该内容放入Dialog的默认槽中,而不是使用v-html
呈现它(这是危险的部分)在Vue中获得VNode
的Array
最简单的方法是使用插槽...
因此组件:
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
methods: {
showModal() {
this.$buefy.dialog.confirm({
title: 'myTitle',
message: this.$slots.default, // <- this is important part
cancelText: 'Cancel',
confirmText: 'OK',
type: 'is-success',
onConfirm: async () => {
// something
},
});
},
},
});
</script>
及其用法:
<MyDialog>
<p>Hello {{name}}</p>
</MyDialog>
或
<MyDialog>
<ModalMessage :name="name" />
</MyDialog>
在这两种情况下,如果name
包含任何HTML,则它将是encoded by Vue
Here是上述技术的简单演示(使用纯JS而不是TS)
这篇关于如何将Buefy的对话框组件与用户提供的内容一起使用,并确保XSS的安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将Buefy的对话框组件与用户提供的内容一起使用,并确保XSS的安全性


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01