# Payload collections

Payload collections are reusable, ordered lists of test values (XSS
strings, SQLi probes, usernames, header names). They are a research object
([Concepts](../concepts.md#research-objects-and-references)), not a part of
Intruder: Intruder, Repeater, signatures and any message viewer use the same
collections through one backend service (`app/services/payloads/`), and the
interface, REST and MCP reach that service the same way.

Design background: `design/intruder-payloads.md`.

## Model

**Collection** (`payload_collections`)

| Field | Notes |
|---|---|
| `id`, `uid` | Integer id, plus a 32-hex `uid` that stays the same in exports and is recorded by duplicates. |
| `vault_id` | Owner. `null` means global. Deleting a vault deletes its collections. |
| `name` | Unique per owner, compared case-insensitively. A vault collection and a global collection may share a name. |
| `description`, `category` | The category is short free text, stored lower-case. Suggested values: `xss sqli ssti ssrf lfi rce xxe open-redirect auth idor headers fuzzing wordlist usernames passwords` (`GET .../categories`). |
| `tags` | Lower-case strings (`a-z 0-9 . _ : / + -`). For now this is a plain JSON array. It moves to the shared Tag model of `design/research-annotations.md` when that model is built: each tag becomes a vault tag, or a global one for global collections, and the filter `tag=` keeps working. |
| `origin`, `source_ref` | `manual`, or `duplicate` with the original's `uid`. |
| `actor` | `user` (REST and interface) or `agent` (MCP). Set by the backend, never by the client. |
| `item_count`, `total_bytes` | Updated in the same transaction as every item change, so listing collections never counts rows. |
| `archived_at` | An archived collection is kept, read-only and hidden from default listings. |
| `created_at`, `updated_at` | Every item change also updates the collection's `updated_at`. |

**Item** (`payload_items`)

| Field | Notes |
|---|---|
| `value` | Stored exactly as given: whitespace, line breaks and duplicates are kept. |
| `position` | Sparse ordering key. Items are ordered by `(position, id)`. |
| `size` | UTF-8 byte length. |
| `source` | `{kind, ref, vault_id, flow_id, part}`. `kind` is `manual`, `clipboard`, `import` (`ref` = file name) or `flow` (the vault, flow and message part the selection came from). The flow reference is a plain id, so the provenance survives clearing the history. |
| `actor` | `user` or `agent`. |

Storage: each item is a row indexed on `(collection_id, position, id)`. An
append reads only the last key and adds a gap of 1024. An insertion
(`before_id`) uses the space between two keys. When that space is used up,
the anchor and every item after it move down in one `UPDATE`, so earlier
keys never change. Pages are keyset range scans. Bulk changes and
duplicates run as single statements (`INSERT ... SELECT`). Exports and
normalisation read the collection page by page. No request loads a whole
collection into memory.

Migration: `d8a4f6c2e1b3_payload_collections` (after `b5c8e2f41d07`).

## Limits

Constants in `app/core/payloads/limits.py`, enforced by the backend for
every interface:

| Limit | Value |
|---|---|
| Collections per owner (a vault, or the global library) | 1,000 |
| Items per collection | 500,000 |
| Characters per item | 65,536 |
| Bytes per import (decoded content) | 10 MiB |
| Items per append or batch edit, and ids per selection | 10,000 |
| Index ranges per selection | 100 |
| Items per page | 1,000 (default 200) |
| Items in an import preview | 200 (default 50) |
| Custom delimiter | 64 characters |

When a request goes over a limit, nothing is stored and the call fails
with `invalid_value`. The error details carry the limit and, for an item,
its index.

## Scope and isolation

There are two route trees with the same routes and the same handlers:

| Tree | Reaches | New collections belong to |
|---|---|---|
| `/api/v1/payloads/...` | global collections only | the global library |
| `/api/v1/vaults/{vault_id}/payloads/...` | the vault's collections and the global ones | the vault |

- A vault collection outside the route's scope answers `404 not_found`,
  exactly like an id that does not exist. Every operation, including
  preview with a `collection_id`, behaves this way.
- Bulk operations ignore item ids of other collections. `before_id` and
  single-item routes answer 404 for an item of another collection.
- A `flow` source must name a flow of the route's vault. On the global
  tree it is refused with `invalid_value`.
- Copying out of a vault is always an explicit user action:
  `duplicate` with `target: "global"`, or adding a selection to a global
  collection. The item keeps its source vault and flow.
- Events about a vault collection carry its `vault_id`. Events about a
  global collection carry `null` and reach every vault.

## REST

Paths are relative to a tree prefix (see above). Errors use the standard
body `{"error": {"code", "message", "details"}}`.

### Collections

| Route | Body / query | Response |
|---|---|---|
| `GET /collections` | `visibility=all\|vault\|global` (vault tree), `search`, `category`, `tag`, `archived=exclude\|include\|only` | `[Collection]`, the vault's own first, then by name |
| `POST /collections` | `{name, description?, category?, tags?}` | `201 Collection` |
| `GET /categories` | | `{suggested: [..], in_use: [..]}` |
| `GET /collections/{id}` | | `Collection` |
| `PATCH /collections/{id}` | any of `name, description, category, tags` (`tags` replaces the list) | `Collection` |
| `POST /collections/{id}/duplicate` | `{name?, target: same\|vault\|global}` | `201 Collection` (default name `"<name> (copy)"`, then `(copy 2)`, ...) |
| `POST /collections/{id}/archive` | | `Collection` |
| `POST /collections/{id}/restore` | | `Collection` |
| `DELETE /collections/{id}` | | `204` |

```json
{
  "id": 3, "uid": "69db9b658d394c99b6d1ae56966c9470", "vault_id": 1, "scope": "vault",
  "name": "XSS", "description": null, "category": "xss", "tags": ["reflected"],
  "origin": "manual", "source_ref": null, "actor": "user",
  "item_count": 6, "total_bytes": 48, "archived": false, "archived_at": null,
  "created_at": "2026-11-27T07:23:51Z", "updated_at": "2026-11-27T07:24:02Z"
}
```

### Items

| Route | Body / query | Response |
|---|---|---|
| `GET /collections/{id}/items` | `cursor`, `limit` (1-1000) | `ItemPage` |
| `POST /collections/{id}/items` | `{text}` or `{items: [..]}`, `source?`, `before_id?` | `201 ItemChange` |
| `PATCH /collections/{id}/items` | `{items: [{id, value}]}`, all or nothing | `ItemChange` |
| `PATCH /collections/{id}/items/{item_id}` | `{value}` | `Item` |
| `DELETE /collections/{id}/items/{item_id}` | | `204` |
| `POST /collections/{id}/items/delete` | `{ids?, ranges?: [{start, end}], all?}` | `ItemChange` |
| `POST /collections/{id}/items/normalize` | `{trim?, drop_empty?, dedupe?, selection?}` | `ItemChange` |

"Add to payloads" from a selection in any viewer:

```json
POST /api/v1/vaults/1/payloads/collections/3/items
{"text": " exact selected text", "source": {"kind": "flow", "id": 812, "part": "response_body"}}
```

`part` is one of `request`, `response`, `url`, `request_headers`,
`request_body`, `response_headers`, `response_body`. `source.kind` is
`manual` (default), `clipboard` or `flow`.

```json
{"affected": 1, "collection": {"id": 3, "item_count": 7, "...": "..."}}
```

```json
{
  "collection_id": 3, "item_count": 20000, "start": 1000, "next_cursor": "MjA0ODAwOjEwMDE6MjAwMA",
  "items": [{"id": 1001, "collection_id": 3, "position": 1025024, "value": "<svg onload=alert(1)>", "size": 21,
             "source": {"kind": "import", "ref": "xss.txt", "vault_id": null, "flow_id": null, "part": null},
             "actor": "user", "created_at": "2026-11-27T07:23:51Z", "updated_at": null}]
}
```

`start` is the index of the first item, which the editor uses for line
numbers. Pass `next_cursor` back as `cursor`. It is `null` on the last
page. Ranges are zero-based, with `end` exclusive, in the current order.
Normalisation keeps the first occurrence of a duplicate and the order of
everything that remains. With a selection, only selected items change.

### Import and export

| Route | Body / query | Response |
|---|---|---|
| `POST /import/preview` | `ImportRequest` + `collection_id?`, `preview_items?` | `ImportPreview`; nothing is saved |
| `POST /collections/{id}/import` | `ImportRequest` + `mode: append\|replace` | `ImportResult` |
| `GET /collections/{id}/export` | `format=txt\|json\|csv`, `separator`, `delimiter` | file download (`Content-Disposition: attachment`, `X-Item-Count`) |

```json
{
  "content": "a\n b\n\na",            "content_base64": null,
  "format": "txt",                   "separator": "newline",  "delimiter": null,
  "encoding": "auto",                "csv_column": null,      "csv_header": false,
  "trim": false, "drop_empty": false, "dedupe": false,
  "origin": "file", "filename": "xss.txt"
}
```

- Send exactly one of `content` (pasted or typed text) or `content_base64`
  (a file's bytes, so its encoding can be detected). `origin` (`file`,
  `clipboard`, `manual`) becomes each item's source kind (`import`,
  `clipboard`, `manual`).
- `encoding: auto` detects a UTF-8 or UTF-16 byte order mark, then tries
  strict UTF-8. If that fails it reads Latin-1, which keeps every byte. Any
  Python codec name is accepted, and the preview reports the encoding it
  used.
- `format: csv` imports one column (`csv_column`: an index, or a header name
  with `csv_header: true`; the first column by default). `format: json`
  takes an array of strings or scalars, objects with `value`, or a Phantom
  JSON export.
- Options are off by default, so values import exactly as read. `dedupe`
  also skips values the collection already holds.
- The preview with `collection_id` counts `existing_duplicates` and
  reports whether the result `fits` the item limit:

```json
{"format": "txt", "encoding": "utf-8", "bytes": 15, "parsed": 5, "count": 5, "empties": 1, "duplicates": 1,
 "existing_duplicates": 1, "trimmed": 0, "too_long": 0, "first_too_long": null,
 "items": ["a", " b"], "truncated": true, "fits": true, "item_limit": 500000}
```

```json
{"imported": 2, "skipped_empty": 1, "skipped_duplicates": 1, "replaced": 0, "encoding": "utf-8", "collection": {"...": "..."}}
```

Exports are streamed page by page. TXT joins the values with the chosen
separator; it is lossless only when no value contains that separator. JSON
(`{"schema": "phantom.payload-collection", "version": 1, "collection": {..},
"items": [{value, source, actor, created_at}]}`) and CSV (columns `value,
source, source_ref, actor, created_at`) always round-trip through the
importer. A REST export contains the values as stored. A collection is the
user's own test material and is written to be loaded into other tools. The
export redaction policy applies to captured traffic downloads; it does not
apply here.

## Interface

The Payloads page (rail, or `G` then `L`) is the library:

- The sidebar lists this vault's collections and the global ones with
  their counts, filters them, and creates a collection inline (in this
  vault or in the global library). Archived collections show on request.
  Right-click a collection (or Shift+F10) for the same actions as its ⋯
  menu plus Open and Rename (F2, in place): Duplicate, Export, Copy link
  (`[[payloads:id]]`), Attach to conversation, Archive or Restore, and
  Delete (asked in the row; Delete asks too).
- A collection's header renames it in place and holds category (with the
  backend's suggestions), tags and description. Its actions: Import,
  Export (one per line, comma or tab separated, JSON, CSV), Duplicate
  (next to it, into this vault or into the global library), Archive or
  Restore, and Delete with an in-place confirmation.
- Items are a virtualized list with line numbers, sizes and their source
  (captured items name their flow). Click, Ctrl/Cmd+click and
  Shift+click select; Ctrl+A selects the whole collection; Delete
  removes the selection; the floating bar also trims, drops empty values
  and removes duplicates. Double-click edits an item in place. The box
  above the list adds values by hand, one per line or as one value.
  Right-click a payload (it is selected first unless it already is) to
  edit it, copy it (or the selected values), select all, trim, drop
  empty values, remove duplicates or delete the selection; an archived
  collection only copies and selects.
- Import opens in the docked panel: paste or a file (encoding detected
  by the backend), format, separator (custom delimiters take the escapes
  below), clean-up options, append or replace, and a live preview from
  the same parser the import uses.
- Every message viewer's selection menu has **Add to payloads**: recent
  collections first, then the rest, and a field that creates a
  collection and adds to it. The exact selected text is stored with its
  flow and message part. A selection that still shows a masked secret is
  refused; reveal secrets first.
- In an editable message (the Repeater request and other editable views),
  the selection menu also has **Replace with payload**: pick a
  collection, then one of its first 50 values, and it takes the place of
  the selected text.
- Intruder payload sets read collections directly, and signatures refer
  to one with `collection:<id>` in their `payloads`
  (`reference/signatures/attacks`).



| Name | Splits on |
|---|---|
| `newline` (default for TXT) | `\n`. One `\r` before each `\n` is treated as part of the line ending, so CRLF and LF files give the same values. |
| `comma` (default for CSV) | `,` |
| `semicolon` | `;` |
| `tab` | `\t` |
| `null` | `\0` |
| `custom` | `delimiter`, with escapes `\n \r \t \0 \\ \xHH \uHHHH` (e.g. `\r\n`, `\x1e`, `\|\|`). An unknown escape is an error. |

A trailing separator does not create a final empty item. Empty items in
the middle of the content are kept unless `drop_empty` is set. For CSV the
separator is the field delimiter and must be one character.

## Events

| Event | Data |
|---|---|
| `payload.collection.updated` | `{action: created\|updated\|duplicated\|archived\|restored\|deleted, collection}` |
| `payload.items.updated` | `{action: appended\|updated\|deleted\|normalized\|imported, collection_id, affected, item_count, total_bytes, ...}` |

Events never carry item values.

## MCP

Every tool takes an optional `vault_id`. With it, the tool works on that
vault's tree. Without it, the tool works on the global library. Results use
the REST schemas.

| Tool | Hints | REST equivalent |
|---|---|---|
| `payload_collection_list`, `payload_collection_get`, `payload_categories` | read-only | `GET /collections`, `/collections/{id}`, `/categories` |
| `payload_collection_create` | | `POST /collections` |
| `payload_collection_update` | idempotent | `PATCH /collections/{id}` |
| `payload_collection_duplicate` | | `POST .../duplicate` |
| `payload_collection_archive` (`archived: bool`) | idempotent | `.../archive`, `.../restore` |
| `payload_collection_delete` | destructive | `DELETE /collections/{id}` |
| `payload_items_list` (`reveal_secrets`) | read-only | `GET .../items` |
| `payload_items_append` (`items`, `before_id`, `flow_id`, `part`) | | `POST .../items` |
| `payload_items_update` (`edits: [{id, value}]`) | idempotent | `PATCH .../items` |
| `payload_items_delete` (`ids`, `ranges`, `all`) | destructive | `POST .../items/delete` |
| `payload_items_normalize` | destructive | `POST .../items/normalize` |
| `payload_import_preview` | read-only | `POST /import/preview` |
| `payload_import` | | `POST .../import` |
| `payload_export` (`max_chars`, `reveal_secrets`) | read-only | `GET .../export`, returned as text with `clipped` |

Agent-created collections and items record `actor: agent`. This is decided
by the backend: every MCP call runs as an agent call.

**Redaction decision.** Payloads are stored exactly as given, including
secrets selected from captured traffic, because they exist to be sent
during active testing. Agents read item values through the MCP redaction
policy ([Phantom MCP server](../agents/mcp.md#redaction)), like any other captured data:

- items with source `flow` are masked whole (`•••••••• (captured, <n>
  chars)`). A selected session cookie or token is often recognisable only
  in its context, which the item no longer has;
- every other item is scanned for credential formats (JWT, cloud and API
  keys, PEM keys, `Bearer` values, ...). Only those spans are masked, so
  ordinary attack strings pass unchanged;
- `payload_items_list` and `payload_export` return `redacted`, and accept
  `reveal_secrets` only when the user allows the argument. Policy mode
  `reveal` returns values as stored;
- values containing a redaction placeholder are refused (`invalid_value`)
  by `payload_items_append`, `payload_items_update` and `payload_import`,
  so a masked value can never be stored as a payload;
- collection metadata, import previews (the agent's own input) and events
  carry no captured values and are not redacted. The REST API returns
  values as stored, and the interface masks credential-looking values on
  screen.
