# gmax-13/phishguard architecture

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

- Interactive diagram: https://gitdiagram.com/gmax-13/phishguard
- Repository: https://github.com/gmax-13/phishguard

## Overview

#### Purpose and Top-Level Entry Points
PhishGuard is a Chrome browser-extension plus Flask service that classifies extracted email content as safe, suspicious, or phishing. Runtime entry points are the extension manifest/background scripts and the Flask application: `Extension (Frontend)/manifest.json`, `Extension (Frontend)/background.js`, and `Backend/app.py`.

#### Extension Capture and User Interaction
The extension runs in the browser email context, extracts sender, subject, body, and links, then submits JSON for analysis. `Extension (Frontend)/content.js` is the page-facing extraction boundary; `Extension (Frontend)/background.js` coordinates extension-side messaging/network activity. Results and user-facing risk state are surfaced through `Extension (Frontend)/popup.js` and `Extension (Frontend)/popup.html`; onboarding and settings provide extension lifecycle/configuration UI.

#### Backend API Orchestration
`Backend/app.py` hosts the Flask API, including the documented `POST /analyze` request boundary. It receives extension payloads, invokes preprocessing and ensemble inference, maps probability to GREEN/YELLOW/RED risk plus explanation/action, returns the result to the extension, and persists analysis outcomes. The browser-to-local/API HTTP boundary is the primary trust and serialization boundary.

#### Email Normalization and Feature Extraction
`Backend/email_preprocessing.py` turns incoming HTML/text/link fields into model-ready representations. Its pipeline removes non-visible HTML content, extracts visible text and URLs, and derives structural phishing signals such as URL count, suspicious domains, IP-hosted URLs, punycode, and text length. This separates untrusted email markup from downstream model inputs.

#### Multi-Model Inference and Decisioning
`Backend/ensemble.py` is the central decision engine. It combines probabilities from TF-IDF classical models, sequence neural models, and remote DistilBERT inference using weighted aggregation. Local serialized/model artifacts are `Models/logistic_model.pkl`, `Models/random_forest_model.pkl`, `Models/tfidf_vectorizer.pkl`, `Models/lstm_phishing_model.keras`, and `Models/bilstm_attn_model.h5`; tokenization support is in `Models/tokenizer.pkl`. The transformer introduces an external synchronous inference dependency rather than a checked-in local model artifact.

#### Feedback-Driven Adaptation
Analysis records include final prediction/confidence and per-model probabilities; feedback supplies a later ground-truth label. The intended control loop compares individual model outputs with feedback, applies reward-based and time-decayed updates to ensemble weights, then affects subsequent decisions. Persistence/access logic belongs in `Backend/database.py`, while ensemble state/update behavior is architecturally coupled to `Backend/ensemble.py`.

#### Persistence, Runtime, and External Services
The backend is Python/Flask with dependencies declared in `Backend/requirements.txt`; the extension is a Chrome extension using HTML/CSS/JavaScript. MongoDB stores email analyses and feedback through `Backend/database.py`. Environment configuration supplies `MONGO_URL`, Hugging Face authentication (`HF_TOKEN`), and `BERT_API_URL` for the hosted DistilBERT endpoint. Thus, local model loading, MongoDB availability, and Hugging Face network/API availability are material runtime boundaries.

