返回博客技术

LangChain CVE-2025-68664:个人信息如何通过 RAG 管道泄露及修复方案

CVSS 9.3。LangChain 的序列化函数将环境变量和密钥暴露给攻击者控制的 LLM。如何检测并修复个人信息泄露问题。

March 16, 20268 分钟阅读
LangChainRAG pipelineCVEPII leakagedeveloper securityAPI keysLLM security

LangChain CVE-2025-68664:个人信息如何通过 RAG 管道泄露

2026 年更新版。

2025 年底,LangChain 被发现存在严重安全漏洞,CVE 编号为 CVE-2025-68664,CVSS 评分 9.3(严重)

该漏洞位于 LangChain 的序列化代码中。

CVE-2025-68664 的工作原理

LangChain 提供两个序列化函数:dumps()dumpd(),用于将 Python 对象转换为文本。

漏洞存在于闭包处理机制中。

当 LangChain 序列化可调用对象时,会捕获其闭包上下文。

控制 LLM 响应的攻击者可以触发 dumps(),该函数随即读取 Python 进程中的环境变量。

后果是数据泄露:API 密钥、数据库连接字符串、JWT 密钥和 AWS 凭证均可能出现在模型输出中。

能够向 RAG 源文档注入文本的攻击者,即可读取您的生产环境密钥。

受影响版本: Python 版 LangChain 0.3.22 以下。修复版本为 0.3.22。

PyPI 数据显示,截至 2026 年 3 月,旧版本仍被广泛使用。

个人信息如何在 RAG 管道中泄露

CVE-2025-68664 的危害极为严峻,但它只是更广泛问题的一个具体案例。

数据在 RAG 管道中的泄露是常态,无需攻击者介入。

以下是一个典型的企业 RAG 架构。

第一步:数据入库。 将企业文档索引至向量存储。这些文档包括支持工单、客户邮件、合同和人力资源档案。

常见的向量存储包括 Pinecone、Weaviate 和 pgvector。

第二步:检索。 用户提出问题,系统从存储中检索最相关的五个文本块。

第三步:生成。 这些文本块作为上下文发送给 LLM——GPT-4o、Claude 或 Gemini。

问题出在第二步。检索到的文本块包含源文档中的所有内容,其中可能包括:

  • 客户姓名、电子邮件地址和电话号码
  • 合同金额、账号和税务标识符
  • 员工薪资数据和绩效评估记录
  • 临床记录中的患者姓名
  • 移民档案中的国家 ID 号码

这些数据原封不动地发送给 LLM,并可能出现在模型输出中。

它们被 LLM 提供商的日志记录,留存于会话历史,流入可观测性技术栈。

这一切无需任何攻击行为,这就是 RAG 的设计工作方式,而这种设计本身就带来了真实的隐私风险。

企业文档库中的 68 种密钥泄露模式

安全工具追踪着 68 种已知的密钥泄露模式,它们在文档库中出现的频率往往超出预期。

