Fortifying LLM-Based Code Generation with Graph-Based Reasoning on Secure Coding Practices

Rupam Patir, Keyan Guo, Haipeng Cai, Hongxin HuarXiv 2025 · DOI

Introduction

Large Language Models (LLMs) have revolutionized software development, yet their propensity to generate insecure code remains a significant hurdle for production deployment. Research indicates that even state-of-the-art models like GPT-4o and Claude generate vulnerable code in a substantial percentage of cases. Current mitigation strategies, such as fine-tuning on secure datasets or using external static analysis feedback, often suffer from high computational costs, lack of interpretability, or inability to generalize to "zero-day" vulnerabilities.

This paper introduces GRASP (Graph-based Reasoning over Secure coding Practices), a model-agnostic framework designed to bridge the "theory-practice gap" in LLM security. GRASP treats security not as a pattern-matching exercise but as a structured logical traversal of established security principles.

The GRASP Framework

The core innovation of GRASP is the use of a Directed Acyclic Graph (DAG) of Secure Coding Practices (SCPs). The authors curated 28 foundational practices from industry standards like OWASP and CERT, organizing them based on specificity and sequential dependencies.

The GRASP workflow: from SCP graph creation to the iterative graph reasoning process.

The GRASP pipeline operates in three distinct stages:

  1. Seed Generation: The LLM produces an initial functional solution for a given programming task.
  2. Graph Traversal & SCP Enforcement: The system systematically walks the LLM through the SCP graph. For each node, the model determines the relevance of a specific practice. If relevant, the model refines the code to incorporate the security measure.
  3. Functional Verification: A final verification step ensures that security enhancements have not compromised the original logic or functionality of the code.

Experimental Evaluation

To evaluate GRASP, the researchers developed a new benchmark consisting of 54 natural language prompts across Python and C, covering 17 Common Weakness Enumerations (CWEs). Unlike previous benchmarks, every prompt includes unit tests to verify functional correctness alongside security. Furthermore, the framework was tested against 24 real-world CVEs disclosed after the models' training cutoffs to assess performance on zero-day vulnerabilities.

Key Results and Comparative Advantages

GRASP demonstrates significant improvements in security across both proprietary and open-source models, including Claude-3-Haiku, GPT-4o-mini, Gemini-1.5-Flash, and Llama-3-8B. The framework consistently achieves security rates exceeding 80% and shows up to an 88% improvement over baselines like PromSec for zero-day vulnerabilities.

Comparison of GRASP against existing security-focused code generation frameworks.

As shown in the comparison above, GRASP offers several unique advantages over existing methods like SVEN or SafeCoder:

  • Model Agnostic: It works with "black-box" closed-source LLMs without requiring internal model access.
  • Zero Retraining: It eliminates the need for expensive fine-tuning or specialized vulnerability datasets.
  • Interpretability: The graph-based traversal provides a clear "reasoning path," making the security decisions transparent and auditable.
  • Efficiency: By leveraging graph dependencies, the system can skip irrelevant security checks, maintaining a lower computational cost than feedback-heavy alternatives.

Conclusion

GRASP represents a shift toward reasoning-centric security in AI-assisted coding. By activating the latent security knowledge already present in LLMs through structured graphs, the framework achieves high security and functional reliability without the overhead of traditional retraining. Future work aims to integrate "Graph RAG" techniques to automatically expand the SCP graph from evolving security documentation, further enhancing the system's scalability and depth.

Summary updated

BibTeX
@misc{patir2025fortifying,
  author = {Rupam Patir and Keyan Guo and Haipeng Cai and Hongxin Hu},
  title = {{Fortifying LLM-Based Code Generation with Graph-Based Reasoning on Secure Coding Practices}},
  year = {2025},
  eprint = {2510.09682},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.09682},
}
Figure