# Control who can frame an app

Let only chosen sites show an app inside an iframe, on every response and every domain of the app, without changing its code.

By default the platform adds no framing header, and the app decides with its own
`X-Frame-Options` or `Content-Security-Policy`. Set `frameAncestors` and every response of the app
carries `Content-Security-Policy: frame-ancestors <your list>`: on every domain of the app, for
both hosting modes, the platform's own pages included (a kill switch's page, a pooled app's page
while it starts). Replace app-id with the app's id.

## What the setting means

| `frameAncestors`                           | Who may show the app in a frame           |
| ------------------------------------------ | ----------------------------------------- |
| `null` (default)                           | Whatever the app's own headers say        |
| `["'self'"]`                               | Only pages of the app's own origin        |
| `["'self'", "https://portal.example.com"]` | Those origins                             |
| `["https://*.example.com"]`                | Every subdomain of `example.com` on HTTPS |
| `["'none'"]`                               | No one                                    |

At most 10 entries. An entry is `'self'`, `'none'` (only on its own), or an `https://` origin with
an optional port and an optional `*.` as its leftmost label. A bare `*`, `http://` origins, paths
and an empty list are refused with `400`; to allow anyone, leave the setting `null`.

While the setting is on, the app's own `X-Frame-Options` header is removed: browsers that know
`frame-ancestors` ignore it anyway, and older ones then behave the same. The app's own
`Content-Security-Policy` stays beside the platform's. A browser applies both, so the app can
narrow the list (its own `frame-ancestors 'none'` still forbids every frame) but never widen it.

## Set the list

**Console**
1. Open the app and choose **Domains**.
2. Under **Framing**, choose **Only these sites** and enter the sites, one per line.
3. Choose **Save**. **Decided by the app** turns the header off again.

**CLI**
```sh
dcloud deploy apps update app-id --frame-ancestors '["'"'"'self'"'"'", "https://portal.example.com"]'
```

`--frame-ancestors null` turns it off.

**API**
```http
PATCH /deploy/api/apps/3f1c2b4a-5d6e-4f70-8a91-b2c3d4e5f601
{ "frameAncestors": ["'self'", "https://portal.example.com"] }
```

**Terraform**
```hcl
resource "dcloud_app" "web" {
  name            = "my-app"
  frame_ancestors = ["'self'", "https://portal.example.com"]
}
```

The change needs `deploy:UpdateApp` on the app and no deployment. A pooled app's pool host applies
it at its next pass, within seconds; an app's machines at their agent's next poll, within a minute.

## Related

- [Security](security.md)
- [Add a custom domain](add-a-custom-domain.md)
- `deploy.apps.update`
