Insecure Coding Preferences in Long-Term Memory: Security Risks for LLM-based Code Generation

๐Ÿ“… 2026-07-20
๐Ÿ“ˆ Citations: 0
โœจ Influential: 0
๐Ÿ“„ PDF
๐Ÿค– AI Summary
This study demonstrates that incorporating long-term memory into large language models significantly amplifies the risk of generating vulnerable codeโ€”by 2.7 to 50.3 percentage pointsโ€”due to the persistent retention of unsafe coding preferences. The work provides the first empirical evidence of the persistence and cross-task generalization of such preferences and introduces novel mitigation strategies, including memory-layer security filtering. Evaluations span four major models (ChatGPT, Gemini, Qwen, and Grok) across five programming languages (Python, C, C++, Go, and JavaScript), assessing techniques such as security-aware prompt augmentation, memory control, and security filtering. Results show that the proposed memory-level security filter achieves 100% detection of risky memory entries and fully restores model safety to the baseline level observed without long-term memory.
๐Ÿ“ Abstract
LLM-based systems increasingly incorporate long-term memory to improve cross-session continuity. However, once insecure coding preferences are stored, they may silently influence security-critical decisions in subsequent generations. In this study, we conduct the first systematic empirical study on the impact of insecure coding preferences stored in long-term memory on the security of LLM-based code generation. We evaluate four LLMs (ChatGPT, Gemini, Qwen, and Grok) across five programming languages (Python, C, C++, Go, and JavaScript). Our results show that insecure memories significantly increase the risk of generating vulnerable code by 2.7-50.3 percentage points (pp). Moreover, they create a 5.4-14.0 percentage-point risk-warning gap, where warning-rate increases lag behind vulnerability-rate increases. Further analysis reveals that insecure memories are difficult to overwrite through normal interactions and can broadly influence model outputs even when prompts are phrased differently. Finally, we evaluate three mitigation strategies: security-requirement appending and memory storage reduce vulnerability rates by 19.7-33.6 pp but may degrade functional correctness by up to 15.9 pp; memory-level safety filtering achieves a 100\% detection rate on our evaluated risky memory entries and restores generation behavior to the without-memory baseline. Based on these findings, we provide actionable suggestions to improve the security of long-term memory in LLM-based code generation.
Problem

Research questions and friction points this paper is trying to address.

insecure coding preferences
long-term memory
LLM-based code generation
security risks
vulnerable code
Innovation

Methods, ideas, or system contributions that make the work stand out.

long-term memory
insecure coding preferences
LLM-based code generation
security risks
memory-level safety filtering
๐Ÿ”Ž Similar Papers