Vulnerable Code Search: Transferable Attack for Code Language Models

Kaicheng Wang, Liyan Huang, Jesse Thomason, Weihang WangarXiv 2026

TL;DR

Code language models (CLMs) used for code search are highly vulnerable to adversarial attacks that rename variables and function identifiers without changing code functionality. By optimizing attacks on a small, open-source model (CodeT5+), researchers successfully deceived massive, closed-source models like Voyage-code-3 and LLMs like Gemini-3.1-Pro, causing retrieval performance (MRR) to plummet by up to 77%. This reveals that modern code search engines still rely heavily on superficial lexical features rather than a deep semantic understanding of code.

What Problem Does This Paper Address?

Modern software development relies heavily on embedding-based code search to find and reuse code snippets from massive repositories. These systems map natural language queries and code snippets into a shared latent space, ranking results based on vector similarity.

However, this paper exposes a critical vulnerability: code embedding models are overly sensitive to non-functional textual elements, such as variable and function names (identifiers). An adversary can exploit this by performing a "corpus poisoning" attack—injecting irrelevant or malicious code snippets into a codebase with strategically renamed identifiers. Because the code's execution logic remains completely unchanged, it easily bypasses standard syntax checks. Yet, it tricks the search engine into ranking the irrelevant code at the top of the results for specific user queries, crowding out legitimate code and potentially leading developers to adopt insecure or broken software.

Figure 1: Overview of our attack for code search. Given the target query “fastest way to load data”, the attack replaces identifiers in an unrelated code snippet while preserving functionality to deceive code search models. All attack variants are optimized on CodeT5+, but the resulting adversarial code snippets transfer beyond the surrogate model, also increasing similarity on the larger Nomic-embed-code model.

Methods

The authors introduce a programming language-agnostic, transferable adversarial attack framework. The attack operates under a practical black-box threat model where the attacker doesn't have access to the target model's weights. Instead, they use a small, white-box surrogate model to craft the attack, which then successfully transfers to the target model.

The framework relies on three main pillars:

  1. Gradient-Guided Optimization: Using white-box access to the surrogate model, the attack computes the gradient of the similarity score with respect to the input token embeddings. It uses a first-order Taylor expansion to greedily select identifier replacements that maximize the similarity between the irrelevant code and the target query.
  2. Identifier Style Constraints: Naive gradient attacks often produce long, gibberish variable names that are easily spotted by humans or LLMs. The authors enforce style-consistency constraints (preserving conventions like camelCase or snake_case) to ensure the modified code looks natural and plausible.
  3. Query Token Similarity: To exploit the lexical bias of code search models, the objective function is augmented to favor replacement tokens that share explicit textual or semantic overlap with the target query tokens.

Datasets

The attack was evaluated across multiple programming languages using several prominent datasets:

  • CosQA: A dataset of 20,000 web queries paired with Python code snippets.
  • CLARC: A benchmark specifically designed for evaluating robust code search in C/C++.
  • RepoQA: Used to evaluate the attack's transferability to long-context LLM-based repository retrieval across Python, C++, Java, Rust, and TypeScript.
  • HumanEval-X & CodeSearchNet: Used to validate cross-language stability and large-scale generalizability.

Key Insights and Ideas

  • High Benchmark Scores Mask Fragility: Models that achieve >90% top-5 recall on standard benchmarks are not necessarily understanding code semantics. They are heavily relying on brittle lexical features, making them easy to trick.
  • Surrogate-to-Target Transferability is Robust: Attacks optimized on a tiny 110M parameter model (CodeT5+) transfer remarkably well to models 60 times larger (Nomic-embed-code) and proprietary commercial APIs (Voyage-code-3). Tokenizer and architecture differences do not stop the attack.
  • Attacking Intent, Not Just Strings: Adversarial code snippets optimized for a specific query remain highly effective against paraphrased versions of that query. The attack successfully targets the underlying semantic intent rather than just overfitting to specific keywords.
  • The Defense Dilemma: Standard adversarial finetuning fails. Training a model only on adversarial examples severely degrades its baseline retrieval performance (cutting it in half). Conversely, mixing clean and adversarial data preserves baseline utility but leaves the model entirely vulnerable to adaptive white-box attacks.

Key Results

The authors demonstrated catastrophic performance drops across all tested evaluation models when only 10% of the corpus was corrupted with adversarial snippets:

Table 3: Code search degradation under a 10% adversar- ial attack on CosQA using CodeT5+ as the surrogate model. The attack causes large drops across retrieval metrics and Eval Models. Full metrics are reported in Appendix E.7.
  • Catastrophic Metric Drops: For the state-of-the-art commercial model Voyage-code-3, the Mean Reciprocal Rank (MRR) dropped from an excellent 87.06 down to 9.52 (an absolute drop of 77.54%).
  • Potency at Scale: Even under a highly constrained threat model where only 1% of the corpus (5 out of 500 snippets) was manipulated, Voyage-code-3 suffered a 62.8% drop in Recall@1, proving that a tiny amount of adversarial noise can completely bury the ground-truth results.
  • LLM Vulnerability: The attack successfully transferred to generative LLMs via the RepoQA benchmark, consistently degrading the retrieval accuracy of both GPT-5.4-mini and Gemini-3.1-Pro across multiple programming languages.

Limitations and Future Work

While the paper proves that cross-model transferability is highly effective, the exact underlying theoretical cause remains an open question. The authors hypothesize it is due to overlapping pretraining datasets, but closed-source data policies prevent a definitive conclusion.

Furthermore, current defense mechanisms (like robust finetuning) introduce an unacceptable trade-off between baseline search accuracy and security. The authors suggest that future work must move away from superficial token-matching and focus on building truly semantic-grounded code representations, potentially through functionality-aware contrastive training and the explicit integration of Abstract Syntax Trees (ASTs).

Summary updated

BibTeX
@misc{wang2026vulnerable,
  author = {Kaicheng Wang and Liyan Huang and Jesse Thomason and Weihang Wang},
  title = {{Vulnerable Code Search: Transferable Attack for Code Language Models}},
  year = {2026},
  eprint = {2608.26031},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2608.26031},
}
Figure