# WAF

Switch the WAF of a domain on or off, browse its rules and create exceptions that turn one rule off.

This part of the API controls the WAF of one domain: whether it is on, which rules it has, and the exceptions that switch a rule off. The paths start with `/v2/cdn/waf/{domain_public_id}/`, where the id is the public id of the domain, taken from [the domains list](https://docs.diamwall.com/products/domains/#list-your-domains).

- [WAF status](#waf-status): Read whether the WAF is on, and switch it on or off.
- [Rules](#rules): List the rules of the WAF, with their ids.
- [Rule exceptions](#rule-exceptions): List, create, read and delete the exceptions of a domain.

The examples on this page use these shell variables:

```bash
export DIAMWALL_API_KEY="your key"
export WAF="https://api.diamwall.com/v2/cdn/waf/your-domain-public-id"
```

> **WAF and DWAF are separate**
> Nothing on this page reads or changes DWAF. See [DWAF](https://docs.diamwall.com/products/dwaf/) for that.

## WAF status

### Read whether the WAF is on

[`GET /v2/cdn/waf/{domain_public_id}/settings/waf/` in the API reference](https://docs.diamwall.com/api/#tag/waf/GET/v2/cdn/waf/{domain_public_id}/settings/waf/)

```bash
curl "$WAF/settings/waf/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:aea6d4e65b77...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "status": { "state": "resolved", "value": { "enabled": true } }
  }
}
```

- `data.status.value.enabled` is the answer, and it is only there when `data.status.state` is `resolved`. A `false` under `resolved` is a WAF that is off.
- A `state` of `unknown` means the status is unavailable. Show it as unavailable, never as `false`.
- When the whole group could not be read, the answer has a top-level `state` of `unknown`, a `revision` of `null`, no `data` and no `ETag`. There is nothing to write against until a later read succeeds.
- Keep the `ETag` header of the answer. The switch below takes it in `If-Match`.

### Switch the WAF on or off

[`PATCH /v2/cdn/waf/{domain_public_id}/settings/waf/` in the API reference](https://docs.diamwall.com/api/#tag/waf/PATCH/v2/cdn/waf/{domain_public_id}/settings/waf/)

Read the status first to get its `ETag`, then send it in `If-Match`:

```bash
ETAG=$(curl -s -o /dev/null -D - "$WAF/settings/waf/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  | tr -d '\r' | awk 'tolower($1) == "etag:" { print $2 }')
```

```bash
curl -X PATCH "$WAF/settings/waf/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "enabled": false }'
```

The body is exactly `{ "enabled": true }` or `{ "enabled": false }`. `enabled` must be a JSON boolean: `"true"`, `1`, `null`, an empty body and any other field answer `400 INVALID_REQUEST`.

The answer is the status as it is after the change, in the same form as the read, with a new `ETag` header for your next write.

> **Two answers you must not simply send again**
> - `409 INVALID_STATE`: the change was refused for the state the domain is in, and was not made. Read the status to see what is published.
> - `503 PUBLICATION_FAILED`: the change could not be published and verified, and may not have been fully undone. Read the status again and compare it with what you meant before you try anything else.
>
> Both carry a `request_id` to quote to support.

## Rules

[`GET /v2/cdn/waf/{domain_public_id}/references/waf-rules/` in the API reference](https://docs.diamwall.com/api/#tag/waf/GET/v2/cdn/waf/{domain_public_id}/references/waf-rules/)

The rules reference is the catalog of the WAF. It is read-only, and it is where you take the `rule_id` of an exception from.

```bash
curl "$WAF/references/waf-rules/?limit=50" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:b39a2d0c9ab7...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "state": "resolved",
    "items": [
      { "id": "10001", "description": "Illustrative WAF rule", "tag": "SQL" },
      { "id": "10002", "description": "Another illustrative WAF rule", "tag": "XSS" }
    ],
    "page": { "limit": 50, "next_cursor": null, "total": 2 }
  }
}
```

- `id` is a string of 1 to 10 digits. Send it back as a string, never as a number.
- `description` and `tag` are text to show.
- `limit` goes from 1 to 100. Without it a page holds 50 rules.
- To read the next page, send `data.page.next_cursor` back as `cursor`, until it is `null`. If the catalog changes between two pages, the next page answers `409 CURSOR_STALE`. Start again from the first page.
- `data.state` is always there. Only under `resolved` does the answer hold `items` and `page`. Under any other state the catalog is unavailable: show it as unavailable, never as a catalog with no rules.
- The answer has no `ETag`, and `limit` and `cursor` are the only query parameters accepted.

## Rule exceptions

An exception switches one rule of the WAF off. It has two fields:

| Field | Value |
| --- | --- |
| `rule_id` | The `id` of a rule from [the rules reference](#rules), as a string. |
| `scope` | `null` for the whole domain, or an object that holds the exception to one path and one part of the request. |

A scope object looks like this:

| `target` | `url` | `variable` |
| --- | --- | --- |
| `body` | Required. The path, starting with `/`. | Required. The name of the body field. |
| `args` | Required. The path, starting with `/`. | Required. The name of the argument. |
| `cookie` | Required. The path, starting with `/`. | Not accepted. |

`url` is one line of at most 2048 characters. `variable` is one line of 1 to 2048 characters. Neither may hold a control character.

An exception cannot be edited. To change one, delete it and create another.

### Writes need the latest revision

A create and a delete each have to prove they saw the latest state of the exceptions. The `ETag` header of a read is the revision of the whole collection, not of one exception. Send it back in `If-Match`, exactly as it came, with its quotes.

- Without `If-Match` the write answers `428 PRECONDITION_REQUIRED`.
- If the exceptions changed since your read, it answers `412 REVISION_MISMATCH`. Read again and retry with the new `ETag`.
- Every successful write returns a new `ETag` to use on the next one.

### List the exceptions

[`GET /v2/cdn/waf/{domain_public_id}/waf-rule-exceptions/` in the API reference](https://docs.diamwall.com/api/#tag/waf/GET/v2/cdn/waf/{domain_public_id}/waf-rule-exceptions/)

```bash
curl "$WAF/waf-rule-exceptions/?limit=50" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:b21425ed5c35...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "items": [
      {
        "id": "j2-Z9dZNCvV0qchtvGSZir0T",
        "rule_id": "10001",
        "scope": { "target": "args", "url": "/search", "variable": "q" },
        "scoped": true
      },
      {
        "id": "Qm4-xT7pLk2NwE9sVb1cHy3R",
        "rule_id": "10002",
        "scope": null,
        "scoped": false
      }
    ],
    "page": { "limit": 50, "next_cursor": null, "total": 2 }
  }
}
```

- `limit` goes from 1 to 100. Without it a page holds 50 exceptions.
- Paging works as for the rules: send `data.page.next_cursor` back as `cursor`, and start again from the first page on `409 CURSOR_STALE`.
- `data.page.total` is the number of exceptions of the domain, on every page together.
- An empty list still has an `ETag`. It is the one you send to create the first exception.
- `data.state` is not always there. When it is there and is not `resolved`, the answer has no `items`, no `page` and no `ETag`: the exceptions are unavailable, which is not the same as a domain with none.

### Create an exception

[`POST /v2/cdn/waf/{domain_public_id}/waf-rule-exceptions/` in the API reference](https://docs.diamwall.com/api/#tag/waf/POST/v2/cdn/waf/{domain_public_id}/waf-rule-exceptions/)

Read the list first to get the `ETag`:

```bash
ETAG=$(curl -s -o /dev/null -D - "$WAF/waf-rule-exceptions/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  | tr -d '\r' | awk 'tolower($1) == "etag:" { print $2 }')
```

**One argument**

```bash
curl -X POST "$WAF/waf-rule-exceptions/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{
    "rule_id": "10001",
    "scope": { "target": "args", "url": "/search", "variable": "q" }
  }'
```

**Cookies of a path**

```bash
curl -X POST "$WAF/waf-rule-exceptions/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{
    "rule_id": "10001",
    "scope": { "target": "cookie", "url": "/account" }
  }'
```

**Whole domain**

```bash
curl -X POST "$WAF/waf-rule-exceptions/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "rule_id": "10002", "scope": null }'
```

The answer is `201` with the new exception under `data`, its id in `operation.id` as well, and a new `ETag` header.

```json
{
  "revision": "sha256:c0afaf4f272d...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "id": "j2-Z9dZNCvV0qchtvGSZir0T",
    "rule_id": "10001",
    "scope": { "target": "args", "url": "/search", "variable": "q" },
    "scoped": true
  },
  "operation": { "type": "create", "warning_count": 0, "id": "j2-Z9dZNCvV0qchtvGSZir0T" }
}
```

Things to know:

- The body is exactly `rule_id` and `scope`. The `scope` key is always required: for the whole domain send `"scope": null`, do not leave it out.
- Nothing is trimmed or converted. A field in the wrong form answers `400 INVALID_REQUEST` and names it in `fields`, with an inner field as a path such as `scope.variable`.
- The API does not check `rule_id` against the rules reference. A rule id that no rule has is refused when the exception is published, and nothing is written.
- An exception that already exists answers `409 RESOURCE_CONFLICT`.
- Another change to the domain running at the same time can answer `409 DOMAIN_BUSY`. Try again shortly.

### Read one exception

[`GET /v2/cdn/waf/{domain_public_id}/waf-rule-exceptions/{exception_id}/` in the API reference](https://docs.diamwall.com/api/#tag/waf/GET/v2/cdn/waf/{domain_public_id}/waf-rule-exceptions/{exception_id}/)

```bash
curl "$WAF/waf-rule-exceptions/j2-Z9dZNCvV0qchtvGSZir0T/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

The exception comes back under `data`, in the same form as an item of the list. The `ETag` header is again the revision of the whole collection, so it also works in `If-Match`.

An id that no exception has answers `404 RESOURCE_ITEM_NOT_FOUND`. The exception may have been deleted, so read the list again.

### Delete an exception

[`DELETE /v2/cdn/waf/{domain_public_id}/waf-rule-exceptions/{exception_id}/` in the API reference](https://docs.diamwall.com/api/#tag/waf/DELETE/v2/cdn/waf/{domain_public_id}/waf-rule-exceptions/{exception_id}/)

The rule applies again where the exception held it off. The request takes no body. `If-Match` is the `ETag` of a read of the list or of the exception.

```bash
curl -X DELETE "$WAF/waf-rule-exceptions/j2-Z9dZNCvV0qchtvGSZir0T/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "If-Match: $ETAG"
```

The answer has no `data`. `operation.id` is the id the exception had, and the `ETag` header is the new revision of the collection.

> **When the outcome is not confirmed**
> A create or a delete that answers `503 ROLLBACK_FAILED` could not be published, and what was there before could not be restored. Do not send the request again. Read the list, compare it with what you meant, and only then decide what to send, with the `ETag` of that read. Quote the `request_id` to support.

## Limits and permissions

- **Credits.** Each domain has a budget of credits per minute, shared by everyone who calls the API for that domain. A WAF call costs 8 credits. Listing the rules costs 15.
- **Creating an exception costs 38.** Besides the 8 of the write, the API spends 30 on reading the counts that go into the plan limit. A create that is refused after those reads, for example with `400 PLAN_LIMIT_EXCEEDED` or `412 REVISION_MISMATCH`, keeps the credits it spent.
- **Out of credits.** The call answers `429 TOO_MANY_REQUESTS`, with the wait in `details.retry_after_seconds` and in the `Retry-After` header.
- **Plan limit.** Pro 25, Business 100, Enterprise 200. The limit counts four things together: the firewall rules of the domain, its access rules, the WAF rule exceptions on this page, and the exceptions of the root of its DWAF. A create at or above the limit answers `400 PLAN_LIMIT_EXCEEDED`, with `details.limit`, `details.current`, `details.plan` and `details.feature`. `details.current` can be above `details.limit` after a move to a lower plan.
- **Hard ceiling.** A domain holds at most 4096 exceptions, whatever its plan. One more answers `400 INVALID_REQUEST`.
- **Permissions.** Each action has its own permission on the domain, and `waf.edit` alone does not let you create or delete an exception.

| Action | Permission |
| --- | --- |
| Read the status, the rules or the exceptions | `waf.view` |
| Switch the WAF on or off | `waf.edit` |
| Create an exception | `waf.create` |
| Delete an exception | `waf.delete` |

## Errors you are likely to see

| Status | Code | What happened |
| --- | --- | --- |
| `400` | `INVALID_REQUEST` | A field is wrong, or the body has a field that is not accepted. The answer names the field in `fields` when it can. |
| `400` | `INVALID_PRECONDITION` | The `If-Match` header is not one quoted `ETag` as a read returned it. |
| `400` | `PLAN_LIMIT_EXCEEDED` | The domain is at the rule limit of its plan. Nothing was created. |
| `404` | `RESOURCE_ITEM_NOT_FOUND` | No exception has that `id`. |
| `404` | `REFERENCE_NOT_FOUND` | The rules reference is not available for this domain. Asking again answers the same. |
| `404` | `SETTINGS_GROUP_NOT_FOUND` | The WAF settings are not available for this domain. |
| `409` | `RESOURCE_CONFLICT` | That exception already exists. |
| `409` | `CURSOR_STALE` | The list changed while you were paging. Start from the first page. |
| `409` | `DOMAIN_BUSY` | Another change to the domain is running. Try again shortly. |
| `409` | `INVALID_STATE` | The switch was refused for the state the domain is in. Nothing changed. |
| `412` | `REVISION_MISMATCH` | What you are writing changed since your read. Read again and retry. |
| `428` | `PRECONDITION_REQUIRED` | The `If-Match` header is missing. |
| `429` | `TOO_MANY_REQUESTS` | The domain is out of credits for now. |
| `503` | `PUBLICATION_FAILED` | The switch could not be published and verified. Read the status before trying again. |
| `503` | `ROLLBACK_FAILED` | An exception write failed and could not be undone. Read the list before trying again. |
| `503` | `UNSAFE_PATH` | The rules could not be read safely. The catalog is unavailable, not empty. |

The errors that come from the key itself, such as `API_KEY_IP_NOT_ALLOWED`, are on the [Errors](https://docs.diamwall.com/reference/errors/) page.

## All WAF endpoints

Each row opens the endpoint in the API reference, with every parameter, rule and response.

See the [API reference](https://docs.diamwall.com/api/#tag/waf) for every endpoint, with parameters and responses.
