Insights / Security
Content Security Policy for business web applications: stop XSS before it ships
A Content Security Policy tells the browser which scripts, styles and images your app may load, and blocks the rest. How to write one that stops XSS without breaking your app.
Kiran Bandarupalli · 15 Nov 2026 · 5 min read

Cross-site scripting is still one of the most common ways a business application gets compromised, and it rarely looks dramatic. One crafted comment or compromised third-party widget is enough to run an attacker's code in every logged-in user's browser. Input validation and output encoding are the first line of defence, but they depend on every developer getting every field right. A Content Security Policy is the second line: a header your server sends that tells the browser exactly which scripts, styles, images and connections your application is allowed to load, and instructs it to block everything else. When an injection slips through, the policy is what stops it from executing.
This post covers what a policy does, how to write a strict one, and how to roll it out without breaking your app.
What a Content Security Policy actually does
A Content Security Policy is delivered as an HTTP response header. The browser reads it before running anything on the page and enforces it for the page's lifetime. The most important directive is script-src: it lists the origins allowed to supply JavaScript, so an injected inline script or a script tag pointing at an attacker's server is refused. That single behaviour makes CSP the strongest practical mitigation for cross-site scripting available today.
The policy covers more than scripts. style-src controls stylesheets, img-src controls images, connect-src controls fetch and WebSocket connections, frame-src controls embedded frames, and object-src controls plugins. Each directive falls back to default-src when it is not set. A policy is not a replacement for fixing the injection itself, but it breaks the most damaging outcome: arbitrary code running in your users' browsers.
CSP is the browser-side equivalent of the discipline you already apply to files: secure image uploads validates the bytes before they are served, the file upload security checklist walks through the whole pipeline, and malware scanning and quarantine catches what validation misses. Assume something hostile will get in, and make sure it cannot execute.
Start in report-only mode
The biggest mistake is deploying an enforcement policy on a Friday and discovering on Monday that half the application stopped working: inline scripts, event handlers, third-party widgets and analytics tags nobody documented. Start with Content-Security-Policy-Report-Only, which logs violations without blocking anything, and run it in production for at least a week.
# Report-only first: violations are logged, nothing is blocked
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; report-uri /api/csp-report; report-to csp-endpoint
# After the reports are clean, enforce the same policy
Content-Security-Policy: default-src 'self'; script-src 'self'; report-uri /api/csp-report; report-to csp-endpoint
Collect the reports somewhere you will actually read them. A reporting endpoint that writes to a log file nobody opens is the same as no reporting at all. Group violations by blocked URI and triggering page, and expect a long tail of surprises: a marketing pixel nobody owns, a chat widget from an unknown CDN, an inline handler in a legacy admin page. The goal of the report-only week is a complete inventory of what your application legitimately loads, because that inventory is the policy.
A strict policy that survives a real application
A policy worth deploying is strict where it matters and explicit everywhere else. A strong baseline for a business application: default-src 'self' so nothing loads from an unknown origin, script-src 'self' with no 'unsafe-inline' and no 'unsafe-eval', object-src 'none' to remove plugins entirely, base-uri 'self' so an injected base tag cannot redirect relative URLs, and frame-ancestors 'self' so your pages cannot be framed for clickjacking.
Inline scripts are the hard part. Legacy pages are full of them, and adding 'unsafe-inline' to make them work removes most of the protection. The correct fix is a nonce: the server generates a random value per response, adds it to the policy as script-src 'self' 'nonce-r4nd0m', and stamps it on each legitimate script tag. An injected script cannot guess the nonce, so it is blocked while your own scripts run. Hashes work the same way for scripts that never change. Start with the pages that handle money, personal data and admin actions.
Third-party scripts deserve the same scrutiny as any dependency: a script from an analytics provider or a payment widget runs with the full privileges of your page, so know exactly which external scripts your pages load. API keys and secrets covers the same class of incident for when a credential leaks.
The directives that matter most
- script-src — stops injected JavaScript. Keep it to
'self'plus named origins, nonces or hashes — never'unsafe-inline'. - default-src — the fallback for every unset directive.
'self'is the safe starting point. - object-src 'none' — no plugins in a modern business app. Disable them outright.
- base-uri 'self' — stops an injected
<base>tag redirecting relative URLs to an attacker's site. - frame-ancestors 'self' — the modern X-Frame-Options replacement. Stops your pages being embedded in a clickjacking frame.
- connect-src — limits where the page can send data: your own API and named services only, so fetch-based exfiltration is blocked too.
- upgrade-insecure-requests — upgrades HTTP subresources to HTTPS automatically. Cheap mixed-content protection.
Rollout checklist
- Run report-only mode for at least a week of real traffic, including admin pages and rarely visited flows.
- Build the allowlist from the reports, not from memory — every origin should trace back to a real report or a named integration.
- Remove inline scripts and event handlers from money, personal-data and admin pages first, using nonces or hashes.
- Enforce on one high-value section first, watch reports for a day, then extend to the whole application.
- Keep reporting live after enforcement — new violations warn you a dependency changed or an injection is being attempted.
- Re-check the policy after every new integration: a widget, analytics tag or chat provider missing from it will break silently in production.
- Pair CSP with the rest of the hardening work: hardening your Linux VPS covers the server side, and signed URLs with expiry keep file downloads from becoming a second injection path.
What CSP does not do
A Content Security Policy is a mitigation, not a cure. It does not fix the injection: a stored cross-site scripting flaw still lets an attacker write content into your pages, and content using only allowed origins will still render. It does not protect data leaving through an allowed channel either, so a form posting to your own API can still be abused if the API does not validate input. Input validation, output encoding and a strict policy are three layers of the same defence.
The cost is small and the payoff is large. A header, a week of reports, and a page-by-page cleanup of inline scripts buys a browser-enforced guarantee that an injection is blocked and logged instead of running in your users' sessions. For an application that handles real customer data, that is one of the highest-value security changes you can make — no new service, no new vendor, just a header your server already knows how to send.
If you are planning a security review of your own application, or building one that needs these protections from the start, our cloud and DevOps work covers the server and deployment side, and you can talk to us about your application to scope the rest.