Manuscript Offprint

arXiv:2608.26031

Vulnerable Code Search: Transferable Attack for Code Language Models

Kaicheng Wang, Liyan Huang, Jesse Thomason, Weihang Wang

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Reliable code retrieval is crucial for developer productivity and effective code reuse. However, current neural code language models (CLMs) powering search tools are susceptible to adversarial attacks targeting non-functional textual elements. In this paper, we introduce a programming language-agnostic, transferable, adversarial attack that exploits this CLM vulnerability. Our approach perturbs identifiers within a code snippet without altering the snippet's functionality to artificially align the code with a target query. We demonstrate that our attack, even when computed using smaller code embedding models, such as CodeT5+, is highly effective and transferable to larger, closed-source embedding models, like Voyage-code-3, or LLMs like Gemini-3.1-Pro. Our attack can increase the similarity between the query and arbitrary, irrelevant code snippets, consequently degrading key retrieval metrics such as the Mean Reciprocal Rank (MRR) of state-of-the-art models by up to 77%. The experimental results highlight the fragility of current code search methods and underscore the need for more robust, semantic-aware approaches.

1 Introduction

The rapid expansion of the software ecosystem has intensified the need for efficient code retrieval to improve developers' productivity (Shekhar, 2024; Sun et al., 2024; Li et al., 2025). While the advent of Large Language Models (LLMs) and specialized Code Language Models (CLMs) has revolutionized tasks like repository-level reasoning (Jiang et al., 2026; Rozière et al., 2024; Hui et al., 2024; Chen et al., 2021; Liu et al., 2024a; Jimenez et al., 2024; Liu et al., 2024b), their computational cost often prohibits direct application in large-scale retrieval scenarios involving millions of candidates (Potvin and Levenberg, 2016; Howell et al., 2023). Consequently, for practical and efficient search, the industry continues relying on embedding-based

Original

def write_fits(self, fitsfile): tab = self.create_table() hdu_data = fits.table_to_hdu(tab) hdus = [fits.PrimaryHDU(), hdu_data] fits_utils.write_hdus(hdus, fitsfile) Sim.with Q w/ CodeT5+: 0.26: Sim with Q w/ NOMIC: 0.02

def preloadfastrab( Lo, jobsCONT): MATCH = Lo.create_table() getIoElasticsearchgpu = fits.table_to_hdu(MATCH) wrapossh = [fits.PrimaryHDU(), getIoElasticsearchgpu] fits_utils.write_hdus(wrapossh, jobsCONT) Sim.with Q w/ CodeT5+: 0.49: Sim with Q w/ NOMIC: 0.28

Gradient + Style Preservation

def data_na( Room, getSingletonWebService): deserializes = Room.create_table() Projects_Selection = fits.table_to_hdu(deserializes) acceptswheel = [fits.PrimaryHDU(), Projects_Selection] fits_utils.write_hdus( acceptswheel, getSingletonWebService) Sim.with Q w/ CodeT5+: 0.44: Sim with Q w/ NOMIC: 0.15

Gradient + Style Preservation

def load_fast( data, dataest): data = data.create_table() to_data = fits.table_to_hdu( data) wayus = [fits.PrimaryHDU(), to_data] fits_utils.write_hdus( wayus, dataest) Sim.with Q w/ CodeT5+: 0.68: Sim with Q w/ NOMIC: 0.47

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.

methodologies, which encode queries and code into a shared latent space to rank candidates based on vector similarity (Di Grazia and Pradel, 2023; Liu et al., 2021).

However, embedding-based retrieval is susceptible to a critical vulnerability: modern CLMs are vulnerable to adversarial examples—code snippets containing small, functionality-preserving modifications that can drastically change the resulting embeddings (Chen et al., 2025; Qu et al., 2024; Wan et al., 2022). While most prior research has focused on adversarial attacks in classification tasks (Yefet et al., 2020; Zhou et al., 2022; Yao et al., 2024; Na et al., 2023), our work centers on code search, which leads to unique challenges. First, adversarial attacks in code search can be tailored to spe-cific queries. By targeting specific queries, adversaries can have irrelevant or malicious code snippets crowding out legitimate results during certain task inquiries, subsequently influencing downstream coding decisions. Second, the typical code search workflow introduces additional robustness issues. Unlike classification, where malicious code is processed directly during inference and can be inspected by LLMs with reasoning abilities (Hort et al., 2025; Hossain et al., 2024; Jelodar et al., 2025), code search largely relies on offline embedding. When large code corpora are embedded offline, the modified code snippets maintain valid semantics and appear harmless. Since the subsequent retrieval process operates purely based on the similarity of these precomputed embeddings, detecting or mitigating these adversarial inputs becomes significantly more difficult.

In this paper, we demonstrate the shared vulnerability of the CLMs through transferable adversarial attacks. We generate adversarial examples by strategically replacing identifiers—user-defined symbols including function and variable names—within a code snippet to maximize the snippet's embedding similarity to a target query. The replacements are usually optimized on small white-box CLMs using gradient information, identifier style preservation, and query token similarity. As illustrated in Figure 1, although the attacks are crafted on CodeT5+, the resulting similarity improvement also appears under Nomic-embed-code, a model roughly 60 times larger. Based on our experiments, the attack transferability is broader: adversarial snippets generated on small surrogate models can transfer to larger models (e.g., OASIS, Nomic-embed-code) and closed-source systems, including embedding models (e.g., Voyage-code-3) and LLMs (e.g., Gemini-3.1-Pro) across five programming languages. Furthermore, the similarity change on the small source model strongly correlates with the changes on larger or closed-source target models, suggesting that attack effectiveness can be efficiently estimated.

Although state-of-the-art models achieve high scores on standard code search benchmarks (Li et al., 2024; Ott et al., 2022), they are vulnerable to our transferable adversarial attacks, leading to catastrophic drops in key retrieval metrics. For instance, we observed an absolute drop of up to 77% in Mean Reciprocal Rank (MRR) across all tested models on CosQA (Huang et al., 2021). The performance degradation exposes a critical gap: high benchmark scores do not necessarily reflect true semantic understanding of code. Instead, they highlight a reliance on brittle lexical features, underscoring the urgent need for improved model robustness.

In summary, our contributions are as follows:

• We propose an adversarial attack method for code search, which perturbs the code snippet to maximize its similarity with the target query while preserving functionality;

• We demonstrate the strong transferability of the attack: adversarial code snippets generated using small models can effectively deceive larger or closed-source systems; and

• We reveal that high benchmark scores mask a critical lack of robustness: current models prioritize superficial lexical matching over semantic reasoning, resulting in severe vulnerabilities that standard evaluations fail to capture.

References (32)