Files
Sparkledns/.gitea/workflows/virustotal.yml
T

117 lines
4.4 KiB
YAML

name: VirusTotal
# Scans the APKs attached to a release and adds the result to the release notes.
# Needs the VT_API_KEY secret (a free VirusTotal API key).
on:
release:
types: [published]
workflow_dispatch:
inputs:
tag:
description: Release tag to scan, e.g. v1.0.7
required: true
jobs:
scan:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Scan release APKs
env:
VT_API_KEY: ${{ 451d1ed95c835bf93365bbb609f4be3d877ec23c1935a72b2f346a531cee9758 }}
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
SERVER: ${{ github.server_url }}
REPO: ${{ github.repository }}
TAG: ${{ github.event.release.tag_name || inputs.tag }}
shell: bash
run: |
set -euo pipefail
if [ -z "${VT_API_KEY:-}" ]; then
echo "::error::VT_API_KEY secret is not set"
exit 1
fi
if ! command -v jq >/dev/null; then
(apt-get update -qq && apt-get install -y -qq jq) >/dev/null 2>&1 || apk add --no-cache jq >/dev/null
fi
API="$SERVER/api/v1/repos/$REPO"
AUTH="Authorization: token $GITEA_TOKEN"
VT="https://www.virustotal.com/api/v3"
# Assets can be attached a moment after the release is published
release=""
for _ in $(seq 1 10); do
release=$(curl -fsS -H "$AUTH" "$API/releases/tags/$TAG")
if [ "$(echo "$release" | jq '[.assets[] | select(.name | endswith(".apk"))] | length')" -gt 0 ]; then
break
fi
echo "No APK on $TAG yet, waiting..."
sleep 30
done
release_id=$(echo "$release" | jq -r '.id')
apks=$(echo "$release" | jq -c '.assets[] | select(.name | endswith(".apk"))')
if [ -z "$apks" ]; then
echo "::error::Release $TAG has no APK attached"
exit 1
fi
lines=""
while IFS= read -r asset; do
name=$(echo "$asset" | jq -r '.name')
url=$(echo "$asset" | jq -r '.browser_download_url')
echo "Downloading $name"
curl -fsSL -H "$AUTH" -o "$name" "$url"
sha=$(sha256sum "$name" | cut -d' ' -f1)
size=$(stat -c %s "$name")
# Files over 32 MB need a one-time upload URL
upload="$VT/files"
if [ "$size" -gt 33554432 ]; then
upload=$(curl -fsS -H "x-apikey: $VT_API_KEY" "$VT/files/upload_url" | jq -r '.data')
fi
echo "Uploading $name ($sha) to VirusTotal"
analysis=$(curl -fsS -H "x-apikey: $VT_API_KEY" -F "file=@$name" "$upload" | jq -r '.data.id')
# The free API allows 4 requests per minute
stats=""
for _ in $(seq 1 40); do
sleep 20
result=$(curl -fsS -H "x-apikey: $VT_API_KEY" "$VT/analyses/$analysis")
if [ "$(echo "$result" | jq -r '.data.attributes.status')" = "completed" ]; then
stats=$(echo "$result" | jq -c '.data.attributes.stats')
break
fi
done
link="https://www.virustotal.com/gui/file/$sha"
if [ -z "$stats" ]; then
echo "::warning::$name: analysis did not finish in time"
lines+="- \`$name\`: scan still running, [report]($link)"$'\n'
continue
fi
bad=$(echo "$stats" | jq '.malicious + .suspicious')
total=$(echo "$stats" | jq '.malicious + .suspicious + .undetected + .harmless')
mark="✅"
[ "$bad" -gt 0 ] && mark="⚠️"
echo "$name: $bad/$total"
lines+="- \`$name\`: $bad/$total $mark [report]($link)"$'\n'
done <<< "$apks"
# Replace an earlier VirusTotal block instead of adding a second one
body=$(echo "$release" | jq -r '.body // ""' \
| awk '/<!-- virustotal -->/{skip=1} !skip{print} /<!-- \/virustotal -->/{skip=0}')
block=$(printf '<!-- virustotal -->\n**VirusTotal**\n%s<!-- /virustotal -->' "$lines")
if [ -n "$body" ]; then
new_body=$(printf '%s\n\n%s' "$body" "$block")
else
new_body="$block"
fi
jq -n --arg body "$new_body" '{body: $body}' \
| curl -fsS -X PATCH -H "$AUTH" -H "Content-Type: application/json" \
-d @- "$API/releases/$release_id" >/dev/null
echo "Release notes updated"