<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pip-Audit on HiDa</title><link>https://www.0niu.cn/tags/pip-audit/</link><description>Recent content in Pip-Audit on HiDa</description><generator>Hugo -- gohugo.io</generator><language>zh</language><lastBuildDate>Mon, 15 Dec 2025 14:43:51 +0800</lastBuildDate><atom:link href="https://www.0niu.cn/tags/pip-audit/index.xml" rel="self" type="application/rss+xml"/><item><title>pip-audit：Python 依赖安全扫描工具</title><link>https://www.0niu.cn/posts/pip-audit-python-dependency-vulnerability-scanner/</link><pubDate>Mon, 15 Dec 2025 14:43:51 +0800</pubDate><guid>https://www.0niu.cn/posts/pip-audit-python-dependency-vulnerability-scanner/</guid><description>&lt;p>pip-audit 是 PyPA（Python Packaging Authority）维护的依赖安全扫描工具，用于检查 Python 环境和依赖文件中是否存在已知漏洞。数据来源是 Python Packaging Advisory Database，通过 PyPI JSON API 或 OSV 查询。&lt;/p>
&lt;p>和 npm audit 类似，但 pip 本身没有内置 audit 功能，pip-audit 填补了这个空缺。&lt;/p>
&lt;h2 id="安装">安装&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip install pip-audit
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>需要 Python 3.10+。也支持 conda 安装：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>conda install -c conda-forge pip-audit
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="基本用法">基本用法&lt;/h2>
&lt;p>&lt;strong>扫描当前环境&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>直接扫描当前虚拟环境中安装的所有包，输出有漏洞的包及 CVE 编号。&lt;/p>
&lt;p>&lt;strong>扫描 requirements 文件&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit -r requirements.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>扫描项目路径&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit /path/to/project
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>会自动识别项目中的依赖文件（requirements.txt、pyproject.toml、setup.cfg 等）。&lt;/p>
&lt;p>&lt;strong>扫描 lock 文件&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit --locked /path/to/project
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description><content>&lt;p>pip-audit 是 PyPA（Python Packaging Authority）维护的依赖安全扫描工具，用于检查 Python 环境和依赖文件中是否存在已知漏洞。数据来源是 Python Packaging Advisory Database，通过 PyPI JSON API 或 OSV 查询。&lt;/p>
&lt;p>和 npm audit 类似，但 pip 本身没有内置 audit 功能，pip-audit 填补了这个空缺。&lt;/p>
&lt;h2 id="安装">安装&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip install pip-audit
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>需要 Python 3.10+。也支持 conda 安装：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>conda install -c conda-forge pip-audit
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="基本用法">基本用法&lt;/h2>
&lt;p>&lt;strong>扫描当前环境&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>直接扫描当前虚拟环境中安装的所有包，输出有漏洞的包及 CVE 编号。&lt;/p>
&lt;p>&lt;strong>扫描 requirements 文件&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit -r requirements.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>扫描项目路径&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit /path/to/project
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>会自动识别项目中的依赖文件（requirements.txt、pyproject.toml、setup.cfg 等）。&lt;/p>
&lt;p>&lt;strong>扫描 lock 文件&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit --locked /path/to/project
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="输出格式">输出格式&lt;/h2>
&lt;p>默认是列格式，支持多种输出：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit -f json &lt;span style="color:#75715e"># JSON&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pip-audit -f markdown &lt;span style="color:#75715e"># Markdown 表格&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pip-audit -f cyclonedx-json &lt;span style="color:#75715e"># CycloneDX SBOM&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pip-audit -f cyclonedx-xml &lt;span style="color:#75715e"># CycloneDX SBOM&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>JSON 格式适合接入 CI/CD 流水线做自动化处理，CycloneDX 格式可以生成 SBOM（软件物料清单）。&lt;/p>
&lt;h2 id="自动修复">自动修复&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit --fix
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>--fix&lt;/code> 会自动升级有漏洞的依赖到安全版本。加 &lt;code>--dry-run&lt;/code> 可以预览修复结果而不实际执行：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit --fix --dry-run
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="漏洞源">漏洞源&lt;/h2>
&lt;p>pip-audit 支持三个漏洞数据库：&lt;/p>
&lt;ul>
&lt;li>&lt;strong>PyPI&lt;/strong>（默认）：通过 PyPI JSON API 查询，数据来自 Python Packaging Advisory Database&lt;/li>
&lt;li>&lt;strong>OSV&lt;/strong>：Google 的开源漏洞数据库，覆盖范围更广&lt;/li>
&lt;li>&lt;strong>ESMS&lt;/strong>：Ecosystem SBOM Service&lt;/li>
&lt;/ul>
&lt;p>切换漏洞源：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip-audit -s osv
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="实用参数">实用参数&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>参数&lt;/th>
&lt;th>作用&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>-l&lt;/code> / &lt;code>--local&lt;/code>&lt;/td>
&lt;td>只显示本地安装的包，跳过依赖的依赖&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>-S&lt;/code> / &lt;code>--strict&lt;/code>&lt;/td>
&lt;td>依赖收集失败时直接报错退出&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>--skip-editable&lt;/code>&lt;/td>
&lt;td>跳过 editable 模式安装的包&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>--no-deps&lt;/code>&lt;/td>
&lt;td>不做依赖解析，要求所有包锁定精确版本&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>--ignore-vuln ID&lt;/code>&lt;/td>
&lt;td>忽略指定漏洞（可多次使用）&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>-o FILE&lt;/code>&lt;/td>
&lt;td>输出到文件&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>--require-hashes&lt;/code>&lt;/td>
&lt;td>要求 requirements 中有 hash 校验&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="集成-cicd">集成 CI/CD&lt;/h2>
&lt;p>&lt;strong>Gitea Actions&lt;/strong>&lt;/p>
&lt;p>pip-audit 有官方 GitHub Action，Gitea Actions 兼容该格式：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">jobs&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">pip-audit&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">runs-on&lt;/span>: &lt;span style="color:#ae81ff">ubuntu-latest&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">steps&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#f92672">uses&lt;/span>: &lt;span style="color:#ae81ff">actions/checkout@v4&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#f92672">uses&lt;/span>: &lt;span style="color:#ae81ff">pypa/gh-action-pip-audit@v1.1.0&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">with&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">inputs&lt;/span>: &lt;span style="color:#ae81ff">requirements.txt&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>pre-commit&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>- &lt;span style="color:#f92672">repo&lt;/span>: &lt;span style="color:#ae81ff">https://github.com/pypa/pip-audit&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">rev&lt;/span>: &lt;span style="color:#ae81ff">v2.10.1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">hooks&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#f92672">id&lt;/span>: &lt;span style="color:#ae81ff">pip-audit&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">args&lt;/span>: [&lt;span style="color:#e6db74">&amp;#34;-r&amp;#34;&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;requirements.txt&amp;#34;&lt;/span>]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">ci&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">skip&lt;/span>: [&lt;span style="color:#ae81ff">pip-audit]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>注意 pre-commit.ci 不允许网络调用，所以 CI 环境中需要跳过。&lt;/p>
&lt;h2 id="和其他工具的对比">和其他工具的对比&lt;/h2>
&lt;ul>
&lt;li>&lt;strong>safety&lt;/strong>：商业产品，漏洞数据库需要付费订阅，pip-audit 完全免费开源&lt;/li>
&lt;li>&lt;strong>Snyk&lt;/strong>：SaaS 服务，需要注册账号，pip-audit 本地运行不传数据&lt;/li>
&lt;li>&lt;strong>Trivy&lt;/strong>：通用扫描器（容器+依赖），pip-audit 专注 Python 生态，更轻量&lt;/li>
&lt;/ul>
&lt;p>pip-audit 的优势在于：PyPA 官方维护、MIT 协议、本地运行、复用 pip 缓存、支持自动修复。&lt;/p></content></item></channel></rss>