Skip to content

AadhityaS-2124/Guard-Agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🛡️ GuardAgent: Multi-Agent Security Remediation Engine

Intelligent vulnerability detection and automated code remediation powered by AI

TypeScript Python FastAPI React LangGraph


🎯 Overview

GuardAgent is a sophisticated security platform that combines static code analysis with AI-powered remediation to automatically detect and fix security vulnerabilities in Python code. It leverages a multi-agent architecture with enterprise-grade security controls to provide intelligent, context-aware vulnerability remediation with a self-healing validation loop.

Key Capabilities

Automated Vulnerability Detection - Static AST analysis identifies common security issues
AI-Powered Remediation - Google Gemini LLM generates secure code patches
Self-Healing Loop - Iterative refinement with compilation validation (max 3 iterations)
Real-Time Streaming - Live pipeline status and execution updates
Interactive UI - Modern, skeuomorphic interface with visual security insights
Comprehensive Logging - Detailed error tracking and refinement history
🔒 Enterprise Security - Built-in secret redaction, CORS protection, and secure error handling


🏗️ Architecture

┌─────────────────────────────────────────────────────────────────┐
│                    FRONTEND (React + TypeScript)                 │
│            Interactive Code Editor & Visualization              │
└──────────────────────────┬──────────────────────────────────────┘
                           │ HTTP Streaming (SSE)
                           ↓
┌─────────────────────────────────────────────────────────────────┐
│                   BACKEND (FastAPI + LangGraph)                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                   │
│  ┌──────────────┐    ┌──────────────┐    ┌──────────────┐       │
│  │  AST Scan    │───→│  LLM Patch   │───→│  Validation  │       │
│  │  & Analyze   │    │  (Gemini)    │    │  (Compiler)  │       │
│  └──────────────┘    └──────────────┘    └────────┬─────┘       │
│         ↓                                         ↓              │
│    🔐 Security Layer (Secret Redaction & Error Sanitization)   │
│                                                                   │
│                              ┌─────────────────────┴──────┐       │
│                              │                            │       │
│                              ↓ (on error & max < 3)      ↓       │
│                         Loop Back          Finalize & Exit       │
│                                                                   │
└─────────────────────────────────────────────────────────────────┘

Processing Pipeline

  1. Scanning 🔍 - AST-based vulnerability detection
  2. Analyzing 📊 - Vulnerability classification and severity assessment
  3. Patching 🔧 - LLM-powered code remediation
  4. Validating ✓ - Native Python compilation verification
  5. 🔐 Sanitizing - Secret redaction & error handling
  6. Complete ✨ - Results delivery with refinement history

🚀 Quick Start

Prerequisites

  • Node.js 18+ (Frontend)
  • Python 3.11+ (Backend)
  • Google Gemini API Key (Get one here)

Installation & Setup

1️⃣ Backend Setup

# Navigate to backend directory
cd backend

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Set your Gemini API key
export GEMINI_API_KEY="your_api_key_here"  # Or set in frontend UI

# Run the FastAPI server
python app/main.py
# Server runs at http://127.0.0.1:8000

2️⃣ Frontend Setup

# Navigate to frontend directory
cd frontend

# Install dependencies
npm install

# Start development server
npm run dev
# UI runs at http://localhost:5173

3️⃣ Access the Application

Open your browser and navigate to:

http://localhost:5173

📋 Vulnerability Detection

GuardAgent detects the following security vulnerabilities:

Vulnerability Detection Method Remediation Strategy
SQL Injection AST pattern matching (string interpolation in queries) Parameterized queries
Command Injection Detection of os.system(), subprocess.run(shell=True) Subprocess with shell=False + arg list
Hardcoded Secrets String literal pattern recognition Environment variables
Eval/Exec Direct eval(), exec() calls Safe parsing alternatives
Insecure Deserialization pickle.loads(), yaml.load() detection Secure formats (JSON)

💻 Technology Stack

Frontend

  • React 19 - UI framework
  • TypeScript - Type-safe development
  • Tailwind CSS - Utility-first styling
  • Vite - Next-gen build tool
  • Lucide React - Icon library

Backend

  • FastAPI - High-performance async web framework
  • Python 3.11+ - Core language
  • LangGraph - Multi-agent orchestration
  • Google Genai SDK - LLM integration (Gemini 2.5-Flash)
  • AST Module - Static code analysis

