Rename `form_max_part_size` to `max_part_size`

This commit is contained in:
Yurii Motov 2025-12-03 22:52:36 +01:00
parent 6b670a9d77
commit 45b472be8d
3 changed files with 14 additions and 20 deletions

View File

@ -2646,14 +2646,11 @@ class FastAPI(Starlette):
""" """
), ),
] = 1000, ] = 1000,
form_max_part_size: Annotated[ max_part_size: Annotated[
int, int,
Doc( Doc(
""" """
Maximum size (in bytes) for each part in a form submission. Maximum size (in bytes) for each part in a multipart form submission.
This limits the size of each part in a form submission to prevent
potential denial-of-service attacks.
""" """
), ),
] = 1024 * 1024, ] = 1024 * 1024,
@ -2704,7 +2701,7 @@ class FastAPI(Starlette):
generate_unique_id_function=generate_unique_id_function, generate_unique_id_function=generate_unique_id_function,
form_max_fields=form_max_fields, form_max_fields=form_max_fields,
form_max_files=form_max_files, form_max_files=form_max_files,
form_max_part_size=form_max_part_size, max_part_size=max_part_size,
) )
def delete( def delete(

View File

@ -302,7 +302,7 @@ def get_request_handler(
embed_body_fields: bool = False, embed_body_fields: bool = False,
form_max_fields: int = 1000, form_max_fields: int = 1000,
form_max_files: int = 1000, form_max_files: int = 1000,
form_max_part_size: int = 1024 * 1024, max_part_size: int = 1024 * 1024,
) -> Callable[[Request], Coroutine[Any, Any, Response]]: ) -> Callable[[Request], Coroutine[Any, Any, Response]]:
assert dependant.call is not None, "dependant.call must be a function" assert dependant.call is not None, "dependant.call must be a function"
is_coroutine = dependant.is_coroutine_callable is_coroutine = dependant.is_coroutine_callable
@ -329,7 +329,7 @@ def get_request_handler(
body = await request.form( body = await request.form(
max_fields=form_max_fields, max_fields=form_max_fields,
max_files=form_max_files, max_files=form_max_files,
max_part_size=form_max_part_size, max_part_size=max_part_size,
) )
file_stack.push_async_callback(body.close) file_stack.push_async_callback(body.close)
else: else:
@ -544,7 +544,7 @@ class APIRoute(routing.Route):
] = Default(generate_unique_id), ] = Default(generate_unique_id),
form_max_fields: int = 1000, form_max_fields: int = 1000,
form_max_files: int = 1000, form_max_files: int = 1000,
form_max_part_size: int = 1024 * 1024, max_part_size: int = 1024 * 1024,
) -> None: ) -> None:
self.path = path self.path = path
self.endpoint = endpoint self.endpoint = endpoint
@ -577,7 +577,7 @@ class APIRoute(routing.Route):
self.path_regex, self.path_format, self.param_convertors = compile_path(path) self.path_regex, self.path_format, self.param_convertors = compile_path(path)
self.form_max_fields = form_max_fields self.form_max_fields = form_max_fields
self.form_max_files = form_max_files self.form_max_files = form_max_files
self.form_max_part_size = form_max_part_size self.max_part_size = max_part_size
if methods is None: if methods is None:
methods = ["GET"] methods = ["GET"]
self.methods: Set[str] = {method.upper() for method in methods} self.methods: Set[str] = {method.upper() for method in methods}
@ -676,7 +676,7 @@ class APIRoute(routing.Route):
embed_body_fields=self._embed_body_fields, embed_body_fields=self._embed_body_fields,
form_max_fields=self.form_max_fields, form_max_fields=self.form_max_fields,
form_max_files=self.form_max_files, form_max_files=self.form_max_files,
form_max_part_size=self.form_max_part_size, max_part_size=self.max_part_size,
) )
def matches(self, scope: Scope) -> Tuple[Match, Scope]: def matches(self, scope: Scope) -> Tuple[Match, Scope]:
@ -1056,7 +1056,7 @@ class APIRouter(routing.Router):
generate_unique_id_function=current_generate_unique_id, generate_unique_id_function=current_generate_unique_id,
form_max_fields=form_max_fields, form_max_fields=form_max_fields,
form_max_files=form_max_files, form_max_files=form_max_files,
form_max_part_size=form_max_part_size, max_part_size=form_max_part_size,
) )
self.routes.append(route) self.routes.append(route)
@ -2581,14 +2581,11 @@ class APIRouter(routing.Router):
""" """
), ),
] = 1000, ] = 1000,
form_max_part_size: Annotated[ max_part_size: Annotated[
int, int,
Doc( Doc(
""" """
Maximum size (in bytes) for each part in a form submission. Maximum size (in bytes) for each part in a multipart form submission.
This limits the size of each part in a form submission to prevent
potential denial-of-service attacks.
""" """
), ),
] = 1024 * 1024, ] = 1024 * 1024,
@ -2643,7 +2640,7 @@ class APIRouter(routing.Router):
generate_unique_id_function=generate_unique_id_function, generate_unique_id_function=generate_unique_id_function,
form_max_fields=form_max_fields, form_max_fields=form_max_fields,
form_max_files=form_max_files, form_max_files=form_max_files,
form_max_part_size=form_max_part_size, form_max_part_size=max_part_size,
) )
def delete( def delete(

View File

@ -6,7 +6,7 @@ from fastapi.testclient import TestClient
app = FastAPI() app = FastAPI()
@app.post("/", form_max_files=2, form_max_part_size=1024, form_max_fields=2) @app.post("/", form_max_files=2, max_part_size=1024, form_max_fields=2)
async def upload_files(files: List[UploadFile] = File(...)): async def upload_files(files: List[UploadFile] = File(...)):
return {"filenames": [file.filename for file in files]} return {"filenames": [file.filename for file in files]}
@ -54,7 +54,7 @@ def test_max_part_size_exceeds_custom_limit():
"small content\r\n" "small content\r\n"
f"--{boundary}\r\n" f"--{boundary}\r\n"
f'Content-Disposition: form-data; name="large"\r\n\r\n' f'Content-Disposition: form-data; name="large"\r\n\r\n'
+ ("x" * 1024 * 10 + "x") # 1MB + 1 byte of data + ("x" * 1024 + "x") # 1KB + 1 byte of data
+ "\r\n" + "\r\n"
f"--{boundary}--\r\n" f"--{boundary}--\r\n"
).encode("utf-8") ).encode("utf-8")