Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice For XSS Attacks in Rest Api [closed]

I have read a lot about it, but couldnt really decide which way is the best. I have a web app and a java rest application which serves to customers. What is the best way to prevent xss attacks using parameters in rest api and frontend?

  • Validating each parameter in both server and client side
  • Filter and control request params
  • On client side control before putting every data in between tags etc... Thank you for your time.
like image 542
Mesut Can Avatar asked Oct 23 '25 14:10

Mesut Can


1 Answers

As with anything defense in depth is important, so validation and encoding should be done on any user provided input. Encoding is very important because what might be considered malicious is contextual. For example, what might be safe HTML might be an SQL Injection attack.

Parameters in a REST API may be saved which means they are returned from subsequent requests or the results may be reflected back to the user in the request. This means that you can get both reflected and stored XSS attacks. You also need to be careful about DOM Based XSS attacks. A more modern categorization that addresses overlap between stored, reflected, and DOM XSS is Server XSS and Client XSS.

OWASP has a great Cross Site Scripting Prevention Cheat Sheet that details out how to prevent cross site scripting. I find the XSS Prevention Rules Summary and the Output Encoding Rules Summary sections to be very handy.

The big take away is that browsers parse data differently depending on the context, so it is very important that you don't just HTML Entity Encode the data everywhere. This means it is important to do two things:

  • Rule #0 - Only insert untrusted (user provided) data in allowed locations. Only insert data into an HTML document into a "slot" defined by Rules #1-5.

  • When you insert data into one of the trusted slots follow the encoding rules for that specific slot. Again the rules are detailed in the previously linked Cross Site Scripting Prevention Cheat Sheet.

There is also a DOM based XSS Prevention cheat sheet. Like the server side XSS cheat sheet, it provies a set of rules to prevent DOM based XSS.

like image 50
Rob Winch Avatar answered Oct 25 '25 03:10

Rob Winch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!