I have a nodejs / express application that server some html pages as well as a rest service.
Those html pages call the rest service via XMLHttpRequest. The rest service can also be called by external application.
On server side, in my rest service, I want to check if the request comes from the same domain or not because I want external application only to authenticate their request (e.g. providing an access token).
To do so, I look at req.headers.referer to get the url of the page originating the request and compare with my server domain.
Is is safe? Can req.headers.referer be altered by an external app trying to call my service without authentication?
Is there a smarter / safer way to do so?