# mdjunaid07/supplyguard architecture

> Architecture diagram of the GitHub repository mdjunaid07/supplyguard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mdjunaid07/supplyguard
- Repository: https://github.com/mdjunaid07/supplyguard

## Overview

#### 1. Purpose and Entry Points
SupplyGuard is a PR-triggered npm supply-chain risk analysis system. It evaluates dependency additions/updates before merge, posts a GitHub PR report, persists scan history, and exposes results to a dashboard.

- Backend ASGI app: `backend/main.py`
- GitHub webhook ingress: `backend/webhook/router.py`
- Dashboard bootstrap/routes: `frontend/src/main.jsx`, `frontend/src/App.jsx`
- Standalone model training/inference: `ml-model/train.py`, `ml-model/predict.py`

#### 2. Webhook-Orchestrated Scan Pipeline
A GitHub App sends pull-request events to `POST /webhook`. The webhook boundary validates GitHub signatures, filters/processes relevant PR events, and coordinates the end-to-end scan.

- Signature validation: `backend/webhook/validator.py`
- Workflow coordinator: `backend/webhook/router.py`
- GitHub API/comment integration: `backend/utils/github_client.py`

The pipeline is externally driven and must tolerate GitHub API, registry, NVD, model, and database failures without accepting untrusted webhook payloads.

#### 3. Dependency Discovery and Intelligence Collection
For a changed PR, the system derives changed npm dependencies from the package manifest diff, then enriches each package from three external intelligence sources.

- PR diff/dependency extraction: `backend/services/dependency_extractor.py`
- npm registry metadata: `backend/services/npm_collector.py`
- GitHub repository/maintainer activity: `backend/services/github_collector.py`
- NVD CVE lookup: `backend/services/cve_collector.py`

This stage is the boundary between GitHub PR state and normalized package-security inputs. External services are GitHub APIs, npm registry APIs, and the NVD CVE API.

#### 4. Feature, ML Risk, and Explanation Layer
Collected signals become a 14-feature vector covering package age, maintainers, activity, popularity, ownership/release anomalies, CVEs, dependency depth, and historical risk. The backend loads persisted scikit-learn artifacts to produce a probability/risk level and can generate SHAP explanations.

- Feature assembly: `backend/services/feature_engineer.py`
- Runtime prediction/preprocessing/explanations: `backend/ml/predictor.py`, `backend/ml/preprocessor.py`, `backend/ml/explainer.py`
- Versioned model artifacts: `ml-model/models/model.pkl`, `ml-model/models/preprocessor.pkl`

Training is separate from serving: `ml-model/train.py` builds artifacts from `ml-model/data/sample_dataset.csv`; serving consumes those artifact paths via backend configuration.

#### 5. Dependency Graph Trust Analysis
ML risk is augmented with a graph-derived trust score. Package/dependency relationships form a NetworkX graph, and PageRank-based scoring estimates package trust within that topology.

- Graph construction: `backend/graph/trust_graph.py`
- Trust/PageRank scoring: `backend/graph/trust_scorer.py`

This is a distinct analytical path from CVE lookup and behavioral ML; the final per-package assessment combines those inputs for reporting and persistence.

#### 6. Outputs, Persistence, and Read APIs
The scan coordinator converts package assessments into Markdown, posts it to the originating GitHub PR, and records scan/package history in MongoDB. Dashboard-facing REST endpoints read this stored state rather than re-running scans.

- Report rendering: `backend/services/report_generator.py`
- Mongo connection and schemas: `backend/db/mongo.py`, `backend/db/models.py`
- Dashboard REST API: `backend/api/router.py`

MongoDB is the durable system-of-record for PR scans and package risk history, supporting overview, scan history, package detail, and trend queries.

#### 7. Runtime and Deployment Topology
The backend is Python/FastAPI (typically Uvicorn, port 8000); the frontend is React/Vite and communicates through `frontend/src/api/client.js`. Production frontend packaging uses Nginx. Docker Compose provisions backend, frontend, and MongoDB together.

