Home / security / csp-and-trusted-urls
csp-and-trusted-urls
Configure Content Security Policy via Trusted URLs and CSP Trusted Sites so Lightning, LWR, and LWC can call third-party scripts, APIs, and frame sources. NOT for clickjack configuration — use lwc/static-resources-in-lwc.
Open the skill folder on GitHub
CSP and Trusted URLs
Salesforce sets a Content Security Policy header on Lightning pages. The framework default is restrictive — script-src 'self', and font-src, img-src, media-src, frame-src, style-src, and connect-src likewise scoped to the org's own origin. Any external resource a component needs is blocked until an administrator adds a Trusted URL (called a CSP Trusted Site in API 58.0 and earlier) that grants that specific origin, that specific directive, in that specific context.
Setup path, verbatim from Salesforce Help: From Setup, in the Quick Find box, enter Trusted URLs, and then select Trusted URLs.
The metadata type is CspTrustedSite, stored in cspTrustedSites/ with the .cspTrustedSite extension, available in API 39.0 and later.
Before Starting
- Read the actual console message. It names the directive that blocked the
request, and that directive is the only checkbox you need. Configuring from a guess is how allow-lists rot.
- Establish which surfaces the component runs on. Lightning Experience,
Experience Builder site, and Visualforce are separate contexts, and a URL trusted in one is not trusted in another.
- Enumerate every origin the feature touches, not just the one in the first
error. A third-party SDK typically needs a loader host, an API host, a frame host for challenge flows, an asset CDN, and a telemetry beacon.
- Decide whether this is a browser problem at all. If the request originates
in Apex, you need a Remote Site Setting or a Named Credential, and no Trusted URL will help.
Core Concepts
The six directives you can grant
CspTrustedSite exposes exactly six directive fields. There is no script-src field — Lightning fixes script-src at 'self'.
| Field | Grants | Typical console phrase |
|---|---|---|
isApplicableToConnectSrc | fetch, XMLHttpRequest, WebSockets | Refused to connect to |
isApplicableToFrameSrc | <iframe> sources | Refused to frame |
isApplicableToImgSrc | images | Refused to load the image |
isApplicableToFontSrc | fonts | Refused to load the font |
isApplicableToStyleSrc | stylesheets | Refused to load the stylesheet |
isApplicableToMediaSrc | audio and video resources | Refused to load media |
Context scopes the whole record
context | Applies to |
|---|---|
All | All supported context types |
LEX | Lightning Experience pages only |
Communities | Experience Builder sites only |
VisualForce | Custom Visualforce pages only, and only if the page's cspHeader attribute is true (API 55.0+) |
FieldServiceMobileExtension | Field Service Mobile Extensions only (API 47.0+) |
LightningOut | "Reserved for future use" (API 64.0+) |
The endpointUrl grammar
Must include a domain name; may include a port. Wildcards are supported (*.example.com). Third-party APIs must begin with https://; WebSockets must begin with wss://. The value is a literal — build any templated URL before you write it into the metadata.
Malformed values (malformed^url.example.com, https://{subdomain}.example.com) could be saved before February 2025 and are "excluded from generated CSP HTTP headers" — an entry that looks Active and correct but has never been in the header.
The default for unset directives is version-dependent
| API version of the deployment | If every isApplicable* is false |
|---|---|
| ≤ 49.0 | All of them default to true — an open grant |
| 50.0–58.0 | isApplicableToImgSrc is set to true |
| ≥ 59.0 | At least one isApplicable* or canAccess* must be true, or the deploy is rejected |
Write every field explicitly, true and false alike.
Camera and microphone are Permissions-Policy, not CSP
canAccessCamera and canAccessMicrophone (API 59.0+) are inert unless the org has enablePermissionsPolicy = true and grantCameraAccess / grantMicrophoneAccess = TrustedUrls in SecuritySettings.
Trusted URL is not Remote Site Setting
| Trusted URL | Remote Site Setting / Named Credential | |
|---|---|---|
| Enforced by | The user's browser, via the CSP header | The Salesforce application server |
| Governs | LWC/Aura fetch, <img>, <iframe>, fonts, CSS, WebSockets | Apex Http.send(), Visualforce |
| Error text | Refused to ... violates the following Content Security Policy directive | Unauthorized endpoint, please check Setup->Security->Remote site settings |
Common Patterns
Pattern A — one origin, one directive, one context
The default and the correct shape for most cases. Read the console error, create one record with one directive ticked and every other field explicitly false, and put the depending component's name in description. Example 1 in