Skip to content

Commit 85208b6

Browse files
feat: sanitize ticket field descriptions in Service Catalog using DOMPurify
1 parent 7a20f68 commit 85208b6

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

src/modules/new-request-form/usePrefilledTicketFields.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
import { useMemo } from "react";
22
import DOMPurify from "dompurify";
33
import type { TicketFieldObject } from "../ticket-fields/data-types/TicketFieldObject";
4+
import { ALLOWED_HTML_TAGS } from "../shared";
45

56
const MAX_URL_LENGTH = 2048;
67
const TICKET_FIELD_PREFIX = "tf_";
78
const DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;
89

910
const ALLOWED_BOOLEAN_VALUES = ["true", "false"];
10-
const ALLOWED_HTML_TAGS = [
11-
"pre",
12-
"strong",
13-
"b",
14-
"p",
15-
"blockquote",
16-
"ul",
17-
"ol",
18-
"li",
19-
"h2",
20-
"h3",
21-
"h4",
22-
"i",
23-
"em",
24-
"br",
25-
];
2611

2712
interface Fields {
2813
ticketFields: TicketFieldObject[];

src/modules/service-catalog/hooks/useItemFormFields.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { TicketFieldObject } from "../../ticket-fields/data-types/TicketFie
55
import type { EndUserCondition } from "../../ticket-fields/data-types/EndUserCondition";
66
import { getCustomObjectKey } from "../../ticket-fields/fields/LookupField";
77
import { getVisibleFields } from "../../ticket-fields/getVisibleFields";
8+
import DOMPurify from "dompurify";
9+
import { ALLOWED_HTML_TAGS } from "../../shared";
810

911
const getFieldValue = (field: TicketField) => {
1012
if (field.type === "tagger") {
@@ -26,11 +28,16 @@ const formatField = (field: TicketField): TicketFieldObject => {
2628
relationship_target_type,
2729
relationship_filter,
2830
} = field;
31+
32+
const sanitizedDescription = DOMPurify.sanitize(description, {
33+
ALLOWED_TAGS: ALLOWED_HTML_TAGS,
34+
});
35+
2936
return {
3037
id,
3138
type,
3239
name: `custom_fields_${id}`,
33-
description,
40+
description: sanitizedDescription,
3441
label: title_in_portal,
3542
options: custom_field_options,
3643
required: required_in_portal,

src/modules/shared/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./notifications";
22
export * from "./i18n";
33
export * from "./garden-theme";
44
export * from "./error-boundary";
5+
export * from "./validations";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const ALLOWED_HTML_TAGS = [
2+
"pre",
3+
"strong",
4+
"b",
5+
"p",
6+
"blockquote",
7+
"ul",
8+
"ol",
9+
"li",
10+
"h2",
11+
"h3",
12+
"h4",
13+
"i",
14+
"em",
15+
"br",
16+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./constants";

templates/document_head.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"service-catalog-translations": "{{asset 'service-catalog-translations-bundle.js'}}",
2121
"shared": "{{asset 'shared-bundle.js'}}",
2222
"ticket-fields": "{{asset 'ticket-fields-bundle.js'}}",
23-
"wysiwyg": "{{asset 'wysiwyg-bundle.js'}}"
23+
"wysiwyg": "{{asset 'wysiwyg-bundle.js'}}",
2424
}
2525
}
2626
</script>

0 commit comments

Comments
 (0)