From f6dbb848d0fd73aef2e996fbf011cfdff4dbbe4f Mon Sep 17 00:00:00 2001 From: harumaki4649 <83683593+harumaki4649@users.noreply.github.com> Date: Sat, 11 Jul 2026 08:37:13 +0900 Subject: [PATCH] Validate message file upload limit --- discord/http.py | 3 +++ discord/webhook/async_.py | 3 +++ tests/test_http.py | 53 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 tests/test_http.py diff --git a/discord/http.py b/discord/http.py index 639b1e7a7..eb58b785c 100644 --- a/discord/http.py +++ b/discord/http.py @@ -173,6 +173,9 @@ def handle_message_parameters( if attachments is not MISSING and files is not MISSING: raise TypeError('Cannot mix attachments and files keyword arguments.') + if files is not MISSING and len(files) > 10: + raise ValueError('files has a maximum of 10 elements.') + payload = {} if embeds is not MISSING: if len(embeds) > 10: diff --git a/discord/webhook/async_.py b/discord/webhook/async_.py index 2ef0102c8..c30d286f4 100644 --- a/discord/webhook/async_.py +++ b/discord/webhook/async_.py @@ -571,6 +571,9 @@ def interaction_message_response_params( if attachments is not MISSING and files is not MISSING: raise TypeError('Cannot mix attachments and files keyword arguments.') + if files is not MISSING and len(files) > 10: + raise ValueError('files has a maximum of 10 elements.') + data: Optional[Dict[str, Any]] = { 'tts': tts, } diff --git a/tests/test_http.py b/tests/test_http.py new file mode 100644 index 000000000..382881857 --- /dev/null +++ b/tests/test_http.py @@ -0,0 +1,53 @@ +""" +The MIT License (MIT) + +Copyright (c) 2015-present Rapptz + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +""" + +from __future__ import annotations + +from io import BytesIO + +import discord +import pytest + +from discord.http import handle_message_parameters +from discord.webhook.async_ import interaction_message_response_params + + +def make_file(index: int) -> discord.File: + return discord.File(BytesIO(b'test'), filename=f'test-{index}.txt') + + +def test_handle_message_parameters_rejects_too_many_files(): + files = [make_file(index) for index in range(11)] + + with pytest.raises(ValueError, match='files has a maximum of 10 elements'): + handle_message_parameters(content='test', files=files) + + +def test_interaction_message_response_params_rejects_too_many_files(): + files = [make_file(index) for index in range(11)] + + with pytest.raises(ValueError, match='files has a maximum of 10 elements'): + interaction_message_response_params(type=4, content='test', files=files) + +