|
| 1 | +# Copyright 2026 Chainguard, Inc. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +name: Fuzz Tests |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - "main" |
| 10 | + schedule: |
| 11 | + # Run weekly on Sunday at midnight UTC |
| 12 | + - cron: "0 0 * * 0" |
| 13 | + workflow_dispatch: |
| 14 | + inputs: |
| 15 | + fuzz_target: |
| 16 | + description: "Specific fuzzer to run (leave empty for all)" |
| 17 | + required: false |
| 18 | + default: "" |
| 19 | + type: string |
| 20 | + fuzz_time: |
| 21 | + description: "Fuzz duration per target (e.g., 30s, 1m, 5m)" |
| 22 | + required: false |
| 23 | + default: "30s" |
| 24 | + type: choice |
| 25 | + options: |
| 26 | + - "10s" |
| 27 | + - "30s" |
| 28 | + - "1m" |
| 29 | + - "5m" |
| 30 | + - "10m" |
| 31 | + - "30m" |
| 32 | + - "60m" |
| 33 | + - "180m" |
| 34 | + |
| 35 | +permissions: {} |
| 36 | + |
| 37 | +jobs: |
| 38 | + discover: |
| 39 | + if: ${{ github.repository == 'chainguard-dev/malcontent' }} |
| 40 | + runs-on: ubuntu-latest |
| 41 | + permissions: |
| 42 | + contents: read |
| 43 | + outputs: |
| 44 | + targets: ${{ steps.find.outputs.targets }} |
| 45 | + steps: |
| 46 | + - name: Checkout code |
| 47 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 48 | + with: |
| 49 | + persist-credentials: false |
| 50 | + |
| 51 | + - name: Discover fuzz targets |
| 52 | + id: find |
| 53 | + env: |
| 54 | + TARGET_FILTER: ${{ inputs.fuzz_target }} |
| 55 | + run: | |
| 56 | + # Find all Fuzz* functions in test files and build JSON array |
| 57 | + # Format: [{"test": "FuzzName", "package": "./pkg/path/"}] |
| 58 | + all_targets=$(grep -r "^func Fuzz" --include="*_test.go" -l pkg/ | while read -r file; do |
| 59 | + dir="./$(dirname "${file}")/" |
| 60 | + grep -o "^func Fuzz[A-Za-z0-9_]*" "${file}" | sed 's/^func //' | while read -r func; do |
| 61 | + echo "{\"test\":\"${func}\",\"package\":\"${dir}\"}" |
| 62 | + done |
| 63 | + done | jq -s -c '.') |
| 64 | +
|
| 65 | + targets="${all_targets}" |
| 66 | +
|
| 67 | + # If a specific target is requested, validate and filter |
| 68 | + if [ -n "${TARGET_FILTER}" ]; then |
| 69 | + # Validate format: must start with "Fuzz" and contain only alphanumeric/underscore |
| 70 | + if ! echo "${TARGET_FILTER}" | grep -qE '^Fuzz[A-Za-z0-9_]+$'; then |
| 71 | + echo "::error::Invalid fuzz target format: '${TARGET_FILTER}'. Must match pattern 'Fuzz[A-Za-z0-9_]+'" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | +
|
| 75 | + # Filter to the requested target |
| 76 | + targets=$(echo "${all_targets}" | jq -c --arg t "${TARGET_FILTER}" '[.[] | select(.test == $t)]') |
| 77 | +
|
| 78 | + # Check if target exists |
| 79 | + if [ "${targets}" = "[]" ]; then |
| 80 | + echo "::error::Fuzz target '${TARGET_FILTER}' not found." |
| 81 | + echo "Available targets:" |
| 82 | + echo "${all_targets}" | jq -r '.[].test' | sort | sed 's/^/ - /' |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + fi |
| 86 | +
|
| 87 | + echo "targets=${targets}" >> "${GITHUB_OUTPUT}" |
| 88 | + echo "Discovered targets: ${targets}" |
| 89 | +
|
| 90 | + fuzz: |
| 91 | + if: ${{ github.repository == 'chainguard-dev/malcontent' && needs.discover.outputs.targets != '[]' }} |
| 92 | + needs: discover |
| 93 | + runs-on: ubuntu-latest-16-core |
| 94 | + permissions: |
| 95 | + contents: read |
| 96 | + strategy: |
| 97 | + fail-fast: false |
| 98 | + matrix: |
| 99 | + target: ${{ fromJson(needs.discover.outputs.targets) }} |
| 100 | + container: |
| 101 | + image: cgr.dev/chainguard/wolfi-base:latest # zizmor: ignore[unpinned-images] |
| 102 | + options: >- |
| 103 | + --cap-add DAC_OVERRIDE |
| 104 | + --cap-add SETGID |
| 105 | + --cap-add SETUID |
| 106 | + --cap-drop ALL |
| 107 | + --cgroupns private |
| 108 | + --cpu-shares=16384 |
| 109 | + --memory-swappiness=0 |
| 110 | + --security-opt no-new-privileges |
| 111 | + --ulimit core=0 |
| 112 | + --ulimit nofile=65535:65535 |
| 113 | + --ulimit nproc=65535:65535 |
| 114 | + steps: |
| 115 | + - name: Install dependencies |
| 116 | + run: | |
| 117 | + apk update |
| 118 | + apk add curl findutils git go nodejs upx xz yara-x~1.12.0 |
| 119 | +
|
| 120 | + - name: Checkout code |
| 121 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 122 | + with: |
| 123 | + persist-credentials: false |
| 124 | + |
| 125 | + - name: Trust repository |
| 126 | + run: git config --global --add safe.directory "${GITHUB_WORKSPACE}" |
| 127 | + |
| 128 | + - name: Clone malcontent samples (required for compile fuzzers) |
| 129 | + if: contains(matrix.target.package, 'compile') |
| 130 | + run: | |
| 131 | + make samples |
| 132 | +
|
| 133 | + - name: Run fuzzer - ${{ matrix.target.test }} |
| 134 | + env: |
| 135 | + FUZZ_TIME: ${{ inputs.fuzz_time || '30s' }} |
| 136 | + run: | |
| 137 | + go test -timeout 0 -fuzz="${{ matrix.target.test }}" -fuzztime="${FUZZ_TIME}" "${{ matrix.target.package }}" |
0 commit comments