If you run a Sinatra app over HTTPS, you should probably set HSTS.
Not “maybe later.” Not “after the next sprint.” If your app is meant to be HTTPS-only, HSTS is one of the simplest ways to stop protocol downgrade attacks and cookie leakage caused by accidental HTTP visits.
This guide is the practical version: what header to send, where to set it in Sinatra, how to avoid bricking subdomains, and how to roll it out without surprises.
What HSTS does
HSTS stands for HTTP Strict Transport Security. It tells the browser:
- always use HTTPS for this site
- never retry over plain HTTP during the policy lifetime
- optionally apply the rule to subdomains
- optionally allow the domain to be included in browser preload lists
The header looks like this:
Strict-Transport-Security: max-age=31536000; includeSubDomains
Once a browser sees that header over a valid HTTPS response, it remembers it for max-age seconds.
That means future requests like http://example.com get upgraded to https://example.com in the browser before the request is even sent.
The minimum Sinatra setup
If your Sinatra app terminates TLS itself, you can set the header directly in an after block.
require "sinatra"
after do
if request.secure?
response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
end
end
get "/" do
"Hello over HTTPS"
end
A few rules matter here:
- Only send HSTS on HTTPS responses.
- Don’t send it over HTTP.
- Don’t enable
includeSubDomainsunless every subdomain is HTTPS-ready.
I’ve seen people copy a one-year HSTS header into production without checking old admin subdomains, forgotten staging boxes, or mail-related hostnames. That’s how you create a Friday night incident.
If you’re behind Nginx, HAProxy, or a load balancer
A lot of Sinatra apps sit behind a reverse proxy, and Sinatra may not think the request is secure unless forwarded headers are configured correctly.
If TLS is terminated upstream, request.secure? may return false unless Rack sees the original scheme.
A common pattern is to trust X-Forwarded-Proto from your proxy and redirect HTTP to HTTPS.
require "sinatra"
before do
forwarded_proto = request.env["HTTP_X_FORWARDED_PROTO"]
scheme = forwarded_proto || request.scheme
if scheme != "https"
redirect to("https://#{request.host}#{request.fullpath}"), 301
end
end
after do
forwarded_proto = request.env["HTTP_X_FORWARDED_PROTO"]
scheme = forwarded_proto || request.scheme
if scheme == "https"
response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
end
end
That works, but I prefer setting HSTS at the edge when possible. If Nginx is already handling HTTPS, let Nginx own the header.
Example:
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / {
proxy_pass http://sinatra_upstream;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
That always matters, because you want the header on more than just 200 responses.
Recommended rollout path
Don’t jump straight to one year unless you’re very sure your setup is clean.
Use this rollout instead:
1. Start small
response.headers["Strict-Transport-Security"] = "max-age=300"
That’s 5 minutes.
Watch for breakage:
- mixed content issues
- HTTP-only subdomains still in use
- weird callbacks or old bookmarks
- internal tools hitting HTTP endpoints
2. Increase to a day
response.headers["Strict-Transport-Security"] = "max-age=86400"
3. Increase to a month
response.headers["Strict-Transport-Security"] = "max-age=2592000"
4. Move to a year
response.headers["Strict-Transport-Security"] = "max-age=31536000"
5. Add includeSubDomains only after checking everything
response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
That order saves pain.
Preload: only if you mean it
You’ll often see this version:
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
This is for HSTS preload, where browsers ship your domain as HTTPS-only from day one, before any first visit.
That’s powerful, but it’s not casual. If you preload your domain:
- all subdomains must support HTTPS
- you need a long
max-agevalue, usually at least 1 year and often 2 years includeSubDomainsis required- removal from preload lists is slow and annoying
If you run dev.example.com, oldcrm.example.com, mta-sts.example.com, autodiscover.example.com, or any random forgotten hostname, verify them all first.
A preload-ready Sinatra header would be:
after do
if request.secure?
response.headers["Strict-Transport-Security"] = "max-age=63072000; includeSubDomains; preload"
end
end
My opinion: preload only when you have strong control over your DNS and subdomain inventory. For a messy org, plain HSTS without preload is usually the sane move.
Redirect HTTP too
HSTS is not a replacement for redirecting HTTP to HTTPS.
You still need a proper redirect:
before do
unless request.secure?
redirect to("https://#{request.host}#{request.fullpath}"), 301
end
end
If you’re behind a proxy, use the forwarded scheme logic instead of request.secure? alone.
Why both?
- Redirect handles first-time visitors and non-HSTS clients.
- HSTS protects future visits and blocks protocol downgrades.
You want both.
Full Sinatra example
Here’s a realistic app setup with HTTPS redirect and HSTS:
require "sinatra"
helpers do
def https_request?
forwarded_proto = request.env["HTTP_X_FORWARDED_PROTO"]
(forwarded_proto || request.scheme) == "https"
end
end
before do
unless https_request?
redirect to("https://#{request.host}#{request.fullpath}"), 301
end
end
after do
if https_request?
response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
end
end
get "/" do
"Secure Sinatra app"
end
get "/health" do
content_type :json
{ status: "ok" }.to_json
end
If you use modular Sinatra style:
require "sinatra/base"
require "json"
class App < Sinatra::Base
helpers do
def https_request?
forwarded_proto = request.env["HTTP_X_FORWARDED_PROTO"]
(forwarded_proto || request.scheme) == "https"
end
end
before do
unless https_request?
redirect to("https://#{request.host}#{request.fullpath}"), 301
end
end
after do
if https_request?
headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
end
end
get "/" do
"Secure Sinatra app"
end
end
How to disable or shorten HSTS
If you mess up and need to remove HSTS, send:
Strict-Transport-Security: max-age=0
In Sinatra:
after do
if request.secure?
response.headers["Strict-Transport-Security"] = "max-age=0"
end
end
That tells browsers to forget the policy. But if you used preload, this does not instantly fix things. Preload removal is a separate process and takes time.
Common mistakes
Sending HSTS over HTTP
Browsers ignore it over plain HTTP. Send it only on HTTPS responses.
Using includeSubDomains too early
This is the big one. If any subdomain still needs HTTP or has broken TLS, don’t enable it yet.
Assuming localhost needs HSTS
Don’t bother with HSTS for local development domains unless you have a very specific setup. It usually creates friction, not value.
Forgetting upstream proxy behavior
If Sinatra sits behind a TLS terminator and doesn’t know the original scheme, your redirect logic can loop or your header logic can silently fail.
Treating preload as a badge
It’s not a badge. It’s a commitment.
How to verify it
Use curl first:
curl -I https://example.com
You want to see:
Strict-Transport-Security: max-age=31536000; includeSubDomains
Check redirect behavior too:
curl -I http://example.com
You want a 301 or 308 to the HTTPS URL.
For a quicker sanity check across security headers, run a free scan at HeaderTest. It’s handy when you want to confirm what your edge actually returns instead of what you think you configured.
Good default policies
Safe initial policy
Strict-Transport-Security: max-age=300
Solid production policy
Strict-Transport-Security: max-age=31536000
Strong policy for fully HTTPS subdomains
Strict-Transport-Security: max-age=31536000; includeSubDomains
Preload-ready policy
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
My default recommendation for Sinatra
For most production Sinatra apps:
- redirect all HTTP traffic to HTTPS
- set HSTS at the reverse proxy if you have one
- start with short
max-age - move to one year after validation
- add
includeSubDomainsonly after auditing all subdomains - preload only if you really control your domain estate
If you want the shortest copy-paste version for a straightforward Sinatra app, use this:
require "sinatra"
before do
redirect to("https://#{request.host}#{request.fullpath}"), 301 unless request.secure?
end
after do
response.headers["Strict-Transport-Security"] = "max-age=31536000"
end
get "/" do
"Hello, HTTPS"
end
That’s a solid baseline. If your deployment sits behind a proxy, adapt the HTTPS detection before shipping it. That one detail trips people up more than the header itself.