# Browser apps

> Single-page apps call the realm straight from the browser, and each app's answers are readable only on the sites it lists.

A single-page app runs in the browser on its own site, so its calls to the realm are cross-origin. A browser lets
the page read the realm's answer only if the realm says that site may. The realm says so per app: each client lists
the sites its pages run on, in `webOrigins`, and by default that is the sites of its return addresses. One app's page
can never read the answers to another app's requests.

Use it for any app that calls the realm from a browser page. Such an app is a public client: it uses the
[browser sign-in](../authorization-code/) with PKCE and holds no secret, and its refresh tokens always
[rotate](../refresh-tokens/).

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`); `RealmViewer` can read a client's sites but
  not change them.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** the app is registered as a public client (`tokenEndpointAuthMethod` `none`) with its return addresses.

## How it works

| Endpoint | Which sites may read the answer |
|---|---|
| Token, revocation, pushed authorization, userinfo | The sites the client the request was made as lists in its `webOrigins` |
| Discovery document, signing keys, status lists | Any site, because they are public |
| Introspection, device authorization, backchannel authentication, first-party sign-in, registration | None. They are for servers and native apps |

Before a page at `https://app.example.com` posts to the token endpoint with a DPoP proof, the browser sends a
preflight:

```sh
curl -X OPTIONS https://foobar.cvyuh.com/oauth2/token \
  -H "Origin: https://app.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type, dpop"
```

If an active client of the realm lists that site, the realm answers:

```http
HTTP/1.1 204 No Content
Vary: origin
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: authorization, content-type, dpop
Access-Control-Max-Age: 600
```

The preflight carries no client, so the realm answers it for any site an active client of the realm lists. **The
request itself is answered only for the client it was made as**: the client that authenticated at the token,
revocation and pushed-authorization endpoints, and the access token's client at userinfo. Its answer carries
`Access-Control-Allow-Origin` again, only if that client lists the site, and
`Access-Control-Expose-Headers: dpop-nonce, www-authenticate`.

A site the realm doesn't allow gets the same answers with no `Access-Control-*` headers, so the browser keeps the
answer from the page. The public documents answer any site with `Access-Control-Allow-Origin: *`.

## Set it up

Set `webOrigins` on the client
(`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`; see [Call the API](../../../../develop/api/)):

| Field | Default | Meaning |
|---|---|---|
| `webOrigins` | `["+"]` | The sites this client's pages call from; `+` for the sites of its `redirectUris` |

A page served from a site that isn't one of its return addresses, a sign-in on `foobar.cvyuh.com` for an app on
`app.example.com` for example, needs that site in `webOrigins`.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `webOrigins` | `["+"]`, the sites of the client's `redirectUris` | exact sites, or `+`; never `*` or `null` |
| Plain `http` sites | refused | allowed only for `localhost`, `127.0.0.1` and `[::1]` |
| A preflight's answer kept by the browser | ten minutes | fixed |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The browser reports a CORS error on the token request | The page's site isn't in the client's `webOrigins`, nor the site of one of its return addresses | Add the exact site, such as `https://app.example.com`, to `webOrigins` |
| A CORS error even with the site listed | The request named no client or an unknown one, so no site is told anything | Send the right `client_id`; check the request in the network panel |
| Saving the client is refused | A site with a path, a trailing slash, `*`, `null`, or plain `http` on a public host | Write the site exactly as the browser sends it in `Origin` |

## Audit record

There's no record of a browser's cross-origin check itself. The token requests the page makes are `TOKEN_ISSUED`
records, and a change to `webOrigins` is a change to the client in **Audit → Config**.

## Good to know

- **`webOrigins` holds sites, or `+`.** A site is written exactly as a browser sends it: `https://app.example.com`,
  lowercase, with no path and no default port. Plain `http` is accepted only for `localhost`, `127.0.0.1` and
  `[::1]`. `*` and `null` are refused when the client is saved.
- **`+` means "the sites of my return addresses".** It is the default, so a client that sets nothing is allowed
  exactly where it receives its sign-in codes. List sites beside `+` to add more, or leave `+` out to allow only the
  sites you list.
- **When no client authenticated**, a malformed request or an unknown client, no site is told anything, so the page
  sees a browser CORS error rather than the realm's error.
- **A site not allowed is told nothing.** The realm never answers with an error for the site alone.
- **No answer ever carries cookies or other credentials.**
- **Headers and methods:** the page may send `Authorization`, `Content-Type` and `DPoP`, with `GET` or `POST`, and
  may read `DPoP-Nonce` and `WWW-Authenticate`, which a browser app using [DPoP](../../client-security/dpop/) needs. A browser may keep
  a preflight's answer for ten minutes.

## Related

- [Sign users in to a single-page app](../../../guides/sign-in-to-apps/single-page-app/): the task, start to finish.
- [Authorization code with PKCE](../authorization-code/): the sign-in a browser app uses.
- [DPoP](../../client-security/dpop/): bind a browser app's tokens to a key it holds, so a copied token is useless.
- [Cross-origin admin API](/platform/cross-origin/allowed-origins/): your own pages calling the realm's
  management API from another site.