DevOps & Tools

  • Oxlint - High-performance linter
  • Pydantic - Data validation
  • CORS Middleware - Cross-origin request handling with whitelisting
  • SSE (Server-Sent Events) - Real-time streaming
  • Logging - Comprehensive error tracking and audit trails

🎮 Usage Examples

Template 1: SQL Injection Remediation

# Original Vulnerable Code
import sqlite3
def get_user_records(db_path: str, username: str):
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    query = f"SELECT * FROM users WHERE username = '{username}'"
    cursor.execute(query)
    return cursor.fetchall()

GuardAgent Output:

  • ✅ Detects SQL injection vulnerability
  • 🔧 Patches with parameterized queries
  • ✓ Validates syntax and imports

Template 2: Command Injection Fix

# Original Vulnerable Code
import subprocess
def ping_server(ip_address: str):
    command = f"ping -c 1 {ip_address}"
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    return result.stdout

GuardAgent Output:

  • ✅ Identifies shell=True vulnerability
  • 🔧 Converts to safe subprocess list format
  • ✓ Removes shell execution risk

📁 Project Structure

Guard-Agent/
├── frontend/                      # React + TypeScript UI
│   ├── src/
│   │   ├── App.tsx               # Main application component
│   │   ├── components/
│   │   │   ├── Stepper.tsx       # Pipeline progress visualization
│   │   │   ├── CodeViewer.tsx    # Split code comparison view
│   │   │   ├── VulnerabilityList.tsx  # Vulnerability details
│   │   │   └── RefinementHistory.tsx  # Iteration tracking
│   │   └── App.css               # Skeuomorphic styling
│   ├── package.json
│   └── vite.config.ts
│
├── backend/                       # FastAPI + LangGraph
│   ├── app/
│   │   ├── main.py               # FastAPI server & /api/remediate endpoint
│   │   ├── ast_analyzer.py       # Vulnerability detection
│   │   ├── remediation_graph.py  # LangGraph multi-agent pipeline
│   │   └── security.py           # 🔐 Secret redaction & sanitization
│   ├── requirements.txt
│   ├── test_backend.py           # Unit tests
│   ├── test_security.py          # 🔒 Security module tests
│   └── test_adc.py
│
├── README.md                      # This file
└── .gitignore

🧪 Testing

Backend Unit Tests

cd backend
python test_backend.py

Expected Output:

--- Testing AST Security Scanning ---
Scanned and found 5 vulnerabilities:
[HIGH] eval_exec on line 19: Eval/Exec detected
[HIGH] sql_injection on line 23: SQL Injection
[HIGH] command_injection on line 26: Command Injection
...
AST Scanner Test: SUCCESS

--- Testing LangGraph Pipeline Compilation ---
LangGraph Compilation Test: SUCCESS

All backend checks passed successfully!

🔐 Security Module Tests

cd backend
python test_security.py

Expected Output:

--- Running Security Module Tests ---
1. test_sanitize_text_specific_secret: PASSED
2. test_sanitize_text_regex_patterns: PASSED
3. test_redact_secrets_recursive: PASSED

All Security Module Tests PASSED successfully!

🌐 API Reference

Health Check

GET /api/health

Response:

{
  "status": "healthy"
}

Remediate Code

POST /api/remediate
Content-Type: application/json

{
  "code": "import sqlite3\ndef get_user(username):\n    cursor.execute(f\"SELECT * FROM users WHERE name = '{username}'\")",
  "api_key": "your_gemini_api_key_here"  // Optional (uses env var if not provided)
}

Response: Server-Sent Events (streaming JSON)

{
  "original_code": "...",
  "patched_code": "...",
  "vulnerabilities": [...],
  "error_logs": [...],
  "iteration_count": 1,
  "pipeline_step": "analyzing",
  "history": [...],
  "compile_success": false,
  "error_message": null
}

⚙️ Configuration

Environment Variables

# Backend (.env or system variables)
GEMINI_API_KEY=your_api_key_here
CORS_ALLOWED_ORIGINS=http://localhost:5173

# Frontend (.env.local)
VITE_API_URL=http://127.0.0.1:8000

LLM Model Configuration

Edit backend/app/remediation_graph.py to change the model:

# Current default: gemini-2.5-flash
response = client.models.generate_content(
    model='gemini-2.5-flash',  # ← Change model here
    contents=prompt,
)

🔐 Security Architecture

GuardAgent implements enterprise-grade security controls to protect sensitive data:

🛡️ Core Security Features

