CVE-2024-21513: Insufficient Information (NVD-CWE-noinfo)
value: Versions of the package langchain-experimental from 0.0.15 and before 0.0.21 are vulnerable to Arbitrary Code Execution when retrieving values from the database, the code will attempt to call 'eval' on all values. An attacker can exploit this vulnerability and execute arbitrary python code if they can control the input prompt and the server is configured with VectorSQLDatabaseChain.**Notes:**Impact on the Confidentiality, Integrity and Availability of the vulnerable component:Confidentiality: Code execution happens within the impacted component, in this case langchain-experimental, so all resources are necessarily accessible.Integrity: There is nothing protected by the impacted component inherently. Although anything returned from the component counts as 'information' for which the trustworthiness can be compromised.Availability: The loss of availability isn't caused by the attack itself, but it happens as a result during the attacker's post-exploitation steps.Impact on the Confidentiality, Integrity and Availability of the subsequent system:As a legitimate low-privileged user of the package (PR:L) the attacker does not have more access to data owned by the package as a result of this vulnerability than they did with normal usage (e.g. can query the DB). The unintended action that one can perform by breaking out of the app environment and exfiltrating files, making remote connections etc. happens during the post exploitation phase in the subsequent system - in this case, the OS.AT:P: An attacker needs to be able to influence the input prompt, whilst the server is configured with the VectorSQLDatabaseChain plugin.
CWE description: There is insufficient information about the issue to classify it; details are unkown or unspecified.
CVSS v3 base score: 8.5
Published: 2024-07-15T05:15Z
Repository: https://github.com/langchain-ai/langchain
Language: Python
Files changed: libs/experimental/langchain_experimental/sql/vector_sql.py
Commit date: 2023-09-23 08:51:03 -0700
Commit message: Remove python eval from vector sql db chain (#10937)

<!-- Thank you for contributing to LangChain!

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes (if applicable),
  - **Dependencies:** any dependencies required for this change,
- **Tag maintainer:** for a quicker response, tag the relevant
maintainer (see below),
- **Twitter handle:** we announce bigger features on Twitter. If your PR
gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` to check this
locally.

See contribution guidelines for more information on how to write/run
tests, lint, etc:

https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md

If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in `docs/extras`
directory.

If no one reviews your PR within a few days, please @-mention one of
@baskaryan, @eyurtsev, @hwchase17.
 -->
Diff: diff --git a/libs/experimental/langchain_experimental/sql/vector_sql.py b/libs/experimental/langchain_experimental/sql/vector_sql.py
index 43c42f7096..9c0ee021d1 100644
--- a/libs/experimental/langchain_experimental/sql/vector_sql.py
+++ b/libs/experimental/langchain_experimental/sql/vector_sql.py
@@ -6,11 +6,11 @@ from typing import Any, Dict, List, Optional, Union
 from langchain.callbacks.manager import CallbackManagerForChainRun
 from langchain.chains.llm import LLMChain
 from langchain.chains.sql_database.prompt import PROMPT, SQL_PROMPTS
 from langchain.prompts.prompt import PromptTemplate
 from langchain.schema import BaseOutputParser, BasePromptTemplate
-from langchain.schema.base import Embeddings
+from langchain.schema.embeddings import Embeddings
 from langchain.schema.language_model import BaseLanguageModel
 from langchain.tools.sql_database.prompt import QUERY_CHECKER
 from langchain.utilities.sql_database import SQLDatabase
 
 from langchain_experimental.sql.base import INTERMEDIATE_STEPS_KEY, SQLDatabaseChain
@@ -74,27 +74,15 @@ class VectorSQLRetrieveAllOutputParser(VectorSQLOutputParser):
             end = text.upper().find("FROM")
             text = text.replace(text[start + len("SELECT") + 1 : end - 1], "*")
         return super().parse(text)
 
 
-def _try_eval(x: Any) -> Any:
-    try:
-        return eval(x)
-    except Exception:
-        return x
-
-
 def get_result_from_sqldb(
     db: SQLDatabase, cmd: str
 ) -> Union[str, List[Dict[str, Any]], Dict[str, Any]]:
     result = db._execute(cmd, fetch="all")  # type: ignore
-    if isinstance(result, list):
-        return [{k: _try_eval(v) for k, v in dict(d._asdict()).items()} for d in result]
-    else:
-        return {
-            k: _try_eval(v) for k, v in dict(result._asdict()).items()  # type: ignore
-        }
+    return result
 
 
 class VectorSQLDatabaseChain(SQLDatabaseChain):
     """Chain for interacting with Vector SQL Database.
 

Vulnerable code: from langchain.schema.base import Embeddings
def _try_eval(x: Any) -> Any:
    try:
        return eval(x)
    except Exception:
        return x


    if isinstance(result, list):
        return [{k: _try_eval(v) for k, v in dict(d._asdict()).items()} for d in result]
    else:
        return {
            k: _try_eval(v) for k, v in dict(result._asdict()).items()  # type: ignore
        }
Fixed code: from langchain.schema.embeddings import Embeddings
    return result