How gmax-13/phishguard works
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: ensemble.py Ensemble decision engine · app.py Analyze API · background.js Extension coordinator · manifest.json Extension manifest
Components 14
Chrome Extension
- Extension manifest · Declares the browser extension runtime.
Extension (Frontend)/manifest.json - Email extraction · Extracts sender, subject, body, and links from email pages.
Extension (Frontend)/content.js - Extension coordinator · Coordinates extension messages and analysis requests.
Extension (Frontend)/background.js - Risk result UI · Shows returned risk state and guidance.
Extension (Frontend)/popup.js - Popup shell
Extension (Frontend)/popup.html
Flask Backend
- Analyze API · Hosts POST /analyze and maps predictions to risk responses.
Backend/app.py - Email normalization · Sanitizes untrusted markup and derives text, URL, and structural signals.
Backend/email_preprocessing.py - Ensemble decision engine · Aggregates local and hosted model probabilities into a final decision.
Backend/ensemble.py - Analysis and feedback access · Persists predictions, per-model probabilities, and feedback labels.
Backend/database.py
Model Assets
- TF-IDF classical models · Logistic regression, random forest, and vectorizer.
Models/logistic_model.pkl - Sequence neural models · LSTM, BiLSTM attention, and tokenizer artifacts.
Models/lstm_phishing_model.keras
External Runtime
- MongoDB · Stores analysis records and later feedback.
- Hosted DistilBERT · Synchronous transformer inference via BERT_API_URL.
- Runtime secrets and URLs · Supplies MONGO_URL, HF_TOKEN, and BERT_API_URL.
Connections 20
- 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