Skip to content

Commit 9441b37

Browse files
Copilotsensslen
andcommitted
Add additional security hardening to DOMPurify config
Co-authored-by: sensslen <3428860+sensslen@users.noreply.github.com>
1 parent 5433109 commit 9441b37

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/components/MessageItem.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@ const MessageItem: React.FC<MessageItemProps> = ({ message, onShowMessage, onHid
2626
const renderMessageWithTokens = (message: string): string => {
2727
// First, sanitize each token value to prevent XSS
2828
Object.entries(tokenValues).forEach(([name, value]) => {
29-
// Sanitize the token value (strip all HTML tags from user input)
30-
const sanitizedValue = DOMPurify.sanitize(value, { ALLOWED_TAGS: [] });
29+
// Sanitize the token value (strip all HTML tags and attributes from user input)
30+
const sanitizedValue = DOMPurify.sanitize(value, {
31+
ALLOWED_TAGS: [],
32+
ALLOWED_ATTR: []
33+
});
3134
message = message.replace(`{${name}}`, sanitizedValue);
3235
});
3336

3437
// Then sanitize the entire message, allowing safe HTML tags
3538
// This allows XML/HTML tags in the message template while protecting against XSS
3639
const sanitizedMessage = DOMPurify.sanitize(message, {
3740
ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'u', 'br', 'p', 'span', 'a'],
38-
ALLOWED_ATTR: ['href', 'class']
41+
ALLOWED_ATTR: ['href', 'class'],
42+
// Restrict href to safe protocols only (http, https, mailto)
43+
ALLOWED_URI_REGEXP: /^(?:(?:https?|mailto):|[^a-z]|[a-z+.-]+(?:[^a-z+.-:]|$))/i
3944
});
4045

4146
return sanitizedMessage;

0 commit comments

Comments
 (0)