基于LLM的代码生成的安全设计:利用内部表征的概念驱动引导机制 Security-by-Design for LLM-Based Code Generation: Leveraging Internal Representations for Concept-Driven Steering Mechanisms 预印本

⚠️ 本文为 预印本(arXiv 公开版本),尚未经过正式同行评审。作者自发公开,结论未经期刊审稿验证,阅读时请知悉。
发布时间:2026-03-11
👁️ 预览文献
💡 AI 中文简介
大语言模型写代码时经常能写出功能正确但不安全的代码。现有方法靠经验修补,效果有限。本研究深入分析了CodeLLM的内部表征,发现模型其实知道漏洞的存在,还能区分不同的安全子概念。基于此,作者提出SCS-Code方法,在生成每个词时把模型的内部状态往安全方向引导,从而输出既安全又功能正确的代码。该方法轻量、模块化,在多个安全编码基准上超越了现有最优方法。
领域自然语言处理 / 代码生成与安全
适合谁读需要一定专业背景(了解LLM和代码生成基础)
核心贡献1. 首次系统揭示CodeLLM内部对安全概念的表征,证明模型在生成不安全代码时其实“知道”漏洞存在,且能区分安全子概念。2. 提出SCS-Code,一种轻量、模块化的内部表征引导机制,可无缝集成到现有代码模型,显著提升生成代码的安全性和功能正确性。
关键数据在多个安全编码基准上,SCS-Code的性能优于现有最先进方法,具体指标未在摘要中给出。
推荐理由如果你关心AI写代码的安全问题,这篇论文从模型内部找原因,提出了一种即插即用的引导方法,效果比现有方案更好,值得一读。
关键词["代码生成", "大语言模型安全", "内部表征", "概念引导", "安全设计", "漏洞检测"]

原文摘要

查看英文原文摘要

Large Language Models (LLMs) show remarkable capabilities in understanding natural language and generating complex code. However, as practitioners adopt CodeLLMs for increasingly critical development tasks, research reveals that these models frequently generate functionally correct yet insecure code, posing significant security risks. While multiple approaches have been proposed to improve security in AI-based code generation, combined benchmarks show these methods remain insufficient for practical use, achieving only limited improvements in both functional correctness and security. This stems from a fundamental gap in understanding the internal mechanisms of code generation and the root causes of security vulnerabilities, forcing researchers to rely on heuristics and empirical observations. In this work, we investigate the internal representation of security concepts in CodeLLMs, revealing that models are often aware of vulnerabilities as they generate insecure code. Through systematic evaluation, we demonstrate that CodeLLMs can distinguish between security subconcepts, enabling a more fine-grained analysis than prior black-box approaches. Leveraging these insights, we propose Secure Concept Steering for CodeLLMs (SCS-Code). During token generation, SCS-Code steers LLMs' internal representations toward secure and functional code output, enabling a lightweight and modular mechanism that can be integrated into existing code models. Our approach achieves superior performance compared to state-of-the-art methods across multiple secure coding benchmarks.