Skip to content

Latest commit

 

History

History
529 lines (418 loc) · 25.9 KB

GRAPH.SCHEMA.md

File metadata and controls

529 lines (418 loc) · 25.9 KB

RedAmon Neo4j Graph Schema

Overview

This document defines the Neo4j graph database schema for storing reconnaissance data. The schema is designed to enable attack chain analysis by connecting all discovered assets, services, technologies, and vulnerabilities in a navigable graph structure.


🎯 Design Principles

  1. Hierarchical Ownership: All nodes trace back to a Domain with user_id and project_id. A Domain is a registrable domain (the org, read from the Public Suffix List): one per org however many scan roots a project has under it. A deeper scan root that is a target is a Subdomain of its org with is_scan_root: true.
  2. Attack Surface Mapping: Every potential entry point is modeled (ports, URLs, parameters)
  3. Technology-Vulnerability Linkage: Technologies connect to known CVEs for risk assessment
  4. No Redundancy: Information stored once, relationships handle connections
  5. Query Efficiency: Optimized for path traversal (attack chains)
  6. Multi-Tenant Isolation: Every ENTITY node has user_id + project_id for tenant filtering. The three GLOBAL REFERENCE labels are the deliberate exception - see below.

Where the schema itself lives

This document no longer lists node labels, their properties, or the relationships between them. It used to, and that was the problem: the same facts were written out three times - here, in the text-to-cypher prompt, and in graph_db/schema.py - so adding a node type meant editing all three, and forgetting one failed in silence. No error, just an agent that could never query the new node type, or a documented property that nothing writes.

There is now one declaration.

What you want Where it is
Every label, property and relationship, with meaning graph_db/schema_sections.md
The same, parsed into fields a program can read graph_db/schema_catalog.py
A label's uniqueness key (its MERGE identity) graph_db/schema_keys.py
The rendered document an agent is served render_schema() in graph_db/schema_render.py

Reading it

# the whole schema, as the agent sees it
python3 -c "from graph_db.schema_render import render_schema; print(render_schema())"

# one label
python3 -c "from graph_db.schema_render import render_label; print(render_label('Subdomain'))"

# a label's properties as data
python3 -c "from graph_db.schema_catalog import label_properties; print(label_properties('Subdomain'))"

Or ask the running agent, which serves the same content over both surfaces: the graph_schema tool in a chat session, or graph_schema over the inbound MCP server. Neither needs a database.

Changing it

Edit graph_db/schema_sections.md, then re-seed:

python3 tooling/scripts/seed_schema_catalog.py

graph_db/schema.py renders its CREATE CONSTRAINT statements from schema_keys.py, so a new label's uniqueness key is declared once and the database follows.

Constraints and indexes

The DDL is generated too, and init_schema applies all of it on every scan-container spawn and every agent graph call. Every statement is guarded by IF NOT EXISTS / IF EXISTS, so it is idempotent.

Count Source
CREATE CONSTRAINT 45 rendered by schema.build_constraints() from schema_keys.py
CREATE INDEX 77, each with its own name TENANT_INDEXES + ADDITIONAL_INDEXES in schema.py
DROP (legacy, unconditional) 41 DROP_LEGACY_CONSTRAINTS in schema.py
DROP (legacy-shaped, conditional) 3 LEGACY_SHAPED_CONSTRAINTS: drop_legacy_shaped_constraints() drops one only while the live constraint's key differs from schema_keys.py, so a live tenant-scoped constraint is never rebuilt

tests/test_schema_legacy_drops.py fails if an unconditional drop names a constraint or index the code still creates, or if two indexes share a name.

# exactly what the database gets, in order
docker compose exec agent python -c "
from graph_db.schema import DROP_LEGACY_CONSTRAINTS, CONSTRAINTS, TENANT_INDEXES, ADDITIONAL_INDEXES
for s in DROP_LEGACY_CONSTRAINTS + CONSTRAINTS + TENANT_INDEXES + ADDITIONAL_INDEXES: print(s)"

