Skip to content

Instantly share code, notes, and snippets.

@rossja
Last active April 9, 2024 00:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossja/d84a93e5c6b8dd2d4a538aa010b29163 to your computer and use it in GitHub Desktop.
Save rossja/d84a93e5c6b8dd2d4a538aa010b29163 to your computer and use it in GitHub Desktop.
Huggingface SFConvertbot Pull Request Scanner

HuggingFace SF_Convertbot Scanner

This script is designed to assist in identifying pull requests to HuggingFace repositories that are sourced from the SFConvertbot user.

The SFConvertbot user is part of an automated tool used by HuggingFace to provide safetensor versions of models. As published by HiddenLayer this bot can be used by malicious actors to potentially insert malicious content into models.

This tool is a simple script to query all models released by a HuggingFace author, and checks all pull requests for those models to see if any were submitted by the SFConvertbot user.

An author can use this on their own models to determine whether they may have been compromised. Likewise, a consumer of models can run this on an author's models they use to do the same.

License

This code is released under the BSD-3-Clause Open Source License.

Copyright 2024 Jason Ross <algorythm@gmail.com>

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#!/usr/bin/env python3
#
# This script is designed to assist in identifying pull requests to HuggingFace repositories
# that are sourced from the `SFConvertbot` user.
# The `SFConvertbot` user is part of an automated tool used by HuggingFace to provide safetensor
# versions of models. As published by [HiddenLayer](https://hiddenlayer.com/research/silent-sabotage/)
# this bot can be used by malicious actors to potentially insert malicious content into models.
#
# This code is released under the [BSD-3-Clause Open Source License](https://opensource.org/license/bsd-3-clause).
# Copyright 2024 Jason Ross <algorythm@gmail.com>
import sys
# default to openai if an author wasn't specified at runtime
author = sys.argv[1] if len(sys.argv) >= 2 else 'openai'
# csv output (this gets appended to each time it's run)
cbot_countfile = 'convertbot_count.csv'
def get_models_by_author(author):
"""get the list of models a huggingface author has published"""
from huggingface_hub import HfApi
api = HfApi()
models = api.list_models(author=author)
return models
def get_discussions_by_model(model):
"""get the list of discussions associated with a given model"""
import requests
discussions_url = 'https://huggingface.co/api/models/' + model + '/discussions?status=open&type=pull_request'
try:
discussions = requests.get(discussions_url)
except:
print(f"couldn't get discussions for {model}")
discussions = {"discussions": [],"count": 0,"error": "couldn't get discussions for {model}"}
return discussions
def get_certbot_count(discussions):
"""grab the count of SFconverbot pull requests"""
convertbot_count = 0
for thread in discussions:
if thread['author']['name'] == 'SFconvertbot' and thread['isPullRequest'] == True:
convertbot_count += 1
return convertbot_count
def add_to_csv(model, convertbot_count):
"""add data to the csv output file"""
import csv
with open(cbot_countfile, 'a') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow([model, convertbot_count])
if __name__ == '__main__':
"""run all the things"""
try:
models = get_models_by_author(author)
except:
print(f"couldn't get models")
failed_checks = []
add_to_csv('model', 'convertbot_count')
for model in models:
model_id = model.id
print(f"checking {model_id}...")
try:
discussions = get_discussions_by_model(model_id).json()['discussions']
except:
print(f"couldn't get discussions for {model_id}")
failed_checks.append(model_id)
# discussions = {"discussions": [],"count": 0,"error": "couldn't get discussions for {model_id}"}
continue
certbot_count = get_certbot_count(discussions)
add_to_csv(model_id, certbot_count)
with open("failed_checks.txt", "w") as f:
for failed in failed_checks:
f.write(f"{failed}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment