feat: add escapeHtml function
This can be used to escape any special characters in a string with HTML before sending from the server back to the client. This is important to prevent a cross-site scripting attack.
This commit is contained in:
@ -508,3 +508,17 @@ export const isFile = async (path: string): Promise<boolean> => {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes any HTML string special characters, like &, <, >, ", and '.
|
||||
*
|
||||
* Source: https://stackoverflow.com/a/6234804/3015595
|
||||
**/
|
||||
export function escapeHtml(unsafe: string): string {
|
||||
return unsafe
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'")
|
||||
}
|
||||
|
Reference in New Issue
Block a user