Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workflows: add virus scan check (bug 1881777) #1633

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/deploy-gui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,5 @@ jobs:
- name: Print signing manifest
run: |
python ./bin/adhoc-sign.py --os windows ${{ github.event.release.tag_name }}
- name: Test binary for viruses
run: ./bin/virustotal-scan.py ${{ secrets.VIRUSTOTAL_API_KEY }}
78 changes: 78 additions & 0 deletions bin/virustotal-scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#! /usr/bin/env python

import argparse
import requests
from time import sleep
import sys


class APIWrapper:
def __init__(self, apikey):
self.headers = {
"accept": "application/json",
"x-apikey": apikey,
}

def get(self, path):
return requests.get(path, headers=self.headers)

def post(self, path, **kwargs):
return requests.post(path, headers=self.headers, **kwargs)


def main(args: argparse.Namespace):
api = APIWrapper(args.apikey[0])
base_url = "https://www.virustotal.com/api/v3"

file_upload_url_response = api.get(f"{base_url}/files/upload_url")
file_upload_url = file_upload_url_response.json()["data"]

files = {
"file": (
"mozregression-gui.exe",
open("./gui/wininst/mozregression-gui.exe", "rb"),
"application/octet-stream",
),
}

upload_response = api.post(file_upload_url, files=files)
analysis_url = upload_response.json()["data"]["links"]["self"]

while analysis_response := api.get(analysis_url):
# Sleep for one second to throttle requests to VirusTotal.
sleep(1)

if analysis_response.status_code != 200:
# Analysis endpoint does not exist yet, try again...
continue

analysis = analysis_response.json()
if analysis["data"]["attributes"]["status"].lower() != "completed":
# Analysis has not completed yet, try again...
continue
else:
break

stats = analysis["data"]["attributes"]["stats"]
if stats["malicious"]:
sys.stdout.write("VirusTotal scan failed.\n")
for key, value in stats.items():
sys.stderr.write(f"{key}: {value}\n")
sys.stdout.write(f"Analysis ID: {analysis['data']['id']}\n")
sys.exit(1)
else:
sys.stdout.write("VirusTotal scan passed.\n")
sys.stdout.write(f"Analysis ID: {analysis['data']['id']}\n")
sys.exit()


def create_parser():
parser = argparse.ArgumentParser(description="send file for VirusTotal scanning")
parser.add_argument("apikey", nargs=1, help="VirusTotal API Key")
return parser


if __name__ == "__main__":
parser = create_parser()
args = parser.parse_args()
main(args)