# DWAF

Switch DWAF on or off for a domain, pick its profile, turn rules off and add exceptions, for the domain or for one subdomain.

DWAF is the DiamWall Application Firewall. These endpoints control it for one domain at a time: the switch of the domain, and the configuration of the domain and of each subdomain that has its own. The paths start with `/v2/cdn/dwaf/{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).

- [Settings](#settings): See whether DWAF is on for the domain, and switch it.
- [Scopes](#scopes): Profile, disabled rules and exceptions, for the domain or a subdomain.
- [Catalogs](#catalogs): The profiles and the rules you can name in a scope.

The examples on this page use these shell variables:

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

## Settings

### Read the settings

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

One read gives you everything needed to draw a DWAF page: the switch of the domain, its profile and the two catalogs.

```bash
curl "$DWAF/settings/dwaf/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:a58bca68a8f7...",
  "consistency": "verified",
  "data": {
    "enabled": { "state": "resolved", "value": true },
    "profile": { "state": "resolved", "value": "balanced" },
    "available_profiles": {
      "state": "resolved",
      "value": [{ "id": "balanced", "name": "Balanced", "description": "Illustrative balanced profile" }]
    },
    "core_rules": {
      "state": "resolved",
      "value": {
        "categories": ["sqli", "xss"],
        "rules_count": 2,
        "rules": [
          {
            "id": "sqli-001",
            "name": "Example SQL injection rule",
            "description": "Illustrative public rule metadata",
            "category": "sqli",
            "tags": ["sqli", "request"],
            "targets": ["uri", "args"]
          }
        ]
      }
    }
  }
}
```

| Field | What its `value` holds |
| --- | --- |
| `enabled` | A boolean: whether DWAF is on for the domain itself |
| `profile` | The `id` of the profile of the domain, or `null` when it has none |
| `available_profiles` | The whole catalog of profiles |
| `core_rules` | The whole catalog of rules, with `categories`, `rules_count` and `rules` |

- Each field has a `state`, and a `value` only when the `state` is `resolved`. A `state` of `unknown` means the value is unavailable. Never treat it as `true`, `false`, a default or an empty catalog.
- When the whole group could not be read, the answer is still `200`, with `state: "unknown"`, a `revision` of `null`, no `data` and no `ETag`. There is nothing to write with then.
- The `ETag` header is the revision of the group. Keep it for the switch below.
- The profile, the disabled rules and the exceptions are not here. They are in the [scopes](#scopes).

### Switch DWAF on or off

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

This switches DWAF for the domain itself. Read the settings first to get the `ETag`, then send it in `If-Match`:

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

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

- The body is exactly `{"enabled": true}` or `{"enabled": false}`. No body, `{}`, a string, a number or any other field answers `400 INVALID_REQUEST`.
- The answer is the group after the change, with a new `ETag` for the next edit.
- To switch DWAF for one subdomain, edit its scope instead.

## Scopes

A scope is the DWAF configuration of one host. Every domain has a `root` scope, for the domain itself. A subdomain has a scope only when you create one. A subdomain without a scope uses the configuration of the domain.

| Field | Meaning |
| --- | --- |
| `id` | The id of the scope. Use it in the path. It is not a hostname. |
| `scope` | `root` or `subdomain` |
| `hostname` | The full host, such as `api.example.com` |
| `subdomain` | The label of the host, such as `api`. Only on a subdomain scope. |
| `directive` | The `enabled` and `profile` of the domain, each with a `state`. Only on the root scope. |
| `config` | The configuration: `enabled`, `profile`, `disabled_rules`, `exceptions` and a few read-only fields |
| `mutable` | `false` means changes to this scope are refused with `409 INVALID_STATE`. A missing field does not mean `true`. |

A key that is missing from `config` is unavailable. It is never `false`, `null` or an empty list.

### List the scopes

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

```bash
curl "$DWAF/dwaf-scopes/?limit=50" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:da8af4d25e6c...",
  "data": {
    "items": [
      {
        "id": "Rt5kQ8wZ2nB7vX4cM1pL9sJ6",
        "scope": "root",
        "hostname": "example.com",
        "directive": {
          "enabled": { "state": "resolved", "value": true },
          "profile": { "state": "resolved", "value": "balanced" }
        },
        "config": {
          "client_id": "example.com",
          "name": "Example tenant",
          "enabled": true,
          "profile": "balanced",
          "disabled_rules": ["xss-001"],
          "exceptions": [],
          "whitelist_ip_count": 1,
          "block_page_configured": true
        },
        "mutable": true
      },
      {
        "id": "Sb3hG6dF9aK2jL5_P8oI1uY4",
        "scope": "subdomain",
        "hostname": "api.example.com",
        "subdomain": "api",
        "config": {
          "client_id": "api.example.com",
          "name": "API",
          "enabled": true,
          "disabled_rules": [],
          "exceptions": []
        },
        "mutable": true
      }
    ],
    "page": { "limit": 50, "next_cursor": null, "total": 2 }
  }
}
```

- `limit` goes from 1 to 100. Without it a page holds 50 scopes.
- To read the next page, send `data.page.next_cursor` back as `cursor`, until it is `null`.
- If the scopes change between two pages, the next page answers `409 CURSOR_STALE`. Start again from the first page.
- Only hosts that have a scope are listed. To see every name of the domain, read its [DNS records](https://docs.diamwall.com/products/dns/#list-the-records).

### Read one scope

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

```bash
curl "$DWAF/dwaf-scopes/Sb3hG6dF9aK2jL5_P8oI1uY4/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

