Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): reject conflicting multipart file shapes (#44903)

Winoren2c3cbd50455a
diff --git a/api/controllers/openapi/_multipart.py b/api/controllers/openapi/_multipart.py
index b994e59a6ad..fdd8ba07a0e 100644
--- a/api/controllers/openapi/_multipart.py
+++ b/api/controllers/openapi/_multipart.py
@@ -54,7 +54,7 @@ def _place(body: dict[str, Any], name: str, parts: list[FileStorage], *, file_fi
value: FileStorage | list[FileStorage] = parts if many else parts[0]
target = body if key is None else body.setdefault(field, {})
leaf = field if key is None else key
- if leaf in target:
+ if not isinstance(target, dict) or leaf in target:
raise InvalidFilePart(f"{name}: given more than once")
target[leaf] = value
diff --git a/api/tests/unit_tests/controllers/openapi/test__multipart.py b/api/tests/unit_tests/controllers/openapi/test__multipart.py
index 08502de53db..59229e466d5 100644
--- a/api/tests/unit_tests/controllers/openapi/test__multipart.py
+++ b/api/tests/unit_tests/controllers/openapi/test__multipart.py
@@ -66,3 +66,14 @@ def test_multipart_places_every_part_name_shape(app: Flask) -> None:
def test_multipart_rejects_malformed_bodies(app: Flask, data: Mapping[str, object], reason: str) -> None:
with _multipart(app, data), pytest.raises(InvalidFilePart, match=reason):
body_from_request(file_fields=_FILE_FIELDS)
+
+
[email protected]("direct", ["files", "files[]"])
[email protected]("mapped", ["files[doc]", "files[doc][]"])
[email protected]("mapped_first", [False, True], ids=["direct_first", "mapped_first"])
+def test_multipart_rejects_conflicting_file_shapes(app: Flask, direct: str, mapped: str, mapped_first: bool) -> None:
+ names = [mapped, direct] if mapped_first else [direct, mapped]
+ data = {name: _part() for name in names}
+ with _multipart(app, data), pytest.raises(InvalidFilePart, match="given more than once") as exc:
+ body_from_request(file_fields=_FILE_FIELDS)
+ assert exc.value.code == 422