Understanding Secret Leakage Risks in Code LLMs: A Tokenization Perspective

Meifang Chen, Zhe Yang, Huang Nianchen, Yizhan Huang, Yichen Li, Michael R. Lyu

TL;DR

This paper reveals a new security vulnerability in Code Large Language Models (LLMs) called "gibberish bias," where the way secrets are tokenized makes them surprisingly easy for LLMs to memorize and leak. The authors demonstrate that standard tokenization methods, particularly Byte-Pair Encoding (BPE), can transform high-entropy secrets into low-entropy token sequences, counterintuitively increasing memorization risk.

What Problem Does This Paper Address?

The rapid adoption of AI code assistants powered by Code Large Language Models (CLLMs) has revolutionized software development. However, a significant concern is their tendency to memorize and inadvertently leak sensitive information, including code secrets like API keys and passwords, which are often present in training data. While previous research has explored factors like prompting and training data, this paper focuses on an under-explored aspect: the role of tokenization in secret leakage. The core problem is understanding why certain secrets, despite appearing random and high-entropy at the character level, are easily memorized by LLMs.

Methods

The paper introduces the concept of "gibberish bias" to explain this phenomenon. The authors hypothesize that the Byte-Pair Encoding (BPE) tokenization process, commonly used in LLMs, is the culprit. BPE works by merging frequent character pairs to create subword tokens. The paper's methodology involves:

  • Case Studies: Analyzing how specific LLMs (e.g., Deepseek Coder, Qwen2.5-Coder) tokenize both normal code and secret strings, highlighting differences in token granularity and distribution.
  • Entropy Analysis: Quantifying the character-level and token-level entropy of secrets. They leverage the "Entropy-Memorization Law," which suggests higher token-level entropy correlates with lower memorization.
  • Distribution Shift Analysis: Comparing the token distribution of secret data with that of typical code training data to identify discrepancies.
  • Tokenizer Experiments: Building and evaluating custom tokenizers with varying vocabulary sizes to understand the impact of "larger vocabulary" trends on gibberish bias.

Datasets

  • Secret Dataset: The paper utilizes a dataset of code secrets, likely sampled from public code repositories, for analysis. The authors mention identifying 102 "gibberish-like secrets" through manual labeling from a larger set of 847 instances within a "zero-distance set" (where memorization score is zero).
  • General Code LLM Training Data: The paper uses a subsample of the Stack V2 dataset (the training corpus for StarCoder2) as a representative example of typical code LLM training data.
  • OLMO-1B Training Corpus (Dolma): The experiments for entropy-memorization law analysis were conducted using sequences sampled from the Dolma dataset, the training corpus for the OLMO-1B model.

Key Insights and Ideas

  • Gibberish Bias: The most significant insight is the identification of "gibberish bias." This bias arises because BPE tokenization can transform high-entropy, seemingly random secret strings (high character-level entropy) into low-entropy token sequences (low token-level entropy). This counter-intuitive outcome makes these secrets more susceptible to memorization by LLMs, despite their apparent randomness to humans.
  • Token-Level vs. Character-Level Entropy: The paper emphasizes the critical distinction between how humans perceive entropy (character-level) and how LLMs process information (token-level). What appears "gibberish" and high-entropy to humans might become predictable and low-entropy after tokenization.
  • Distribution Shift is Key: The root cause of gibberish bias is identified as a significant distribution shift between the token distributions found in typical code LLM training data and the token distributions of secret strings. Secrets often contain unique character combinations that are rare or absent in general code, leading to BPE creating less efficient token representations for them.
  • Larger Vocabularies Exacerbate Bias: The study suggests that the trend towards larger tokenizer vocabularies, while potentially beneficial for general code, may further amplify gibberish bias, making secrets even more vulnerable.

Key Results

  • High Character Entropy, Low Token Entropy: Secrets identified as "gibberish-like" exhibit high character-level entropy (e.g., 6.086 bits) but significantly lower token-level entropy (e.g., 8.804 bits) compared to non-secrets (4.744 bits character-level, 11.175 bits token-level).
  • Increased Memorization Risk: The lower token-level entropy of secrets, due to gibberish bias, implies a higher chance of memorization according to the Entropy-Memorization Law.
  • BPE's Sensitivity to Distribution Shift: The paper demonstrates that BPE's tokenization strategy is sensitive to distribution shifts. When comparing secret data to general code data, a significant KL divergence (2.668) was observed, indicating a substantial difference in token distributions.
  • Tokenizer Impact: Experiments with different tokenizer sizes showed that as vocabulary size increases, the gibberish bias tends to become more pronounced.

Limitations and Future Work

  • Limitations:
    • The study focuses on BPE, the most popular tokenization strategy, but other tokenization methods might exhibit different behaviors.
  • The manual labeling of "gibberish-like" secrets introduces a subjective element.
  • The research is primarily empirical, and a deeper theoretical understanding of why BPE creates these specific tokenization patterns for secrets could be beneficial.
  • Future Work:
    • Mitigation Strategies: The paper proposes a promising mitigation strategy: character-wise tokenization for code secrets. This involves forcing tokenizers to process secrets at the character level, aligning with human perception and potentially eliminating gibberish bias. This approach draws inspiration from digit-wise tokenization used in some LLMs for arithmetic tasks.
  • Tokenizer Design: The authors advocate for more flexible tokenizer designs that can adapt to different downstream tasks or data distributions without requiring full model retraining. They suggest exploring tokenizer adaptation methods.
  • Adversarial Exploitation: The paper poses open questions about how adversaries can proactively and effectively exploit CLLMs to extract secrets, suggesting further research into red teaming strategies.
  • Broader Implications: The findings have broader implications for the design of tokenizers in general, highlighting the need to consider their impact on sensitive data handling.

Summary updated

BibTeX
@inproceedings{chen2025understanding,
  author = {Meifang Chen and Zhe YANG and HUANG Nianchen and Yizhan Huang and Yichen LI and Michael R. Lyu},
  title = {{Understanding Secret Leakage Risks in Code LLMs: A Tokenization Perspective}},
  booktitle = {DL4C @ NeurIPS},
  year = {2025},
  url = {https://openreview.net/forum?id=c3OYeGH1Zj},
}
Figure