Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strict https headers and redirect from http to https in production mode #410

Merged
merged 26 commits into from
Feb 14, 2025

Conversation

frankieroberto
Copy link
Contributor

@frankieroberto frankieroberto commented Nov 13, 2024

This aims to avoid browser warnings if users accidentally visit a prototype in non-secure mode.

It does this by adding the Strict-Transport-Security HTTP header and Content-Security-Policy to upgrade-insecure-requests.

This will ensure that all subsequent requests will automatically use https, even if the http:// prefix is typed into the URL bar.

Additionally, the kit will automatically respond with a redirect to the https url whenever insecure requests are made.

This only happens when NODE_ENV is set to production, to avoid any issues when running the prototype over localhost

Once your prototype is running with the header set, you can also submit the domain to https://hstspreload.org/ (although this doesn't work for subdomains) which will ensure the browsers at it to their list of domains to always use https on without even having to visit it first.

Fixes #141

Checklist

  • CHANGELOG entry

This will ensure that browsers automatically load the prototype over https, even if the `http://` prefix is typed into the URL bar.

It does require `NODE_ENV` to be set to `production`, however many platforms such as Heroku will [set this by default](https://devcenter.heroku.com/changelog-items/688) for Node.js apps.

Once this is set you can also submit the domain to https://hstspreload.org which will ensure the browsers at it to their list of domains to always use https on.
@frankieroberto
Copy link
Contributor Author

@edwardhorsford @vickytnz ok to approve this?

We should then add some guidance to suggest adding the NODE_ENV=production env variable in hosting environments (Heroku, etc) - although some of them might add it by default.

@frankieroberto
Copy link
Contributor Author

Setting NODE_ENV=production also does some performance things in Express: https://expressjs.com/th/advanced/best-practice-performance.html#set-node_env-to-production

@vickytnz
Copy link
Contributor

oh wow - i didn't realise that was the case. I know that I had to do this for railway but didn't think I needed to for heroku but that does have some performance benefits

@vickytnz
Copy link
Contributor

vickytnz commented Dec 10, 2024

@edwardhorsford @vickytnz ok to approve this?

We should then add some guidance to suggest adding the NODE_ENV=production env variable in hosting environments (Heroku, etc) - although some of them might add it by default.

Reviewing the guidance, we already say that railway users have to do this. https://prototype-kit.service-manual.nhs.uk/how-tos/publish-your-prototype-online

In Railway, you also need to create:

a variable called NODE-ENV and set the value to production
a variable called USE_AUTH with the value of true

Unless this breaks Heroku (which I don't think it does), this should be OK to go without changes.

My only thought is that whether this makes enough of a difference to performance that we need to headline it as adding it in and saying (the prototype will run without it but it will take images slower to load and use more data)

EDIT: read your notes from the top, you're saying that it's done by default by Heroku so actually the guidance is probably good enough.

@frankieroberto
Copy link
Contributor Author

@vickytnz yep, I believe that's the case, although we should probably test it. Can't remember if I added it manually or not.

@frankieroberto
Copy link
Contributor Author

@anandamaryon1 could you review?

app.js Outdated Show resolved Hide resolved
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 10, 2025 15:05 Inactive
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 10, 2025 15:15 Inactive
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 10, 2025 15:23 Inactive
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 10, 2025 15:24 Inactive
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 14, 2025 09:51 Inactive
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 14, 2025 09:56 Inactive
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 14, 2025 09:58 Inactive
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 14, 2025 10:09 Inactive
@frankieroberto frankieroberto temporarily deployed to prototype-kit-test-hsts-header February 14, 2025 10:31 Inactive
@frankieroberto
Copy link
Contributor Author

I’ve verified this works on Heroku.

For Railway, they do their own redirecting of HTTP -> HTTPS at the proxy layer for all apps, so the code is unneeded, but doesn’t do any harm.

lib/middleware/production.js Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@colinrotherham colinrotherham self-requested a review February 14, 2025 12:25
Copy link
Contributor

@colinrotherham colinrotherham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, let's give it a go. Can always tweak some more

@frankieroberto frankieroberto changed the title Use strict https in production Use strict https headers and redirect from http to https in production mode Feb 14, 2025
@frankieroberto frankieroberto merged commit 28730eb into main Feb 14, 2025
3 checks passed
@frankieroberto frankieroberto deleted the use-strict-https-in-production branch February 14, 2025 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Force https when running in production
3 participants