Skip to content

Commit 432f11e

Browse files
author
Z User
committed
fix: 修复 OWASP Dependency-Check NVD API Key 警告
- 添加 NVD_API_KEY 环境变量(从 GitHub Secrets 读取) - 添加 NVD 数据库缓存,避免每次重新下载 - 命令行模式和 Gradle 模式都支持传入 NVD API Key - 未配置 API Key 时输出友好提示信息 - 设置 nvdApiDelay=6000 避免 API 限流
1 parent 0b8cb33 commit 432f11e

1 file changed

Lines changed: 52 additions & 9 deletions

File tree

.github/workflows/code-check.yml

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,12 @@ jobs:
770770
runs-on: ubuntu-latest
771771
needs: build
772772

773+
# 为 OWASP Dependency-Check 提供环境变量
774+
# NVD_API_KEY: 从 GitHub Secrets 读取,加速 NVD 数据库下载
775+
# 如果未配置此 Secret,扫描仍然可以运行,但会非常慢
776+
env:
777+
NVD_API_KEY: ${{ secrets.NVD_API_KEY }}
778+
773779
steps:
774780
- name: 检出代码仓库
775781
uses: actions/checkout@v4
@@ -790,6 +796,18 @@ jobs:
790796
restore-keys: |
791797
gradle-${{ runner.os }}-
792798
799+
# 缓存 OWASP NVD 数据库,避免每次运行都重新下载
800+
# 数据库每天更新一次,缓存可以有效加速扫描
801+
- name: 缓存 NVD 数据库
802+
uses: actions/cache@v4
803+
with:
804+
path: |
805+
~/.m2/repository/org/owasp/dependency-check-data/
806+
/tmp/dependency-check-data/
807+
key: nvd-db-${{ runner.os }}-${{ github.run_id }}
808+
restore-keys: |
809+
nvd-db-${{ runner.os }}-
810+
793811
- name: 赋予 Gradlew 可执行权限
794812
run: chmod +x gradlew
795813

@@ -813,12 +831,30 @@ jobs:
813831
echo '```' >> $GITHUB_STEP_SUMMARY
814832
815833
# 步骤 5.3: 使用 OWASP Dependency-Check 进行漏洞扫描
834+
# 重要:需要配置 NVD_API_KEY Secret 才能快速完成扫描
835+
# 免费申请 API Key:https://nvd.nist.gov/developers/request-an-api-key
836+
# 配置方法:GitHub 仓库 → Settings → Secrets → New repository secret → Name: NVD_API_KEY
816837
- name: OWASP 依赖漏洞扫描
817838
run: |
839+
# 检查是否配置了 NVD API Key
840+
if [ -n "$NVD_API_KEY" ]; then
841+
echo "✅ 已检测到 NVD API Key,将使用 API Key 加速扫描"
842+
else
843+
echo "⚠️ 未检测到 NVD API Key,扫描可能会非常缓慢"
844+
echo "💡 请前往 https://nvd.nist.gov/developers/request-an-api-key 免费申请"
845+
echo "💡 然后在 GitHub 仓库 Settings → Secrets → Actions 中添加 NVD_API_KEY"
846+
fi
847+
818848
# 尝试使用 Gradle OWASP 插件运行
819849
if ./gradlew tasks --no-daemon --quiet 2>/dev/null | grep -q "dependencyCheckAnalyze"; then
820850
echo "检测到项目已配置 OWASP 插件,使用 Gradle 运行..."
821-
./gradlew dependencyCheckAnalyze --no-daemon || true
851+
if [ -n "$NVD_API_KEY" ]; then
852+
./gradlew dependencyCheckAnalyze --no-daemon \
853+
-DnvdApiKey=$NVD_API_KEY \
854+
-DnvdApiDelay=6000 || true
855+
else
856+
./gradlew dependencyCheckAnalyze --no-daemon || true
857+
fi
822858
else
823859
echo "项目未配置 OWASP 插件,使用命令行工具运行..."
824860
# 下载 OWASP Dependency-Check
@@ -831,15 +867,22 @@ jobs:
831867
# 先生成 Gradle 依赖的 lock 文件
832868
./gradlew dependencies --no-daemon --write-locks || true
833869
870+
# 构建 OWASP 命令参数
871+
DC_ARGS="--project Mcpatch2JavaClient"
872+
DC_ARGS="$DC_ARGS --scan build/"
873+
DC_ARGS="$DC_ARGS --out build/reports/dependency-check"
874+
DC_ARGS="$DC_ARGS --format HTML"
875+
DC_ARGS="$DC_ARGS --format JSON"
876+
DC_ARGS="$DC_ARGS --failOnCVSS 7"
877+
DC_ARGS="$DC_ARGS --data /tmp/dependency-check-data"
878+
879+
# 如果有 NVD API Key,添加到参数中以加速下载
880+
if [ -n "$NVD_API_KEY" ]; then
881+
DC_ARGS="$DC_ARGS --nvdApiKey $NVD_API_KEY"
882+
fi
883+
834884
# 运行 OWASP Dependency-Check
835-
/tmp/dependency-check/bin/dependency-check.sh \
836-
--project "Mcpatch2JavaClient" \
837-
--scan build/ \
838-
--out build/reports/dependency-check \
839-
--format HTML \
840-
--format JSON \
841-
--failOnCVSS 7 \
842-
|| true
885+
/tmp/dependency-check/bin/dependency-check.sh $DC_ARGS || true
843886
fi
844887
845888
# 步骤 5.4: 上传漏洞扫描报告

0 commit comments

Comments
 (0)