diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 42ce804dd0..0d225b45c9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -883,6 +883,16 @@ jobs: variants: ${{ needs.build.outputs.variants }} secrets: inherit + scan: + needs: sign + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' + permissions: + contents: read + uses: ./.github/workflows/scan.yml + with: + artifact: droiddeck-signed + secrets: inherit + packs: needs: [build, checks] if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a151592681..eabc77a358 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -63,6 +63,15 @@ jobs: artifact: droiddeck-release secrets: inherit + scan: + needs: sign + permissions: + contents: read + uses: ./.github/workflows/scan.yml + with: + artifact: droiddeck-release + secrets: inherit + packs: needs: find permissions: diff --git a/.github/workflows/scan.yml b/.github/workflows/scan.yml new file mode 100644 index 0000000000..408983955b --- /dev/null +++ b/.github/workflows/scan.yml @@ -0,0 +1,45 @@ +name: Scan APKs + +on: + workflow_call: + inputs: + artifact: + description: Prefix of the signed artifacts to scan, as passed to sign.yml + type: string + required: true + fail_on_detection: + type: boolean + default: false + +permissions: + contents: read + +jobs: + virustotal: + name: VirusTotal + runs-on: ubuntu-24.04 + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + sparse-checkout: tools/release + + - uses: actions/download-artifact@v4 + with: + pattern: ${{ inputs.artifact }}-* + merge-multiple: true + path: scan + + - name: Scan + continue-on-error: ${{ !inputs.fail_on_detection }} + env: + VT_API_KEY: ${{ secrets.VIRUSTOTAL_API_KEY }} + FAIL_ON_DETECTION: ${{ inputs.fail_on_detection && '1' || '0' }} + run: | + set -euo pipefail + if [ -z "$VT_API_KEY" ]; then + echo "::warning title=VirusTotal::no VIRUSTOTAL_API_KEY secret; skipping the scan" + exit 0 + fi + bash tools/release/virustotal-scan.sh scan/*.apk diff --git a/tools/release/virustotal-scan.sh b/tools/release/virustotal-scan.sh new file mode 100755 index 0000000000..8138a428ae --- /dev/null +++ b/tools/release/virustotal-scan.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# Usage: virustotal-scan.sh ... +# Scans each file on VirusTotal and writes the verdicts to $GITHUB_STEP_SUMMARY. +# Exits 1 when an engine flags a file and FAIL_ON_DETECTION=1. +set -euo pipefail + +API=${VT_API:-https://www.virustotal.com/api/v3} +: "${VT_API_KEY:?VT_API_KEY is not set}" +POLL_SECONDS=${POLL_SECONDS:-30} +TIMEOUT_SECONDS=${TIMEOUT_SECONDS:-1200} +SUMMARY=${GITHUB_STEP_SUMMARY:-/dev/stdout} + +vt() { + curl -sS --fail-with-body --retry 5 --retry-delay 20 --retry-all-errors \ + -H "x-apikey: $VT_API_KEY" "$@" +} + +# Workflow commands end at a newline and decode %XX sequences. +warn() { + local message=$1 + message=${message//'%'/'%25'} + message=${message//$'\r'/'%0D'} + message=${message//$'\n'/'%0A'} + printf '::warning title=VirusTotal::%s\n' "$message" +} + +# The free API allows 4 requests a minute; poll slowly. +wait_for_analysis() { + local id=$1 waited=0 status + while :; do + status=$(vt "$API/analyses/$id" | jq -r .data.attributes.status) + [ "$status" = completed ] && return 0 + [ "$waited" -ge "$TIMEOUT_SECONDS" ] && return 1 + sleep "$POLL_SECONDS" + waited=$((waited + POLL_SECONDS)) + done +} + +flagged_total=0 +{ + echo "### VirusTotal" + echo + echo "| File | Flagged | Engines | Detections |" + echo "| --- | --- | --- | --- |" +} >> "$SUMMARY" + +for file in "$@"; do + name=$(basename "$file") + sha=$(sha256sum "$file" | cut -d' ' -f1) + link="https://www.virustotal.com/gui/file/$sha" + + known=$(curl -sS --retry 5 --retry-delay 20 -o /dev/null -w '%{http_code}' \ + -H "x-apikey: $VT_API_KEY" "$API/files/$sha") + if [ "$known" != 200 ] && [ "$known" != 404 ]; then + echo "::error title=VirusTotal::hash lookup for $name returned HTTP $known" + exit 1 + fi + if [ "$known" = 404 ]; then + # Files over 32 MB need a one-time upload URL. + url=$(vt "$API/files/upload_url" | jq -r .data) + id=$(vt -X POST -F "file=@$file" "$url" | jq -r .data.id) + echo "uploaded $name ($sha), analysis $id" + if ! wait_for_analysis "$id"; then + warn "$name was still being analysed after ${TIMEOUT_SECONDS}s: $link" + echo "| [$name]($link) | pending | | |" >> "$SUMMARY" + continue + fi + fi + + report=$(vt "$API/files/$sha") + malicious=$(jq -r '.data.attributes.last_analysis_stats.malicious // 0' <<<"$report") + suspicious=$(jq -r '.data.attributes.last_analysis_stats.suspicious // 0' <<<"$report") + engines=$(jq -r '[(.data.attributes.last_analysis_stats // {})[]] | add // 0' <<<"$report") + detections=$(jq -r '[(.data.attributes.last_analysis_results // {})[] + | select(.category == "malicious" or .category == "suspicious") + | "\(.engine_name): \(.result)"] | join("
")' <<<"$report") + flagged=$((malicious + suspicious)) + flagged_total=$((flagged_total + flagged)) + echo "$name: $flagged/$engines flagged $link" + echo "| [$name]($link) | $flagged | $engines | ${detections:-none} |" >> "$SUMMARY" + if [ "$flagged" -gt 0 ]; then + warn "$name flagged by $flagged/$engines engines: ${detections//
/, } $link" + fi +done + +if [ "$flagged_total" -gt 0 ] && [ "${FAIL_ON_DETECTION:-0}" = 1 ]; then + echo "::error::VirusTotal flagged the release" + exit 1 +fi