# Template repositories

A template repository is a bundle of YAML signatures served over HTTPS.
You add it once, review what it would install, and approve it. From
then on its version is pinned by the bundle's SHA-256: a different
bundle is never installed until you have reviewed and approved that
exact digest.

Templates are YAML that Phantom compiles with its own compiler. Nothing
in a bundle runs as code. A template that does not compile, or that uses
fields Phantom does not support, is listed as invalid and never
installed.

## The bundle

One JSON document, at most 5 MB, with at most 1,000 templates of up to
200 KB each:

```json
{
  "name": "Team templates",
  "version": "1.4.0",
  "templates": [
    { "path": "exposure/git-config.yaml", "content": "id: team-git-config\nname: Exposed Git config\n..." }
  ]
}
```

`path` identifies a template across versions. It must be relative and
must not contain `..`. Each `content` is a signature as described in
[the format reference](./index.md).

## Adding and reviewing

In **Signatures**, the download button opens **Template repositories**.
Give a name and the bundle's HTTPS URL, and optionally the SHA-256 you
expect. Phantom fetches the bundle (HTTPS only, no redirects) and shows
what approving it would do:

| Mark | Meaning |
|---|---|
| `+` | A new template, installed as an active signature |
| `~` | A template that changed; the signature is updated and its previous template stays in its history |
| `−` | A template no longer in the bundle; its signature is turned off, not deleted |
| `!` | A template that does not compile, or whose id another signature already uses; never installed |

If you gave a SHA-256 and the bundle does not match it, the repository
says **Digest mismatch** and the bundle cannot be approved.

**Approve and install** installs the bundle you reviewed and pins it.
If the bundle changed in the meantime, approval is refused and you
review the new one. **Discard** drops a pending bundle and keeps what is
installed.

## Updates

Right click a repository (or press Shift+F10) to **Check for update**.
When the bundle's digest is the pinned one, nothing happens. Otherwise
the new bundle waits for review exactly like the first one.

Signatures a repository manages can be turned on and off but not edited:
duplicate one to change it. **Remove repository** forgets the
repository and turns its signatures off. They stay in the catalogue as
ordinary custom signatures, and findings that reference them are kept.

## REST

User only: agents and requests with agent headers are refused.

| Route | Body | Response |
|---|---|---|
| `GET /template-repositories` | | `[Repository]` with pinned version and digest, status, installed count and any pending review |
| `POST /template-repositories` | `{name, url, sha256?}` | `201 Repository` with the bundle pending review |
| `POST /template-repositories/{id}/check` | | `Repository` |
| `GET /template-repositories/{id}/review` | | `{version, sha256, fetched_at, review}` |
| `POST /template-repositories/{id}/approve` | `{sha256}` | `Repository`, or `409` when the pending digest differs |
| `POST /template-repositories/{id}/discard` | | `Repository` |
| `DELETE /template-repositories/{id}` | | `204` |
