Hardening a Small Site
The protections that matter most for a small public website with sign-in, payments and an admin area, and how each one is checked by a test.
Proportionate security
A small site is not a bank, but it still has things worth protecting: visitors' accounts and email addresses, payment records, an admin area, and its own reputation. The goal is to close the doors that attackers try first, and to make each protection something a test checks, so it cannot quietly disappear.
1. Security headers
The server sends standard security headers on every response, set in one place with the helmet library:
- Strict-Transport-Security (HSTS): browsers use HTTPS only, for a long period.
- Content-Security-Policy, including
frame-ancestors: limits where scripts may load from, and stops other sites from embedding pages in a frame to trick clicks. - X-Content-Type-Options, Referrer-Policy and others: small, cheap protections.
Ravi OS's health check reads these headers from live pages and shows whether each one is present, so a change that drops them is noticed.
2. Know the real visitor address
Behind Cloud Run's front end, Express must trust exactly one proxy hop. Too little trust and all visitors share one address and one rate-limit bucket; too much and anyone can fake an address with a header. Tests pin both halves of this setting.
3. Rate limits
Requests are counted per visitor address over fifteen-minute windows, with separate limits for sign-in, the admin area, public forms, payments and the AI mentor. That blunts password guessing, scraping and runaway costs on a paid AI API. The limits live in memory on each instance, which is exact while one instance serves the site and approximate if several do. That limit is written down, not discovered.
4. Sign-in and sessions
- Sessions are checked for revocation, so signing out everywhere actually works.
- Repeated or simultaneous requests share one revocation check, so safety does not cost speed.
- Admin rights based on an email address require that the address is verified.
- Basic-auth admin passwords may contain any character, and wrong ones are refused (tests cover both).
5. Private pages stay private
Every admin and dashboard page requires a login, is marked noindex so search engines never list it, and is sent with no-cache headers so a shared computer or proxy does not keep a copy. Requests that change data must also carry a same-site header, which blocks cross-site request forgery. Tests open every private route without a login and expect a refusal.
6. Payments and webhooks
Payment providers report payments to your server with a webhook. Anyone can send a request to that address, so:
- every webhook's signature is verified before anything else;
- the amount must match the price, and the payment must belong to the person and section it claims;
- the ledger is idempotent: the same payment arriving twice, or three handlers racing on one payment, still produce exactly one entry;
- if the provider is down, the server asks it to retry instead of granting access blindly.
Each of those sentences is a test on this site.
7. The database is not a public API
Firestore can be reached directly from browsers if its rules allow. Here, the security rules deny all direct browser access to private collections; only the server, with its own identity, reads and writes them.
8. The container
Non-root user, slim base, no secrets inside, production dependencies only (see Smaller, Safer Images).
9. Dependencies
Most code running on any Node site was written by someone else. Habits:
npm cifrom a lock file, with install scripts disabled in CI;npm audit --omit=devregularly, fixing what affects production;- fixes go through a normal branch and pull request (
fix/npm-audit-<month>), with the full test suite as the safety net.
10. Keep the list honest
Some protections are deliberately postponed: a stricter content-security policy without inline styles, and a shared store for rate limits. They are on the backlog with a reason, not forgotten. A short list of "known gaps, and why they are acceptable for now" is part of good security.
Try it yourself
Pick three items from this chapter that your site lacks a test for. Write one test each, starting with the one that would hurt most if it silently broke: usually private pages or webhooks.