The scope comes back under `data`, in the same form as an item of the list. An id that no scope has answers `404 RESOURCE_ITEM_NOT_FOUND`. The scope may have been deleted, so read the list again.

### Writes need the latest revision

Every create, edit and delete of a scope takes an `If-Match` header. Its value is the `ETag` of a read of the scopes. The list and a single scope both work, because each carries the revision of the whole collection.

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

Without the header the write answers `428`. If the scopes changed since your read it answers `412 REVISION_MISMATCH`. Every successful write returns a new `ETag` to use on the next one.

### Create a scope for a subdomain

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

`scope` is always the word `subdomain`, and `subdomain` is the label of the host: 1 to 63 lower-case letters, digits, `_` and `-`, with no dot. The root scope is never created. It is always there.

**Empty scope**

```bash
curl -X POST "$DWAF/dwaf-scopes/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "scope": "subdomain", "subdomain": "api" }'
```

**With its configuration**

```bash
curl -X POST "$DWAF/dwaf-scopes/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{
    "scope": "subdomain",
    "subdomain": "shop",
    "enabled": true,
    "profile": "balanced",
    "disabled_rules": ["xss-001"],
    "exceptions": [
      {
        "id": "public-search",
        "name": "Public search",
        "enabled": true,
        "rule_ids": ["sqli-001"],
        "conditions": { "methods": ["GET"], "uri_prefixes": ["/search"] }
      }
    ]
  }'
```

The answer is `201` with the new scope under `data`, its `id` under `operation`, and a new `ETag` header. A subdomain that already has a scope answers `409 RESOURCE_CONFLICT`.

### Change a scope

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

The body holds exactly one of four fields. Two fields in one body answer `400 INVALID_REQUEST`, and an empty body answers `400 NO_CHANGES`. To change two things, send two requests, each with the `ETag` of the answer before it.

