基于Github任务流的开源AI代码审计框架

admin 2026-01-17 01:45:48 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文介绍了GitHubSecurityLab开源的AI代码审计框架seclab-taskflow-agent,旨在通过任务流自动化安全研究。框架基于MCP和CodeQL,利用YAML定义审计流程并演示变体分析。项目分离引擎与任务流集,采用Python打包系统促进社区协作与工具共享,旨在加速漏洞发现。 综合评分: 93 文章分类: 代码审计,AI安全,安全工具,漏洞分析,安全运营


cover_image

基于Github任务流的开源AI代码审计框架

Dubito Dubito

云原生安全指北

2026年1月16日 08:35 江苏

注:本文翻译自 GitHub Security Lab – Kevin Backhouse[1] 的文章《Community-powered security with AI: an open source framework for security research》[2],可点击文末“阅读原文”按钮查看英文原文。

全文如下:

一、引言

自2019年成立[3]以来,GitHub Security Lab 一直有一个主要目标:社区驱动的安全。我们相信,提升软件安全的最佳途径是分享知识、工具,并通过使用开源软件,让每个人都能审计代码并报告他们发现的漏洞。

六年后的今天,一个崭新的机遇出现了,它将把社区驱动的安全推向新的高度。得益于 AI,我们现在可以使用自然语言来编码、分享和扩展我们的安全知识,这将使构建和分享新的安全工具变得更加容易。而在底层,我们可以利用 Model Context Protocol (MCP) 接口[4] 在现有安全工具(如 CodeQL[5])的基础上进行构建。

作为一个社区,如果我们分享如何发现漏洞的知识,就能更快地消除软件漏洞。怀着这个目标,我们团队一直在试验一个名为 GitHub Security Lab Taskflow Agent(GitHub 安全实验室任务流代理)[6] 的智能体框架。我们内部已使用了一段时间,最近也分享给了 GitHub Secure Open Source Fund(GitHub 安全开源基金)[7] 的参与者。虽然它仍处于实验阶段,但已可供他人使用。

二、Demo:变体分析

只需几步即可开始使用 seclab-taskflow-agent:

  1. 1. 创建个人访问令牌(personal access token)
  2. 2. 添加 Codespace secrets
  3. 3. 启动一个 Codespace
  4. 4. 用一行命令运行一个任务流(taskflow)

请按步骤操作并尝试一下!

注意:此Demo将消耗您的一些 token 配额,并且您可能会遇到速率限制[8],尤其是如果您使用的是免费的 GitHub 账户。但我已尽力设计该Demo,使其能在免费账户上运行。如果您真的触发了速率限制,配额将在一天后刷新。

三、创建细粒度个人访问令牌(fine-grained personal access token)

前往您的开发者设置页面[9]并创建一个个人访问令牌(PAT,Personal Access Token)。

向下滚动并添加 “models” 权限:

四、添加 Codespaces secrets

出于安全考虑,将您刚刚创建的 PAT 保存在磁盘文件中并非明智之举。我建议将其保存为一个 “codespace secret”,这意味着在下一步启动 codespace 时,它将作为一个环境变量可用。

前往您的 Codespaces 设置[10]并创建一个名为 GH_TOKEN 的 secret:

在 “Repository access” 下,添加 GitHubSecurityLab/seclab-taskflows[11] 仓库,我们后续将从这个仓库启动 codespace。

现在,返回您的 Codespaces 设置[10]并创建第二个名为 AI_API_TOKEN 的 secret。您可以对这两个 secret 使用同一个 PAT。

我们希望使用两个 secret,是为了让 GH_TOKEN 用于访问 GitHub 的 API 并执行诸如读取代码等操作,而 AI_API_TOKEN 则用于访问 AI API。本Demo只需要一个 PAT,因为它使用的是 GitHub Models API,但该框架也支持为 AI 请求使用其他(非 GitHub 的)API。

五、启动一个 Codespace

现在,前往 seclab-taskflows 仓库[11] 并启动一个 codespace:

Codespace 启动后,请等待几分钟,直到您看到如下提示符:

请务必等待,直到您在提示符前看到 (.venv)。这表明 Python 虚拟环境[12] 已创建完成。

六、使用一行命令运行任务流(taskflow)

在 codespace 终端中,输入以下命令来运行变体分析Demo任务流:

python -m seclab_taskflow_agent -t seclab_taskflows.taskflows.audit.ghsa_variant_analysis_demo -g repo=github/cmark-gfm -g ghsa=GHSA-c944-cv5f-hpvr