This file used to carry that listing verbatim. It was accurate - checked name by name at removal - but being accurate was never the point: it was a copy that had to be maintained by hand, and the next label added would have been the one nobody remembered to add here.

Renaming a constraint is a migration, not a refactor. A same-name CREATE ... IF NOT EXISTS against a database that still holds the old constraint is a silent no-op, so a changed key needs a new name AND a matching entry in DROP_LEGACY_CONSTRAINTS.

What stops it drifting again

Four tests, and they fail rather than warn:

  • every label declared in schema_keys.py has catalog documentation
  • every relationship and property the CODE writes is documented (recon/tests/test_graph_writes_documented.py, hermetic, runs in the unit gate ./redamon.sh test unit)
  • every relationship and property in a LIVE graph is documented (recon/tests/test_schema_catalog.py, skips without a database)
  • no query reads a relationship nothing writes - this one found a real bug, a modal reporting zero because it read HAS_SERVICE where every writer creates RUNS_SERVICE

What this document still covers

The parts a generator cannot produce: why the graph is shaped the way it is, the multi-tenant strategy, the Muted label's rationale, how Scan Timeline versions live in Postgres rather than in the graph, and why the AI agent's own findings reuse Vulnerability.

For query GUIDANCE - how to phrase a traversal, which node types to union, the gotchas - read the text-to-cypher prompt's rule sections in agentic/prompts/base.py, which is what the agent is actually instructed with.

🌍 Global Reference Nodes (CVE, MitreData, Capec)

CVE, MitreData and Capec are the public NVD/MITRE catalogue, not findings. They are UNIQUE on their natural id (c.id, m.id, cap.capec_id), so there is exactly ONE node per CVE for the whole database, shared by every project that finds it. They carry no user_id / project_id.

// entity node - tenant-scoped
MERGE (v:Vulnerability {id: $id, user_id: $uid, project_id: $pid})

// reference node - natural id ONLY, never a tenant key
MERGE (c:CVE {id: $cve_id})
  ON CREATE SET c.source = 'nuclei'        // provenance: first writer wins

Three rules follow from that, each of which was a real defect:

  1. Never stamp a tenant on one. SET c += props with user_id/project_id in the dict made the last project to touch a CVE its owner, and every project-scoped delete then removed the shared node along with every other project's links to it.
  2. Never key a MERGE on the tenant triple. MERGE (c:CVE {id, user_id, project_id}) collides with the uniqueness constraint on id and raises ConstraintValidationFailed whenever the CVE already exists.
  3. Never delete one by project. Project wipes exclude these labels and then sweep the nodes no project can REACH any more. Reachability, not degree: the catalogue is internally linked as CVE -> MitreData -> Capec, so an unreferenced CVE still holds its CWE.

Reading them is by traversal, not by tenant filter. graph_db/tenant_filter.py exempts these labels from injection - a filter on an unstamped node matches nothing - but only for a pattern that names reference labels ONLY, and only in a query that carries a tenant-scoped pattern of its own:

MATCH (t:Technology)-[:HAS_KNOWN_CVE]->(c:CVE) RETURN c.id, c.cvss   // ✅
MATCH (c:CVE) RETURN c.id                                            // ❌ refused

The idx_cve_tenant / idx_mitredata_tenant / idx_capec_tenant indexes indexed a property these nodes no longer carry, so init_schema drops them: they are listed in DROP_LEGACY_CONSTRAINTS (graph_db/schema.py) and go on the next connection to any existing database.


🔇 The Muted Label (suppressed findings)

Muted is the one label that is added to a node rather than being its type. When an operator suppresses a finding as noise it keeps its functional label and gains this one, so a suppressed Nuclei finding is :Vulnerability:Muted.

// mute   - add the label, keep the type
MATCH (v:Vulnerability {id: $id, user_id: $uid, project_id: $pid})
SET v:Muted, v.muted = true, v.muted_at = datetime(), v.muted_by = $uid

