# Policy

## Introduction

A source's **policy** is the list of calls it accepts. Each line names one or
more methods and a path. A call that no line allows is refused with `403`
before it reaches your origin and before it takes a concurrency slot.

A policy has two parts:

- The **default source policy** is what every caller gets. Keep it to the
  least any caller needs.
- **More for groups** adds calls for the members who need them, and for no
  one else.

An [API token](/docs/api/control) or a [connected agent](/docs/agents) can be
held to fewer calls than the person behind it, and never to more.

The policy is on the source's **Access** tab, under **Who can do what**. It
applies to API sources. A
[database source](/docs/sources/database) is read-only, and an
[MCP source](/docs/sources/mcp) lists the tools callers may use.

## Rules

A rule is the methods, then a path:

```text
GET HEAD OPTIONS /**
POST             /services/rest/query/v1/suiteql
POST PATCH       /services/rest/record/v1/salesOrder/**
```

With that policy, callers can read anything, run SuiteQL, and create or change
sales orders. A `DELETE`, or a `POST` to any other path, is refused.

| In a path | Matches | Example |
|---|---|---|
| A word | That segment exactly | `/orders` matches `/orders` only |
| `*` | Exactly one segment | `/orders/*` matches `/orders/42`, not `/orders/42/lines` |
| `**` at the end | Everything below, including nothing | `/orders/**` matches `/orders`, `/orders/42`, and `/orders/42/lines` |

- **Methods:** list the ones you mean, or choose **Any method**.
- **Order does not matter.** A call is allowed if any rule matches it. There
  is no deny rule: leave a call off the list to refuse it.
- **Paths are case-sensitive.** `/Orders` and `/orders` are different.
- **The query string is not matched.** `/search?q=shoes` is the path `/search`.
- **One trailing slash is ignored.** `/orders` and `/orders/` are the same.
- **Odd paths only match `/**`.** A path with an encoded slash (`%2F`), a
  backslash, or an empty segment (`//`) could be read differently by your
  origin, so a narrower rule never matches it.
- A policy holds up to **64** rules, and a path up to **200** characters.

### What a policy does not check

The policy looks at the method and the path, not the request body. A rule that
allows `POST` to a query endpoint (SuiteQL, GraphQL, a search API) allows every
query that endpoint will run. Limit what such a call can reach with the
upstream account's own permissions.

## The default source policy

A new API source starts with one rule:

```text
GET HEAD OPTIONS /**
```

Callers can read any path and nothing can be changed through the source. That
is the default to keep. When some callers need to write, give the write to
their group rather than adding it here, where every key on the source gets it.

On the create form, check **Callers need to create, change or delete through
this source** to start with every method on every path instead, then narrow
it here.

A source that allows a write method on `/**` shows **Writes are allowed on
every path** above its rules. Any key on that source can create, change, and
delete anything the upstream account can.

## More for groups

A [group](/docs/groups) can hold a **group policy**: calls its members can
make on top of the source policy.

1. On the source's **Access** tab, click **Edit**.
2. Under **Group policies**, click **Add a group policy** and choose the group.
3. List the calls its members need, in the same form as any rule.
4. Click **Review changes**, then **Save**.

A source holds no rules for one person. To give one member more calls, put
them in a group that has them, or create a group for it.

