-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly sanitizes loaded messages in tgui chat
- Loading branch information
Showing
3 changed files
with
119 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Uses DOMPurify to purify/sanitise HTML. | ||
*/ | ||
|
||
import DOMPurify from 'dompurify'; | ||
|
||
// Default values | ||
let defTag = [ | ||
'b', | ||
'br', | ||
'center', | ||
'code', | ||
'div', | ||
'font', | ||
'hr', | ||
'i', | ||
'li', | ||
'menu', | ||
'ol', | ||
'p', | ||
'pre', | ||
'span', | ||
'table', | ||
'td', | ||
'th', | ||
'tr', | ||
'u', | ||
'ul', | ||
]; | ||
let defAttr = ['class', 'style']; | ||
|
||
/** | ||
* Feed it a string and it should spit out a sanitized version. | ||
* | ||
* @param {string} input | ||
* @param {array} tags | ||
* @param {array} forbidAttr | ||
*/ | ||
export const sanitizeText = (input, tags = defTag, forbidAttr = defAttr) => { | ||
// This is VERY important to think first if you NEED | ||
// the tag you put in here. We are pushing all this | ||
// though dangerouslySetInnerHTML and even though | ||
// the default DOMPurify kills javascript, it dosn't | ||
// kill href links or such | ||
return DOMPurify.sanitize(input, { | ||
ALLOWED_TAGS: tags, | ||
FORBID_ATTR: forbidAttr, | ||
}); | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.