1. Secret Redaction System

  • Pattern-Based Detection: Identifies API keys, tokens, and credentials using regex patterns
  • Supported Patterns:
    • Google Gemini API Keys (AIzaSy*)
    • Stripe API Keys (sk_test_*, sk_live_*)
    • Bearer Tokens and JWT-style credentials
  • Recursive Scanning: Redacts secrets from nested structures (dicts, lists, strings)
  • Context-Aware: Sanitizes both error messages and pipeline logs

2. CORS Security

  • Strict Whitelist: Only allows requests from localhost development ports
  • Production Ready: Configure allow_origins for production deployments
  • Credentials Disabled: Prevents cookie/session leakage

3. Error Handling & Logging

  • Global Exception Handler: Captures unhandled exceptions with automatic secret sanitization
  • Sanitized Tracebacks: All error logs have secrets redacted before logging
  • Audit Trail: Comprehensive logging for debugging without exposing credentials

4. Input Validation

  • Pydantic Models: All API requests validated against strict schemas
  • Type Safety: TypeScript on frontend, Python type hints on backend

5. No Code Execution

  • Safe Compilation Only: Uses Python's compile() function for syntax validation
  • No eval() or exec(): Prevents arbitrary code execution

🔒 Secret Management Best Practices

# ❌ DON'T: Hardcode API keys
GEMINI_API_KEY = "AIzaSy..."

# ✅ DO: Use environment variables
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")

# ✅ DO: Use frontend UI for temporary keys
# Keys are automatically sanitized before logging

📊 Security Test Suite

All security features are tested in backend/test_security.py:

# Test specific secret redaction
python -m pytest backend/test_security.py::test_sanitize_text_specific_secret -v

# Test regex pattern detection
python -m pytest backend/test_security.py::test_sanitize_text_regex_patterns -v

# Test recursive redaction
python -m pytest backend/test_security.py::test_redact_secrets_recursive -v

🤝 Contributing

Contributions are welcome! Areas for enhancement:

  • Support for JavaScript/TypeScript vulnerability detection
  • Extended vulnerability taxonomy
  • Custom LLM model integration
  • Persistent result storage & database
  • GitHub Actions integration
  • Performance benchmarking suite
  • Docker containerization
  • Advanced encryption for secret storage

📊 Performance Insights

Metric Typical Value
AST Analysis Time 50-100ms
LLM Patching Time 2-5s (Gemini 2.5-Flash)
Compilation Validation 10-50ms
Secret Redaction Overhead <5ms
Max Refinement Iterations 3
Average Total Runtime 3-8s

🎨 UI Features

  • Skeuomorphic Design - macOS/Windows-inspired aesthetic
  • Real-Time Pipeline Visualization - Step-by-step progress tracking
  • Split Code View - Side-by-side original vs. patched code
  • Vulnerability Highlighting - Interactive vulnerability markers
  • Refinement History - Detailed iteration tracking with error logs
  • Responsive Layout - Optimized for desktop viewing
  • Secure Error Display - Sanitized error messages without sensitive data leakage

📝 License

This project is open source and available for educational and research purposes.


🚀 Roadmap

  • v1.1 - Multi-language support (JavaScript, Java, Go)
  • v1.2 - Docker deployment with docker-compose
  • v1.3 - GitHub integration & CI/CD plugins
  • v1.4 - 🔐 Advanced secret vaulting & encryption
  • v2.0 - Advanced dataflow analysis & taint tracking

💡 Tips & Tricks

  1. Demo Mode: Leave API key blank to see hardcoded examples
  2. Custom Code: Edit the Python code textarea directly
  3. Error Analysis: Check the "Instrumentation" panel for refinement details
  4. Syntax Learning: Each iteration shows compiler feedback
  5. Security First: All API keys are automatically redacted from logs and error messages

📞 Support

For issues, questions, or security concerns, please:

  • Open an issue on GitHub
  • Contact the maintainers
  • Report security vulnerabilities responsibly

🔐 Security Disclosure

If you discover a security vulnerability, please email the maintainers directly instead of using the issue tracker. We take security seriously and will respond promptly.


Made with ❤️ by Aadhitya S

⭐ Star us on GitHub | 🐛 Report Issues | 🤝 Contribute

⬆ Back to Top

About

GuardAgent: An intelligent security remediation platform that automatically detects and fixes Python vulnerabilities using multi-agent architecture, AST analysis, and Google Gemini AI. Features real-time streaming UI, self-healing compilation loops, and comprehensive security patching.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors