Event models silently drop fields that `event.json()` emits (`archive_url`, `reemit_source`, `envelopes`, `parent_uuid`)

Open
#3,630 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, sql
Domain
backend, databases

Research direction

Start with bbot.core.event.base.BaseEvent.json(), the models in bbot.models.pydantic and bbot.models.sql, and test_db_models.py. Read how event fields are defined and how the existing round-trip test builds its fixtures. Done means both models preserve every field emitted by json() and the test covers those fields.

Written by the indexing model from the issue text.

Description

bug

event.json() emits fields that BBOT's two event models don't declare. Both models use extra="ignore", so the fields disappear without an error:

Model Drops Used by
bbot.models.pydantic.Event archive_url, reemit_source, envelopes webhook, mongo
bbot.models.sql.Event archive_url, reemit_source, envelopes, parent_uuid postgres, mysql, sqlite (via templates/sql.py)

What gets lost:

  • archive_url: the Wayback snapshot an event's content came from. The from-wayback tag survives, but not which snapshot.
  • reemit_source: part of an HTTP_RESPONSE's id. When an event is rebuilt from these models, a response re-emitted by js_unpacker gets the id of the response it was unpacked from (output below).
  • envelopes: how a WEB_PARAMETER's value is wrapped (base64, JSON and so on) and which sub-parameter it is. The sub-parameter is part of its id.
  • parent_uuid (SQL only): which emission of the parent this event came from. Only the parent's id is kept.

The cause is that the event's JSON shape is written out by hand three times: in BaseEvent.json() and in each model. json() gained these fields and the models didn't. test_db_models.py round-trips json() through the pydantic model and would catch this, but none of its fixture events (ipv4, http_response, finding, storage_bucket) set these fields.

Reproduction
from bbot.scanner import Scanner
from bbot.core.event.base import event_from_json
from bbot.models.pydantic import Event as PydanticEvent
from bbot.models.sql import Event as SQLEvent

scan = Scanner("evilcorp.com", config={"dns": {"minimal": True}})
dns = scan.make_event("www.evilcorp.com", "DNS_NAME", parent=scan.root_event)
response = {"url": "https://www.evilcorp.com/app.js", "method": "GET", "status_code": 200,
            "raw_header": "HTTP/1.1 200 OK\r\n\r\n", "archive_url": "https://web.archive.org/web/2020/https://www.evilcorp.com/app.js"}
wayback = scan.make_event(response, "HTTP_RESPONSE", parent=dns, tags=["from-wayback"])
unpacked = scan.make_event({k: v for k, v in response.items() if k != "archive_url"}, "HTTP_RESPONSE", parent=wayback, tags=["from-wayback"])
unpacked.reemit_source = "js_unpacker"
finding = scan.make_event({"name": "Secret", "description": "Secret found", "severity": "HIGH", "confidence": "HIGH",
                           "url": "https://www.evilcorp.com/app.js"}, "FINDING", parent=unpacked, tags=["from-wayback"])
param = scan.make_event({"host": "www.evilcorp.com", "url": "https://www.evilcorp.com/?q=1", "name": "q", "type": "GETPARAM",
                         "original_value": "1", "description": "d"}, "WEB_PARAMETER", parent=dns)

for e in (unpacked, finding, param):
    j = e.json()
    for model in (PydanticEvent, SQLEvent):
        dumped = model(**j).model_dump(exclude_none=True)
        print(f"{e.type:14} {model.__module__}.Event drops: {sorted(set(j) - set(dumped))}")

j = PydanticEvent(**unpacked.json()).model_dump(exclude_none=True)
print(f"\nreemitted HTTP_RESPONSE id:       {unpacked.id}")
print(f"rebuilt from the pydantic model:  {event_from_json(j).id}")
print(f"original (not reemitted) id:      {wayback.id}")
HTTP_RESPONSE  bbot.models.pydantic.Event drops: ['archive_url', 'reemit_source']
HTTP_RESPONSE  bbot.models.sql.Event drops: ['archive_url', 'parent_uuid', 'reemit_source']
FINDING        bbot.models.pydantic.Event drops: ['archive_url']
FINDING        bbot.models.sql.Event drops: ['archive_url', 'parent_uuid']
WEB_PARAMETER  bbot.models.pydantic.Event drops: ['envelopes']
WEB_PARAMETER  bbot.models.sql.Event drops: ['envelopes', 'parent_uuid']

reemitted HTTP_RESPONSE id:       HTTP_RESPONSE:58df7ea59c3871f50b3975fbb792eb69896794d0
rebuilt from the pydantic model:  HTTP_RESPONSE:21a98504b05b747248e78b334e6bc192d0c2ffa7
original (not reemitted) id:      HTTP_RESPONSE:21a98504b05b747248e78b334e6bc192d0c2ffa7

Proposed Behavior

  • The event's JSON fields are defined once, and json() and both models are built from that definition, so a field can't be added in one place and missed in the others.
  • The round-trip test covers every field json() can emit, for both models, so a missing field fails CI.
Dominant language
Python
Stars
10.9k
Forks
959
Avg merge
2d 7h
Merged PRs (30d)
84

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from blacklanternsecurity/bbot

All issues in blacklanternsecurity/bbot

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.