当它请求运行 memcache_clear_cache 的权限时,回答 “yes”;这是首次运行,所以缓存本来就是空的。该Demo会从仓库下载并分析一个安全通告[13](在本例中,是来自 cmark-gfm[14] 的 GHSA-c944-cv5f-hpvr[15])。它会尝试定位导致漏洞的源代码文件,然后下载该源代码文件并审计其中其他类似的 bug。这并不是一个复杂的Demo,并且(谢天谢地)它在 cmark-gfm 中没有发现任何新 bug 🫣。但它简短明了,稍后我将用它来解释什么是任务流。您也可以通过更改命令末尾的仓库名称,在其他仓库(也许是您自己的仓库)上尝试运行它。

七、其他运行方式

我推荐使用 codespace,因为它是一种快速、可靠的入门方式。它也是一个沙盒环境,对安全性有利。但如果您愿意,也有其他方式来运行这个框架。

八、在 Linux 终端中运行

以下是在 Linux 系统上本地安装并运行Demo的命令:

export AI_API_TOKEN=github_pat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
export GH_TOKEN=$AI_API_TOKEN
python3 -m venv .venv
source .venv/bin/activate
pip install seclab-taskflows
python -m seclab_taskflow_agent -t seclab_taskflows.taskflows.audit.ghsa_variant_analysis_demo -g repo=github/cmark-gfm -g ghsa=GHSA-c944-cv5f-hpvr

这些命令会从 PyPI[16] 下载我们的最新版本。请注意,框架包含的一些工具箱(toolbox)可能无法通过此方式开箱即用,因为它们依赖于已安装的其他软件。例如,CodeQL 工具箱[17] 依赖于已安装的 CodeQL[5]。您可以复制我们用于构建 codespace 环境的 devcontainer 配置文件[18] 中的安装说明。

九、在 Docker 中运行

我们发布了一个预装了 CodeQL 等工具的 Docker 镜像[19]。您可以使用这个脚本[20] 来运行它。请注意,这个 Docker 镜像只包含 seclab-taskflow-agent[6]。我们计划未来发布第二个“开箱即用”的镜像,其中也将包含 seclab-taskflows[11]。注意:我将在关于协作模型的部分解释 seclab-taskflow-agent 和 seclab-taskflows 之间的关系。

十、任务流(Taskflows)

一个任务流(taskflow)就是一个 YAML 文件,其中包含一系列要由框架执行的任务。让我们看看我演示用的任务流(源代码[21]):

seclab-taskflow-agent:
  filetype: taskflow
  version: 1

globals:
  repo:
  ghsa:

taskflow:
  - task:
      must_complete: true
      agents:
        - seclab_taskflow_agent.personalities.assistant
      toolboxes:
        - seclab_taskflow_agent.toolboxes.memcache
      user_prompt: |
        Clear the memory cache.
        # 译文:清除内存缓存。

  - task:
      must_complete: true
      agents:
        - seclab_taskflow_agent.personalities.assistant
      toolboxes:
        - seclab_taskflows.toolboxes.ghsa
        - seclab_taskflows.toolboxes.gh_file_viewer
        - seclab_taskflow_agent.toolboxes.memcache
      user_prompt: |
        Fetch the details of the GHSA {{ GLOBALS_ghsa }} of the repo {{ GLOBALS_repo }}.

        Analyze the description to understand what type of bug caused
        the vulnerability. DO NOT perform a code audit at this stage, just
        look at the GHSA details.

        Check if any source file is mentioned as the cause of the GHSA.
        If so, identify the precise file path and line number.

        If no file path is mentioned, then report back to the user that
        you cannot find any file path and end the task here.

        The GHSA may not specify the full path name of the source
        file, or it may mention the name of a function or method
        instead, so if you have difficulty finding the file, try
        searching for the most likely match.

        Only identify the file path for now, do not look at the code or
        fetch the file contents yet.

        Store a summary of your findings in the memcache with the GHSA
        ID as the key. That should include the file path and the function that
        the file is in.
        # 译文:获取仓库 {{ GLOBALS_repo }} 中 GHSA {{ GLOBALS_ghsa }} 的详细信息。

        # 分析描述以了解导致漏洞的 bug 类型。在此阶段**不要**进行代码审计,只需查看 GHSA 详情。

        # 检查是否有任何源文件被提及为导致 GHSA 的原因。
        # 如果有,请确定精确的文件路径和行号。

        # 如果没有提及文件路径,则向用户报告您找不到任何文件路径,并在此结束任务。

        # GHSA 可能没有指定源文件的完整路径名,或者它可能提到了函数或方法名称。
        # 因此,如果您在查找文件时遇到困难,请尝试搜索最可能的匹配项。

        # 目前只确定文件路径,**不要**查看代码或获取文件内容。

        # 将您的发现摘要存入 memcache,以 GHSA ID 作为键。该摘要应包括文件路径以及文件所在的函数。

  - task:
      must_complete: true
      agents:
        - seclab_taskflow_agent.personalities.assistant
      toolboxes:
        - seclab_taskflows.toolboxes.gh_file_viewer
        - seclab_taskflow_agent.toolboxes.memcache
      user_prompt: |
        Fetch the GHSA ID and summary that were stored in the memcache
        by the previous task.

        Look at the file path and function that were identified. Use the
        get_file_lines_from_gh tool to fetch a small portion of the file instead of
        fetching the entire file.

        Fetch the source file that was identified as the cause of the
        GHSA in repo {{ GLOBALS_repo }}.

        Do a security audit of the code in the source file, focusing
        particularly on the type of bug that was identified as the
        cause of the GHSA.
        # 译文:获取前一个任务存储在 memcache 中的 GHSA ID 和摘要。

        # 查看已识别的文件路径和函数。使用 `get_file_lines_from_gh` 工具获取文件的一小部分,而不是获取整个文件。

        # 获取仓库 {{ GLOBALS_repo }} 中被识别为 GHSA 原因的源文件。

        # 对源文件中的代码进行安全审计,重点关注被识别为 GHSA 原因的 bug 类型。