Who can use the source is separate. Under **Who can use this source**, choose
**Everyone in the organization** or **Only members of these groups**
([restrict a source](/docs/groups#restrict-a-source)). When the source is
limited to some groups, another group's policy reaches only its members who
are also in one of those groups.

Rules add up. A member can make the default calls, plus the calls of every
group they are in:

| Who | Holds | Can call |
|---|---|---|
| Any caller | The default: `GET HEAD OPTIONS /**` | Reads |
| A member of Finance | `POST PATCH /services/rest/record/v1/vendorBill/**` | Reads, and create or change vendor bills |
| A member of Order admins | `DELETE /services/rest/record/v1/salesOrder/*` | Reads, and delete a sales order |

- **Rules only add.** There is no rule that takes a call away from someone.
- **Owners and admins get no extra calls for their role.** They can edit the
  policy, but their keys follow it like anyone else's.
- **Changes apply to keys already issued**, within about a minute.
- **Remove someone from a group and the keys they made on the group's
  sources are revoked.** Their next key carries what they still hold.
- A source holds rules for up to **200** groups, API tokens and agents
  together.

## Edit the policy

Owners and admins click **Edit** on the Access tab. Who can use the source,
whether AI agents can, the source policy and each group policy are edited
together, one call per row, and nothing changes until you save.

**Review changes** shows each change before it is saved, and who it affects:
who can now make a call, who can no longer make it, and who keeps it through
another policy. When who can use the source changes, it also lists the group
policies that now reach different people. People who can no longer use the
source at all lose the keys they hold on it, and turning AI agents off ends
the keys agents hold on it; keys and API tokens for integrations keep working. If someone else saved a change while you were editing, the
review says so and nothing is saved.

**Save** applies the policy to every key on the source, including ones
already issued, within about a minute.

### Suggest from traffic

**Suggest from traffic** reads the source's last 7 days of
[logs](/docs/sources/logs) and offers what callers did as rules:

- Reads are folded into one rule, `GET HEAD OPTIONS /**`.
- Each path that was written to gets its own rule, with how often it was
  called and an example.
- Numbers and long IDs in a path are shown as `*`, so `/orders/42` and
  `/orders/43` become `/orders/*`.

Check the ones to keep, then **Replace the list with these** or **Add to the
list**. Nothing changes until you save.

The log is a sample. A job that did not run in those 7 days is not in it, and
a call the policy refused is, because a caller tried it. Read the list before
you drop a call.

### Narrow it safely

A saved policy is enforced right away: there is no trial mode. Narrow a
source in small steps, then check **See refused calls** for anything a caller
still needs.
[Narrow a source to the calls it needs](/docs/knowledge-base/narrow-source-policy)
walks through this.

## Who can call what

An access key carries who it acts for, not a copy of the rules. swarmvia
checks each call against the policy as it stands, in this order:

1. **The person.** The default source policy, plus the rules of the groups the
   person is in. The person is whoever created the key: you
   for a test key, the token's owner for a key from the Control API, and the
   member who connected the agent for a session key.
2. **The API token or agent the key came from.** If it was limited to chosen
   calls on this source, the call must be on that list too.
3. **A read-only key.** It can `GET`, `HEAD`, and `OPTIONS`, plus any `POST`
   the source [counts as a read](/docs/sources/access#what-a-key-can-call).
   The other writes the steps above allow are left out.

Each step can only narrow the one before it.

To see the result for one person, owners and admins click **Check someone's
access** on the Access tab and pick a person, API token or agent. It shows
whether they can use the source, and each call they can make, marked with the
source policy or group policy it comes from. Anyone else sees **What you can
call**: their own calls.

A key from a limited API token or agent is refused if swarmvia cannot find
that limit, rather than served without it. Keys are revoked, and the caller
mints a new one, when:

- an API token or agent starts or stops being limited on the source;
- the API token is revoked or the agent is disconnected;
- the member leaves the organization or a group that holds rules here.

An API token limited to chosen sources cannot create, change or delete
sources, and cannot read or revoke keys on a source outside its list.

### API tokens

When you create a [Control API](/docs/api/control) token, **What this token
can do** is one of:

- **Only the sources and calls I choose.** The token's keys work on those
  sources and nowhere else, and make only the calls you list. Each list starts
  from what you can call there. Use this for an integration.
- **Everything I can do.** Every source you can use, with every call you can
  make, now and as your access changes. Use this for your own admin scripts.

A token's list cannot be edited after it is created. Revoke the token and
create another.

### Agents

When you [connect an agent](/docs/agents#approve-the-grant), each API source
you give it is one of **Read-only** (the default), **Chosen calls**, or
**Everything I can do**. Change it later under **Settings → Connected
agents**. Disconnecting the agent removes its list.

### Read-only keys

Check **Read-only key** when you create a test key, or send
`"access": "read"` to the [Control API](/docs/api/control#key-access). On a
source where you can only read, your key is already read-only and the option
is not shown.

A read-only key cannot `POST` to a query endpoint such as SuiteQL or GraphQL
unless the source counts that `POST` as a read. For a caller that only runs
queries, give it **Chosen calls** with reads and that one `POST`.

Some origins run the method named in a header instead of the one the request
is sent with. A request that carries `X-HTTP-Method-Override`,
`X-HTTP-Method`, or `X-Method-Override` must be allowed as the method the
header names as well as the one it is sent with, so a `POST` rule does not
grant `DELETE` through the header. The refusal names the header:

```json
{
  "error": "forbidden",
  "message": "this source's policy does not allow DELETE /orders/42 (x-http-method-override header)"
}
```

swarmvia does not read the request body or query string. If your origin
takes the method from a `_method` field, leave those paths out of the policy
or turn that off at the origin.

[Async job](/docs/sources/async) polling is not subject to the policy. It
reads a job from swarmvia and does not reach your origin.

## Refused calls

A call that neither the default policy nor the caller's group rules allow
returns `403`:

```json
{
  "error": "forbidden",
  "message": "this source's policy does not allow DELETE /services/rest/record/v1/salesOrder/42"
}
```

A call the person may make, sent with a key from an API token or agent that is
limited to less:

```json
{
  "error": "forbidden",
  "message": "this access key is not permitted to POST /services/rest/record/v1/salesOrder"
}
```

A write sent with a read-only key also returns `403`:

```json
{
  "error": "unauthorized",
  "message": "access key is not permitted to POST"
}
```

On the **Logs** tab, filter by **Denied by policy** to see them, with the key that made each call. **See refused calls** under the
policy on the Access tab opens that filter.

## From the API and from agents

- The [Control API](/docs/api/control#source-policies) reads a policy and, with
  an owner's or admin's token, sets the default source policy and a group's
  rules.
- Over [MCP](/docs/mcp), `get_source_policy` tells an agent what its own keys
  can call. An agent can change a policy only when an owner or admin checked
  **This agent may change source policies** for it.

Neither can change what an API token or an agent is itself limited to.

## Next steps

- [Access](/docs/sources/access)
- [Groups](/docs/groups)
- [Narrow a source to the calls it needs](/docs/knowledge-base/narrow-source-policy)
- [Logs](/docs/sources/logs)
- [Understand admission errors](/docs/knowledge-base/admission-errors)
