Is there any value in sending security headers like Content-Security-Policy (I am mainly thinking of using Helmetjs) from the backend, if it is used as an APIs and the frontend is served separately by a hosting service?
I tried things locally and found out those headers don't seem to work unless I server the frontend from the backend, e.g. Expressjs serving static files.
Is this the case or am I missing something?
Yes, it does. Many security headers like the mentioned CSP, but also many others, are client-side controls which are implemented on the server side. CSP is quite a complex header to use. It tells for example which JS-files (files from which domain) may be executed and which not or which backend the communication may take place with. As you may already expect, doing a misconfiguration will crash your whole application. The solution is not to give up, but to push through. For big systems it takes years before they are able to fully embrace CSP with all their contents with decent configuration.
Just remember, CSP has got the feature of reporting issues instead of blocking them (
Content-Security-Policy-Report-Only). Make sure you use this first before you implement something you may regret.