verify_allowed_addons.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. import json
  3. import sys
  4. import hashlib
  5. import zipfile
  6. def find_addon(addons, addon_id):
  7. results = addons['results']
  8. for x in results:
  9. addon = x['addon']
  10. if addon['guid'] == addon_id:
  11. return addon
  12. sys.exit("Error: cannot find addon " + addon_id)
  13. def verify_extension_version(addons, addon_id, version):
  14. addon = find_addon(addons, addon_id)
  15. expected_version = addon['current_version']['version']
  16. if version != expected_version:
  17. sys.exit("Error: version " + version + " != " + expected_version)
  18. def verify_extension_hash(addons, addon_id, hash):
  19. addon = find_addon(addons, addon_id)
  20. expected_hash = addon["current_version"]["files"][0]["hash"]
  21. if hash != expected_hash:
  22. sys.exit("Error: hash " + hash + " != " + expected_hash)
  23. def read_extension_manifest(path):
  24. return json.loads(zipfile.ZipFile(path, 'r').read('manifest.json'))
  25. def main(argv):
  26. allowed_addons_path = argv[0]
  27. noscript_path = argv[1]
  28. addons = None
  29. with open(allowed_addons_path, 'r') as file:
  30. addons = json.loads(file.read())
  31. noscript_hash = None
  32. with open(noscript_path, 'rb') as file:
  33. noscript_hash = "sha256:" + hashlib.sha256(file.read()).hexdigest()
  34. noscript_version = read_extension_manifest(noscript_path)["version"]
  35. verify_extension_hash(addons, '{73a6fe31-595d-460b-a920-fcc0f8843232}', noscript_hash)
  36. verify_extension_version(addons, '{73a6fe31-595d-460b-a920-fcc0f8843232}', noscript_version)
  37. if __name__ == "__main__":
  38. main(sys.argv[1:])