# Protection against cross-site requests

A browser sends a web site's cookies with every request to that site, also with requests that **another** site started. A page on some other site can so try to make your browser do things on a server you are logged in to: delete a file, add a user. This is called cross-site request forgery.

CrushMFT stops it with a token that only its own pages know. This page explains how, and what an administrator can and cannot change about it.

## The token

When somebody logs in to the web interface, the server makes a random token for that session, 43 characters long, and gives it to the page. It has nothing to do with the session cookie.

Every request that **does** something must carry the token, as the parameter `c2f`. A request without it, or with a wrong one, is refused, whatever cookies it brings. Another site can make the browser send cookies; it cannot read the token, so it cannot add it.

| Property | |
|---|---|
| where it lives | on the server, with the session; and in the page |
| how the page gets it | in the answer to the login, and in a cookie named `CrushC2F` that scripts of the server's own pages may read |
| how long it lasts | as long as the session. A new login gets a new token; a token never carries over to another session. |
| how it is compared | exactly, upper and lower case included |
| in the log | never: it is masked wherever a request is written down |

The server does not trust the cookie. It compares what a request presents with its own copy.

## What needs no token

| Request | Why |
|---|---|
| one that logs in with a header, as scripts do (`Authorization: Basic` or `Bearer`) | a browser never adds such a header to a request another site starts |
| a short list of commands that only read | they change nothing |
| the API, for a browser session | it wants the token in a header, `X-CrushMFT-C2F`, which a form on another site cannot set |

A download needs the token: it is not on the read-only list.

## Settings

| Setting | What it does | New server |
|---|---|---|
| `csrf` | the check itself | on. Leave it on. |
| `whitelist_web_commands` | further commands that need no token. Only commands that read can be put here; the server refuses to exempt one that changes anything, and says so in the log. | a few read commands |
| `http_same_site` | the `SameSite` mark of the session cookie: `Lax` keeps the browser from sending it with most requests other sites start | `Lax` |

These are settings of the server's `prefs.XML`.

## Cross-origin access

Related, and set on the **CSP** tab: **Access Control Allow Origin** says which other sites' pages may read the server's answers. See [Content Security Policy](Prefs-WebInterface-CSP.md#access-control-allow-origin). With `*` there, the server answers a literal `*`, which browsers never combine with a login. A named site is trusted with the logged-in user's answers: name only sites you run.

## Links that log in

A link can carry a user name and a password, as shares do. A hostile page could use such a link to log a visitor in to the **attacker's** account without their noticing, so that what they upload lands with the attacker. The server guards against that:

- a link to a **file** from another site serves the file and sets no session;
- a link into the **web interface** from another site, while the visitor is logged in as somebody else, shows a page that says so and asks before it switches;
- a link that the visitor typed or opened from a mail behaves as it always did.

## For people who write clients

- Log in, read `<c2f>` from the answer, and send it as `c2f` with every later request.
- A program that holds only a session cookie, and did not log in itself, asks for the token with the command `getC2F`.
- Or do not use cookies at all: authenticate each request with a header, and no token is needed. For anything new, use the [API](API.md) with an [access token](Security-Roles-Tokens.md).

The four characters that earlier versions used in place of this token are no longer accepted. Clients written for those versions must be updated; the apps of the CrushMFT family that ship with version 27 are.

## Things to know

- Behind a proxy that rewrites cookies or strips headers, the token's cookie must come through unchanged.
- The token protects sessions in a browser. It does not replace HTTPS, and it does not protect against a script that runs **inside** the server's own pages: keep **Custom Javascript** of users to what you wrote. See [WebInterface](Users-WebInterface.md#custom-code).