- Service composition: `docker-compose.yml`
- Backend container: `backend/Dockerfile`
- Frontend container/proxy: `frontend/Dockerfile`, `frontend/nginx.conf`
- Dashboard views/visualizations: `frontend/src/pages/Dashboard.jsx`, `frontend/src/components/TrustGraph.jsx`

Required operational integrations are a GitHub App/private key and webhook secret, MongoDB (local or Atlas), optional NVD API key, and model artifact availability.

Main entry points: [`backend/webhook/router.py`](https://github.com/mdjunaid07/supplyguard/blob/main/backend/webhook/router.py) (Webhook coordinator), [`backend/services/report_generator.py`](https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/report_generator.py) (PR report renderer), [`docker-compose.yml`](https://github.com/mdjunaid07/supplyguard/blob/main/docker-compose.yml) (Docker Compose), [`backend/services/dependency_extractor.py`](https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/dependency_extractor.py) (Dependency extractor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_github["GitHub boundary"]
  node_github_app{{"GitHub App<br/>external integration"}}
  node_github_client["GitHub client<br/>API client<br/>[github_client.py]"]
  node_intel_sources{{"npm, GitHub &amp; NVD APIs<br/>external intelligence"}}
end

subgraph group_backend["Scan backend"]
  node_webhook["Webhook coordinator<br/>FastAPI route<br/>[router.py]"]
  node_validator["Signature validator<br/>security boundary<br/>[validator.py]"]
  node_dependency_extractor["Dependency extractor<br/>diff analysis"]
  node_collectors["Package intelligence collectors<br/>enrichment services<br/>[npm_collector.py]"]
  node_report["PR report renderer<br/>Markdown report"]
end

subgraph group_analysis["Risk analysis"]
  node_features["Feature assembly<br/>feature engineering"]
  node_predictor["ML predictor<br/>risk inference<br/>[predictor.py]"]
  node_trust_graph["Dependency trust graph<br/>NetworkX graph<br/>[trust_graph.py]"]
  node_trust_scorer["PageRank trust scorer<br/>graph scoring<br/>[trust_scorer.py]"]
end

subgraph group_storage["Persistence &amp; API"]
  node_mongo[("MongoDB<br/>system of record<br/>[mongo.py]")]
  node_api["Dashboard REST API<br/>FastAPI router<br/>[router.py]"]
end

subgraph group_dashboard["Dashboard"]
  node_frontend["React dashboard<br/>frontend app<br/>[App.jsx]"]
  node_frontend_client["API client<br/>REST client<br/>[client.js]"]
  node_trust_view["Trust graph view<br/>visualization<br/>[TrustGraph.jsx]"]
end

subgraph group_ml["Model lifecycle"]
  node_training["Model training<br/>offline training<br/>[train.py]"]
  node_dataset["Training dataset<br/>CSV data<br/>[sample_dataset.csv]"]
  node_artifacts["Model artifacts<br/>serialized model<br/>[model.pkl]"]
end

node_compose["Docker Compose<br/>deployment composition<br/>[docker-compose.yml]"]

node_github_app -->|"PR event"| node_webhook
node_webhook -->|"validate signature"| node_validator
node_webhook -->|"read PR / comment"| node_github_client
node_webhook -->|"changed manifest diff"| node_dependency_extractor
node_dependency_extractor -->|"changed packages"| node_collectors
node_collectors -->|"metadata, activity, CVEs"| node_intel_sources
node_collectors -->|"normalized signals"| node_features
node_features -->|"14-feature vector"| node_predictor
node_dependency_extractor -->|"dependency relations"| node_trust_graph
node_trust_graph -->|"package topology"| node_trust_scorer
node_predictor -->|"ML risk + explanation"| node_report
node_trust_scorer -->|"trust score"| node_report
node_report -->|"Markdown report"| node_github_client
node_report -->|"scan and package history"| node_mongo
node_frontend -->|"dashboard queries"| node_frontend_client
node_frontend_client -->|"REST"| node_api
node_api -->|"read persisted state"| node_mongo
node_frontend -->|"renders"| node_trust_view
node_dataset -->|"training data"| node_training
node_training -->|"produces"| node_artifacts
node_artifacts -->|"loads for serving"| node_predictor
node_compose -.->|"deploys backend"| node_webhook
node_compose -.->|"deploys frontend"| node_frontend
node_compose -.->|"provisions"| node_mongo

click node_webhook "https://github.com/mdjunaid07/supplyguard/blob/main/backend/webhook/router.py"
click node_validator "https://github.com/mdjunaid07/supplyguard/blob/main/backend/webhook/validator.py"
click node_github_client "https://github.com/mdjunaid07/supplyguard/blob/main/backend/utils/github_client.py"
click node_dependency_extractor "https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/dependency_extractor.py"
click node_collectors "https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/npm_collector.py"
click node_features "https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/feature_engineer.py"
click node_predictor "https://github.com/mdjunaid07/supplyguard/blob/main/backend/ml/predictor.py"
click node_trust_graph "https://github.com/mdjunaid07/supplyguard/blob/main/backend/graph/trust_graph.py"
click node_trust_scorer "https://github.com/mdjunaid07/supplyguard/blob/main/backend/graph/trust_scorer.py"
click node_report "https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/report_generator.py"
click node_mongo "https://github.com/mdjunaid07/supplyguard/blob/main/backend/db/mongo.py"
click node_api "https://github.com/mdjunaid07/supplyguard/blob/main/backend/api/router.py"
click node_frontend "https://github.com/mdjunaid07/supplyguard/blob/main/frontend/src/App.jsx"
click node_frontend_client "https://github.com/mdjunaid07/supplyguard/blob/main/frontend/src/api/client.js"
click node_trust_view "https://github.com/mdjunaid07/supplyguard/blob/main/frontend/src/components/TrustGraph.jsx"
click node_training "https://github.com/mdjunaid07/supplyguard/blob/main/ml-model/train.py"
click node_dataset "https://github.com/mdjunaid07/supplyguard/blob/main/ml-model/data/sample_dataset.csv"
click node_artifacts "https://github.com/mdjunaid07/supplyguard/blob/main/ml-model/models/model.pkl"
click node_compose "https://github.com/mdjunaid07/supplyguard/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_github_app,node_github_client,node_intel_sources toneBlue
class node_webhook,node_validator,node_dependency_extractor,node_collectors,node_report toneAmber
class node_features,node_predictor,node_trust_graph,node_trust_scorer toneMint
class node_mongo,node_api toneRose
class node_frontend,node_frontend_client,node_trust_view toneIndigo
class node_training,node_dataset,node_artifacts toneTeal
class node_compose toneNeutral
```

## Components

### GitHub boundary

- **GitHub App**: external integration · Sends PR webhooks and receives PR comments.
- [GitHub client](https://github.com/mdjunaid07/supplyguard/blob/main/backend/utils/github_client.py): API client · `backend/utils/github_client.py`
- **npm, GitHub & NVD APIs**: external intelligence

### Scan backend

- [Webhook coordinator](https://github.com/mdjunaid07/supplyguard/blob/main/backend/webhook/router.py): FastAPI route · `backend/webhook/router.py`
- [Signature validator](https://github.com/mdjunaid07/supplyguard/blob/main/backend/webhook/validator.py): security boundary · `backend/webhook/validator.py`
- [Dependency extractor](https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/dependency_extractor.py): diff analysis · `backend/services/dependency_extractor.py`
- [Package intelligence collectors](https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/npm_collector.py): enrichment services · Collect registry metadata, repository activity, and CVEs. · `backend/services/npm_collector.py`
- [PR report renderer](https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/report_generator.py): Markdown report · `backend/services/report_generator.py`

### Risk analysis

- [Feature assembly](https://github.com/mdjunaid07/supplyguard/blob/main/backend/services/feature_engineer.py): feature engineering · Builds the 14-feature package risk vector. · `backend/services/feature_engineer.py`
- [ML predictor](https://github.com/mdjunaid07/supplyguard/blob/main/backend/ml/predictor.py): risk inference · Loads artifacts, predicts risk, and produces explanations. · `backend/ml/predictor.py`
- [Dependency trust graph](https://github.com/mdjunaid07/supplyguard/blob/main/backend/graph/trust_graph.py): NetworkX graph · `backend/graph/trust_graph.py`
- [PageRank trust scorer](https://github.com/mdjunaid07/supplyguard/blob/main/backend/graph/trust_scorer.py): graph scoring · `backend/graph/trust_scorer.py`

### Persistence & API

- [MongoDB](https://github.com/mdjunaid07/supplyguard/blob/main/backend/db/mongo.py): system of record · Stores PR scans and package risk history. · `backend/db/mongo.py`
- [Dashboard REST API](https://github.com/mdjunaid07/supplyguard/blob/main/backend/api/router.py): FastAPI router · Serves persisted scan, package, and trend data. · `backend/api/router.py`

### Dashboard

- [React dashboard](https://github.com/mdjunaid07/supplyguard/blob/main/frontend/src/App.jsx): frontend app · `frontend/src/App.jsx`
- [API client](https://github.com/mdjunaid07/supplyguard/blob/main/frontend/src/api/client.js): REST client · `frontend/src/api/client.js`
- [Trust graph view](https://github.com/mdjunaid07/supplyguard/blob/main/frontend/src/components/TrustGraph.jsx): visualization · `frontend/src/components/TrustGraph.jsx`

### Model lifecycle

- [Model training](https://github.com/mdjunaid07/supplyguard/blob/main/ml-model/train.py): offline training · Builds versioned scikit-learn serving artifacts. · `ml-model/train.py`
- [Training dataset](https://github.com/mdjunaid07/supplyguard/blob/main/ml-model/data/sample_dataset.csv): CSV data · `ml-model/data/sample_dataset.csv`
- [Model artifacts](https://github.com/mdjunaid07/supplyguard/blob/main/ml-model/models/model.pkl): serialized model · Versioned model and preprocessing artifacts. · `ml-model/models/model.pkl`

### Other components

- [Docker Compose](https://github.com/mdjunaid07/supplyguard/blob/main/docker-compose.yml): deployment composition · Runs backend, frontend, and MongoDB together. · `docker-compose.yml`

## Connections

- GitHub App → Webhook coordinator: PR event
- Webhook coordinator → Signature validator: validate signature
- Webhook coordinator → GitHub client: read PR / comment
- Webhook coordinator → Dependency extractor: changed manifest diff
- Dependency extractor → Package intelligence collectors: changed packages
- Package intelligence collectors → npm, GitHub & NVD APIs: metadata, activity, CVEs
- Package intelligence collectors → Feature assembly: normalized signals
- Feature assembly → ML predictor: 14-feature vector
- Dependency extractor → Dependency trust graph: dependency relations
- Dependency trust graph → PageRank trust scorer: package topology
- ML predictor → PR report renderer: ML risk + explanation
- PageRank trust scorer → PR report renderer: trust score
- PR report renderer → GitHub client: Markdown report
- PR report renderer → MongoDB: scan and package history
- React dashboard → API client: dashboard queries
- API client → Dashboard REST API: REST
- Dashboard REST API → MongoDB: read persisted state
- React dashboard → Trust graph view: renders
- Training dataset → Model training: training data
- Model training → Model artifacts: produces
- Model artifacts → ML predictor: loads for serving
- Docker Compose → Webhook coordinator: deploys backend
- Docker Compose → React dashboard: deploys frontend
- Docker Compose → MongoDB: provisions
