mirror of
https://github.com/guillaumemeyer/watermarks-remover.git
synced 2026-08-22 13:11:57 +02:00
fix: validate clean option types (#111)
Co-authored-by: Guillaume Meyer (The Opinionated Man) <1385518+guillaumemeyer@users.noreply.github.com>
This commit is contained in:
co-authored by
Guillaume Meyer
parent
07be21e54a
commit
5e43d53fc3
@@ -462,9 +462,13 @@ class Handler(BaseHTTPRequestHandler):
|
||||
options = {}
|
||||
if not isinstance(options, dict):
|
||||
raise ValueError("'options' must be an object")
|
||||
for key in options:
|
||||
for key, value in options.items():
|
||||
if key not in ALLOWED_CLEAN_OPTIONS:
|
||||
raise ValueError(f"unknown option: {key}")
|
||||
expected_type = ALLOWED_CLEAN_OPTIONS[key]
|
||||
if not isinstance(value, expected_type):
|
||||
type_name = "boolean" if expected_type is bool else "string"
|
||||
raise ValueError(f"option {key!r} must be a {type_name}")
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix="wm-clean-") as tmp:
|
||||
tmpdir = Path(tmp)
|
||||
|
||||
@@ -182,6 +182,25 @@ def test_unknown_option_rejected(conn):
|
||||
assert "unknown option" in body["error"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("key", "value", "type_name"),
|
||||
[
|
||||
("nfkc", "false", "boolean"),
|
||||
("aggressive_homoglyphs", 1, "boolean"),
|
||||
("keep_non_ai_metadata", None, "boolean"),
|
||||
("also_layer_a_text", {}, "boolean"),
|
||||
("strip_all_metadata", [], "boolean"),
|
||||
("remove_pixel", False, "string"),
|
||||
],
|
||||
)
|
||||
def test_option_wrong_type_rejected(conn, key, value, type_name):
|
||||
status, body = _post(
|
||||
conn, "/clean", {"file": _b64(b"x"), "name": "x.txt", "options": {key: value}}
|
||||
)
|
||||
assert status == 400
|
||||
assert body["error"] == f"option '{key}' must be a {type_name}"
|
||||
|
||||
|
||||
def test_bad_base64_rejected(conn):
|
||||
status, _body = _post(conn, "/inspect", {"file": "!!!not-base64!!!"})
|
||||
assert status == 400
|
||||
|
||||
Reference in New Issue
Block a user