Skip to content

Commit bc869fe

Browse files
committed
fix: implement XSS protection and HTML sanitization in ZeroStylesRichText component
1 parent a6bcc0a commit bc869fe

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

adminforth/spa/src/renderers/ZeroStylesRichText.vue

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<script setup lang="ts">
66
import { onMounted, ref, watch } from 'vue'
77
import type { AdminForthResourceColumnCommon, AdminForthResourceCommon, AdminUser } from '@/types/Common'
8+
import sanitizeHtml from 'sanitize-html';
89
910
const props = defineProps<{
1011
column: AdminForthResourceColumnCommon
@@ -28,10 +29,28 @@
2829
iframe.style.height = "400px"
2930
3031
doc.open()
31-
doc.write(props.record[props.column.name] || '')
32+
doc.write(protectAgainstXSS(props.record[props.column.name]) || '')
3233
doc.close()
3334
}
34-
35+
36+
function protectAgainstXSS(value: string) {
37+
return sanitizeHtml(value, {
38+
allowedTags: [
39+
"address", "article", "aside", "footer", "header", "h1", "h2", "h3", "h4",
40+
"h5", "h6", "hgroup", "main", "nav", "section", "blockquote", "dd", "div",
41+
"dl", "dt", "figcaption", "figure", "hr", "li", "main", "ol", "p", "pre",
42+
"ul", "a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn",
43+
"em", "i", "kbd", "mark", "q", "rb", "rp", "rt", "rtc", "ruby", "s", "samp",
44+
"small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr", "caption",
45+
"col", "colgroup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", 'img'
46+
],
47+
allowedAttributes: {
48+
'li': [ 'data-list' ],
49+
'img': [ 'src', 'srcset', 'alt', 'title', 'width', 'height', 'loading' ]
50+
}
51+
});
52+
}
53+
3554
onMounted(renderHtml)
3655
watch(() => props.record[props.column.name], renderHtml)
3756
</script>

0 commit comments

Comments
 (0)