| Field | Value |
| --- | --- |
| `enabled` | `true` or `false`. For the domain itself, use the [settings switch](#switch-dwaf-on-or-off) instead. |
| `profile` | The `id` of a profile from the [catalog](#catalogs), or `null` for no profile |
| `disabled_rules` | The whole list of rules that are off: at most 1024 entries, none twice |
| `exceptions` | The whole list of exceptions: at most 512, no id twice |

**Set the profile**

```bash
curl -X PATCH "$DWAF/dwaf-scopes/Sb3hG6dF9aK2jL5_P8oI1uY4/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "profile": "balanced" }'
```

**Turn rules off**

```bash
curl -X PATCH "$DWAF/dwaf-scopes/Sb3hG6dF9aK2jL5_P8oI1uY4/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "disabled_rules": ["xss-001", "sqli-*"] }'
```

**Switch off**

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

An entry of `disabled_rules` is the `id` of a rule, `*` for every rule, or a prefix that ends in `*`, such as `sqli-*`.

> **A list replaces the whole list**
> `disabled_rules` and `exceptions` are never merged with what the scope has. Send every entry you want to keep. An empty list removes them all.

### Add or change exceptions

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

An exception has an `id`, the `rule_ids` it covers (1 to 256) and its `conditions`, and optionally a `name` and `enabled`. It applies to a request that matches every condition it has.

| Condition | Value |
| --- | --- |
| `ips` | IPv4 and IPv6 addresses, each whole or a network in CIDR form |
| `methods` | A list, such as `["GET"]` |
| `uri_prefixes` | A list, such as `["/search"]` |
| `uri_contains` | A list of values |
| `arg_names` | A list of values |
| `user_agents` | A list of values |
| `headers` | An object: the name of a header and what it holds |

Each condition except `headers` is a list of 1 to 256 values, with none twice. No other condition is accepted.

```bash
curl -X PATCH "$DWAF/dwaf-scopes/Sb3hG6dF9aK2jL5_P8oI1uY4/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{
    "exceptions": [
      {
        "id": "public-search",
        "name": "Public search",
        "enabled": true,
        "rule_ids": ["sqli-001"],
        "conditions": { "methods": ["GET"], "uri_prefixes": ["/search"] }
      },
      {
        "id": "office-network",
        "name": "Office network",
        "enabled": true,
        "rule_ids": ["sqli-001"],
        "conditions": { "ips": ["192.0.2.0/24", "2001:db8::1"] }
      }
    ]
  }'
```

The ids in your list decide what happens:

- An id the scope does not have yet is a new exception.
- An id of the scope that your list leaves out is removed.
- An id the scope already has is edited.

The whole body holds at most 32768 bytes of JSON.

> **Exceptions with conditions_withheld**
> An exception can come back with `conditions_withheld: true` and no `conditions`. It has a condition this API does not show.
>
> - To keep it, send it back with the same `id`, `conditions_withheld: true` and no `conditions`. Its conditions stay as they are.
> - You may rename it, set `enabled` to `false`, or give it fewer of the rule ids it already has. Send back its `name` to keep the name: a `name` left out is removed.
> - Turning it back on or adding a rule id answers `409 LEGACY_EXCEPTION_CONDITIONS`.
> - To do more, send it with `conditions` in place of the mark. That replaces its hidden conditions with the ones you send.

### Delete a scope

[`DELETE /v2/cdn/dwaf/{domain_public_id}/dwaf-scopes/{scope_id}/` in the API reference](https://docs.diamwall.com/api/#tag/dwaf/DELETE/v2/cdn/dwaf/{domain_public_id}/dwaf-scopes/{scope_id}/)

Deleting the scope of a subdomain sends that host back to the configuration of the domain. The request has no body.

```bash
curl -X DELETE "$DWAF/dwaf-scopes/Sb3hG6dF9aK2jL5_P8oI1uY4/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "If-Match: $ETAG"
```

The answer has `operation` with the `id` that was deleted, and no `data`. The root scope cannot be deleted: the request answers `405 METHOD_NOT_ALLOWED`.

## Catalogs

The catalogs are read-only lists of the ids you can use in a scope.

| Catalog | Call | Its ids go in |
| --- | --- | --- |
| Profiles | `GET .../references/dwaf-profiles/` | `profile` |
| Rules | `GET .../references/dwaf-rules/` | `disabled_rules` and the `rule_ids` of an exception |

> **You may not need these calls**
> `GET .../settings/dwaf/` already answers both catalogs whole, for 8 credits. The two calls here answer the same catalogs page by page, at 25 credits each.

### List the profiles

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

```bash
curl "$DWAF/references/dwaf-profiles/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "data": {
    "state": "resolved",
    "items": [
      { "id": "balanced", "name": "Balanced", "description": "Illustrative balanced profile" },
      { "id": "strict", "name": "Strict", "description": "" }
    ],
    "page": { "limit": 50, "next_cursor": null, "total": 2 }
  }
}
```

The profiles above are examples. Read the catalog of your domain for the real ids.

### List the rules

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

```bash
curl "$DWAF/references/dwaf-rules/?limit=100" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

Each rule has `id`, `name`, `description`, `category`, `tags` and `targets`. These describe the rule. They do not say how it detects a request.

Both catalogs behave the same way:

- `limit` goes from 1 to 100, and 50 without it. Page with `cursor` as for the scopes. A catalog that changes between two pages answers `409 CURSOR_STALE`.
- `data.state` is always answered. Only under `resolved` are there `items` and `page`. Under any other state the catalog is unavailable: the answer is still `200`, with no `items` and no `page`. Show it as unavailable, never as an empty catalog.
- There is no `ETag` and nothing to send in `If-Match`.

## Limits and permissions

**Credits.** Each domain has a budget of 3000 credits a minute, shared by everyone who calls the API for that domain. A call that runs out answers `429 TOO_MANY_REQUESTS`, with the wait in `details.retry_after_seconds`.

| Call | Credits |
| --- | --- |
| Read or switch the settings | 8 |
| List, read, create, edit or delete a scope | 8 |
| Edit the `exceptions` of a scope | 16 |
| Create or edit a scope in a way that adds exceptions | 46 |
| List the profiles or the rules | 25 |

**Exception limit.** DWAF exceptions count toward the `waf_rules` limit of the plan: 25 on Pro, 100 on Business, 200 on Enterprise. The limit is shared with the firewall rules, the access rules and the exceptions of WAF rules. A write that adds exceptions beyond it answers `400 PLAN_LIMIT_EXCEEDED`, with `details.limit`, `details.current`, `details.plan` and `details.feature`. A write that only changes or removes exceptions is never counted.

**Permissions.** DWAF uses the `waf` permissions on the domain.

| You want to | Permission |
| --- | --- |
| Read the settings, the scopes or the catalogs | `waf.view` |
| Switch DWAF, or edit a scope | `waf.edit` |
| Create a scope, or add an exception to one | `waf.create` |
| Delete a scope, remove an exception, or set `profile` to `null` | `waf.delete` |

`waf.create` and `waf.delete` each include `waf.edit`. `waf.edit` includes neither.

## Errors you are likely to see

| Status | Code | What happened |
| --- | --- | --- |
| `400` | `INVALID_REQUEST` | A field is wrong, or the profile or rule does not exist. The answer names the field in `fields` when it can. |
| `400` | `NO_CHANGES` | A scope edit was sent with an empty body. |
| `400` | `PLAN_LIMIT_EXCEEDED` | The new exceptions go over the `waf_rules` limit of the plan. |
| `403` | `PERMISSION_DENIED` | You lack the permission this body needs, such as `waf.create` to add an exception. |
| `404` | `RESOURCE_ITEM_NOT_FOUND` | No scope has that `id`. Read the list again. |
| `404` | `REFERENCE_NOT_FOUND` | The catalog does not exist for this domain. |
| `405` | `METHOD_NOT_ALLOWED` | You tried to delete the root scope. |
| `409` | `CURSOR_STALE` | The list changed while you were paging. Start from the first page. |
| `409` | `RESOURCE_CONFLICT` | The subdomain already has a scope. |
| `409` | `PROFILE_OVERRIDE_CONFLICT` | The profile change conflicts with an override of the scope. Nothing was changed. |
| `409` | `LEGACY_EXCEPTION_CONDITIONS` | The change is not allowed on an exception with `conditions_withheld`. |
| `409` | `INVALID_STATE` | The change is refused in the current state of the domain. Read again to see what is published. |
| `412` | `REVISION_MISMATCH` | The settings or the scopes changed since your read. Read again and retry. |
| `428` | | The `If-Match` header is missing. |
| `429` | `TOO_MANY_REQUESTS` | The domain is out of credits for now. |
| `429` | `RATE_LIMIT_EXCEEDED` | The domain is refused for a while. Wait `details.retry_after_seconds`. |
| `503` | `CLIENT_CONFIG_NOT_WIRED`, `CLIENT_CONFIG_PATH_FOREIGN`, `CLIENT_CONFIG_WIRING_AMBIGUOUS` | DWAF cannot be written for this domain. Nothing was changed and no retry helps. Quote `request_id` to support. |
| `503` | `PUBLICATION_FAILED` | The switch could not be published and verified. Read the settings again before trying anything else. |

> **Do not blindly retry FILE_SERVER_V2_ERROR**
> A write that answers `FILE_SERVER_V2_ERROR` with a `request_id` has an unconfirmed outcome: it may have been refused, or made. Read the settings or the scopes again, compare with what you meant, and only then decide whether to send the change, with the new `ETag`.

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 DWAF 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/dwaf) for every endpoint, with parameters and responses.