// unmute - lossless, nothing was destroyed
MATCH (v:Vulnerability:Muted {id: $id, user_id: $uid, project_id: $pid})
REMOVE v:Muted, v.muted, v.muted_at, v.muted_by, v.muted_reason

Only finding-bearing nodes may be muted. Vulnerability, JsReconFinding, Secret, MultiscannerFinding, GithubSecret, GithubSensitiveFile, MalPackageFinding, ExploitGvm. Asset and reference nodes (IP, Port, Domain, Endpoint, CVE, ...) are context: muting one would orphan the real findings hanging off it.

Who muted it: a person, an agent (MCP), or a node-filter rule

muted_by holds the user id of the person who muted the finding, or rule:<kind>/<rule id> when a mute rule did (rule:<kind>/allowlist in allowlist mode), with muted_reason set to Filter rule: <name>.

The third provenance is an external agent muting over the inbound MCP server (mute_findings), with an operator's access token that holds the opt-in triage:mute permission. muted_by stays that operator's user id, because the token carries their authority, and two more properties record how it arrived: muted_channel = 'mcp' and muted_token, the token's display prefix (never the token). Only mute_findings_delegated in graph_db/mixins/recon/triage_mixin.py writes them. Every unmute removes them, and every other mute (a person's, a rule's) clears them, so a later mute never inherits an old token's stamp. tooling/scripts/mute_provenance_cleanup.py strips leftovers from unmuted findings after a rollback and roll-forward.

An agent's mute carries its owner's delegated authority, so the prune (kept, stamped stale_since), the rule sweep (never touched) and partial-recon seeding (excluded) treat it exactly as a person's. Every display and report treats it as an agent's: Muted Nodes, the MCP listings and the pentest report count it apart from people's, per token. It is never allowed to overwrite an existing mute, to hide a proven finding, or to hide one a person brought back (one with an exemption).

