Writing secure Vert.x Web apps
This is a starting guide for securing vert.x web applications. It is by no means a comprehensive guide on web application security such as OWASP. Standard rules and practices apply to vert.x apps as if they would to any other web framework.
The post will cover the items that always seem to come up on forums.
Don’t run as root
It is a common practise that your devops team member will constantly say, one shall run a service with the least amount of privileges necessary and no more. Although this might sound like folklore to less experienced developers that hit an issue when trying to run on privileged ports 80, 443, running as root solves it quickly but open a door to bigger problems. Lets look at this code:
When started with the CWD
set to /
(java -Dvertx.cwd=/ ...
) you just created a simple file server for all your server storage. Now imagine that you want to start this application you will hit the error:
So if you do now run as root
it will start, however in your browser now try to navigate to: http://localhost/etc/shadow
congratulations you just exposed your server logins
and passwords
!
There are several ways to run as a under privileged user, you can use iptables
to forward requests to higher ports, use authbind
, run behind a proxy like ngnix
, etc…
Sessions
Many applications are going to deal with user sessions at some point.
Session cookies should have the SECURE
and HTTPOnly
flags set. This ensures that they can only be sent over HTTPS
(you are using HTTPS
right?) and there is no script access to the cookie client side:
And in this case when inspecting your browser you should see:
Of course if you do not do that any script on your browser has the capability of reading, sniffing hijacking or tampering your sessions.
Security Headers
There are plenty of security headers that help improve security with just a couple of lines of code. There is no need to explain them here since there are good articles online that will probably do it better than me.
Here is how one could implement a couple of them:
Cross-Site Request Forgery (CSRF) Protection
Vert.x web provides CSRF protection using an included handler. To enable CSRF protections you need to add it to your router as you would add any other handler:
The handler adds a CSRF token to requests which mutate state. In order change the state a (XSRF-TOKEN
) cookie is set with a unique token, that is expected to be sent back in a (X-XSRF-TOKEN
) header.
Limit uploads
When dealing with uploads always define a upper bound, otherwise you will be vulnerable to DDoS
attacks. For example lets say that you have the following code:
Now a bad intentioned person could generate a random file with 1GB of trash:
And then upload it to your server:
Your application will happily try to handle this until one of 2 things happens, it will run out of disk space or memory. In order to mitigate these kind of attacks always specify the maximum allowed upload size:
Final Words
Although this is just a small list of things you should remember when implementing your application there are more comprehensive checklists to check: