Language: Python
Label: safe
CWE: CWE-770 \u2014 Allocation of Resources Without Limits or Throttling
The product allocates a reusable resource or group of resources on behalf of an actor without imposing any restrictions on the size or number of resources that can be allocated, in violation of the intended security policy for that actor.

Code:
def create_multipart_extractor(
    signature_field: "SignatureField", is_data_optional: bool
) -> Callable[["ASGIConnection[Any, Any, Any]"], Coroutine[Any, Any, Any]]:
    """Create a multipart form-data extractor.

    Args:
        signature_field: A SignatureField instance.
        is_data_optional: Boolean dictating whether the field is optional.

    Returns:
        An extractor function.
    """
    body_kwarg_multipart_form_part_limit: Optional[int] = None
    if signature_field.kwarg_model and isinstance(signature_field.kwarg_model, BodyKwarg):
        body_kwarg_multipart_form_part_limit = signature_field.kwarg_model.multipart_form_part_limit

    async def extract_multipart(
        connection: "Request[Any, Any]",
    ) -> Any:
        multipart_form_part_limit = (
            body_kwarg_multipart_form_part_limit
            if body_kwarg_multipart_form_part_limit is not None
            else connection.app.multipart_form_part_limit
        )
        connection.scope["_form"] = form_values = (  # type: ignore[typeddict-item]
            connection.scope["_form"]  # type: ignore[typeddict-item]
            if "_form" in connection.scope
            else parse_multipart_form(
                body=await connection.body(),
                boundary=connection.content_type[-1].get("boundary", "").encode(),
                multipart_form_part_limit=multipart_form_part_limit,
            )
        )

        if signature_field.is_non_string_sequence:
            return list(form_values.values())
        if signature_field.is_simple_type and signature_field.field_type is UploadFile and form_values:
            return [v for v in form_values.values() if isinstance(v, UploadFile)][0]

        return form_values if form_values or not is_data_optional else None

    return cast("Callable[[ASGIConnection[Any, Any, Any]], Coroutine[Any, Any, Any]]", extract_multipart)