# Variables and DSL

Placeholders make a signature independent of the target and let one
request depend on another. Anything between double braces in a path,
header value, body or raw request is filled in just before the request is
sent. DSL matchers add simple conditions on the response.

## Order of substitution

Each text is processed in three passes:

1. Variables: names from `variables`, payload values and values from
   internal extractors.
2. Target context: `BaseURL`, `Hostname` and the other names below.
3. Functions: calls such as `base64(...)`.

Because variables are filled in first, a function can take a variable's
value as its argument, and a variable can hold a function call that runs
each time the variable is used. A placeholder with no value is left in
the text as written.

## Variables

```yaml
variables:
  username: admin
  password: admin
  marker: "{{rand_text_alpha(12)}}"

requests:
  - method: POST
    path:
      - "/login"
    body: '{"user": "{{username}}", "pass": "{{password}}", "note": "{{marker}}"}'
```

`variables` must be a mapping. Values are used as text. Payload lists
([Payload attacks](./attacks.md)) and internal extractors
([Extractors](./extractors.md#internal-extractors)) set variables the same
way, so all three share one namespace; a later value replaces an earlier
one with the same name.

## Target context

These names describe the scan target:

```text
BaseURL    the target URL as configured on the scan     https://shop.example/app
RootURL    scheme and host, without the path            https://shop.example
Scheme     the URL scheme                               https
Hostname   the host name                                shop.example
Host       host with the port when one is given         shop.example:8443
Port       the port, 443 or 80 by default               8443
Path       the target's path, / when it has none        /app
```

```yaml
requests:
  - raw:
      - |
        GET /api/users HTTP/1.1
        Host: {{Hostname}}
        Referer: {{BaseURL}}
```

## Functions

A function call is written as the name and its arguments inside double
braces. Arguments are separated by commas; quotes around an argument are
optional and removed. Arguments are text: to pass a variable, write the
variable's placeholder as the argument.

```yaml
headers:
  X-Request-ID: "{{rand_text_alphanumeric(16)}}"
  Authorization: "Basic {{base64({{username}}:{{password}})}}"
```

| Function | Result |
|---|---|
| `base64(text)`, `base64_decode(text)` | Base64 encoding and decoding |
| `url_encode(text)`, `url_decode(text)` | Percent-encoding and decoding |
| `hex_encode(text)`, `hex_decode(text)` | Hexadecimal encoding and decoding |
| `md5(text)`, `sha1(text)`, `sha256(text)` | Hex digest |
| `hmac(text, key, algorithm)` | Hex HMAC; the algorithm defaults to `sha256` |
| `len(text)` | Length in characters |
| `to_lower(text)`, `to_upper(text)`, `trim(text)`, `reverse(text)` | Case, whitespace and order |
| `replace(text, old, new)` | Replace every occurrence |
| `substr(text, start, end)` | Slice from `start`, up to `end` when given |
| `repeat(text, count)` | The text repeated |
| `rand_int(min, max)` | A random integer, both bounds included |
| `rand_text_alpha(n)`, `rand_text_numeric(n)`, `rand_text_alphanumeric(n)` | Random text of length `n` |

A call to an unknown function, or one that fails (for example
`hex_decode` on text that is not hexadecimal), is left in the text unchanged. Calls cannot be nested inside
other calls, and an argument cannot contain a comma unless it is quoted.

## Conditions

DSL matchers evaluate one comparison per expression, after the same three
substitution passes. The left side is one of these names, a variable, or
a value produced by a placeholder:

| Name | Value |
|---|---|
| `status_code` | Response status, `0` when the request failed |
| `content_length` | Length of the response body in characters |
| `body` | The response body; use it with `contains` |

| Operator | Meaning |
|---|---|
| `contains 'text'` | The body (or the status code as text) contains the quoted text |
| `==`, `!=` | Text equality |
| `>`, `>=`, `<`, `<=` | Numeric comparison |

```yaml
matchers:
  - type: dsl
    dsl:
      - "status_code == 200"
      - "content_length > 0"
      - "body contains 'admin'"
    condition: and
```

Placeholders work on either side, for example to compare a computed
length:

```yaml
- type: dsl
  dsl:
    - "{{len({{token}})}} > 20"
```

What conditions do not support today:

- `and`, `or`, `!` or parentheses inside one expression. Write several
  expressions and set the matcher's `condition`.
- Function calls without braces: `len(body) > 0` is never true. Use
  `content_length > 0`.
- Response timing: `duration` is not available, so time-based checks
  cannot be expressed as a condition yet.
- Header values: use a word or regex matcher on the `header` part.
