Skip to content

Commit

Permalink
become even stricter with pickles
Browse files Browse the repository at this point in the history
no pickle shall pass
thank you again, RyotaK
  • Loading branch information
AUTOMATIC1111 committed Oct 11, 2022
1 parent a05c824 commit 66b7d75
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy
import _codecs
import zipfile
import re


# PyTorch 1.13 and later have _TypedStorage renamed to TypedStorage
Expand Down Expand Up @@ -54,11 +55,27 @@ def find_class(self, module, name):
raise pickle.UnpicklingError(f"global '{module}/{name}' is forbidden")


allowed_zip_names = ["archive/data.pkl", "archive/version"]
allowed_zip_names_re = re.compile(r"^archive/data/\d+$")


def check_zip_filenames(filename, names):
for name in names:
if name in allowed_zip_names:
continue
if allowed_zip_names_re.match(name):
continue

raise Exception(f"bad file inside {filename}: {name}")


def check_pt(filename):
try:

# new pytorch format is a zip file
with zipfile.ZipFile(filename) as z:
check_zip_filenames(filename, z.namelist())

with z.open('archive/data.pkl') as file:
unpickler = RestrictedUnpickler(file)
unpickler.load()
Expand Down

0 comments on commit 66b7d75

Please sign in to comment.