diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..e188015 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,101 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL Advanced" + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + schedule: + - cron: '27 15 * * 4' + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} + permissions: + # required for all workflows + security-events: write + + # required to fetch internal or private CodeQL packs + packages: read + + # only required for workflows in private repositories + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: actions + build-mode: none + - language: java-kotlin + build-mode: none # This mode only analyzes Java. Set this to 'autobuild' or 'manual' to analyze Kotlin too. + # CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift' + # Use `c-cpp` to analyze code written in C, C++ or both + # Use 'java-kotlin' to analyze code written in Java, Kotlin or both + # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both + # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, + # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. + # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how + # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Add any setup steps before running the `github/codeql-action/init` action. + # This includes steps like installing compilers or runtimes (`actions/setup-node` + # or others). This is typically only required for manual builds. + # - name: Setup runtime (example) + # uses: actions/setup-example@v1 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v4 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + # If the analyze step fails for one of the languages you are analyzing with + # "We were unable to automatically build your code", modify the matrix above + # to set the build mode to "manual" for that language. Then modify this step + # to build your code. + # ℹ️ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + - name: Run manual build steps + if: matrix.build-mode == 'manual' + shell: bash + run: | + echo 'If you are using a "manual" build mode for one or more of the' \ + 'languages you are analyzing, replace this with the commands to build' \ + 'your code, for example:' + echo ' make bootstrap' + echo ' make release' + exit 1 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v4 + with: + category: "/language:${{matrix.language}}" diff --git a/.github/workflows/codeql1.yml b/.github/workflows/codeql1.yml new file mode 100644 index 0000000..3a91992 --- /dev/null +++ b/.github/workflows/codeql1.yml @@ -0,0 +1,140 @@ +name: "CodeQL (Java - Security Extended + Build Fallback + Scan Summary)" + +on: + push: + branches: ["main", "master"] + tags: + - "**" + pull_request: + branches: ["main", "master"] + workflow_dispatch: + inputs: + ref: + description: "要扫描的分支或 Tag(例如:master / v1.2.3)" + required: true + default: "master" + +permissions: + contents: read + security-events: write + +jobs: + analyze-build: + name: Analyze (Build if possible) + runs-on: ubuntu-latest + outputs: + build_ok: ${{ steps.build_out.outputs.build_ok }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || github.ref }} + + - name: Detect build system + id: detect + run: | + set -e + if [ -f "build.gradle" ] || [ -f "build.gradle.kts" ]; then + echo "build=gradle" >> $GITHUB_OUTPUT + elif [ -f "pom.xml" ]; then + echo "build=maven" >> $GITHUB_OUTPUT + else + echo "build=none" >> $GITHUB_OUTPUT + fi + + - name: Set up Java 17 + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: "17" + + - name: Initialize CodeQL (security-extended) + uses: github/codeql-action/init@v4 + with: + languages: java-kotlin + build-mode: manual + queries: security-extended + + - name: Build (try) + id: build_try + continue-on-error: true + run: | + set -euxo pipefail + if [ "${{ steps.detect.outputs.build }}" = "gradle" ]; then + chmod +x ./gradlew || true + ./gradlew --no-daemon clean assemble -x test --warning-mode all + elif [ "${{ steps.detect.outputs.build }}" = "maven" ]; then + chmod +x ./mvnw || true + mvn -B -DskipTests package + fi + + - name: Mark build result + id: build_out + run: | + if [ "${{ steps.build_try.outcome }}" = "success" ]; then + echo "build_ok=true" >> $GITHUB_OUTPUT + else + echo "build_ok=false" >> $GITHUB_OUTPUT + fi + + - name: Perform CodeQL Analysis (build) + if: ${{ steps.build_out.outputs.build_ok == 'true' }} + uses: github/codeql-action/analyze@v4 + with: + category: "/language:java-kotlin" + + - name: Summary (build mode) + if: always() + shell: bash + run: | + echo "## 🛡️ CodeQL Scan Summary" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Ref:** \`${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Build system:** \`${{ steps.detect.outputs.build }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Query suite:** \`security-extended\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + + if [ "${{ steps.build_out.outputs.build_ok }}" = "true" ]; then + echo "**Scan mode:** ✅ Build (manual) + security-extended" >> $GITHUB_STEP_SUMMARY + echo "- Results are uploaded automatically by CodeQL action." >> $GITHUB_STEP_SUMMARY + echo "- See **Security → Code scanning** for alerts." >> $GITHUB_STEP_SUMMARY + else + echo "**Scan mode:** ⚠️ Build failed — Falling back to no‑build scan." >> $GITHUB_STEP_SUMMARY + fi + + analyze-nobuild: + name: Analyze (No‑Build Fallback) + needs: analyze-build + if: ${{ needs.analyze-build.outputs.build_ok != 'true' }} + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || github.ref }} + + - name: Initialize CodeQL (no‑build) + uses: github/codeql-action/init@v4 + with: + languages: java-kotlin + build-mode: none + queries: security-extended + + - name: Perform CodeQL Analysis (no‑build) + uses: github/codeql-action/analyze@v4 + with: + category: "/language:java-kotlin" + + - name: Summary (no‑build) + shell: bash + run: | + echo "## 🛡️ CodeQL Scan Summary (No‑Build)" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Ref:** \`${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Scan mode:** ⚠️ No‑build fallback + security‑extended" >> $GITHUB_STEP_SUMMARY + echo "- Results are uploaded automatically." >> $GITHUB_STEP_SUMMARY + echo "- See **Security → Code scanning** for alerts." >> $GITHUB_STEP_SUMMARY diff --git a/.github/workflows/codeql19.yml b/.github/workflows/codeql19.yml new file mode 100644 index 0000000..5308d8f --- /dev/null +++ b/.github/workflows/codeql19.yml @@ -0,0 +1,272 @@ +name: "CodeQL (Java - Security Extended, build fallback + Rule Summary)" + +on: + push: + branches: ["main", "master"] + tags: + - "**" + pull_request: + branches: ["main", "master"] + workflow_dispatch: + inputs: + ref: + description: "要扫描的分支或 Tag(例如:master / v1.2.3)" + required: true + default: "master" + +permissions: + contents: read + +jobs: + # ========================= + # 1) 尝试“构建模式”扫描(更准确) + # ========================= + analyze-build: + name: Analyze (Java - build if possible) + runs-on: ubuntu-latest + + permissions: + contents: read + security-events: write + + outputs: + build_ok: ${{ steps.build_out.outputs.build_ok }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || github.ref }} + fetch-depth: 0 + + - name: Detect build system + id: detect + shell: bash + run: | + set -e + if [ -f "build.gradle" ] || [ -f "build.gradle.kts" ]; then + echo "build=gradle" >> "$GITHUB_OUTPUT" + elif [ -f "pom.xml" ]; then + echo "build=maven" >> "$GITHUB_OUTPUT" + else + echo "build=none" >> "$GITHUB_OUTPUT" + fi + if [ -f "./gradlew" ]; then + echo "has_gradlew=true" >> "$GITHUB_OUTPUT" + else + echo "has_gradlew=false" >> "$GITHUB_OUTPUT" + fi + if [ -f "./mvnw" ]; then + echo "has_mvnw=true" >> "$GITHUB_OUTPUT" + else + echo "has_mvnw=false" >> "$GITHUB_OUTPUT" + fi + # 没 wrapper 时装一个可控 Gradle(避免系统 Gradle 9.x 造成 JDK/插件兼容问题) + - name: Setup Gradle (only when no wrapper) + if: ${{ steps.detect.outputs.build == 'gradle' && steps.detect.outputs.has_gradlew == 'false' }} + uses: gradle/actions/setup-gradle@v4 + with: + gradle-version: "7.6.4" + + - name: Set up Java 17 + uses: actions/setup-java@v4 + with: + distribution: "temurin" + java-version: "17" + + - name: Initialize CodeQL (build mode, security-extended) + uses: github/codeql-action/init@v4 + with: + languages: java-kotlin + build-mode: manual + queries: security-extended + + - name: Build (try) + id: build_try + continue-on-error: true + shell: bash + run: | + set -euxo pipefail + if [ "${{ steps.detect.outputs.build }}" = "gradle" ]; then + if [ -f "./gradlew" ]; then + chmod +x ./gradlew + ./gradlew --no-daemon clean assemble -x test --warning-mode all + else + gradle --no-daemon clean assemble -x test --warning-mode all + fi + elif [ "${{ steps.detect.outputs.build }}" = "maven" ]; then + if [ -f "./mvnw" ]; then + chmod +x ./mvnw + ./mvnw -B -DskipTests package + else + mvn -B -DskipTests package + fi + else + echo "No build files found (Gradle/Maven)." + exit 1 + fi + - name: Mark build result + id: build_out + shell: bash + run: | + if [ "${{ steps.build_try.outcome }}" = "success" ]; then + echo "build_ok=true" >> "$GITHUB_OUTPUT" + else + echo "build_ok=false" >> "$GITHUB_OUTPUT" + fi + - name: Perform CodeQL Analysis (build) + if: ${{ steps.build_out.outputs.build_ok == 'true' }} + uses: github/codeql-action/analyze@v4 + with: + category: "/language:java-kotlin" + + # ✅ 从 SARIF 里打印“本次 suite 包含哪些规则 / 本次触发哪些规则” + - name: Print CodeQL rules used (from SARIF) + if: always() + shell: bash + run: | + echo "## 🧾 CodeQL rules used (from SARIF)" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Query suite:** \`security-extended\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + if [ ! -d "results" ]; then + echo "- SARIF folder not found: \`results/\`(可能 build 没成功或 analyze 没执行)" >> $GITHUB_STEP_SUMMARY + exit 0 + fi + shopt -s nullglob + FILES=(results/*.sarif) + if [ ${#FILES[@]} -eq 0 ]; then + echo "- No SARIF files found in \`results/*.sarif\`(可能 analyze 没执行)" >> $GITHUB_STEP_SUMMARY + exit 0 + fi + for SARIF in "${FILES[@]}"; do + echo "### 📄 ${SARIF}" >> $GITHUB_STEP_SUMMARY + TOTAL_RULES=$(jq '[.runs[].tool.driver.rules[]?] | length' "$SARIF" 2>/dev/null || echo "0") + TRIGGERED_RULES=$(jq '[.runs[].results[]? | .ruleId] | unique | length' "$SARIF" 2>/dev/null || echo "0") + echo "- Rules in SARIF: **$TOTAL_RULES**" >> $GITHUB_STEP_SUMMARY + echo "- Rules triggered (alerts exist): **$TRIGGERED_RULES**" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "
✅ 展开:本次 suite 的全部规则 ruleId" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + jq -r '.runs[].tool.driver.rules[]?.id' "$SARIF" | sort -u >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + echo "
" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "
🔥 展开:本次实际触发告警的 ruleId(有 results)" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + jq -r '.runs[].results[]?.ruleId' "$SARIF" | sort -u >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + echo "
" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + done + - name: Summary (build mode) + if: always() + shell: bash + run: | + echo "## 🛡️ CodeQL Scan Summary" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Ref:** \`${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Build tool:** \`${{ steps.detect.outputs.build }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Build wrapper:** gradlew=${{ steps.detect.outputs.has_gradlew }}, mvnw=${{ steps.detect.outputs.has_mvnw }}" >> $GITHUB_STEP_SUMMARY + echo "**Query suite:** \`security-extended\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + if [ "${{ steps.build_out.outputs.build_ok }}" = "true" ]; then + echo "**Scan mode:** ✅ Build (manual) + security-extended" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "### 🔍 Where to view results" >> $GITHUB_STEP_SUMMARY + echo "- **Security → Code scanning**(默认更偏向展示默认分支告警)" >> $GITHUB_STEP_SUMMARY + echo "- 本次运行日志:*Perform CodeQL Analysis (build)*" >> $GITHUB_STEP_SUMMARY + else + echo "**Scan mode:** ⚠️ Build failed → will run **no-build fallback** in next job" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "> ℹ️ Build failed. Some historical tags use very old Gradle/Maven settings; fallback will still produce results." >> $GITHUB_STEP_SUMMARY + fi + # ========================= + # 2) 构建失败就“无构建模式”扫描(保证能出结果) + # ========================= + analyze-nobuild: + name: Analyze (Java - no-build fallback) + needs: analyze-build + if: ${{ needs.analyze-build.outputs.build_ok != 'true' }} + runs-on: ubuntu-latest + + permissions: + contents: read + security-events: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || github.ref }} + fetch-depth: 0 + + - name: Initialize CodeQL (no-build, security-extended) + uses: github/codeql-action/init@v4 + with: + languages: java-kotlin + build-mode: none + queries: security-extended + + - name: Perform CodeQL Analysis (no-build) + uses: github/codeql-action/analyze@v4 + with: + category: "/language:java-kotlin" + + - name: Print CodeQL rules used (from SARIF) + if: always() + shell: bash + run: | + echo "## 🧾 CodeQL rules used (from SARIF)" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Query suite:** \`security-extended\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + if [ ! -d "results" ]; then + echo "- SARIF folder not found: \`results/\`" >> $GITHUB_STEP_SUMMARY + exit 0 + fi + shopt -s nullglob + FILES=(results/*.sarif) + if [ ${#FILES[@]} -eq 0 ]; then + echo "- No SARIF files found in \`results/*.sarif\`" >> $GITHUB_STEP_SUMMARY + exit 0 + fi + for SARIF in "${FILES[@]}"; do + echo "### 📄 ${SARIF}" >> $GITHUB_STEP_SUMMARY + TOTAL_RULES=$(jq '[.runs[].tool.driver.rules[]?] | length' "$SARIF" 2>/dev/null || echo "0") + TRIGGERED_RULES=$(jq '[.runs[].results[]? | .ruleId] | unique | length' "$SARIF" 2>/dev/null || echo "0") + echo "- Rules in SARIF: **$TOTAL_RULES**" >> $GITHUB_STEP_SUMMARY + echo "- Rules triggered (alerts exist): **$TRIGGERED_RULES**" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "
✅ 展开:本次 suite 的全部规则 ruleId" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + jq -r '.runs[].tool.driver.rules[]?.id' "$SARIF" | sort -u >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + echo "
" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "
🔥 展开:本次实际触发告警的 ruleId(有 results)" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + jq -r '.runs[].results[]?.ruleId' "$SARIF" | sort -u >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + echo "
" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + done + - name: Summary (no-build fallback) + if: always() + shell: bash + run: | + echo "## 🛡️ CodeQL Scan Summary" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Ref:** \`${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Scan mode:** ✅ No-build fallback + security-extended" >> $GITHUB_STEP_SUMMARY + echo "**Query suite:** \`security-extended\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "> ℹ️ Build failed, but CodeQL still scanned the code in **no-build** mode to ensure results are produced." >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "### 🔍 Where to view results" >> $GITHUB_STEP_SUMMARY + echo "- **Security → Code scanning**(tag 扫描结果可能不会在默认视图立刻显示,需要调整过滤条件)" >> $GITHUB_STEP_SUMMARY + echo "- 本次运行日志:*Perform CodeQL Analysis (no-build)*" >> $GITHUB_STEP_SUMMARY