# Allowed origins

> Choose which of your own web pages may call the realm's APIs from a browser, and whether they act as the signed-in user.

The dashboard calls the realm's APIs from the realm's own host name, so it needs nothing here. A page you serve from
anywhere else does: your own admin tool, a status page, or a sign-in page of your own design. A browser lets such a
page read the realm's answers only if the realm lists the page's **origin** in its `corsSettings`: the scheme, host
and port the page is served from, such as `https://admin.example.com`. A realm lists nothing until you add it.

For each origin you list, you also choose whether its requests may carry the signed-in user's session.

## Before you begin

- **Privilege:** `MODIFY` on the realm's `corsSettings`, which `RealmOwner` gives.
- **Where:** the API.
- **First:** the exact origin each page is served from, and your session in `$CVYUH_SESSION`
  ([Call the API](/develop/api/#get-a-session)).

## How it works

Before a cross-origin write, the browser asks the realm first (a **preflight**). You can make the same request
yourself to see what a browser would be told:

```sh
curl -i -X OPTIONS https://foobar.cvyuh.com/api/v0/am/objects/v1/group \
  -H "Origin: https://admin.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type"
```

For a listed origin, the realm answers, in part:

```http
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://admin.example.com
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, QUERY
Access-Control-Max-Age: 600
Vary: origin
```

`Access-Control-Allow-Credentials` appears only for an origin listed with `credentials`. An origin that isn't listed
gets the `204` with none of the `Access-Control-` headers, and the browser stops there.

Two more rules protect writes, whatever you list:

- **Every write sends JSON.** A `POST`, `PUT` or `PATCH` whose body isn't JSON (`application/json`, or a type ending
  in `+json`) is answered `415`. So a browser always asks the realm before a cross-origin write.
- **A write carrying the session cookie comes only from where it may.** It is refused (`403`) unless it comes from
  the realm's own pages or from an origin listed with `credentials`. A script that sends no `Origin` header, and a
  call that sends an `Authorization` header instead of the cookie, aren't affected.

## Set it up

Write the realm's `corsSettings` object, whose id is `corsSettings`:

```sh
curl -X PUT https://foobar.cvyuh.com/api/v0/am/objects/v1/corsSettings/corsSettings \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "origins": [
          { "origin": "https://admin.example.com", "credentials": true },
          { "origin": "https://status.example.com" } ] }'
```

Each entry names one `origin`, written as a browser sends it, and `credentials` (`false` by default): whether that
origin's requests carry the signed-in user's session. [Call the API](/develop/api/) shows how to get
`$CVYUH_SESSION`.

## What happens when it fails

| What you see | Cause | What to do |
|---|---|---|
| `400` when you save the list | `*`, `null`, a wildcard, a path, upper case, a default port, plain `http` off loopback, or an origin listed twice | Write each origin as a browser sends it, once |
| The browser reports a CORS error, and your page can't read the answer | The page's origin isn't listed | Add it |
| `403` on a write from your page | The page sends the session cookie, and its origin isn't listed with `credentials` | List it with `"credentials": true`, if you control it |
| `415` on a write | The body isn't JSON | Send `Content-Type: application/json` |

## Limits and defaults

| What | Default | Can it change |
|---|---|---|
| Origins listed | None: only the realm's own pages | Add your own |
| `credentials` per origin | `false` | Set per origin |
| How long a browser may keep a preflight answer | 600 seconds | No |
| Plain `http` origins | Only `localhost`, `127.0.0.1` and `[::1]` | No |

## Troubleshooting

- **It works in `curl` and fails in the browser.** `curl` doesn't enforce CORS. Send the preflight above with your
  page's `Origin`, and check the answer carries `Access-Control-Allow-Origin`.
- **Reads work and writes are refused with `403`.** The origin is listed without `credentials`, and the write carries
  the session cookie. List it with `credentials`, or have the page send an `Authorization` header instead.
- **Your page never receives the session.** The browser sends the cookie only within the realm host name's own
  registered domain; see Good to know.

## Audit record

Each change to `corsSettings` is a change record naming who made it, before and after. Read it as the setting's
[change history](/audit-reports/audit-trail/change-history/):
`GET https://foobar.cvyuh.com/api/v0/am/objects/v1/corsSettings/corsSettings/history`.

## Good to know

- **With `credentials`, an origin's scripts act as the signed-in user.** They read and write as that user. List an
  origin that way only if you control it.
- **Each realm's list is its own.** No realm inherits its parent's list.
- **An origin is written exactly as a browser sends it**: `https://admin.example.com`, lowercase, no path, no default
  port.
- **`Origin: null` is never allowed.** A sandboxed frame or a local file can't be listed, and its writes with the
  session are refused.
- **The session cookie goes only where the browser sends it.** With the default cookie setting, a browser sends it
  to pages under the same registered domain as the realm's host name, and not to an unrelated domain, whatever the
  list says.
- **What a page may send and read.** Requests may send `Content-Type`, `If-Match`, `If-None-Match` and the
  request-id header. The page may read `Location`, `ETag`, the request id and `WWW-Authenticate`.
- **OAuth endpoints follow their own rule**, set per client: see
  [Browser apps](/workforce-identity/applications/single-sign-on/browser-apps/).

## Related

- [Call the API](/develop/api/): the calls a page of yours makes, and how a script calls them instead.
- [Browser apps](/workforce-identity/applications/single-sign-on/browser-apps/): which origins may call the realm's
  OAuth endpoints.
- [Realm base URL](/platform/realms/realm-base-url/): the one public address of the realm.
