Demo: SGCode: A Flexible Prompt-Optimizing System for Secure Generation of Code

Khiem Ton, Nhi Nguyen, Mahmoud Nazzal, Abdallah Khreishah, Cristian Borcea, NhatHai Phan, Ruoming Jin, Issa Khalil, Yelong Shen · DOI

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 modular architecture of SGCode, showing the interaction between back-end services and the front-end interface.

The system consists of two primary components:

  1. 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.
  2. 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.

The SGCode Report Analysis interface, displaying vulnerability counts, severity levels, and a side-by-side code difference view.

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

BibTeX
@inproceedings{ton2024demo,
  author = {Khiem Ton and Nhi Nguyen and Mahmoud Nazzal and Abdallah Khreishah and Cristian Borcea and NhatHai Phan and Ruoming Jin and Issa Khalil and Yelong Shen},
  title = {{Demo: SGCode: A Flexible Prompt-Optimizing System for Secure Generation of Code}},
  booktitle = {CCS},
  year = {2024},
  doi = {10.1145/3658644.3691367},
}
Figure