The kinds differ in what they mean and in how they are kept:

  • The one sweep that applies rules (apply_node_filters in graph_db/mixins/node_filter_mixin.py) only mutes, re-attributes or releases RULE mutes. A person's mute is never touched, in either direction, and it never writes updated_at.
  • A rule never mutes a guarded finding: one with triage_source = 'human' (a person's verdict, whatever its status), triage_status = 'confirmed' or a triage_proof (the agent or a triage run proved it), or one a ChainFinding points at with CONFIRMS. Guards only ever block: a finding that becomes guarded while a rule has it muted is released at the next sweep, and the mute write re-checks them so a verdict set mid-sweep is never hidden (graph_db/node_filters/guards.py).
  • The ingest-then-prune keep predicate is (n:Muted AND NOT coalesce(n.muted_by,'') STARTS WITH 'rule:') OR coalesce(n.triage_source,'') = 'human': a rule mute is not a judgement of that finding, so a stale one is pruned.
  • The rules (project_node_filters) and the operator's exemptions (node_filter_exemptions: a node someone unmuted, which no rule may mute again) live in Postgres, keyed by project, finding label and natural key, not on the node: the prune, the recon asset clear, version activation and import would each delete a node property. What a rule may match is declared in graph_db/node_filters/catalog.yaml.

Why add a label instead of swapping it

Adding is what makes unmute lossless and what makes mute survive a re-scan. Recon re-runs MERGE (v:Vulnerability {id, user_id, project_id}), which still matches a :Vulnerability:Muted node, refreshes its scan properties and leaves the mute intact. Had mute replaced the functional label, that MERGE would match nothing and create a second, un-muted duplicate of the same finding, because vulnerability_tenant_unique is on :Vulnerability(id, user_id, project_id) and the duplicate would satisfy it.

The single-label convention this bends, and its price

Everywhere else in this schema a node has exactly ONE label, because the graph renderer and several aggregations take labels[0] and Neo4j does not order labels. A muted node is dual-labelled, so its labels[0] is nondeterministic.

That is safe only because of a containment rule that must hold for every read path: no labels[0] consumer ever receives a muted node. Excluding :Muted is therefore a correctness requirement, not only a visibility one - a reader that forgets the filter both leaks a suppressed finding and may mis-type it as "Muted". The one legitimate reader of muted nodes is the list_muted fixed op behind Muted Nodes (and the MCP list_muted_findings tool), which derives the type as [l IN labels(n) WHERE l <> 'Muted'][0] and never uses labels[0]; the node-filter sweep reads them only to reconcile its own rule mutes.

Invisibility is enforced at the tenant chokepoint

graph_db/tenant_filter.py rewrites every node pattern to carry the tenant keys; the same rewrite now also excludes Muted, so the guarantee rides on the mechanism that already covers every query shape the agent can emit:

MATCH (v:Vulnerability)  ->  MATCH (v:Vulnerability&!Muted {user_id: .., project_id: ..})
MATCH (n)                ->  MATCH (n:!Muted {user_id: .., project_id: ..})
MATCH (n:A|B)            ->  MATCH (n:(A|B)&!Muted {user_id: .., project_id: ..})

A query that names the label itself is refused rather than scoped, so the agent has no vocabulary for mute at all:

MATCH (v:Vulnerability&!Muted {...}) RETURN v   // ✅ what injection produces
MATCH (n:Muted) RETURN n                        // ❌ refused: reserved label
MATCH (v:Vulnerability) WHERE NOT v:Muted ...   // ❌ refused: exclusion is automatic

Readers that hand-write their own Cypher and never reach scope_query are separate enforcement sites and must exclude :Muted themselves: the /graph loader (webapp/src/app/api/graph/liveRead.ts), the fixed-op node-type query (agentic/api.py _GRAPH_TYPES_CYPHER), analytics, insights and reports.

🏗️ Multi-Tenant AWS Scalability Strategy

This schema uses Logical Partitioning with Composite Indexes for multi-tenant isolation. Every node type includes user_id and project_id properties with composite indexes.

Why This Approach?

┌─────────────────────────────────────────────────────────────────┐
│                    Single Neo4j Database                        │
│                                                                 │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐             │
│  │  User A     │  │  User B     │  │  User C     │  ...        │
│  │  Project 1  │  │  Project 1  │  │  Project 1  │             │
│  │  Project 2  │  │  Project 2  │  │  Project 2  │             │
│  └─────────────┘  └─────────────┘  └─────────────┘             │
│                                                                 │
│  Composite Constraints: (fields, user_id, project_id) IS UNIQUE │
│  Query Pattern: Always filter by tenant FIRST                   │
└─────────────────────────────────────────────────────────────────┘

Query Pattern (CRITICAL)

All queries MUST start by filtering on user_id and project_id to leverage indexes:

// ✅ CORRECT - Uses composite index, scans only tenant's data
MATCH (d:Domain {user_id: $userId, project_id: $projectId})
-[:HAS_SUBDOMAIN]->(s:Subdomain)
-[:RESOLVES_TO]->(ip:IP)
-[:HAS_PORT]->(p:Port)
RETURN d, s, ip, p

// ❌ WRONG - Full graph scan, affects all tenants
MATCH (v:Vulnerability {severity: 'critical'})
RETURN v

A query that starts from a target name matches the org Domain and filters the Subdomains, never Domain {name: <scan root>}: a project scoped to *.api.example.com has Domain {name: 'example.com'} and its hosts under it.

MATCH (d:Domain {user_id: $userId, project_id: $projectId, name: 'example.com'})
      -[:HAS_SUBDOMAIN]->(s:Subdomain)
WHERE s.name = 'api.example.com' OR s.name ENDS WITH '.api.example.com'
RETURN s

The org Domain also carries the run's coverage record (recon_coverage_at, recon_coverage_gaps, recon_skipped_hosts, recon_nuclei_truncated), merged across the run's scan roots of that org (recon_coverage_roots, keyed by recon_coverage_run), and openapi_source_status entries prefixed with their scan root (<scanRoot>|<id>|<hash>|<code>|<time>).

That record lists what a run CUT. What a module did on each web host lives on the host instead: flat <module>_at / _status / _inputs / _outputs / _reason properties on BaseURL (a property cannot hold a map), written by JS Recon and Nuclei for every host they considered and by MATCH only. A host without its stamp reads "not scanned" only when the module stamped another host of the project, "coverage not recorded" otherwise. See README.RECON, "Per-host coverage stamps".

AWS Deployment Architecture

Node.js API (EKS/ECS Fargate)
        │
        ├── ElastiCache Redis ─── Query caching per tenant
        │
        └── Neo4j AuraDB / Neo4j on EC2
                │
                └── Composite indexes on (user_id, project_id)

Scaling Path

Phase Users Strategy AWS Services
MVP 0-100 Single DB + Indexes ECS Fargate, Neo4j AuraDB
Growth 100-1K Read Replicas EKS, AuraDB Professional
Scale 1K+ Sharded by User Pools EKS Multi-AZ, Neo4j Cluster

📝 Notes for Implementation

  1. Deduplication: Before creating nodes, check if they exist (MERGE vs CREATE)
  2. Timestamps: Store as Neo4j datetime type for proper querying
  3. Arrays: Neo4j supports array properties (tags, references, etc.)
  4. Large Text: Keep descriptions under 10KB, store curl_command and request/response separately if needed
  5. Batch Import: For large scans, use APOC procedures for batch imports

🕰️ Scan Timeline (versions live in Postgres, NOT in the graph)

The Neo4j schema does not change for the Scan Timeline. There is no version node, no version property, and no :VERSION_OF relationship — nothing in this document is modified by the feature.

The model

  • The live Neo4j graph IS the current version. It behaves exactly as before: a full recon wipes and rebuilds it. Everything that reads or writes the graph (the agent, partial recon, the ~30 RedZone/analytics endpoints, saved views) therefore sees whichever version is active, unchanged.
  • A past version is a saved snapshot: the project's subgraph, serialized and gzipped into Postgres. Past versions are read-only and never present in Neo4j, so old data can never reach the agent or an analytics query.
  • Snapshots are taken before a new full scan overwrites the graph — and only when the user chooses to keep it (the "new version vs overwrite" modal).

Postgres models

Model Purpose
ScanVersion One point-in-time identity for the recon graph. isCurrent = true on exactly one row per project — that row IS the live graph and has snapshot = null. A frozen (past) version carries snapshot bytes. @@unique([projectId, seq]) keeps numbering from forking.
ScanJob Run history: trigger (manual/scheduled), mode, status (queued/running/completed/failed/canceled/deferred_ram), who started it, timings, ramReason. kind records WHICH scan the row is for (full_recon | partial_recon | gvm | github_hunt | trufflehog | supply_chain | supply_chain_repo | ai_attack) — before it, a directly-started non-recon run had no record at all. runId is the orchestrator run id for the kinds that allow several concurrent runs per project (partial_recon, ai_attack), and is empty for the one-per-project kinds.
ScanSchedule A future/recurring full scan: once / interval / cron (UTC), plus the scanMode to use for the previous graph.

Project also gains the activation lock columns activation_state, activation_started_at, activation_version_id (see below).

Snapshot payload shape

Snapshots are stored in the export (restore-fidelity) format, not the UI render shape, because a snapshot must be restorable back into Neo4j:

// gzip( JSON ) in ScanVersion.snapshot
{
  "nodes": [
    { "labels": ["Subdomain"], "properties": { /* ALL properties, incl. project_id/user_id */ },
      "_exportId": "<uuid>" }
  ],
  "relationships": [
    { "startExportId": "<uuid>", "endExportId": "<uuid>", "type": "RESOLVES_TO", "properties": {} }
  ],
  // absent in snapshots stored before plan 09.5
  "referenceLinks": [
    { "startExportId": "<uuid>", "endExportId": null, "type": "HAS_KNOWN_CVE",
      "refLabel": "CVE", "refKey": "CVE-2021-23017", "properties": {} }
  ]
}

Reference nodes are never captured, their links are. CVE, MitreData and Capec carry no tenant key and are shared by every project, so a snapshot or a project export (manifest 1.1.0, neo4j/reference_links.json) holds no such node and no property of one. Each edge to one is a link record: the project end by export id, the other end (the null id) by its natural key. A restore validates the label against the catalogue and the key against its shape, MATCHes the node (never MERGE, never SET; label and key property from a fixed map), and skips and counts a link whose node is missing. A payload that carries a reference node anyway (one stored before the catalogue lost its tenant stamp) has the node dropped and its edges turned into link records. Activation sweeps orphaned reference nodes only after the restore, so a CVE only this project linked survives the swap (webapp/src/lib/graphReferenceLinks.ts).

The graph screen renders a version by converting this to the same { nodes, links } payload /api/graph returns, so the canvas, the clustering and the node/link tables are unchanged.

Agent session nodes are excluded. The AttackChain family (AttackChain, ChainStep, ChainFinding, ChainDecision, ChainFailure) is agent-run state, not recon state: it is filtered out of every capture, and preserved (not deleted) when a version is activated. Chains stay conversation-scoped exactly as documented in the Attack Chain Graph section above.

Activation (switching the active version)

Viewing a version only renders its bytes. Activating it swaps the live graph:

  1. freeze the outgoing current version from the live graph (not from its old stored bytes — partial recon may have edited it since),
  2. delete the live recon graph excluding the AttackChain family, and restore the target version through the same code path the project import uses,
  3. only then move the isCurrent pointer, and invalidate the graph cache.

A failure in step 1 aborts before anything is deleted; a failure in step 2 leaves both endpoints intact in Postgres, so the activation is simply retriable.

Activation holds a project activation lock (Project.activation_state) and is mutually exclusive with a full scan, a partial recon run, an agent session, and the scan scheduler — in both directions.

Only the recon graph is versioned. GVM/secret-scan output files, remediations, reports and captured HTTP traffic are project-level and always reflect the latest scan, whichever version is active. The AI agent's own findings (Vulnerability {source: "agent"}, below) are project-level too: a capture leaves them out, step 2 never deletes them, and they are re-anchored to whatever the restored version holds.


🤖 The agent's own findings reuse Vulnerability

When the AI agent finds a weakness on a host recon knows, it is promoted from session history (ChainFinding) into a Vulnerability with source: "agent", not into a label of its own. About twenty sources already share Vulnerability, and source tells them apart, so:

  • every reader of findings (the Priority Board, triage, the report, Insights, the mute rules, the MCP and Navigator tools) already includes it, with no reader learning a ninth finding label;
  • recon's clear never deletes a finding label, and its prune names only its own sources, so another subsystem's findings in that label survive recon; agent sits in NON_RECON_SOURCES beside GVM's, the supply chain's and the AI attack-surface scanners';
  • agent-only facts carry an agent_ prefix (agent_proven, agent_anchor_state, ...), and the properties a scanner's reader would misread on it are never set.

A ChainFinding stays what it was: one node per sighting, deleted with its chat. The Vulnerability is one node per weakness, host, port, route, method, parameter and CVE, with exactly one recon parent, so it is never disconnected from the surface it describes and every later sighting lands on the same node. How it is written and re-attached is in README.GRAPH_DB.md.


🔮 Future Extensions (Not Implemented Yet)

  • GVMScan, GVMVulnerability, DetectedProduct, OSFingerprint nodes (GVM integration - designed but not yet created by code; GVM vulns currently stored as Vulnerability nodes with source="gvm"; Traceroute nodes now implemented)
  • Screenshot nodes linking to stored images