# How do CORS configurations permit authorized web applications to execute client-side API requests against Salesforce?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, CORS, Web Security

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-cors-configurations-permit-authorized-web-applications-to-execute-client-side-api-requests-against

## Answer

**Basics:** Browsers block a page at `https://app.example.com` from reading responses from another origin (`https://myorg.my.salesforce.com`) unless that server replies with `Access-Control-Allow-Origin` for it. Salesforce lets admins whitelist origins so single-page apps and widgets can call its APIs from client-side code.

**How it works:**
1. Setup, Security, CORS, New: enter the origin URL pattern (scheme, host, optional port; `https://*.example.com` style wildcards for subdomains).
2. The browser sends a preflight `OPTIONS` request for requests with custom headers such as `Authorization`.
3. If the origin is whitelisted, Salesforce answers with the allow headers; the actual call proceeds.
4. The call still needs a valid OAuth access token (obtained through a flow such as Web Server with PKCE, or User-Agent).
Check also the setting for enabling CORS on OAuth endpoints if your SPA calls token or userinfo endpoints from the browser.

**Advanced / deep dive:** CORS is supported for Salesforce REST-based APIs (REST, Chatter REST, Apex REST, Bulk API 2.0 and OAuth endpoints depending on settings); SOAP API is not covered. It does not apply to Visualforce pages or Apex callouts. Security caveats: only the browser enforces it, so server-to-server clients ignore it; whitelisting a wildcard or origin you do not control exposes users' tokens to scripts there. Exposing an SPA that holds access tokens in the browser carries XSS risk; for sensitive apps use a backend-for-frontend that holds tokens server-side. Do not confuse with CSP Trusted Sites (controls what Salesforce pages may call outward) and with Remote Site Settings (Apex callouts).

**Code / config example:**
```javascript
// Runs on https://app.example.com after OAuth login returned accessToken
const r = await fetch(`${instanceUrl}/services/data/v62.0/query?q=SELECT+Id,Name+FROM+Account+LIMIT+5`, {
  headers: { Authorization: `Bearer ${accessToken}` }
});
const data = await r.json();
```

**Hidden gotchas & must-remember points:**
- Origin must match scheme, host and port exactly; `http` vs `https` is a different origin.
- CORS errors appear in the browser even though the server request may have succeeded.
- Whitelisting does not grant access; users still need permissions.
- Wildcards are limited to subdomain patterns.
- Missing entries show up as opaque "blocked by CORS policy" console errors.

**Best practice:** List only specific HTTPS origins you control and keep access tokens out of long-lived browser storage.

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