Skip to content

Commit 6ea51fb

Browse files
committed
refactor: move XSS protection logic to utils and update related components
1 parent bc869fe commit 6ea51fb

File tree

5 files changed

+22
-59
lines changed

5 files changed

+22
-59
lines changed

adminforth/documentation/docs/tutorial/03-Customization/13-standardPagesTuning.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ export default {
534534

535535
Doing so, will result in UI displaying each item of the array as a separate input corresponding to `isArray.itemType` on create and edit pages.
536536

537-
`itemType` value can be any of `AdminForthDataTypes` except `JSON` and `RICHTEXT`.
537+
`itemType` value can be any of `AdminForthDataTypes` except `JSON` and `TEXT`.
538538

539539
By default it is forbidden to store duplicate values in an array column. To change that you can add `allowDuplicateItems: true` to `isArray`, like so:
540540

adminforth/spa/src/components/ValueRenderer.vue

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ import utc from 'dayjs/plugin/utc';
8989
import timezone from 'dayjs/plugin/timezone';
9090
import {checkEmptyValues} from '@/utils';
9191
import { useRoute, useRouter } from 'vue-router';
92-
import sanitizeHtml from 'sanitize-html';
9392
import { JsonViewer } from "vue3-json-viewer";
9493
import "vue3-json-viewer/dist/index.css";
9594
import type { AdminForthResourceColumnCommon } from '@/types/Common';
@@ -108,26 +107,6 @@ const props = defineProps<{
108107
record: any
109108
}>();
110109
111-
112-
function protectAgainstXSS(value: string) {
113-
return sanitizeHtml(value, {
114-
allowedTags: [
115-
"address", "article", "aside", "footer", "header", "h1", "h2", "h3", "h4",
116-
"h5", "h6", "hgroup", "main", "nav", "section", "blockquote", "dd", "div",
117-
"dl", "dt", "figcaption", "figure", "hr", "li", "main", "ol", "p", "pre",
118-
"ul", "a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn",
119-
"em", "i", "kbd", "mark", "q", "rb", "rp", "rt", "rtc", "ruby", "s", "samp",
120-
"small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr", "caption",
121-
"col", "colgroup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", 'img'
122-
],
123-
allowedAttributes: {
124-
'li': [ 'data-list' ],
125-
'img': [ 'src', 'srcset', 'alt', 'title', 'width', 'height', 'loading' ]
126-
}
127-
});
128-
}
129-
130-
131110
function formatDateTime(date: string) {
132111
if (!date) return '';
133112
return dayjs.utc(date).local().format(`${coreStore.config?.datesFormat} ${coreStore.config?.timeFormat}` || 'YYYY-MM-DD HH:mm:ss');

adminforth/spa/src/renderers/RichText.vue

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
<script setup lang="ts">
66
import type { AdminForthResourceColumnCommon, AdminForthResourceCommon, AdminUser } from '@/types/Common'
7-
import { protectAgainstXSS } from '@/components/ValueRenderer.vue' // путь замени на актуальный
8-
import sanitizeHtml from 'sanitize-html';
7+
import { protectAgainstXSS } from '@/utils'
98
109
const props = defineProps<{
1110
column: AdminForthResourceColumnCommon
@@ -14,23 +13,6 @@ const props = defineProps<{
1413
resource: AdminForthResourceCommon
1514
adminUser: AdminUser
1615
}>()
17-
function protectAgainstXSS(value: string) {
18-
return sanitizeHtml(value, {
19-
allowedTags: [
20-
"address", "article", "aside", "footer", "header", "h1", "h2", "h3", "h4",
21-
"h5", "h6", "hgroup", "main", "nav", "section", "blockquote", "dd", "div",
22-
"dl", "dt", "figcaption", "figure", "hr", "li", "main", "ol", "p", "pre",
23-
"ul", "a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn",
24-
"em", "i", "kbd", "mark", "q", "rb", "rp", "rt", "rtc", "ruby", "s", "samp",
25-
"small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr", "caption",
26-
"col", "colgroup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", 'img'
27-
],
28-
allowedAttributes: {
29-
'li': [ 'data-list' ],
30-
'img': [ 'src', 'srcset', 'alt', 'title', 'width', 'height', 'loading' ]
31-
}
32-
});
33-
}
3416
const htmlContent = protectAgainstXSS(props.record[props.column.name])
3517
3618
</script>

adminforth/spa/src/renderers/ZeroStylesRichText.vue

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +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';
8+
import { protectAgainstXSS } from '@/utils'
99
1010
const props = defineProps<{
1111
column: AdminForthResourceColumnCommon
@@ -33,23 +33,6 @@
3333
doc.close()
3434
}
3535
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-
}
5336
5437
onMounted(renderHtml)
5538
watch(() => props.record[props.column.name], renderHtml)

adminforth/spa/src/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useCoreStore } from './stores/core';
66
import { useUserStore } from './stores/user';
77
import { Dropdown } from 'flowbite';
88
import adminforth from './adminforth';
9+
import sanitizeHtml from 'sanitize-html'
910

1011
const LS_LANG_KEY = `afLanguage`;
1112

@@ -183,3 +184,21 @@ export function humanifySize(size) {
183184
}
184185
return `${size.toFixed(1)} ${units[i]}`
185186
}
187+
188+
export function protectAgainstXSS(value: string) {
189+
return sanitizeHtml(value, {
190+
allowedTags: [
191+
"address", "article", "aside", "footer", "header", "h1", "h2", "h3", "h4",
192+
"h5", "h6", "hgroup", "main", "nav", "section", "blockquote", "dd", "div",
193+
"dl", "dt", "figcaption", "figure", "hr", "li", "main", "ol", "p", "pre",
194+
"ul", "a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn",
195+
"em", "i", "kbd", "mark", "q", "rb", "rp", "rt", "rtc", "ruby", "s", "samp",
196+
"small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr", "caption",
197+
"col", "colgroup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", 'img'
198+
],
199+
allowedAttributes: {
200+
'li': [ 'data-list' ],
201+
'img': [ 'src', 'srcset', 'alt', 'title', 'width', 'height', 'loading' ]
202+
}
203+
});
204+
}

0 commit comments

Comments
 (0)