# Vulnerability Editor

Vulnerability-Lookup creates and modifies vulnerability records with [Vulniverse](https://github.com/vulnerability-lookup/vulniverse), a modular and extensible vulnerability record editor. Its functionality can be customized through optional panels, actions, and features.

The editor is an optional module, off by default. Turn it on in `config/website.py`:

```python
WEB_MODULES = {
    # ...
    "vulnerability_editor": True,
}
```

The editor pages are:

- `/vuln/create`, for a new record;
- `/vuln/<id>/edit`, for an existing record.

The former `/user/submit` and `/user/edit/<id>` redirect there (301). The create page is
under `Advisories → New vulnerability advisory` in the main menu.

When it is off:

- The editor pages, and the former URLs, answer 404.
- The "New vulnerability advisory" menu entry and the Edit button of records are not shown.
- The API of the editor (`/api/vulniverse_templates`, `/api/vulniverse_record_quality`, `/api/patch2vuln`) is not registered.
- The CNA API (`/api/cna`) still works, so API clients can create and update records.

## Configure Vulniverse

[Vulniverse](https://github.com/vulnerability-lookup/vulniverse) can be embedded into Vulnerability-Lookup and uses the host application's existing services for loading and saving vulnerability records.

```{figure} /_static/img/vulniverse.png
:alt: Vulniverse Editor.
:target: /_static/img/vulniverse.png

Vulniverse Editor.
```

Vulniverse-specific settings are defined in `config/vulniverse.json`, located in the Vulnerability-Lookup `config/` directory. This configuration controls which optional panels, actions, and features are available in the embedded editor.

The `actions` key lists the buttons of the editor's header, all on by default:

- `view-vulnerability`: open the record's page.
- `download`: download the record as JSON, HTML or PDF.
- `tutorial`: a short tour of the editor.

Set an action to `false` to hide its button.

Depending on the enabled functionality and the services provided by Vulnerability-Lookup, Vulniverse can offer capabilities such as record quality assessments, CPE recommendations, and AI-assisted vulnerability record generation.

## Drafts and publication state

The editor follows [GCVE-BCP-05-X-05](https://gcve.eu/bcp/extension/gcve-bcp-05-x-05/)
(a draft for public review, v0.2: it may still change). Toggles above the editor say what
**Save** does. They are independent:

- **Draft / Interim / Final**: the maturity of the record. None selected states none.
- **Private / Public**: who sees it.
- **Announce the identifier**: for a private record only, see below.

What is saved:

- **Public.** The record is published like any other. With a maturity, it carries the
  state `published` / `public` and that maturity in its `x_gcve` entry: a public draft is
  `published` / `public` / `draft`.
- **Private.** The record is stored apart from the public database: no page, API, index,
  search, dump or feed shows it. Its identifier stays reserved. Those who may modify the
  record can open it; `/vuln/drafts` lists the private records. Making it public publishes
  it and removes the private copy.

A record being edited opens with its current state. Saving a public record as private
keeps the changes as a private copy of its next version: the public record is unchanged
until it is saved as public.

A new record is a **public draft**, as the extension recommends. `drafts` in
`config/vulniverse.json` changes what the editor proposes for a new record:

- `visibility`: `public` (default) or `private`.
- `maturity`: `draft` (default), `interim`, `final`, or `""` for none.

### Announcing the identifier of a private record

By default the identifier of a private record is hidden: every public request for it is
a 404. With *Announce the identifier*, `GET /api/cna/cve/<id>/reservation` tells anyone
that the identifier is reserved, and nothing else:

- the identifier and its GNA;
- when it was reserved;
- the state `not-published` / `private`, dated when the identifier was announced.

The page of the identifier, `/vuln/<id>`, says the same: reserved, not public yet. It
shows the record once it is published. For a visitor who is not logged in the page is
cached for a minute, as every vulnerability page.

A record family can refuse it (`announces_identifiers = False`), when the identifier it
stores is not the one it shows to the public. The toggle is then not offered,
`?public_reservation=true` is a 400, and the identifier always answers 404.

Later private saves do not change that answer. It goes away when the record is published,
discarded or rejected.

### Who sees a private record

- **Its author**, always.
- **Those who hold `vulnerability:view_private`**, globally or on the record: by default
  the administrators, and the owner of a record on its own record.

`vulnerability:modify`, which every reporter holds for the public records, is not enough
on a private one: changing, publishing or rejecting a private record also needs to see
it. So a role with `vulnerability:view_private` alone gives read-only access to the
private records, and a reporter does not open the private records of the others.

To someone who may not see it, a private record that exists and one that does not get the
same answer.

### History

Every action on a record through this layer is an entry of the audit trail: private
record created or saved, published, private copy discarded, rejected, deleted. An entry
says what was done, by whom and when, with the maturity or the reason, never the content.

- *History*, under the editor, lists the entries of the record to those who may see it.
- `/publications/audit` lists them with the CNA publications, to those who also hold
  `vulnerability:view_private`.
- Creation, publication and rejection are also logged: see [Logging](logging.md).

### Rejecting a record

A record, private or public, can be rejected with a reason, from *Reject this record*
under the editor. It is for the author of the record or an administrator
(`vulnerability:delete` on the record), and it is final:

- nothing can be saved or published under its identifier afterwards, and the identifier
  is marked as rejected;
- a record that never was public leaves nothing public, and its identifier is no longer
  announced;
- a record that was public is replaced by a `REJECTED` record with the reason, and its
  state, if it had one, becomes `withdrawn` / `public`;
- the content as it was, the reason, who rejected it and when are kept, and shown in
  `/vuln/drafts` to those who may open the record.

### Timeline

When a private record is published, or the maturity of a public one changes, the step is
added to the timeline of the record
([GCVE-BCP-05-X-03](https://gcve.eu/bcp/extension/gcve-bcp-05-x-03/)), shown on its page:

| Step | Event type | Event id |
|---|---|---|
| Private copy started | `other`, "Draft created." | `evt-draft-created` |
| First publication | `advisory-published` | `evt-advisory-published` |
| Maturity of a public record changed | `advisory-updated`, for instance "Draft made final." | `evt-<maturity>-<time>` |

- The state of the record names the events that explain it in `relatedEvents`.
- Events already in the timeline are kept, and an `advisory-published` event is not added
  beside one the publisher wrote.
- A record published without a maturity, and that never was private, gets no event and no state.
- A private record that is discarded leaves no event: there is no record to carry it.

### API

- `PUT /api/cna/cve/<id>/draft` saves a private record, `GET` reads it, `DELETE` discards
  it. `?maturity=draft|interim|final` states its maturity, `?public_reservation=true|false`
  announces or hides its identifier.
- `POST` or `PUT /api/cna/cve/<id>?maturity=draft|interim|final` publishes the record with
  that maturity. Without `maturity`, no state is written. A record whose state has an
  active embargo is refused.
- `POST /api/cna/cve/<id>/draft/publish` publishes the private record as it is, and
  removes the private copy. `?maturity=` replaces the maturity it states.
- `POST /api/cna/cve/<id>/rejection` rejects the record, with `{"reason": "..."}`. An API
  key only carries global permissions, so it rejects with `vulnerability:delete` held
  globally.
- `GET /api/cna/cve_draft` lists the private records the caller may view, with what
  recognises each one: identifier, title, description, affected vendors and products,
  maturity, author. `?state=private|rejected|all`, `?family=`, `?page=`, `?per_page=`
  (100 at most). Private records are in no search index: the listing reads them with the
  authorization of each one.
- `GET /api/cna/cve/<id>/reservation` is public, see above.

### Other kinds of records

The private copies, their state and their timeline are the same for every record. What
depends on the kind of record is asked to its *family*
(`website/web/services/record_families.py`):

- which identifiers are its own;
- whether an identifier is reserved, and by which GNA;
- where the public record is, and whether this instance may edit it;
- what a record needs before it can be published;
- how it is published;
- whether it can be rejected, and what a rejection leaves in public: the extension allows
  a withdrawal notice, and allows none.
- which permission each action on a private record needs (view, create, modify, publish,
  reject), and the resource type of its roles: a family with its own staff names its own.

The records of the local source, with the identifiers of the GNA allocator, are the
default family. Another one is added with `record_draft_service.register(family)`.

The editor is the same for every family. It reads and saves through `GET`, `POST` and
`PUT /api/cna/cve/<id>`, which answer for the family of the identifier: its public record,
its publication rules, its store. A record of another family is in the BCP-05 format like
the others, under its own identifier.

## Configure Patch2Vuln

[Patch2Vuln](https://github.com/gcve-eu/gcve-lab-patch2vuln) is an optional service that uses large language models (LLMs) to generate vulnerability record drafts from software patches.

How the pieces fit together, from a patch to a published record: [AI-Assisted Vulnerability Records](ai-assisted-records.md).

Vulnerability-Lookup does not run Patch2Vuln: it forwards the jobs to an external Patch2Vuln HTTP service and relays the results. To show the Patch2Vuln panel in Vulniverse:

1. Run a Patch2Vuln service that Vulnerability-Lookup can reach.
2. Configure it in `config/patch2vuln.json`:

   ```json
   {
     "patch2vuln_enabled": true,
     "patch2vuln_url": "http://<host>:<port>",
     "patch2vuln_timeout": 300,
     "api_key": ""
   }
   ```

   - `patch2vuln_enabled`: registers `/api/patch2vuln`. The panel is only shown when it is `true`.
   - `patch2vuln_url`: the address of the service.
   - `patch2vuln_timeout`: in seconds, how long to wait for the result of a job.
   - `api_key`: the token the service expects, if any. The `PATCH2VULN_API_TOKEN` environment variable takes precedence, to keep the secret out of the file.
3. Enable the panel in `config/vulniverse.json`: `"patch2vuln": true` under `panels`.
4. Restart the website: the panel and the endpoint are set up at start-up.

The panel needs patch2vuln 1.3.0 or later, the first version with the web service. It accepts the "Additional instructions" field (`prompt`).

The panel needs both `patch2vuln_enabled` and `panels.patch2vuln`. With `patch2vuln_enabled` on and the service unreachable, jobs fail with "Could not reach the Patch2Vuln service".

Once configured, users can submit patches for analysis directly in Vulniverse. The generated vulnerability information can be reviewed before being selectively applied to the current record.

## Update Vulniverse

Vulniverse is installed from npm as `@nmd03/vulniverse`, pinned to an exact version in `package.json`. To move to a new version:

```bash
npm install --save-exact @nmd03/vulniverse@1.0.x   # the new version
python3 tools/copy_frontend_assets.py               # npm install <package> skips postinstall
poetry run pytest -q tests/test_static_assets.py && npm test
```

Then reload the editor page (`/vuln/create`), check that it loads, saves and reserves a GCVE ID, and commit `package.json` and `package-lock.json`. Installations pick the new version up with the usual update (`npm ci --omit=dev`, or `poetry run update`).
