Reputation: 437
I have an API only rails app and another angular app as frontend
Currently I'm securing the API by sending X-APP-Token with each request on headers
The other APIs which has logged in user has X-User-Token on headers as (JWT)
My question here, with each request sent from angular, the token are exposed on headers and anyone can have access to them
How can i secure these tokens from both front-end and back-end perspective ? like salting and encrypting so that if someone stole that token, it should be useless
Thanks in advance
Upvotes: 1
Views: 1453
Reputation: 2137
Security has many layers but there is some stuff that you definitely need to include while securing your API here are some of the more important security measures based on my experience:
before_action :set_headers
def set_headers
response.set_header('Referrer-Policy', 'strict-origin-when-cross-origin')
response.set_header('X-Content-Type-Options', 'nosniff')
response.set_header('X-Frame-Options', 'SAMEORIGIN')
response.set_header('X-XSS-Protection', '1; mode=block')
response.set_header('Content-Security-Policy', "default-src 'self' https:; " \
"img-src 'self' https:" \
"media-src 'none'; " \
"object-src 'none'; " \
"script-src 'self'; " \
"style-src 'self' ")
end
Since you are in rails 5.x
you can go to the configuration of your env lets say production.rb and add the following line: config.force_ssl = true
, this will force all connections against your API through HTTPS
and it will also add this response header strict-transport-security
which increment security, be aware that if you try to reach your API via HTTP
your API will try to force HTTPS
and in some scenarios return a code of 301
which means redirected this is a good hint in case you have a health check.
Definitely you need to take care of common issues like Insecure Direct Object Reference (IDOR), this issue as his name indicates is when you have direct references to private resources, let's say in the URL of your app you have something like ***/shop/23
, where 23
is the autoincrement id of your model I think I don't need to explain why is that wrong, from that moment you can be exposed to enumeration attacks, access to info (which you should avoid with authorizations), destroy objects, etc, in these cases one possible approach is to use indirect references or change that id with a value that is not guessable.
Last but not less, you need to include proper passwords policies for your users you can look in other sites, or consult NSA for that kind of info.
In most cases security depends on the nature of your application, you may need some other security (for example if your logic includes external links/URLs you may want to check that those links are valid and safe, etc...), but as I said before I think that the list described above is a good fit for a good set of applications, the rest depends on your logic/flows, hope the above helps to clarify.
Upvotes: 1
Reputation: 1492
In production your client code should be communicating with your backend API with an SSL secured connection. Most of the time you would put an SSL certificate on a web server or load-balancer that is proxying your API or app server. This would prevent a middle-man from intercepting the token and then impersonating the user.
Edit: Also, maybe you need to clarify "My question here, with each request sent from angular, the token are exposed on headers and anyone can have access to them" - I'm curious how anyone could have access to these headers? Also your JWT should be signed and have an expire time set thats respected by the app server.
Upvotes: 1