您可以看到,它的结构与 GitHub Actions 工作流非常相似。顶部有一个header,然后是主体body,包含一系列任务。这些任务由智能体(agent)框架逐一完成。让我们逐一浏览各个部分,重点关注最重要的部分:

10.1 Header

Header 的第一部分定义了文件类型。最常用的文件类型有:

  • • taskflow:描述供框架执行的一系列任务。
  • • personality:在执行任务时,要求智能体(agent)扮演特定的角色/人格通常很有用。例如,我们有一个 action_expert[22] 角色,它对审计 actions 工作流非常有用。
  • • toolbox:包含运行 MCP 服务器的说明。例如,演示中使用的 gh_file_viewer[23] 工具箱,用于从 GitHub 下载源代码文件。

globals 部分定义了名为 “repo” 和 “ghsa” 的全局变量,我们通过命令行参数 -g repo=github/cmark-gfm 和 -g ghsa=GHSA-c944-cv5f-hpvr 对其进行了初始化。这是一种粗糙的参数化任务流方式。

10.2 任务 1

任务总是会指定要使用的 “personality(角色定义)”。对于非专业化的任务,我们通常只使用 assistant[24] 这个角色。

每个任务都从一个全新的上下文开始,因此将一个任务的结果传递给下一个任务的唯一方法,是使用一个工具箱作为中介。在这个演示中,我使用了 memcache[25] 工具箱,它是一个简单的键值存储。我们发现这种方法更利于调试,因为这意味着在测试时,您可以使用一致的输入重新运行单个任务。

这个任务还展示了,工具箱在执行具有潜在破坏性的操作前可以请求确认[26],这是防范提示注入(prompt injection)[27]攻击的一项重要保护措施。

10.3 任务 2

此任务使用 ghsa[28] 工具箱从仓库下载安全通告,并使用 gh_file_viewer[23] 工具箱查找通告中提及的源文件。它会创建一个摘要,并使用 memcache[25] 工具箱将其传递给下一个任务。

10.4 任务 3

此任务使用 memcache[25] 工具箱获取前一个任务的结果,并使用 gh_file_viewer[23] 工具箱下载源代码并进行审计。

通常,提示词(prompt)的措辞比看起来更加微妙,这第三个任务就是一个例子。此任务的早期版本尝试一次性分析整个源文件,导致消耗了过多的 token。因此,第二段要求分析”文件的一小部分”非常重要,它是确保此任务成功运行的关键。

10.5 任务流总结

我希望这个演示能让您了解什么是任务流。您可以在 README.md[29] 和 GRAMMAR.md[30] 中找到更详细的文档。您也可以在 seclab-taskflow-agent 的这个子目录[31] 和 seclab-taskflows 的这个子目录[32] 中找到更多示例。

十一、协作模型

我们非常欢迎社区成员发布他们自己的任务流套件。为了便于协作,我们基于 Python 的打包生态系统进行构建。我们自己的两个仓库已作为软件包发布在 PyPI[16] 上:

  1. 1. seclab-taskflow-agent[33]:任务流框架的实现。
  2. 2. seclab-taskflows[34]:由我们团队编写的一套任务流。

