Prompt
How do I harden a SaaS app against common web attacks?
Latest observation
Oct 23, 2026
OpenAI APIWeb search: off
Hardening a SaaS app is mostly about reducing your attack surface, enforcing strong defaults, and assuming parts of the app will be probed or compromised. A good way to approach it is by layer.
1) Secure authentication and session handling
- Use MFA for admins and high-risk actions.
- Store passwords with a strong, modern hash: Argon2id preferred, or bcrypt/scrypt with a strong work factor.
- Enforce unique, random session tokens and rotate them after login/privilege change.
- Set cookies with:
HttpOnlySecureSameSite=LaxorStrictwhere possible
- Implement:
- account lockout or throttling for brute-force attempts
- password reset tokens that are single-use and short-lived
- login anomaly detection for suspicious IPs/devices
2) Prevent injection attacks
- Use parameterized queries / prepared statements everywhere for SQL.
- Avoid building queries, shell commands, or LDAP filters by string concatenation.
- Validate inputs with allowlists, not blocklists.
- Escape output based on context:
- HTML
- attributes
- JavaScript
- URLs
- SQL, if unavoidable
- If you use an ORM, don’t assume it prevents all injection automatically.
3) Defend against XSS
- Treat all user-controlled content as untrusted.
- Use a templating framework that auto-escapes by default.
- Add a strong Content Security Policy (CSP).
- Avoid inline scripts and
eval. - Sanitize rich text with a proven HTML sanitizer if you must accept HTML.
- Use
HttpOnlycookies so XSS can’t directly read session tokens.
4) Defend against CSRF
- Use CSRF tokens on state-changing requests.
- Prefer
SameSitecookies. - Require re-authentication or step-up verification for sensitive changes.
- Check
Originand/orRefererheaders for browser-based requests.
5) Protect your APIs
- Authenticate every request properly; never trust client-side role claims alone.
- Enforce object-level authorization on every resource access.
- Use scoped API keys or OAuth tokens with least privilege.
- Rate limit by user, token, IP, and endpoint.
- Validate request bodies against strict schemas.
- Return generic errors to avoid leaking internals.
6) Prevent broken access control / IDOR
- Never rely on sequential IDs alone.
- Check authorization server-side for every tenant/object access.
- Use tenant scoping in every query.
- Test for horizontal and vertical privilege escalation.
- Consider opaque identifiers where helpful, but don’t confuse obscurity with authorization.
7) Harden file uploads
- Restrict file types by actual content, not just extension.
- Store uploads outside the web root.
- Rename files and strip metadata if needed.
- Scan uploads for malware if your threat model calls for it.
- Limit size, dimensions, and processing time to avoid zip/image bombs.
- Serve uploads with safe headers so they don’t execute as code.
8) Set secure HTTP headers
Common useful headers:
Content-Security-PolicyStrict-Transport-SecurityX-Content-Type-Options: nosniffReferrer-PolicyPermissions-PolicyX-Frame-Optionsor CSPframe-ancestors
9) Use TLS everywhere
- Enforce HTTPS site-wide.
- Redirect HTTP to HTTPS.
- Enable HSTS.
- Use modern TLS settings and disable weak ciphers/protocols.
- Manage certificates automatically and rotate them safely.
10) Secrets management
- Never hardcode secrets in code or client-side apps.
- Store secrets in a dedicated secrets manager or secure environment store.
- Rotate secrets regularly and on compromise.
- Scope API keys narrowly.
- Make sure logs, crash dumps, and telemetry never leak secrets.
11) Secure your infrastructure and dependencies
- Patch OS, runtime, libraries, and containers quickly.
- Use dependency scanning and lockfiles.
- Remove unused packages and services.
- Run containers as non-root and apply least privilege.
- Restrict outbound network access where possible.
- Separate environments and credentials for dev/staging/prod.
12) Logging, monitoring, and detection
- Log auth events, permission failures, admin actions, and suspicious patterns.
- Avoid logging sensitive data like passwords, tokens, and full payment details.
- Centralize logs and alert on:
- repeated failures
- impossible travel
- privilege changes
- unusual data access
- Keep tamper-resistant audit trails for admin and tenant actions.
13) Tenant isolation for SaaS
- Enforce tenant boundaries in:
- database queries
- cache keys
- background jobs
- object storage paths
- analytics exports
- Consider separate encryption keys per tenant for sensitive data.
- Be careful with shared resources like search indexes, queues, and caches.
- Test for cross-tenant data leakage regularly.
14) Rate limiting and abuse controls
- Rate limit login, password reset, signup, search, export, and API endpoints.
- Add bot detection where appropriate.
- Use quotas for expensive operations.
- Protect against enumeration of users, orgs, and resources.
15) Secure development practices
- Do code reviews with security checks.
- Add SAST, dependency scanning, and secret scanning to CI.
- Use DAST or automated security tests against staging.
- Run threat modeling for new features, especially auth, payments, file handling, and admin tooling.
- Maintain a vulnerability disclosure process and incident response plan.
16) OWASP Top 10 as a baseline
Map your work to the OWASP Top 10:
- Broken access control
- Cryptographic failures
- Injection
- Insecure design
- Security misconfiguration
- Vulnerable/outdated components
- Identification and authentication failures
- Software and data integrity failures
- Security logging/monitoring failures
- SSRF
Practical “first 10” checklist
If you want the fastest high-impact improvements:
- Enforce MFA for admins
- Add CSRF protection
- Switch to parameterized queries
- Add object-level authorization checks
- Set secure cookie flags
- Deploy CSP + other security headers
- Centralize secret management
- Add rate limiting to auth and expensive endpoints
- Lock down file uploads
- Add logging and alerts for auth/permission anomalies
If you want, I can also give you:
- a SaaS security checklist you can hand to your team,
- a prioritized roadmap for startup vs. enterprise SaaS,
- or concrete examples for a specific stack like Node/Express, Django, Rails, or Go.