From e396fff790262ade17078acb1877e617232e058b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A6=E9=BE=99=E8=A8=80?= Date: Tue, 22 Sep 2026 15:30:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E4=B8=A5=E6=A0=BC?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E6=B5=8B=E8=AF=95=E6=89=B9=E6=AC=A1=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module_business/dependency/test_batch_dependency.py | 7 +++---- .../tests/module_business/test_test_batch_access.py | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/shierfeng-fastapi-backend/module_business/dependency/test_batch_dependency.py b/shierfeng-fastapi-backend/module_business/dependency/test_batch_dependency.py index 9fe68ff..1185ae3 100644 --- a/shierfeng-fastapi-backend/module_business/dependency/test_batch_dependency.py +++ b/shierfeng-fastapi-backend/module_business/dependency/test_batch_dependency.py @@ -14,10 +14,9 @@ def parse_optional_positive_batch_id(raw: str | None) -> int | None: """解析可选的测试批次请求头。""" if raw is None: return None - try: - batch_id = int(raw) - except (TypeError, ValueError): - raise ServiceWarning('', '测试批次标识必须为正整数') from None + if not isinstance(raw, str) or not raw or not raw.isascii() or not raw.isdigit(): + raise ServiceWarning('', '测试批次标识必须为正整数') + batch_id = int(raw) if batch_id <= 0: raise ServiceWarning('', '测试批次标识必须为正整数') return batch_id diff --git a/shierfeng-fastapi-backend/tests/module_business/test_test_batch_access.py b/shierfeng-fastapi-backend/tests/module_business/test_test_batch_access.py index 46e182c..7678031 100644 --- a/shierfeng-fastapi-backend/tests/module_business/test_test_batch_access.py +++ b/shierfeng-fastapi-backend/tests/module_business/test_test_batch_access.py @@ -82,6 +82,11 @@ class TestBatchHeaderTest(unittest.TestCase): with self.subTest(raw=raw), self.assertRaisesRegex(ServiceWarning, '测试批次标识必须为正整数'): parse_optional_positive_batch_id(raw) + def test_python_coercible_values_are_not_valid_ascii_decimal_literals(self) -> None: + for raw in (' 7', '7 ', '+7', '1_0', '7', True, 7, b'7'): + with self.subTest(raw=raw), self.assertRaisesRegex(ServiceWarning, '测试批次标识必须为正整数'): + parse_optional_positive_batch_id(raw) + class TestBatchAccessTest(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self) -> None: