How mdjunaid07/supplyguard works
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: router.py Webhook coordinator · report_generator.py PR report renderer · docker-compose.yml Docker Compose · dependency_extractor.py Dependency extractor
Components 21
GitHub boundary
- GitHub App · Sends PR webhooks and receives PR comments.
- GitHub client
backend/utils/github_client.py - npm, GitHub & NVD APIs
Scan backend
- Webhook coordinator
backend/webhook/router.py - Signature validator
backend/webhook/validator.py - Dependency extractor
backend/services/dependency_extractor.py - Package intelligence collectors · Collect registry metadata, repository activity, and CVEs.
backend/services/npm_collector.py - PR report renderer
backend/services/report_generator.py
Risk analysis
- Feature assembly · Builds the 14-feature package risk vector.
backend/services/feature_engineer.py - ML predictor · Loads artifacts, predicts risk, and produces explanations.
backend/ml/predictor.py - Dependency trust graph
backend/graph/trust_graph.py - PageRank trust scorer
backend/graph/trust_scorer.py
Persistence & API
- MongoDB · Stores PR scans and package risk history.
backend/db/mongo.py - Dashboard REST API · Serves persisted scan, package, and trend data.
backend/api/router.py
Dashboard
- React dashboard
frontend/src/App.jsx - API client
frontend/src/api/client.js - Trust graph view
frontend/src/components/TrustGraph.jsx
Model lifecycle
- Model training · Builds versioned scikit-learn serving artifacts.
ml-model/train.py - Training dataset
ml-model/data/sample_dataset.csv - Model artifacts · Versioned model and preprocessing artifacts.
ml-model/models/model.pkl
Other components
- Docker Compose · Runs backend, frontend, and MongoDB together.
docker-compose.yml
Connections 24
- 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