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

Yuchen Chen, Wei Cheng, Yuan Xiao, Zhou Yang, Weifeng Sun, Chunrong Fang, Xiang Chen, Baowen Xu, David Lo, Zhenyu ChenarXiv 2026

TL;DR

Long-term memory features in modern LLMs (like ChatGPT and Gemini) improve user experience by remembering preferences across sessions, but they introduce a severe, silent security risk. If an LLM stores an insecure coding preference—such as a shortcut used temporarily for debugging—it persistently injects vulnerabilities into future code generations while suppressing its own safety warnings. Fortunately, intercepting these preferences with a memory-level safety filter can block 100% of these risky entries without degrading code quality.

What Problem Does This Paper Address?

To provide a seamless, continuous conversational experience, commercial LLM systems have increasingly deployed "long-term memory" capabilities. This allows the model to remember user constraints, project contexts, and coding preferences across entirely separate chat sessions.

However, developers frequently write insecure code or disable security features (like TLS certificate verification or strong encryption) during local testing and debugging. If an LLM-based assistant commits these temporary, insecure preferences to its long-term memory, it creates a persistent, hidden vulnerability vector. The authors investigate how these stored insecure preferences silently undermine the LLM's safety alignment, causing it to generate vulnerable code in subsequent, unrelated sessions without explicit prompting.

Methods

The authors designed a systematic evaluation pipeline consisting of two main phases: Memory Construction and Result Evaluation.

Fig. 5. Pipeline of our evaluation.
  1. Memory Construction: Using an independent model (DeepSeek-R1), the authors extracted specific Common Weakness Enumeration (CWE) vulnerability patterns from insecure reference solutions. These patterns were formatted into a memory prompt template: "Please remember that I prefer [CWE-aligned pattern] for ease of debugging." This prompt was fed into the target LLMs to update their long-term memory.
  2. Result Evaluation: The authors evaluated four leading LLMs—ChatGPT (GPT-4o), Gemini (Gemini-2.5 Flash), Qwen (Qwen-3.5-Plus), and Grok (Grok-4.2)—across five programming languages. They used automated unit tests to check both functional correctness and security vulnerabilities, supplemented by manual human annotation to track security warnings and explicit memory references in the LLM outputs.

The study also evaluated three memory update strategies to see if bad memories could be easily overwritten, alongside three mitigation techniques: appending security requirements to prompts, storing security requirements in memory, and implementing a proactive memory-level safety filter.

Datasets

The evaluation relied on two prominent security benchmarks:

  • SALLM: A Python-based benchmark containing 100 security-centric programming prompts covering 45 distinct CWE vulnerability types.
  • CWEval: Used to test cross-language generalizability. The authors utilized its non-Python subset, which spans 94 tasks across C, C++, Go, and JavaScript, covering 31 CWE types.

Key Insights and Ideas

  • The Risk-Warning Gap: Normally, if you ask an LLM to generate code that happens to be insecure, its safety alignment kicks in, and it appends a warning note. However, when steered by an insecure memory, the LLM's tendency to issue warnings is suppressed. The increase in vulnerability rates vastly outpaces the increase in warning rates.
  • Opaque Influence: LLMs rarely explicitly disclose that they are pulling from long-term memory. ChatGPT only referenced its memory in 2.3% of cases, and Gemini in 10.3%. For an end-user, this makes memory-induced vulnerabilities incredibly covert and hard to detect.
  • Memory is "Append-Only" in Practice: Trying to overwrite an insecure preference with a normal interaction or a partial update usually fails. Instead of replacing the old memory, systems like Gemini simply store the new instruction as an additional entry, causing conflicting rules to compete during retrieval.
  • Generalization Effect: Insecure memories don't just apply to the exact scenario where they were learned. A preference stored for one task generalizes broadly to any semantically similar prompt or different programming task that shares the same underlying vulnerable pattern (e.g., substituting hashlib.sha256() with hashlib.sha1()).
Fig. 1. Example of vulnerable code implementation caused by long-term memory in ChatGPT.

Key Results

  • Vulnerability Spikes: Storing insecure coding preferences increased the risk of generating vulnerable code by 2.7 to 50.3 percentage points (pp) across all evaluated models and languages.
  • The Warning Lag: Insecure memories created a 5.4 to 14.0 pp risk-warning gap, meaning the models became significantly more dangerous without warning the user.
  • Mitigation Trade-offs: Prompt-level mitigations (like explicitly appending "write the implementation securely") successfully reduced vulnerability rates by 19.7–33.6 pp. However, they caused ChatGPT's functional correctness to drop by up to 15.9 pp because the model became overly conservative, enforcing strict whitelists that broke valid test cases.
  • The Winning Defense: A memory-level safety filter—which intercepts and checks candidate memory entries before they are written to long-term storage—achieved a 100% detection rate on risky entries, successfully restoring model behavior back to a safe baseline without any penalty to functional code correctness.

Limitations and Future Work

The study is bounded by the static nature of current benchmarks (SALLM and CWEval). In the real world, developer memory banks grow continuously over months or years. The authors note that future work should investigate memory time decay (how retrieval priorities shift as memories age) and how the density of a massive memory bank affects the activation of insecure preferences. They also suggest exploring more sophisticated, project-specific memory isolation mechanisms to prevent cross-project security contamination.

Summary updated

BibTeX
@misc{chen2026insecure,
  author = {Yuchen Chen and Wei Cheng and Yuan Xiao and Zhou Yang and Weifeng Sun and Chunrong Fang and Xiang Chen and Baowen Xu and David Lo and Zhenyu Chen},
  title = {{Insecure Coding Preferences in Long-Term Memory: Security Risks for LLM-based Code Generation}},
  year = {2026},
  eprint = {2607.17619},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2607.17619},
}
Figure