最常见的包括:

  • AWS 访问密钥 ID(AKIA...
  • OpenAI API 密钥(sk-...
  • Anthropic API 密钥(sk-ant-...
  • 数据库 URI(postgresql://user:password@host/db
  • JWT 令牌(Base64 编码的头部)
  • GitHub 个人访问令牌
  • Stripe 密钥(sk_live_...
  • SendGrid API 密钥
  • Twilio 账户 SID 和认证令牌
  • PEM 格式私钥块

一张支持工单可能包含调试会话中遗留的客户 API 密钥。

一份合同可能包含技术交接文档中的数据库凭证。

一个被误索引的配置文件可能暴露整个密钥存储。

这些文件未经清洗便进入向量存储,每次查询都可能将密钥传递给 LLM,最终甚至到达终端用户。

修复方案:在嵌入前进行匿名化

正确的做法是在分块和向量嵌入之前对文档进行匿名化处理。

对于任何处理客户数据的系统,这一步骤都是必须的。

以下是使用 anonym.legal API 的 Python 示例:

import requests
import os

ANONYM_API_KEY = os.environ["ANONYM_API_KEY"]
ANONYM_BASE_URL = "https://anonym.legal/api"

def anonymize_before_embedding(text: str) -> tuple[str, dict]:
    """在嵌入前对个人信息进行匿名化处理。"""
    response = requests.post(
        f"{ANONYM_BASE_URL}/presidio/anonymize",
        json={
            "text": text,
            "language": "en",
            "anonymizers": {
                "DEFAULT": {"type": "replace", "new_value": "[REDACTED]"},
                "PERSON": {"type": "mask", "masking_char": "*", "chars_to_mask": 4, "from_end": False},
                "EMAIL_ADDRESS": {"type": "replace", "new_value": "[EMAIL]"},
                "PHONE_NUMBER": {"type": "replace", "new_value": "[PHONE]"},
                "CRYPTO": {"type": "replace", "new_value": "[SECRET]"},
                "URL": {"type": "keep"},
            }
        },
        headers={"Authorization": f"Bearer {ANONYM_API_KEY}"}
    )
    result = response.json()
    return result["text"], result.get("items", [])


def build_rag_index(documents: list[str], vectorstore):
    """构建仅包含干净文档的 RAG 索引。"""
    anonymized_docs = []
    for doc in documents:
        clean_text, entities = anonymize_before_embedding(doc)
        anonymized_docs.append(clean_text)
        print(f"已从文档中删除 {len(entities)} 个个人信息实体")
    vectorstore.add_texts(anonymized_docs)

anonym.legal API 覆盖 285+ 种实体类型:姓名、电子邮件、电话号码、国家 ID、API 密钥和数据库 URI 均在检测范围内。

敏感信息永远不会进入向量存储,因此也永远不会泄露给用户。

请参阅开发者指南获取 LangChain 和 LlamaIndex 的集成配置模式。

立即修复 CVE-2025-68664

如果您运行的 LangChain 版本低于 0.3.22,请立即更新:

pip install "langchain>=0.3.22" "langchain-core>=0.3.22"

打补丁后,检查您的链配置是否存在注入风险。以下是三个操作步骤。

第一步:验证检索到的文本块。 在发送给 LLM 之前进行验证。

过滤匹配注入模式的内容,如 ignore previous instructionssystem:<INST>

第二步:在嵌入前进行匿名化处理。 这可以大幅缩小攻击面。

即使发生注入攻击,敏感数据也已不在其中,无从提取。

第三步:限制链的权限范围。 LangChain 链不应读取超出实际需要的环境变量。

使用最小权限范围的服务账户。

逻辑很简单

CVSS 评分 9.3,修复方案是每个文档一次 API 调用。

CVE-2025-68664 与 RAG 数据泄露的组合风险是真实存在的法律风险敞口。

解决方案清晰明确:在入库时进行匿名化,而非在查询时亡羊补牢。

请查阅安全与合规概览了解企业 RAG 的合规要求。

数据来源

  • NVD CVE-2025-68664,CVSS 9.3,LangChain 序列化漏洞
  • LangChain 安全公告,langchain-ai/langchain GitHub,2025 年
  • OWASP LLM Top 10:LLM01 提示注入,LLM06 敏感信息泄露
  • anonym.legal 实体类型文档 — 支持 285+ 种实体类型

准备好保护您的数据了吗?

开始使用 285 种实体类型在 48 种语言中匿名化 PII。

About this page

We update this page when our platform or the law changes.

Read our founder note for how we work.

Each change shows up in the timestamp at the top.

Related reading

We follow these rules

  • GDPR (EU 2016/679).
  • ISO/IEC 27001:2022.
  • NIS2 (EU 2022/2555).
  • HIPAA safe harbor under 45 CFR § 164.514(b)(2).

Our promise

We do not sell your data.

We do not train models on your text.

We store your files in Germany.

You can delete your account at any time.

You own your work.

Where we run

Our servers live in Falkenstein, Germany.

We use Hetzner. They hold ISO 27001 certification.

All data stays in the EU.

Backups run every day.

Need help?

Email support@anonym.legal.

We reply within one business day.

How we test

We run a full check suite on every release.

Each surface gets its own sweep script and report.

Human reviewers spot-check the output each week.

We track recall and precision on a labelled set.

Bad runs block the deploy.

What we never do

  • We never sell your information to third parties.
  • We never train models on what you upload.
  • We never keep your work after you delete it.
  • We never share keys with any outside firm.
  • We never run ads inside the product.

Plans in plain words

We sell credits, not seats.

One credit covers one short job.

Long jobs use a few credits each.

You can top up at any time.

Unused credits roll over each month.

Read the plans page for current rates.

Who built this

A small team of engineers and lawyers built this.

We ship from Europe and work in the open.

Our founder note spells out why we started.

Where to start

How the parts fit

A browser add-on cleans text inside Chrome.

A Word plug-in handles drafts in Office.

A small desktop tool works on whole folders.

An agent protocol link feeds large models safely.

All four share one core engine and one rule set.

Words from our team

We started this work after a lunch about cookies.

One friend kept getting odd ads on her phone.

We asked why a court file leaked through a draft.

We sketched the first build on a napkin that week.

By month three we had a tiny demo for a friend.

She used it on her first case the next day.

Common questions we hear

Can the tool read scanned PDFs? Yes, with OCR.

Does it work on long files? Yes, in small chunks.

Can I roll my own rule set? Yes, save it as a preset.

Does it run offline? The desktop build runs offline.

Do you keep my files? No, the cloud build wipes after each run.

Will it learn from my work? No, we never train on inputs.

A short tour of the workflow

Upload a file or paste a snippet of prose.

Pick the entities you want gone from the draft.

Choose a method: replace, mask, hash, encrypt, or redact.

Press run and watch the side panel show each hit.

Skim the result and tweak any rule that misfired.

Save the cleaned file or send it to a teammate.