R
Railwayβ€’8mo ago
lotario

How to set server response headers?

Mainly security response headers such as HTTP Strict Transport Security, CSP, X-Frame, Referrer-Policy, etc., something similar as to how they're set for apache/nginx servers. I've had a look through the docs but didn't find anything about this. Thank you!
Solution:
https://github.com/brody192/create-react-app-starter copy the nixpacks.toml and Caddyfile from this repo into yours...
Jump to solution
31 Replies
Percy
Percyβ€’8mo ago
Project ID: dbc6a326-959b-4750-b2e7-111baf636e6a
lotario
lotarioβ€’8mo ago
Project id: dbc6a326-959b-4750-b2e7-111baf636e6a
Brody
Brodyβ€’8mo ago
it is up to your app to set these headers for node, theres a great middleware called helmet for example
lotario
lotarioβ€’8mo ago
if my back-end is java and is basically a crud that handles json resources and front end is react that uses it, then I set the headers in the react app?
Brody
Brodyβ€’8mo ago
it still is up to your app to set the headers regardless of what kind of app you have, but of course there is always a way to set some headers, so tell me, what kind of react app is this? create-react-app, vite? what we working with?
lotario
lotarioβ€’8mo ago
it's a create-react-app, I'm basically trying to get a good grade on a website that checks for the security headers, here's the report for railway.app for example: https://securityheaders.com/?q=https%3A%2F%2Frailway.app&followRedirects=on
These are the scan results for https://railway.app which scored the grade B.
lotario
lotarioβ€’8mo ago
on the back-end i've set the headers and all responses received have them, but that doesn't affect the grade at all, so it must be on the front-end side
Brody
Brodyβ€’8mo ago
how are you serving the react app? do you have two railway services? like one for the frontend and one for the backend?
lotario
lotarioβ€’8mo ago
yes
Brody
Brodyβ€’8mo ago
Awesome, good start, gonna eat dinner and I'll be back soon so how are you currently serving your frontend react app?
lotario
lotarioβ€’8mo ago
with a railway service, I pointed it to the repository and railway did the rest
Brody
Brodyβ€’8mo ago
gotcha are you doing anything special with your app? nginx? serve? anything?
lotario
lotarioβ€’8mo ago
nope, it's an out of the box create-react-app project
Brody
Brodyβ€’8mo ago
that means you are running it in dev mode on railway, not ideal and doing that you also cant set headers
lotario
lotarioβ€’8mo ago
actually
Brody
Brodyβ€’8mo ago
so lets get your react app running in a way that it suitable for a production environment
lotario
lotarioβ€’8mo ago
just today I changed the Build Command to npm run build and the Start Command to npx serve -s build
Brody
Brodyβ€’8mo ago
then your answer to this shouldnt have been "nope" 🀣
lotario
lotarioβ€’8mo ago
I read serve and since today was the first time I actually learned about it, my brain lagged 🀣
Solution
Brody
Brodyβ€’8mo ago
https://github.com/brody192/create-react-app-starter copy the nixpacks.toml and Caddyfile from this repo into yours
Brody
Brodyβ€’8mo ago
its as simple as that to set headers, read this page https://caddyserver.com/docs/caddyfile/directives/header and then make your desired changes in your Caddyfile but worry about setting the headers later, make sure the app is working with those files as is first
lotario
lotarioβ€’8mo ago
going to try right now okay so the service started correctly after I removed the deploy npx serve command and the app works, now I'm gonna test with one header to see the result
Brody
Brodyβ€’8mo ago
sounds good
lotario
lotarioβ€’8mo ago
hmm if I add the directive header as the caddy link shows I get a Error during parsing: Unknown directive 'Strict-Transport-Security', and on the caddy community i've read that The header directive has to be inside a server block directive, and used a provided example. the error is gone but the headers are not added if I add the header directive in global options I get Error: adapting config using caddyfile: Caddyfile:15: unrecognized global option: header
Brody
Brodyβ€’8mo ago
the headers go in the site block please read the docs page i linked, not just skim read it
lotario
lotarioβ€’8mo ago
you're right, it's right there, one click away "Directives are functional keywords that appear within site blocks." pardon my ignorance Brody
Brody
Brodyβ€’8mo ago
no worries, i skim read too, but sometimes its worth it to actually sit down and read
lotario
lotarioβ€’8mo ago
indeed it is, after adding the header directive correctly, the security report improved and I can now see the headers in the browser! Thank you very much for your help and patience Brody, I truly appreciate it! ❀️
lotario
lotarioβ€’8mo ago
thank you again for your support and have a great evening!
Brody
Brodyβ€’8mo ago
you too πŸ™‚