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:
初心Yearth
2026-08-17 16:08:31 -07:00
committed by GitHub
co-authored by Guillaume Meyer
parent 07be21e54a
commit 5e43d53fc3
2 changed files with 24 additions and 1 deletions
+5 -1
View File
@@ -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)
+19
View File
@@ -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