Back to Feed

SCA tools are ideal companions for AI/ML engineers

Prabhu Subramanian

-

May 21, 2024

AI/ML packages

AI/ML/LLM applications use open-source libraries and third-party packages as client interfaces to work with the models and services such as ChatGPT. Architecturally, AI/ML applications are like any other application, with one growing difference - the amount of in-house developed code is lower with more open-source libraries and third-party models integrated/bundled into the application. Some services such as ChatGPT allows creating feature-rich AI applications with just a few lines of code; thus, more external code as a proportion of all code. This can be visualized like below:

The Risk of AI packages

AI packages, including popular ones, come with their share of risks and vulnerabilities. At times, a relatively safe package could depend on and introduce vulnerable indirect dependencies into the applications. To test and prove this hypothesis, I created a sample AI application called vuln_llama by heavily utilizing ChatGPT Coding Assistant and GitHub search for sample code.

LlamaIndex is a popular Python data framework (995K downloads/month) for developing LLM applications. The framework provides numerous data connectors for popular file formats and services such as ChatGPT, Hugging Face and query engines and vector stores for MongoDB, PostgreSQL, and so forth. Using LlamaIndex, AI applications can be authored in less than 10 lines of code for use cases like chat app or semi-autonomous agents. 

vuln_llama is a LlamaIndex powered chat application that interfaces with OpenAI. It has a Python FastAPI based backend and a Next.js TypeScript frontend. The project was created using a combination of ChatGPT prompts and samples in the LlamaIndex git repo. We utilized only the standard APIs suggested by the framework and kept the app simple and lightweight, mimicking the typical development style of an AI engineer.

The application was then scanned using the Backslash platform. Below is a summary of the vulnerabilities found in this application with some commentary:

 

OSS Vulnerabilities summary for vuln_llama

llama-index-core Prompt Injection vulnerability leading to Arbitrary Code Execution

The application relies on an OpenAI connector and indexer package called `llama-index-agent-openai`. This package has an indirect dependency on the `llama-index-core` package as identified in the dependency tree. Prompt Injection is a new class of vulnerabilities found in AI/ML applications. As per OWASP, "This manipulates a large language model (LLM) through crafty inputs, causing unintended actions by the LLM.” Since LlamaIndex is a framework used by beginners and advanced users, the maintainers added an allowlist feature to safeguard against such injection attacks. Paradoxically, the function that performed the sanitization then offered a vector for code execution as demonstrated with the sample prompt below:

 

Screenshot showing the details for llama-index-core Prompt Injection Vulnerability

CVEs like these are not an individual’s fault, as skilled security researchers can identify bypasses even in the most secure systems. Thanks to the open-source nature, LlamaIndex package was quickly patched with a new release "0.10.24" becoming available for use.

SQL Injection vulnerabilities via the Text to SQL feature 

The llama-index package introduced via llama-hub had a design flaw. By using "forget all the previous instructions" as a precursor to the prompt followed by a valid SQL command like "DELETE * from data", it was possible to execute SQL injection attacks. This is filed under CVE-2024-23751.

Directory Traversal and Remote Code Execution in langchain-core

It is common for AI/ML applications to process XML files from a local or remote source. Care must be taken when the file or response from an LLM is passed directly to an open-source library for processing. In the case of langchain library, attackers were able to force the LLM to generate a malicious XML and use it to perform Remote Code Execution against the host. This vulnerability is CVE-2024-1455. 

More issues from indirect dependencies 

LlamaIndex provides numerous packages to utilize popular databases and services such as vector stores. This ease of integration could inadvertently introduce vulnerable dependencies. In our example application, a vulnerable pymongo driver with a known bson-related vulnerability was introduced. However, based on the description, the severity of such bugs is quite low and do not usually require immediate patching.

Vulnerabilities in the frontend application

The frontend application also suffers from a similar issue; the openai package appears to depend on a vulnerable version of axios (0.26.1). Most LLM coding assistants, including ChatGPT and GitHub CoPilot, would never knowingly generate code that utilizes indirect dependencies. However, the LLM agents are often unable to comprehend the build and deployment context.

For example, upon inspecting the transpiled and optimized application chunks, we can see the remnants for the vulnerable axios, making the deployed frontend application vulnerable. The screenshot below was taken by building the application and viewing it using the “less” command on Linux. All occurrences of the word “axios” are highlighted in red. The arrow points to the version of the axios library found in the bundle.

Screenshot showing the presence of vulnerable axios library in the bundled app

Thankfully, the dependency is not reachable for an external adversary as identified by Backslash, making this finding a lower priority.

Screenshot showing unreachable JavaScript dependencies

Closing Thoughts

By analyzing the scan results of this prototype AI application, we demonstrated the need for specialist SCA tools to continuously evaluate and identify reachable prioritized vulnerabilities without slowing down the engineers’ and data scientists’ productivity. With a good understanding of your application, build, and business context, SCA platforms like Backslash offer the ideal companion tool for your AI/ML engineers and AppSec to build new generation AI applications quickly but safely.