Created
September 8, 2023 03:16
-
-
Save davecra/2665c05a2dd9ffc8dcfb5fd2484b289c to your computer and use it in GitHub Desktop.
HTML Sanitizer
This file contains hidden or 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
/** | |
* Sanitizes the string for possible malicious values | |
* @param {String} string | |
* @returns {String} | |
*/ | |
static sanitizeString = (string) => { | |
try { | |
string = string.replace(/(javascript:|onerror)/gi, ""); | |
string = string.replace(/undefined/gi, ""); | |
string = string.replace(/<script/gi, "<script"); | |
string = string.replace(/<iframe/gi, "<iframe"); | |
string = string.replace(/<object/gi, "<object"); | |
string = string.replace(/<embed/gi, "<embed"); | |
string = string.replace(/<applet/gi, "<applet"); | |
string = string.replace(/<form/gi, "<form"); | |
string = string.replace(/<meta/gi, "<meta"); | |
string = string.replace(/<link/gi, "<link"); | |
string = string.replace(/<a\s/gi, "<a "); | |
string = string.replace(/<img\s/gi, "<img "); | |
string = string.replace(/="/gi, "='"); | |
string = string.replace(/='/gi, "='"); | |
string = string.replace(/=`/gi, "=`"); | |
string = string.replace(/\/>/gi, "/>"); | |
return string; | |
} catch (e) { | |
return `[[SANITIZED STRING MALFORMED: ${e}]]`; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment