AutoSafeCoder: A Multi-Agent Framework for Securing LLM Code Generation through Static Analysis and Fuzz Testing

Ana Nunez, Nafis Tanveer Islam, Sumit Kumar Jha, Peyman NajafiradarXiv 2024 · DOI

Abstract

Large Language Models (LLMs) have revolutionized coding productivity but often produce code with significant security vulnerabilities. We present AutoSafeCoder, a multi-agent framework that coordinates a Coding Agent, a Static Analyzer, and a Fuzzing Agent to iteratively secure generated code. Our evaluation shows that AutoSafeCoder reduces code vulnerabilities by 13% compared to a GPT-4o baseline while maintaining high functional accuracy.

Introduction

While LLMs like GPT-4o are highly capable of generating functionally correct code, they often suffer from "security-blindness." Studies suggest that LLM-assisted coding can increase the occurrence of vulnerabilities by 10%, with some models producing insecure code in up to 40% of cases. The primary challenge is that most coding assistants focus on functional correctness rather than security robustness. AutoSafeCoder addresses this by introducing a collaborative pipeline that subjects generated code to rigorous static auditing and dynamic stress-testing.

Methodology

AutoSafeCoder employs a three-tier iterative architecture designed to mimic a professional security review process.

The AutoSafeCoder multi-agent pipeline, featuring iterative loops between coding, static analysis, and fuzzing agents.
  1. Coding Agent: Generates initial code and acts as a repair station, refining scripts based on feedback from other agents.
  2. Static Analyzer Agent: Audits source code against the MITRE CWE database to identify patterns of known weaknesses and provide remediation suggestions.
  3. Fuzzing Agent: Performs dynamic analysis by generating seed inputs and applying a type-aware mutation algorithm to identify runtime crashes.

The agents pass the code back and forth for up to four iterations until the Static Analyzer deems it secure and the Fuzzer cannot induce a crash.

Experimental Results

The framework was evaluated using SecurityEval (121 Python prompts) for safety and HumanEval for functional correctness.

Comparative performance of AutoSafeCoder versus GPT-4o across security and functionality metrics.

Security Improvements

AutoSafeCoder demonstrated a significant reduction in vulnerabilities. While standard GPT-4o generated vulnerable code in 49% of samples, AutoSafeCoder reduced this to 36%, representing a 13% absolute improvement. The iterative feedback loop was highly effective, with the Static Analyzer successfully correcting vulnerabilities in 53% of cases within 1 to 4 rounds.

Functional Stability

A key finding is that security enhancements did not significantly compromise utility. AutoSafeCoder maintained a Pass@1 score of 0.8780 on HumanEval, compared to the baseline of 0.9085. This negligible 3% drop suggests that multi-agent security auditing is a viable path for production-ready code generation.

Synergy of Analysis Techniques

The Fuzzing Agent identified and helped fix 5 specific runtime-crashing samples that static analysis missed. This highlights the importance of dynamic testing in catching logic errors that only surface during execution.

Discussion and Limitations

Despite its success, certain vulnerabilities like CWE-94 (Code Injection) remained difficult to resolve, appearing in 25 of the 44 remaining vulnerable samples. Future work will focus on improving sandbox environments for the Fuzzing Agent and integrating a dedicated Functional Testing Agent to further verify that security fixes align with user intent.

Summary updated

BibTeX
@misc{nunez2024autosafecoder,
  author = {Ana Nunez and Nafis Tanveer Islam and Sumit Kumar Jha and Peyman Najafirad},
  title = {{AutoSafeCoder: A Multi-Agent Framework for Securing LLM Code Generation through Static Analysis and Fuzz Testing}},
  year = {2024},
  eprint = {2409.10737},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.10737},
}
Figure