我们设立两个仓库的原因,是为了将“引擎”与使用它的任务流套件分离开来。同时,seclab-taskflows[34] 旨在为任何想要发布自己任务流套件的人提供一个易于复制的模板。要开始构建您的软件包,我们建议使用 hatch new[35] 命令来创建初始的项目结构。该命令会生成如 pyproject.toml 等您上传到 PyPI 所需的文件。接下来,我们建议创建一个类似我们这样的目录结构[36],包含 taskflows、toolboxes 等子目录。您也可以自由复制 seclab-taskflows 的其他部分,例如我们的 publish-to-pypi.yaml[37] 工作流,它能在您推送类似 “v1.0.0” 这样的标签时,自动将您的软件包上传到 PyPI。

该协作模型的一个重要特点是,它也便于分享 MCP 服务器。例如,请查看包含在 seclab-taskflows 包中的 MCP 服务器[38]。每个 MCP 服务器都对应一个工具箱 YAML 文件(位于 toolboxes[39] 目录),其中包含运行该服务器的说明。

十二、Import 系统

任务流通常需要引用其他文件,例如角色定义(personalities)或工具箱(toolboxes)。为了使协作模型良好运行,我们希望您能够复用其他软件包中的角色定义和工具箱。我们利用 Python 的 importlib[40] 来简化从不同软件包引用文件的操作。为了说明其工作原理,这里有一个示例[41],其中 seclab-taskflows 正在使用来自 seclab-taskflow-agent 的一个工具箱:

toolboxes:
  - seclab_taskflow_agent.toolboxes.memcache

其实现[42]会将名称 seclab_taskflow_agent.toolboxes.memcache 拆分为目录部分(seclab_taskflow_agent.toolboxes)和文件名部分(memcache)。然后,它使用 Python 的 importlib.resources.files[43] 来定位该目录,并从该目录加载名为 memcache.yaml 的文件。这个系统唯一的特殊之处在于,名称总是需要至少包含两部分,这意味着您的文件至少需要存储在一层目录之下。但除此之外,我们只是原样使用 Python 的import系统,这意味着您可以在网上找到大量的文档和建议。

十三、项目愿景

我们对此项目有两个主要目标。

首先是鼓励社区驱动的安全。当前涌现出的许多智能体(agentic)安全工具是闭源的黑盒,这与我们团队所秉持的理念背道而驰。我们希望人们能够深入了解其内部机制,看清任务流(taskflows)的工作原理。我们也希望人们能够轻松创建并分享他们自己的任务流。作为一个社区,如果我们分享如何发现漏洞的知识,就能更快地消除软件漏洞。我们希望任务流能成为实现这一目标的有效工具。

第二是创造一个我们自己也想用的工具。作为一个研究团队,我们需要一个适合快速实验的工具。我们必须能够快速创建一条新的安全规则并进行尝试。考虑到这一点,我们并非要打造世界上最完善或最高效的工具,而是力求创造出易于修改的工具。

引用链接

