结构化安全审计:在LLM生成代码的正确性与内容安全之间平衡
Think Before You Code: Dual Reasoning for the NLSafety-Utility Trade-Off in LLM Code Generation
AI总结:
本文提出NLSafety-Utility Duality Score和Dual Reasoning,通过结构化安全审计和任务导向的代码审查,提升LLM生成代码的安全性和正确性,实验显示其在多个模型上显著提升SUDS评分。
摘要:
大型语言模型(LLM)在代码生成中通常仅评估功能性正确性,忽视生成代码是否传播嵌入在提示中的有害内容。现有方法侧重于检测有害内容,忽略了功能性正确性。基于双重通道约束理论,本文提出NLSafety-Utility Duality Score(SUDS)和Dual Reasoning(DR)。SUDS将代码的实用性、安全性和警告意识统一为单一评分,DR是一种结构化推理技术,要求在代码生成前进行显式的安全审计和任务导向的代码审查。在五个LLM上评估两个包含有害关键词注入的基准测试(820和2135样本),DR在所有模型中均获得最高SUDS评分,平均SUDS提升1.32至3.42倍。链式推理提示获得的安全提升微不足道,安全意识提示仅提供部分改进。进一步分析显示,DR的效果随模型容量增加而增加,单次示例主要稳定较小模型的输出格式,结构化推理无法弥补安全词汇有限的模型。
英文摘要:
Large language models (LLMs) for code generation are typically evaluated on functional correctness alone, overlooking whether generated code propagates harmful content embedded in the prompt. Prior work has shown that most Code LLMs reproduce offensive identifiers from injected renaming instructions without warning, yet existing approaches focus on detecting harmful content, neglecting functional correctness. Grounded in the Theory of Dual Channel Constraints (which states that code is a dual-channel medium combining an algorithmic (AL) channel for machine execution and a natural language (NL) channel for human communication, creating a unique safety-utility trade-off where a model must balance functional execution with responsible communication), we propose NLSafety-Utility Duality Score (SUDS), a metric that unifies code utility, safety adherence, and warning awareness into a single score across 12 ranked response scenarios, and Dual Reasoning (DR), a structured inference-time technique that requires an explicit safety audit and task-grounded code review before code generation. Evaluated on six LLMs across two benchmarks augmented with harmful keyword injections (820 and 2,135 samples), DR consistently achieves the highest SUDS across all models, improving mean SUDS by 1.32$\times$ to 3.42$\times$ over the baseline, while chain-of-thought prompting yields negligible safety gains and a safety-aware prompt provides only partial improvement. Further analysis reveals that DR's effectiveness scales with model capacity, that the one-shot exemplar primarily stabilizes output format for smaller models, and that structured reasoning cannot compensate for models with limited safety vocabularies.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。