Safeguard
Security Guides•

Can a CSP stop a compromised script from sending data out?

A CSP can limit where scripts send data with connect-src, form-action and img-src, but it will not block every channel. See what to set and what remains.

Safeguard Security Team
3 min read

Partly. A CSP can cut off many outbound channels: connect-src limits fetch, XHR, WebSocket and beacons, form-action limits form posts, and img-src limits image requests. It cannot make a malicious script safe, so treat it as a way to shrink the damage, not remove the risk.

The channels a script can use to send data

A script running on your page can read whatever the page can read, including form fields. To get it out, it needs a request. These are the usual routes and the directive that governs each.

RouteCSP directive
fetch, XMLHttpRequest, WebSocket, EventSource, sendBeacon, link pingconnect-src
Form submission to another URLform-action
Image request with data in the URLimg-src
Loading another script or framescript-src, frame-src

MDN lists exactly which interfaces connect-src covers: the ping attribute, fetch, fetchLater, XMLHttpRequest, WebSocket, EventSource and sendBeacon.

Two traps in the table

form-action does not fall back to default-src. MDN states that not setting it allows anything, so set it explicitly. And connect-src falls back to default-src, but images do not use connect-src at all. A policy that locks connect-src and leaves img-src open still allows data in an image URL.

A starting policy for a typical app

Content-Security-Policy:
  default-src 'self';
  script-src 'nonce-RANDOM_PER_RESPONSE' 'strict-dynamic';
  connect-src 'self' https://api.example.com;
  img-src 'self' data:;
  form-action 'self';
  object-src 'none';
  base-uri 'none';
  frame-ancestors 'none'

Replace the placeholders with your own values. Your analytics and payment providers will need entries, and each one you add widens the set of places data can go.

What CSP will not do

  • It does not judge intent. If you allow a vendor domain in connect-src, a compromised script from that vendor can send data to it.
  • Navigation is a separate channel. A script can change the page location to a URL carrying data, and fetch directives do not stop that.
  • MDN notes strict-dynamic trades convenience for trust: if a trusted script builds script elements from an XSS source, CSP will not stop them.
  • A policy delivered as a meta element supports fewer features than the header, and a report-only policy cannot be set that way.

How to get there safely

  1. Start in report-only mode. The CSP report-only rollout steps cover this.
  2. Read the reports for a week or two, covering real traffic.
  3. Add each legitimate destination to the right directive, not to default-src.
  4. Enforce, and keep the report endpoint live.
  5. Review the allowed hosts each quarter and drop ones you no longer use.

For choosing between nonce, hash and allowlist, see CSP nonce vs hash vs host allowlist.

Safeguard's dependency scanning helps with the other side, catching known vulnerable or malicious packages before they reach a build. A free scan is at gold.safeguard.sh/scan.

Quick check

If I set connect-src to none, is my data safe?

No. Forms, images and navigation are separate channels. Set form-action and img-src too, and still defend against injected code.

Does default-src cover form-action?

No. MDN says form-action has no default-src fallback, so omitting it allows any target.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.