Vulnerable Code Search: Transferable Attack for Code Language Models
This paper introduces a transferable, programming language-agnostic adversarial attack that perturbs code identifiers to artificially align irrelevant snippets with target queries, significantly degrading the retrieval performance of both open-source and closed-source code language models.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
In the vast, sprawling ecosystem of modern software, developers rely on digital libraries to find the building blocks they need. Imagine a programmer searching for a specific function to load data quickly; they type a question into a search bar, and a system must instantly sift through millions of lines of code to find the perfect match. This process, known as code retrieval, is the backbone of productivity, allowing engineers to reuse existing solutions rather than reinventing the wheel. To make this work at scale, computers use specialized models that translate both the human question and the computer code into a shared mathematical space. In this space, the closer two items are to each other, the more similar they are considered to be. The system then ranks the results, placing the most relevant code at the top. For years, the industry has trusted these systems to be robust, assuming that because the code works correctly, the computer will understand its purpose.
However, a new study reveals a startling fragility in these systems. Researchers from the University of Southern California have discovered that these powerful search tools can be easily tricked, not by changing what the code actually does, but by changing the names of the variables and functions within it. The team demonstrated that by carefully swapping out these names, they could make completely unrelated code appear to be the perfect answer to a specific question. It is as if a librarian could be fooled into pulling a cookbook off the shelf simply because the author changed the title of a chapter to match the words on a patron's request slip, even though the contents of the book remained entirely about cooking, not the patron's actual need.
The researchers developed a method to exploit this weakness. They took a piece of code that had nothing to do with a specific search query—perhaps a snippet for sorting a list of numbers when the user asked for a way to load data—and systematically renamed the internal symbols. They did this without altering a single line of the code's logic, ensuring the program would still run exactly as before. By using a smaller, accessible computer model to guide these changes, they found the specific names that would make the unrelated code look mathematically identical to the target question in the computer's eyes. The result was a piece of code that functioned perfectly but was semantically disconnected from the user's intent, yet the search engine ranked it as the top result.
The most alarming aspect of this discovery is how easily the trick transfers across different systems. The researchers generated these deceptive code snippets using a smaller, open-source model, yet the trick worked just as effectively against much larger, closed-source systems used by major technology companies. When they tested this against state-of-the-art models, the performance of the search engines collapsed. In one experiment, the ability of the system to find the correct answer dropped by as much as 77 percent. The search engines, which had previously scored highly on standard tests, failed to distinguish between the genuine solution and the manipulated impostor. This suggests that these models are not truly understanding the meaning of the code; instead, they are relying too heavily on superficial patterns, such as the specific words used for variable names, rather than the deeper logic of the program.
The study also explored whether these systems could be hardened against such attacks. The researchers tried training the models to ignore these deceptive patterns, but the solution came with a heavy price. When they made the models more resistant to the trick, their ability to perform normal searches suffered significantly, with their accuracy dropping by half. Conversely, when they tried to balance safety with performance, the models remained vulnerable to a more sophisticated version of the attack. The findings indicate that the current generation of code search tools, despite their impressive benchmarks, are built on a foundation that is far more brittle than previously thought. They prioritize matching surface-level text over grasping the actual function of the software, leaving them open to manipulation that could disrupt development workflows or, in a worst-case scenario, lead developers to use malicious code that looks legitimate.
This vulnerability extends beyond simple search engines. The researchers showed that the same trick could fool large language models used for generating code from repositories. When these advanced systems were presented with the manipulated code, their accuracy in selecting the correct function plummeted across multiple programming languages, including Python, C++, and Java. The attack worked because the models were drawn to the renamed identifiers that matched the query, ignoring the fact that the code's behavior was irrelevant. The study concludes that the industry needs to move toward more robust methods that understand the structure and logic of code, rather than just its textual appearance. Until then, the trust placed in these automated tools to guide critical software decisions remains precarious, vulnerable to a simple change of a few words.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.