Prompt
How do I protect my website from SQL injection and XSS without changing the app?
Latest observation
If you truly can’t change the application code, your options are limited to controls in front of the app and hardening the environment. These can reduce risk a lot, but they are not a substitute for fixing the app.
1) Put a WAF / reverse proxy in front of the site
Use a web application firewall such as:
- Cloudflare WAF
- AWS WAF
- Azure WAF
- ModSecurity with OWASP Core Rule Set
- Fastly / Akamai WAF
What it can do:
- Block common SQL injection and XSS payloads
- Normalize and inspect requests
- Rate-limit suspicious traffic
- Challenge bots and scanners
Important:
- Start in log-only / detection mode, then tune rules
- Watch for false positives on legitimate traffic
- Don’t rely on WAF alone; it’s bypassable
2) Add strict security headers at the edge
If you can configure the web server, CDN, or reverse proxy, add:
Content Security Policy (CSP)
This is one of the best defenses for XSS if you can deploy it externally.
Example:
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'
If the site uses inline scripts, you may need a more permissive policy, but tighter is better.
Other useful headers
X-Content-Type-Options: nosniff
Referrer-Policy: no-referrer-when-downgrade
Permissions-Policy: geolocation=(), camera=(), microphone=()
X-Frame-Options: DENY
Note:
- These don’t stop SQL injection
- CSP can greatly reduce impact of XSS, but only if configured well
3) Sanitize and validate at the perimeter
If you control a proxy/API gateway, you can reject obviously malicious input before it reaches the app.
Examples:
- Block request bodies containing classic SQLi patterns like:
' OR '1'='1UNION SELECT--/* ... */
- Block reflected XSS patterns like:
<scriptjavascript:onerror=onload=
Be careful:
- Signature-based filtering is easy to evade
- Avoid breaking legitimate requests
- Prefer anomaly-based rules plus WAF managed rules
4) Restrict what the app can reach
Reduce blast radius if injection succeeds.
Database hardening
- Use a least-privilege DB account
- Remove DROP/ALTER/DELETE permissions if not needed
- Separate read-only and write accounts
- Restrict DB access by network/IP
- Turn off dangerous stored procedures if possible
Server hardening
- Run the app under a low-privilege OS account
- Isolate the app in containers/VMs
- Restrict outbound network access
- Keep secrets out of the environment where possible
5) Use network segmentation and allowlists
- Only allow the web server to talk to the DB on the needed port
- Deny direct DB access from the internet
- Put admin interfaces behind VPN or IP allowlists
- Separate frontend, app, and database networks
6) Add monitoring and detection
Since you can’t fix the code, detection becomes more important.
Log and alert on:
- Repeated 4xx responses
- Requests containing suspicious keywords
- Unusual query volume
- Unexpected response size changes
- Failed DB auth attempts
- Unusual outbound connections
Useful tools:
- SIEM alerts
- IDS/IPS
- WAF logs
- Database audit logs
7) Disable dangerous browser-exposed features
If the app is vulnerable to XSS and you can’t code-fix it, reduce impact with edge config:
- Set cookies as
HttpOnly,Secure,SameSite - Prevent framing with
X-Frame-Optionsor CSPframe-ancestors - Restrict script execution with CSP
- Consider disabling features like inline script execution if feasible
8) If you only have hosting/server access, do these minimums
Priority order:
- Put the site behind a WAF/CDN
- Add CSP and other security headers
- Lock down DB permissions
- Restrict network access to DB/admin ports
- Add monitoring and alerts
Reality check
- SQL injection is best fixed in application code using parameterized queries and ORM-safe queries.
- XSS is best fixed by output encoding, input handling, and safe templating.
- Without app changes, your protections are mostly mitigations, not true fixes.
If you want, I can help you design a no-code-change protection plan for your specific stack, like Apache/Nginx, Cloudflare, AWS, or IIS.