Main entry points: [`Backend/ensemble.py`](https://github.com/gmax-13/phishguard/blob/main/Backend/ensemble.py) (Ensemble decision engine), [`Backend/app.py`](https://github.com/gmax-13/phishguard/blob/main/Backend/app.py) (Analyze API), [`Extension (Frontend)/background.js`](https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/background.js) (Extension coordinator), [`Extension (Frontend)/manifest.json`](https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/manifest.json) (Extension manifest)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_extension["Chrome Extension"]
  node_manifest["Extension manifest<br/>extension entry<br/>[manifest.json]"]
  node_content["Email extraction<br/>content script<br/>[content.js]"]
  node_background["Extension coordinator<br/>background script<br/>[background.js]"]
  node_popup["Risk result UI<br/>popup UI<br/>[popup.js]"]
  node_popup_html["Popup shell<br/>HTML view<br/>[popup.html]"]
end

subgraph group_backend["Flask Backend"]
  node_api["Analyze API<br/>Flask application<br/>[app.py]"]
  node_preprocessing["Email normalization<br/>preprocessing pipeline"]
  node_ensemble["Ensemble decision engine<br/>inference and weighting<br/>[ensemble.py]"]
  node_persistence["Analysis and feedback access<br/>database layer<br/>[database.py]"]
end

subgraph group_models["Model Assets"]
  node_classical_models["TF-IDF classical models<br/>serialized model artifacts<br/>[logistic_model.pkl]"]
  node_sequence_models["Sequence neural models<br/>neural model artifacts"]
end

subgraph group_external["External Runtime"]
  node_mongodb[("MongoDB<br/>analysis datastore")]
  node_distilbert_api{{"Hosted DistilBERT<br/>remote inference API"}}
  node_environment["Runtime secrets and URLs<br/>environment configuration"]
end

node_manifest -->|"loads"| node_content
node_manifest -->|"loads"| node_background
node_manifest -->|"declares"| node_popup_html
node_popup_html -->|"hosts"| node_popup
node_content -->|"extracted email payload"| node_background
node_background -->|"POST /analyze JSON"| node_api
node_api -->|"normalizes payload"| node_preprocessing
node_preprocessing -->|"model-ready features"| node_ensemble
node_ensemble -->|"prediction and confidence"| node_api
node_api -->|"risk result"| node_background
node_background -->|"risk state"| node_popup
node_api -->|"stores outcome"| node_persistence
node_persistence -->|"reads and writes"| node_mongodb
node_persistence -->|"feedback labels and model history"| node_ensemble
node_ensemble -->|"loads"| node_classical_models
node_ensemble -->|"loads"| node_sequence_models
node_ensemble -->|"remote inference"| node_distilbert_api
node_environment -.->|"API configuration"| node_api
node_environment -.->|"MONGO_URL"| node_mongodb
node_environment -.->|"endpoint and token"| node_distilbert_api

click node_manifest "https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/manifest.json"
click node_content "https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/content.js"
click node_background "https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/background.js"
click node_popup "https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/popup.js"
click node_popup_html "https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/popup.html"
click node_api "https://github.com/gmax-13/phishguard/blob/main/Backend/app.py"
click node_preprocessing "https://github.com/gmax-13/phishguard/blob/main/Backend/email_preprocessing.py"
click node_ensemble "https://github.com/gmax-13/phishguard/blob/main/Backend/ensemble.py"
click node_persistence "https://github.com/gmax-13/phishguard/blob/main/Backend/database.py"
click node_classical_models "https://github.com/gmax-13/phishguard/blob/main/Models/logistic_model.pkl"
click node_sequence_models "https://github.com/gmax-13/phishguard/blob/main/Models/lstm_phishing_model.keras"

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_manifest,node_content,node_background,node_popup,node_popup_html toneBlue
class node_api,node_preprocessing,node_ensemble,node_persistence toneAmber
class node_classical_models,node_sequence_models toneMint
class node_mongodb,node_distilbert_api,node_environment toneRose
```

## Components

### Chrome Extension

- [Extension manifest](https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/manifest.json): extension entry · Declares the browser extension runtime. · `Extension (Frontend)/manifest.json`
- [Email extraction](https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/content.js): content script · Extracts sender, subject, body, and links from email pages. · `Extension (Frontend)/content.js`
- [Extension coordinator](https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/background.js): background script · Coordinates extension messages and analysis requests. · `Extension (Frontend)/background.js`
- [Risk result UI](https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/popup.js): popup UI · Shows returned risk state and guidance. · `Extension (Frontend)/popup.js`
- [Popup shell](https://github.com/gmax-13/phishguard/blob/main/Extension%20(Frontend)/popup.html): HTML view · `Extension (Frontend)/popup.html`

### Flask Backend

- [Analyze API](https://github.com/gmax-13/phishguard/blob/main/Backend/app.py): Flask application · Hosts POST /analyze and maps predictions to risk responses. · `Backend/app.py`
- [Email normalization](https://github.com/gmax-13/phishguard/blob/main/Backend/email_preprocessing.py): preprocessing pipeline · Sanitizes untrusted markup and derives text, URL, and structural signals. · `Backend/email_preprocessing.py`
- [Ensemble decision engine](https://github.com/gmax-13/phishguard/blob/main/Backend/ensemble.py): inference and weighting · Aggregates local and hosted model probabilities into a final decision. · `Backend/ensemble.py`
- [Analysis and feedback access](https://github.com/gmax-13/phishguard/blob/main/Backend/database.py): database layer · Persists predictions, per-model probabilities, and feedback labels. · `Backend/database.py`

### Model Assets

- [TF-IDF classical models](https://github.com/gmax-13/phishguard/blob/main/Models/logistic_model.pkl): serialized model artifacts · Logistic regression, random forest, and vectorizer. · `Models/logistic_model.pkl`
- [Sequence neural models](https://github.com/gmax-13/phishguard/blob/main/Models/lstm_phishing_model.keras): neural model artifacts · LSTM, BiLSTM attention, and tokenizer artifacts. · `Models/lstm_phishing_model.keras`

### External Runtime

- **MongoDB**: analysis datastore · Stores analysis records and later feedback.
- **Hosted DistilBERT**: remote inference API · Synchronous transformer inference via BERT_API_URL.
- **Runtime secrets and URLs**: environment configuration · Supplies MONGO_URL, HF_TOKEN, and BERT_API_URL.

## Connections

- Extension manifest → Email extraction: loads
- Extension manifest → Extension coordinator: loads
- Extension manifest → Popup shell: declares
- Popup shell → Risk result UI: hosts
- Email extraction → Extension coordinator: extracted email payload
- Extension coordinator → Analyze API: POST /analyze JSON · Primary browser-to-service trust boundary.
- Analyze API → Email normalization: normalizes payload
- Email normalization → Ensemble decision engine: model-ready features
- Ensemble decision engine → Analyze API: prediction and confidence
- Analyze API → Extension coordinator: risk result
- Extension coordinator → Risk result UI: risk state
- Analyze API → Analysis and feedback access: stores outcome
- Analysis and feedback access → MongoDB: reads and writes
- Analysis and feedback access → Ensemble decision engine: feedback labels and model history · Drives reward-based, time-decayed weight updates.
- Ensemble decision engine → TF-IDF classical models: loads
- Ensemble decision engine → Sequence neural models: loads
- Ensemble decision engine → Hosted DistilBERT: remote inference
- Runtime secrets and URLs → Analyze API: API configuration
- Runtime secrets and URLs → MongoDB: MONGO_URL
- Runtime secrets and URLs → Hosted DistilBERT: endpoint and token
