# How do CORS settings permit authorized web applications to perform cross-origin REST requests against Salesforce?

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

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-cors-settings-permit-authorized-web-applications-to-perform-cross-origin-rest-requests-against

## Answer

**Basics:** Browsers enforce the same-origin policy, so a single-page app on `https://app.example.com` cannot read responses from `https://mydomain.my.salesforce.com` unless Salesforce replies with the right CORS headers. Adding the origin tells Salesforce to send them.

**How it works:**
1. Setup > Security > CORS > New. Enter the origin, for example `https://app.example.com` (a wildcard subdomain such as `https://*.example.com` is supported).
2. The browser sends a preflight `OPTIONS` request, then the real call with an `Authorization: Bearer <token>` header.
3. Salesforce checks the `Origin` header against the allowlist and returns `Access-Control-Allow-Origin`.
4. The call still needs a valid OAuth access token.

**Advanced / deep dive:** CORS only concerns browsers. Server-to-server calls are unaffected. It covers supported Salesforce APIs, such as REST, UI API, Bulk API 2.0 and certain OAuth endpoints (verify the current list, which varies by endpoint), and it does not apply to SOAP. It is not an authorization mechanism, since attackers can call the API from any non-browser client, so token security and permissions still matter. For browser-held tokens use Authorization Code with PKCE. The opposite direction, where Salesforce pages load external resources, is controlled by CSP Trusted Sites and not by CORS. For Experience Cloud and LWR sites, use Trusted Sites for external APIs and CORS only when the external app calls into Salesforce.

**Code / config example:**
```javascript
// Browser app on an allowlisted origin
const res = await fetch(`${instanceUrl}/services/data/v62.0/query?q=${encodeURIComponent('SELECT Id,Name FROM Account LIMIT 5')}`, {
  headers: { Authorization: `Bearer ${accessToken}` }
});
const data = await res.json();
```

**Hidden gotchas & must-remember points:**
- CORS is not CSP. Trusted Sites controls what Salesforce pages may load.
- Use the exact scheme, host and port. `http` and `https` are different origins (non-secure origins are generally rejected apart from localhost, so verify).
- Use your My Domain URL or instance URL as the API base, not `login.salesforce.com`, for API calls.
- A CORS error often hides a 401 or 403 from an expired token.
- CORS does not bypass sharing or field-level security.
- There is a maximum number of allowlisted origins (verify the current number).

**Best practice:** Allowlist only the specific origins you control, and use PKCE with short-lived tokens in browser apps.

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