Pull up a chair. Open any bank’s website, press F12, go to the Network tab and click the first request. Look at the response headers, the lines that come back before any page content. Most people never look there, but some of the most useful security controls on the web live in those lines.
The idea is simple. Your server cannot control the visitor’s browser, but it can ask the browser to behave more strictly on its behalf. A browser that supports a header will then refuse to do certain risky things, even if an attacker tries to trigger them. It is cheap, it needs no code changes in the application, and it closes whole classes of problems. Let us go through the ones you will meet most often.
First, the problem of the very first visit
Suppose your company site has HTTPS everywhere and redirects any plain HTTP request to the secure version. Good. But think about what happens when someone types yourbank.example into the address bar. Most browsers try plain HTTP first. That first request travels in the clear, on the airport Wi-Fi or the café network, and the redirect to HTTPS comes back in the clear too. Someone sitting in the middle can quietly answer that first request themselves and keep the victim on HTTP, relaying everything to the real site. The padlock never appears, but many users do not notice its absence.
HSTS, short for HTTP Strict Transport Security, fixes this. The server sends a header called Strict-Transport-Security with a max-age value in seconds. After the browser has seen it once over HTTPS, it remembers: for the next so many seconds, never even attempt plain HTTP for this domain. It rewrites the request to HTTPS internally, before anything leaves the machine. It also refuses to let the user click through certificate warnings for that site, which matters because people do click through them.
You will have noticed the catch. The browser has to see the header once before it protects you, so the very first visit is still exposed. Browsers handle this with a preload list: a list of domains shipped inside the browser itself, so the protection exists from day zero. To qualify, a site normally has to send a long max-age, include subdomains, and carry a preload token in the header. Be careful here. Preloading is hard to undo, and includeSubDomains means every subdomain, including that forgotten internal tool on a self-signed certificate, must work over HTTPS. Teams have locked themselves out of their own old subdomains this way. Start with a short max-age, say a day, then raise it in steps as you gain confidence.
Clickjacking: a real page under a fake one
Now a different trick. Imagine a page that promises a free wallpaper download with one big button. Behind the button, invisible, an attacker has loaded your net-banking “Confirm transfer” page inside a transparent frame and lined it up so that the confirm button sits exactly under the wallpaper button. The victim is logged in to your bank in another tab, so the framed page is live. They click what they think is the wallpaper button, and they actually click yours. No code was stolen and no password was guessed. The user’s own click was borrowed.
This is clickjacking, also called UI redressing. The fix is to tell the browser which sites may put your pages inside a frame. The older header is X-Frame-Options, with values meaning “never” or “only my own origin”. The newer and more flexible way is the frame-ancestors directive inside a Content Security Policy header, which lets you name exactly the partner domains that may embed you. If both are present, modern browsers follow the CSP directive. For most pages the right answer is “nobody”, and you only loosen it for pages that are genuinely meant to be embedded, like a payment widget.
The quieter ones
Several other headers are small, but each stops one specific bad habit.
X-Content-Type-Optionswith the valuenosniffstops the browser from guessing a file’s type. Without it, a user-uploaded file labelled as an image could be reinterpreted as something executable if its contents look that way.Referrer-Policycontrols how much of your URL is sent to other sites when a user clicks a link or loads an image from them. If your URLs ever contain internal IDs or tokens, this matters more than you think.Permissions-Policylets you switch off browser features, such as camera or geolocation, that your page does not need, so a compromised script cannot use them.- Cookie attributes are not headers in the strict sense, but they ride in one:
Securekeeps the cookie off HTTP,HttpOnlyhides it from page scripts, andSameSitelimits when it is sent on cross-site requests.
Where to set them, and how to check
Do not scatter these across application code if you can avoid it. Set them once at the reverse proxy, load balancer or CDN so every response carries them, including error pages and static files, which people forget. Then confirm from outside. Free online header scanners will grade a site, and the browser’s developer tools will show what actually arrives. Check the real production response, not the staging one, because a CDN or WAF in front sometimes strips or overwrites headers.
A word of honesty about limits. Headers are instructions to a cooperative browser. An attacker using their own scripts or a custom client ignores them completely, so they do nothing for your server-side flaws. They protect the user’s browser from being turned against them. Think of them as seat belts: cheap, nearly invisible, and no substitute for a car that works.
One last practical note. When you add a strict header, test the pages that load third-party widgets, chat tools and payment frames before you roll it out wide. A framing rule that is too tight will break your own checkout, and that is how security headers get a bad name inside a company.
Leave a Reply