verify_allowed_addons.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. https_everywhere_path = argv[2]
  29. addons = None
  30. with open(allowed_addons_path, 'r') as file:
  31. addons = json.loads(file.read())
  32. noscript_hash = None
  33. with open(noscript_path, 'rb') as file:
  34. noscript_hash = "sha256:" + hashlib.sha256(file.read()).hexdigest()
  35. noscript_version = read_extension_manifest(noscript_path)["version"]
  36. https_everywhere_version = read_extension_manifest(https_everywhere_path)["version"]
  37. verify_extension_hash(addons, '{73a6fe31-595d-460b-a920-fcc0f8843232}', noscript_hash)
  38. verify_extension_version(addons, '{73a6fe31-595d-460b-a920-fcc0f8843232}', noscript_version)
  39. verify_extension_version(addons, 'https-everywhere-eff@eff.org', https_everywhere_version)
  40. if __name__ == "__main__":
  41. main(sys.argv[1:])