# Access control

Source: https://docs.apphaven.eu/access

Every app on AppHaven sits behind a managed login gate by default. You decide
who can reach it: keep it private to your project, or make it public. There is
no login form to build and no user accounts to run for the gate itself.

You set this in the app's **Security** section in the console, as an
**access mode**. An app with several [entry points](https://docs.apphaven.eu/manifest.md#ports-and-addresses)
has one access mode per entry point, so a public website and a private CMS can
live in the same app.

Diagram:

1. visitor → AppHaven login gate → your app
2. the login gate authenticates the visitor with the AppHaven identity provider

## Access modes

| Mode | Production | Preview branches |
| ---- | ---------- | ---------------- |
| **Private** (default) | Members only | Members only |
| **Public** | Anyone | Members only |
| **Public, including previews** | Anyone | Anyone |

"Production" is the deployment of your app's production branch. Every other
branch is a **preview**.

- **Private** is the default. Every deployment, production and previews alike, is
  reachable only by the members of the app's project. This is the right setting
  for internal tools and anything not meant for the public.
- **Public** opens your production deployment to anyone, while preview branches
  stay private to your project. Use it for a public site whose in-progress
  branches you would rather not expose.
- **Public, including previews** makes every deployment reachable by anyone.

## Each entry point has its own setting

Every service and port in your manifest is an entry point with its own
address, and each has its own access mode and public paths. The setting
covers every address of that entry point: its generated address, a chosen
address and a custom domain.

A new entry point starts **Private**, whatever the others are set to.
Adding a service with `ports` in a commit, changing a port, or renaming a
service therefore never makes anything public: a project administrator does
that in the console. The one exception is an app's first deploy: if it has a
single entry point, that entry point takes the access setting chosen before
the deploy. With several, every one starts private and you set each one, so
the order of services in your manifest never decides what is public. An app
with a single entry point shows a single setting, as before.

## Who can reach a private app

A private deployment is reachable by the **members of the app's project**. To
give someone access, invite them from the project's **Members** page. Once they
accept, they sign in with AppHaven and can open the app. A member who only needs
access (not the ability to manage the project) sees a simple dashboard listing
the apps they can reach, and nothing else.

Removing access is immediate: drop someone from the project and they can no
longer reach its private apps.

## Changing the mode

Access mode is managed by a project's administrators in the app's **Security**
section, per entry point. A change applies the next time the branch deploys, so
redeploy (or push a new commit) for a new mode to take effect.

## Public paths

Sometimes one small part of a private app has to stay reachable without a
login: a webhook a payment provider calls, a health probe an uptime monitor
hits, an API your partners talk to with their own keys. **Public paths** let you
open exactly those URLs and keep the rest of the app behind the gate.

A public path is an **exemption**. A request whose path matches one of them
skips the login gate entirely and goes straight to your app, with no sign-in and
no identity header. Everything else on the app still needs a session. Public
paths belong to one entry point: opening `/api/*` on the CMS opens nothing on
the website.

:::caution[An exempt path is open to the internet]

Anyone can call it, from anywhere, with no AppHaven login. Your app is fully
responsible for authenticating those requests itself, for example with a webhook
signature, an API key, or a shared secret. AppHaven checks nothing on a public
path.

:::

### Adding one

Public paths live in the app's **Security** section, on the same **Access** card
as the access mode of their entry point, and only a project administrator can
change them. Like the
access mode, a change **applies the next time the branch deploys**: add or remove a
path and then redeploy (or push a commit) for it to take effect. That matters
most when you remove one, because the old exemption keeps working until the next
deploy.

Use [change proposals](https://docs.apphaven.eu/proposals.md) to review paths suggested by an agent or repository. The CLI
opens a review URL; repository recommendations appear from the production branch. Administrators
approve public-path changes in the app's **Proposals** section. Active exemptions remain here in
Security. `auth.public_paths` continues to work as a compatibility input into proposals.
A suggestion or proposal can name the service, and the port, it is for; see
[Suggesting a public path](https://docs.apphaven.eu/manifest.md#suggesting-a-public-path). Without them it
is offered on every entry point, and the administrator accepts it on one.
A public path approved before the app's first deploy goes to the entry point
it names. One that names none is kept only when the first deploy has a single
entry point; otherwise it is dropped and you approve it again on the entry
point it is for.

### The two pattern forms

There are exactly two shapes, and no others:

| Pattern | What it covers |
| ------- | -------------- |
| `/healthz` | That one path, and nothing else. `/healthz/`, `/healthz/x` and `/healthzz` all still require a login. |
| `/api/*` | `/api/` and everything below it, at any depth. A trailing `/*` is the only wildcard, and it may only appear at the end. |

The root path `/` is a valid exact rule: it opens the landing page of your app
and nothing else. Every other path, `/about` included, still requires a login.
To open the whole app instead, use access mode **Public**.

A prefix really does cover everything under it, including paths you may not have
had in mind. If `/api/*` is public, then `/api/admin/delete-everything` is public
too. Choose the narrowest prefix that does the job.

:::tip[Make the public part recognisable]

It helps to group everything that should be reachable without a login under one
clearly named prefix, for example by serving those endpoints from
`/api/public/…` and exempting `/api/public/*`. Anyone reading your code or your
Access card can then see at a glance what is open, and nothing else ever lands
under the exemption by accident.

:::

You can set **up to 10 public paths per entry point**, each **up to 128 characters**.

:::caution[The query string is not part of the rule]

A public path covers the path only. Once `/api/*` is public, every query string
on it is public as well, including `/api/orders?_method=DELETE` on frameworks
that honour a method override. If a read and a write share a path, exempting
that path exempts both.

:::

### How a request is matched

To decide whether a request is exempt, AppHaven looks at the path portion of
the URL only, never the query string. Four rules govern the comparison, and
each one is worth knowing before you rely on an exemption.

**1. The path is tidied up before it is compared.** Percent-escapes are decoded,
`.` and `..` segments are resolved, and repeated slashes are collapsed. The
comparison happens on the result, so you cannot climb out of an exempt prefix
into a protected one.

| Request | Compared as | Result with `/api/*` public |
| ------- | ----------- | --------------------------- |
| `/api/orders` | `/api/orders` | Public |
| `/api/../admin` | `/admin` | Requires a login |
| `/api/%2e%2e/admin` | `/admin` | Requires a login |
| `//api/orders` | `/api/orders` | Public |

**2. Matching ignores letter case.** `/api/*` also exempts `/API/orders` and
`/Api/Orders`. The gate treats them as the same path. Your app may well answer
those with a 404, since it decides its own routing, but the login gate will not
stop them. Keep this in mind when you pick a prefix: exempting `/api/*` exempts
every capitalisation of it.

**3. A path containing `;`, `%25` or `%00` always requires a login.** These
sequences are the ones where your app could read the path differently from the
way the gate read it, so the gate does not treat such a request as exempt. The
request is not blocked or rejected: it simply falls through to the normal login
gate and works for a signed-in user. Ordinary escapes are unaffected, so
`/api/a%20b`, `/api/caf%C3%A9` and `/api/items/foo%2Fbar` all stay public.

**4. Your app receives the original path, byte for byte.** The tidying up in
rule 1 is only how the exemption is decided. Nothing is rewritten: a request for
`/api/items/foo%2Fbar` arrives at your app exactly as it was sent, escapes and
all. Your routing sees what the client typed.

### What a public request looks like to your app

A request on a public path reaches your app with **no `X-Apphaven-Auth` header
at all**. There is no anonymous token and no empty value; the header is simply
absent. If a client tries to supply one, AppHaven removes it first, so a header
that is present always came from the gate and a header that is missing always
means the request was anonymous. The
[Authentication reference](https://docs.apphaven.eu/reference/authentication.md#public-paths-carry-no-header)
is the full contract.

## What your app receives

When a request passes the gate, it reaches your app carrying the signed-in
user's verified identity. Your app reads that identity from a single request
header and uses it to decide what the user may do. The
[Authentication reference](https://docs.apphaven.eu/reference/authentication.md) is the full contract for
that header, and [Security](https://docs.apphaven.eu/security.md) covers how the platform protects the path
in front of your app.
