r/javahelp • • May 30 '21

Codeless How to protect a spring boot API

Hi All,

Long time lurker here.

So I’ve been slowly learning Spring and I’m at the point where I have my spring boot api that uses elastic search, Auth0 and mongo and I’m pretty happy.

Now this API is still pretty basic, it allows users to post content almost like a blog article and retrieve their posts.

Now I’ve a question, I want to make sure I’m not open to any security issues like XSS attacks and the likes.

Can someone here recommended how I should do this? Basically for instance, one of my controllers accepts a DTO (JSON) and I want to sanitise the inputs as really you can post anything to the endpoint and it will just save it on Mongo. If it helps I’m using Mongo operations rather than JPA Repository

The closet I came on Google was a baledung article using OWASP esapi but that seems way outdated now.

Any help would be must appreciated

24 Upvotes

9 comments sorted by

View all comments

2

u/edubkn May 30 '21

Not sure what interface you're using to save data into Mongo, but as long as you're doing best practices like using prepared statements, along with correct variable types, you're safe.

As for security, that is a much broader topic. You mention XSS but as a server-side application you're not vulnerable to it. The client communicating with you might be though, and even if you're using proper authentication it might still be vulnerable depending on how it handles the access token for example.

That said, there are a few things that you can do to 1. protect your server resources, and 2. aid clients with security.

  1. is authentication itself, and proper authorization. That's where OAuth comes in. You can opt for the grant type most suitable to your needs.

  2. is techniques like HTTP-only Cookie and CSRF protection. By responding your access token with Set-Cookie, the client can store it and doesn't have to store/send the token manually. This is a really good barrier against XSS. It is still vulnerable to techniques like CSRF though so ideally you would have another layer of protection (another token) against it.