[1] Kevin Backhouse: https://github.blog/author/kevinbackhouse/ [2] 《Community-powered security with AI: an open source framework for security research》: https://github.blog/security/community-powered-security-with-ai-an-open-source-framework-for-security-research/ [3] 2019年成立: https://github.blog/changelog/2019-11-14-security-lab/ [4] Model Context Protocol (MCP) 接口: https://modelcontextprotocol.io/ [5] CodeQL: https://codeql.github.com/ [6] GitHub Security Lab Taskflow Agent(GitHub 安全实验室任务流代理): https://github.com/GitHubSecurityLab/seclab-taskflow-agent [7] GitHub Secure Open Source Fund(GitHub 安全开源基金): https://github.com/open-source/github-secure-open-source-fund [8] 速率限制: https://docs.github.com/en/github-models/use-github-models/prototyping-with-ai-models#rate-limits [9] 开发者设置页面: https://github.com/settings/personal-access-tokens/new [10] 您的 Codespaces 设置: https://github.com/settings/codespaces/secrets/new [11] GitHubSecurityLab/seclab-taskflowshttps://github.com/GitHubSecurityLab/seclab-taskflows [12] Python 虚拟环境: https://docs.python.org/3/library/venv.html [13] 安全通告: https://docs.github.com/en/code-security/security-advisories/working-with-repository-security-advisories/about-repository-security-advisories [14] cmark-gfm: https://github.com/github/cmark-gfm [15] GHSA-c944-cv5f-hpvrhttps://github.com/github/cmark-gfm/security/advisories/GHSA-c944-cv5f-hpvr [16] PyPI: https://pypi.org/ [17] CodeQL 工具箱: https://github.com/GitHubSecurityLab/seclab-taskflow-agent/blob/v0.0.9/src/seclab_taskflow_agent/toolboxes/codeql.yaml [18] devcontainer 配置文件: https://github.com/GitHubSecurityLab/seclab-taskflows/tree/v0.0.3/.devcontainer [19] Docker 镜像: https://github.com/GitHubSecurityLab/seclab-taskflow-agent/pkgs/container/seclab-taskflow-agent [20] 这个脚本: https://github.com/GitHubSecurityLab/seclab-taskflow-agent/blob/v0.0.9/docker/run.sh [21] 源代码: https://github.com/GitHubSecurityLab/seclab-taskflows/blob/v0.0.3/src/seclab_taskflows/taskflows/audit/ghsa_variant_analysis_demo.yaml [22] action\_experthttps://github.com/GitHubSecurityLab/seclab-taskflows/blob/v0.0.3/src/seclab_taskflows/personalities/action_expert.yaml [23] gh\_file\_viewerhttps://github.com/GitHubSecurityLab/seclab-taskflows/blob/v0.0.3/src/seclab_taskflows/toolboxes/gh_file_viewer.yaml [24] assistanthttps://github.com/GitHubSecurityLab/seclab-taskflow-agent/blob/v0.0.9/src/seclab_taskflow_agent/personalities/assistant.yaml [25] memcachehttps://github.com/GitHubSecurityLab/seclab-taskflow-agent/blob/v0.0.9/src/seclab_taskflow_agent/toolboxes/memcache.yaml [26] 请求确认: https://github.com/GitHubSecurityLab/seclab-taskflow-agent/blob/v0.0.9/src/seclab_taskflow_agent/toolboxes/memcache.yaml#L17-L18 [27] 提示注入(prompt injection): https://genai.owasp.org/llmrisk/llm01-prompt-injection/ [28] ghsahttps://github.com/GitHubSecurityLab/seclab-taskflows/blob/v0.0.3/src/seclab_taskflows/toolboxes/ghsa.yaml [29] README.mdhttps://github.com/GitHubSecurityLab/seclab-taskflow-agent/blob/v0.0.9/README.md [30] GRAMMAR.mdhttps://github.com/GitHubSecurityLab/seclab-taskflow-agent/blob/v0.0.9/doc/GRAMMAR.md [31] 这个子目录: https://github.com/GitHubSecurityLab/seclab-taskflow-agent/tree/v0.0.9/examples/taskflows [32] 这个子目录: https://github.com/GitHubSecurityLab/seclab-taskflows/tree/v0.0.3/src/seclab_taskflows/taskflows [33] seclab-taskflow-agent: https://pypi.org/project/seclab-taskflow-agent/ [34] seclab-taskflows: https://pypi.org/project/seclab-taskflows/ [35] hatch new: https://hatch.pypa.io/latest/cli/reference/#hatch-new [36] 类似我们这样的目录结构: https://github.com/GitHubSecurityLab/seclab-taskflows/tree/v0.0.3/src/seclab_taskflows [37] publish-to-pypi.yaml: https://github.com/GitHubSecurityLab/seclab-taskflows/blob/v0.0.3/.github/workflows/publish-to-pypi.yaml [38] 包含在 seclab-taskflows 包中的 MCP 服务器: https://github.com/GitHubSecurityLab/seclab-taskflows/tree/v0.0.3/src/seclab_taskflows/mcp_servers [39] toolboxes: https://github.com/GitHubSecurityLab/seclab-taskflows/tree/v0.0.3/src/seclab_taskflows/toolboxes [40] importlib: https://docs.python.org/3/library/importlib.html [41] 示例: https://github.com/GitHubSecurityLab/seclab-taskflows/blob/v0.0.3/src/seclab_taskflows/taskflows/audit/ghsa_variant_analysis_demo.yaml#L22-L23 [42] 实现: https://github.com/GitHubSecurityLab/seclab-taskflow-agent/blob/v0.0.9/src/seclab_taskflow_agent/available_tools.py#L58-L69 [43] importlib.resources.fileshttps://docs.python.org/3/library/importlib.resources.html#importlib.resources.files

交流群


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:云原生安全指北 Dubito Dubito《基于Github任务流的开源AI代码审计框架》

评论:0   参与:  0