Here is a general encode procedure:
var lt = /</g,
gt = />/g,
ap = /'/g,
ic = /"/g;
value = value.toString().replace(lt, "<").replace(gt, ">").replace(ap, "'").replace(ic, """);
If your user doesn't submit anything to your server you don't even need the above. If the user submits and you are using the user input then the above should be safe. As long as the '<' and '>' are globally sanitized and the parenthesis also are you are good to go.
Answer from Konstantin Dinev on Stack OverflowHere is a general encode procedure:
var lt = /</g,
gt = />/g,
ap = /'/g,
ic = /"/g;
value = value.toString().replace(lt, "<").replace(gt, ">").replace(ap, "'").replace(ic, """);
If your user doesn't submit anything to your server you don't even need the above. If the user submits and you are using the user input then the above should be safe. As long as the '<' and '>' are globally sanitized and the parenthesis also are you are good to go.
Considering https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html
Here is an implementation of their recommendations :
function escapeOutput(toOutput){
return toOutput.replace(/\&/g, '&')
.replace(/\</g, '<')
.replace(/\>/g, '>')
.replace(/\"/g, '"')
.replace(/\'/g, ''')
.replace(/\//g, '/');
}
Also make sure you use this function only when necessary or you might break some stuff.
But I suggest you to take a look at already made libraries for sanitizing output :
https://github.com/ecto/bleach
[AskJS] How do you protect against XSS?
How exactly does Cross Site Scripting (XSS) work?
Is this good enough to prevent XSS attacks?
XSS in modern web development
How can developers prevent XSS attacks?
What are XSS Attacks?
What are the Tools and Solutions to Detect and Prevent XSS Attacks?
Videos
Greetings, I am working on web app where i have back end utilizing spring And front end utilizing JavaScript And jQuery. I need to implement total xss protection on the front end side. How do you handle xss to make your app Safe? Would you recommend any libraries which can be used to filter input And other things related to xss (these libraries should be safe) Or how do you usually defend against xss when you Are developing such application ? Thank you