From a065a9081f3a2be90903ea6f9a5cf27899dd0c38 Mon Sep 17 00:00:00 2001 From: Thomas Ward Date: Thu, 4 Jul 2024 14:38:18 -0400 Subject: [PATCH] server.py: compare_image_hash configurable hash function Uses an argument added in cli_args to specify the type of hashing to default to for duplicate hash checking. Uses an `eval()` to identify the specific hashlib class to utilize, but ultimately safely operates because we have specific options and only those options/choices in the arg parser. So we don't have any unsafe input there. --- server.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server.py b/server.py index 38b1bab80..dbcfad27a 100644 --- a/server.py +++ b/server.py @@ -156,10 +156,16 @@ class PromptServer(): return type_dir, dir_type def compare_image_hash(filepath, image): + # This eval operation is *safe* because we can only accept certain strings passed in via args. + # Because that can only be one of 'md5', 'sha1', 'sha256', or 'sha512' which we already define + # in comfy.cli_args as an argument passable to the args and only from a set of options, we can + # safely use an eval here, as the eval data is "safe" already. + hasher = eval(f"hashlib.{args.duplicate_check_hash_function}") + # function to compare hashes of two images to see if it already exists, fix to #3465 if os.path.exists(filepath): - a = hashlib.sha256() - b = hashlib.sha256() + a = hasher() + b = hasher() with open(filepath, "rb") as f: a.update(f.read()) b.update(image.file.read())