# How do Content Security Policy (CSP) Trusted Sites protect Lightning Web Components during external API communications?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, CSP Trusted Sites, LWC Security

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-content-security-policy-csp-trusted-sites-protect-lightning-web-components-during-external-api

## Answer

**Basics:** Salesforce sends a Content-Security-Policy header with Lightning Experience and Experience Cloud pages. A browser enforcing it refuses to make requests to, or load content from, hosts not listed. If your LWC calls `fetch('https://api.partner.com')` from the browser, or displays an image or iframe from another domain, you must add that host or the browser console shows a CSP violation.

**How it works:**
1. Setup, Security, CSP Trusted Sites (or Trusted URLs), New.
2. Enter name, URL (HTTPS recommended), and tick the directives it should apply to: connect-src (fetch/XHR/WebSocket), img-src, font-src, style-src, frame-src, media-src.
3. Set context (Lightning/Aura, Experience Builder sites, or all) and Save.
4. Salesforce adds the host to the matching directives in the response header; users get it on the next page load.

**Advanced / deep dive:** CSP is a browser-side control and is different from Remote Site Settings or Named Credentials, which authorize server-side Apex callouts. A common mistake is adding a trusted site when the real need is server-side. For secrets or CORS-less APIs, call through Apex with a Named Credential instead of the browser: credentials stay on the server and CSP is not involved. For scripts, Lightning does not let you load arbitrary third-party JavaScript from a CDN; ship libraries as static resources and use `loadScript`. Lightning Web Security (the successor to Lightning Locker) provides separate JavaScript sandboxing. External hosts must also send CORS headers allowing the Salesforce origin for browser fetches. In Experience Cloud, the site's Security settings let you pick a CSP level and manage trusted sites too.

**Code / config example:**
```text
Name: Partner_API
URL:  https://api.partner.com
Directives: connect-src (scripts) = checked, img-src = checked
Context: All
```
```javascript
// LWC: allowed only after the CSP entry above AND CORS allowing your org
const res = await fetch('https://api.partner.com/v1/ping');
```

**Hidden gotchas & must-remember points:**
- A trusted site does not bypass CORS; the remote server must allow your origin.
- It affects only browser requests, not Apex callouts.
- Wildcards cover subdomains only when you specify them (for example `https://*.partner.com`).
- Each directive is opened separately; allowing images does not allow fetch.
- Broad entries weaken the protection; keep the list minimal and review it.

**Best practice:** Prefer server-side Apex callouts for sensitive APIs, and add only the exact hosts and directives the browser truly needs.

---
Source: ForceChallenges · last updated 2026-11-02
