Introduction
Large Language Models (LLMs) like GPT-4 and GitHub Copilot have significantly boosted developer productivity. However, they often inherit a "security debt" from their training data—massive repositories of open-source code that frequently contain bugs and vulnerabilities. Consequently, LLM-powered tools often generate code with well-known Common Weakness Enumerations (CWEs).
SGCode is a flexible, modular system designed to bridge this gap. It provides a unified framework that automatically detects vulnerabilities in LLM-generated code and optimizes prompts to mitigate them. Beyond simple fixing, SGCode offers a user-friendly interface to analyze the critical trade-offs between security, performance, and code utility.
System Architecture
SGCode is designed as an API-accessible service with a modular architecture, enabling researchers to evaluate different prompt-optimization techniques against real-world metrics.
The system consists of two primary components:
- Back-end Services (FastAPI): This orchestrator integrates security analysis tools (e.g., Bandit, CodeQL), optimization modules (e.g., PromSec’s graph-based GAN), and various LLMs via OpenAI-compatible APIs.
- User Front-end (Streamlit): This includes a chat interface for prompting and a comprehensive report analysis interface.
User Interface and Analysis
The "Report Analysis Interface" is a core feature of SGCode, providing developers with a side-by-side comparison of original vulnerable code and its secured counterpart.
This interface allows teams to review "fixed" code line-by-line, understanding exactly how prompt optimization mitigated risks like SQL injection or hardcoded passwords. The system successfully generates shareable security reports via MongoDB, facilitating collaborative review.
Key Insights and Performance
Evaluation using datasets from PromSec research revealed several critical insights regarding the system's performance and the nature of secure code generation:
- LLM Latency: Quantitative analysis shows that security scanning and optimization (gGAN) are extremely fast, taking only milliseconds. However, the LLM's token generation accounts for over 95% of total system latency.
- The Utility Challenge: A significant finding is the "Functionality Deviation" trap. When using standalone optimization with commercial LLMs, approximately 98% of the generated code deviated from its intended function, highlighting the difficulty of maintaining utility while ensuring security.
- Efficiency: SGCode is remarkably lightweight. On a modest AWS instance (2 vCPUs, 4GB RAM), the system added negligible overhead, with CPU usage at 0.06% and memory usage around 2.1 GB.
Conclusion and Future Work
SGCode successfully demonstrates an end-to-end pipeline for secure code generation. However, the high rate of functionality deviation remains a primary hurdle. Future development will focus on incorporating automated "utility tests" to verify that secured code remains functional. Additionally, the team aims to scale the system for multi-file projects and allow users to define custom security rules tailored to specific corporate standards.
Summary updated