Content security policy without breaking features

A content security policy (CSP) is a browser-level defence that tells a page which scripts, styles, images, frames and network connections it may load. A strong policy reduces the impact of injected code, but an over-restrictive one can disable checkout, analytics, support widgets or account controls. The aim is to permit every required feature while blocking untrusted code.

For teams serving Australian customers, CSP must also coexist with familiar compliance and commerce duties. Privacy notices and consent controls should align with the Australian Privacy Principles overseen by the Office of the Australian Information Commissioner. Product pages commonly show Australian dollar prices, GST-inclusive totals, an ABN and Australian support details, while payment flows can invoke Afterpay, PayPal, Stripe or a bank transfer. Each dependency deserves a deliberate policy decision.

Australian development teams also bring distinct working habits to security reviews. In a stand-up, “arvo testing” may refer to afternoon test runs, while “no dramas” can signal that a deployment is considered low risk; neither phrase replaces a precise change record. Briefs should still name the policy directive, affected browser, expected reports, rollback route and accountable owner.

Geography adds another layer. Traffic from Sydney, Melbourne, Brisbane or Perth may reach servers through different routes, and a page dependent on slow API calls can feel broken even when CSP is working correctly. Australian NBN connections vary by address and exchange, so a sensible deployment combines an appropriate CDN or edge location with caching, compressed assets and measured API response times.

Map features before writing directives

Map features before writing directives. Walk through public and authenticated journeys while recording every origin used by JavaScript, CSS, fonts, images, media, iframes, WebSockets and API calls. Include less obvious features such as tag managers, consent managers, maps, video embeds, document previews, customer-service chat and admin tooling. Check the network panel on mobile and desktop because responsive pages often load different assets. Turn off optional integrations temporarily to see which behaviours depend on them.

Begin with restrictive baselines, then add explicit exceptions. default-src 'self' provides a fallback, base-uri 'self' prevents a rogue base URL, and object-src 'none' blocks legacy plugin content. Add directives for the resources your application genuinely uses, especially script-src, style-src, img-src, connect-src, frame-src, form-action and frame-ancestors. Treat third-party origins as dependencies to document and monitor, not URLs to paste indiscriminately. CSP Level 3 is widely available in current browsers, but test the browsers your audience actually uses.

Build a policy around real dependencies

Assign each dependency to the directive that controls it. A font CDN belongs in font-src, an analytics endpoint in connect-src, a YouTube player in frame-src, and a hosted checkout in both frame-src and, where relevant, form-action. Images and uploaded previews may need img-src; workers can require worker-src or a carefully scoped blob: source. Avoid making every directive broad simply because one widget fails. Review what each vendor injects, whether it creates inline scripts, and whether it changes after deployment.

Inline code needs a deliberate mechanism. Generate a fresh, unpredictable nonce for each response and place it on approved script or style elements, then include that nonce in the matching directive; never reuse it across responses. A script directive may use 'self' 'nonce-{value}' 'strict-dynamic' so modern browsers trust scripts loaded by an already approved script. Hashes suit small, stable inline blocks. Avoid unsafe-inline and unsafe-eval unless a documented, isolated feature makes them unavoidable. A practical developer utility toolkit can help inspect headers and compare policy behaviour during development.

For external scripts, Subresource Integrity provides a separate integrity check when the vendor supplies stable files and the necessary cross-origin headers; it complements rather than replaces CSP. Dynamic tag managers rarely support integrity checks, which is another reason to narrow their permissions with nonces and review their release process.

Test enforcement before switching it on

Start with report-only enforcement. Deploy Content-Security-Policy-Report-Only during evaluation and configure browser violation reports to reach a protected endpoint. Reports show the blocked URL, directive and document, making it possible to distinguish a real attack from a missing allowance. Keep reports out of the public page, sample noisy events and retain enough context to reproduce the fault without collecting unnecessary personal data. Console messages remain useful, but a central report stream gives better coverage across devices and customer journeys.

Test the complete feature set, not a single homepage. Cover guest checkout, member accounts, password reset, search, uploads, refunds, promotional codes, support chat and third-party consent. Repeat key flows in current Chrome, Safari, Edge and Firefox, and on mobile. Watch for silent failures: analytics can look healthy while a consent event disappears, and a form can submit even when its tracking request is blocked. A regression check should assert the user outcome, such as a completed order or displayed map, rather than merely checking that a request returned 200.

The main deployment and inline-code choices have distinct trade-offs:

Approach Best fit Main benefit Main caveat
Report-only rollout Auditing live traffic Reveals breakage before enforcement Reports need monitoring and triage
Host allowlist Stable, first-party and known vendor code Simple to review and maintain A compromised allowed host can still serve harmful code
Nonce-based scripts Frequently changing inline or injected code Limits trust to one response Requires secure generation and template changes
Hash-based controls Small, stable inline code Precise approval without a nonce Every content change can require a new hash
strict-dynamic Modern apps with trusted script loaders Supports approved dependency chains Older browsers need sensible host fallbacks

Keep checkout, forms and embeds working

Payment pages need the most explicit treatment. A hosted card form may load JavaScript, styles and an iframe from an approved payment origin, while the order form may post to another checkout endpoint. Add only those hosts to script-src, style-src, frame-src, connect-src and form-action. frame-ancestors separately controls who may embed your site. For Australian merchants, test GST-inclusive totals, ABN display, saved cards, Afterpay or PayPal handoffs, bank transfer references and failed-payment retries in both test and live modes.

Embedded content should follow the same discipline. Give map tiles, video players, chat widgets and uploaded files separate directives with narrowly scoped origins; use data: or blob: only where the browser genuinely requires those schemes. A CSP controls what a page may load, not how fast a backend responds. If checkout or search feels sluggish, a database query optimisation guide can help isolate server latency, which a CSP cannot repair. Record API origins in connect-src, but do not confuse network permission with database performance.

Measure performance and visual stability

Measure the experience after enforcement. Compare page load time, interaction latency, completed transactions, form errors and support contacts before and after the policy changes. CSP evaluation is usually modest, but every allowed third-party script can consume bandwidth and main-thread time, so removing unnecessary code may deliver larger gains than many policy tweaks. Preserve asset dimensions during layout testing: reliable image dimension guidance helps prevent shifts when images, ads or recommendation panels load. Such shifts can make a page feel unstable even when the policy blocks nothing.

Watch the reporting endpoint and real-user monitoring together. A sudden rise in script-src reports may follow a tag-manager release, while connect-src reports can expose a broken API client. Compare violations with deployment markers, browser versions and Australian traffic patterns rather than treating every report as an attack. Keep style-src allowances aligned with actual style assets, and use Permissions Policy for camera, microphone and geolocation controls; CSP does not replace those permissions. This separation keeps each security control understandable.

Roll out gradually and keep ownership clear

Roll out in stages with a clear rollback path. Begin on staff accounts, internal tools or low-risk routes, then enforce on the main site once violation volume is stable. Keep the report-only header during the final observation period so new third-party behaviour remains visible. If checkout errors rise, temporarily remove enforcement rather than adding a blanket exception, identify the exact source and restore a narrow rule. Record the date, owner, reason and review date for every policy change.

Make CSP part of routine release quality. A CI check can flag unsafe directives, missing nonces and references to vendors no longer used by the application. Link each external dependency to its business owner, and re-test consent, payments and media whenever a vendor, tag or template changes. The policy should be easy for an on-call engineer in Melbourne or Brisbane to understand during an incident, not dependent on one author’s memory. Used this way, CSP becomes a controlled contract with the site’s features rather than a blunt switch that disables them.