diff --git a/roles/svc-ai-mcp-adapter/files/python/policy.py b/roles/svc-ai-mcp-adapter/files/python/policy.py index e49201e508..d9f606be43 100644 --- a/roles/svc-ai-mcp-adapter/files/python/policy.py +++ b/roles/svc-ai-mcp-adapter/files/python/policy.py @@ -391,6 +391,23 @@ def authorize_call( return method, str(spec["path"]) +def request_length(header: str | None) -> int: + """ + Args: + header: the raw ``Content-Length`` value, ``None`` when the client sent + none. + + Returns: + the announced body size in bytes; ``0`` without a header, since RFC 9112 + reads such a request as bodiless; a negative number when the value is + negative or no integer. + """ + try: + return int(header or 0) + except ValueError: + return -1 + + def clamp_page(contract: Mapping[str, Any], requested: Any) -> int: """Return a page size no larger than the contract allows. diff --git a/roles/svc-ai-mcp-adapter/files/python/server.py b/roles/svc-ai-mcp-adapter/files/python/server.py index 659c408c8e..420070bd88 100644 --- a/roles/svc-ai-mcp-adapter/files/python/server.py +++ b/roles/svc-ai-mcp-adapter/files/python/server.py @@ -413,10 +413,7 @@ class Handler(BaseHTTPRequestHandler): self._respond(401, {"error": policy.DENY_UNAUTHENTICATED}) return - try: - length = int(self.headers.get("Content-Length") or 0) - except ValueError: - length = -1 + length = policy.request_length(self.headers.get("Content-Length")) if length < 0: self._respond(400, {"error": "invalid_content_length"}) return diff --git a/tests/unit/python/cli/contributing/update/merge/test_gate.py b/tests/unit/python/cli/contributing/update/merge/test_gate.py index 41c4da4627..826aa062d0 100644 --- a/tests/unit/python/cli/contributing/update/merge/test_gate.py +++ b/tests/unit/python/cli/contributing/update/merge/test_gate.py @@ -3,6 +3,7 @@ from __future__ import annotations import unittest from cli.contributing.update.merge import gate +from utils.roles.mapping import ROLE_FILE_META_SERVICES REPO = "infinito-nexus/core" @@ -24,7 +25,7 @@ def check(**overrides): "name": "🧹 Lint", "status": "completed", "conclusion": "success", - "details_url": "https://github.com/o/r/actions/runs/1/job/2", + "details_url": "https://github.example/o/r/actions/runs/1/job/2", } run.update(overrides) return run @@ -75,7 +76,7 @@ class TestCheckRefusals(unittest.TestCase): ) def test_only_the_own_run_left_refuses(self): - own = check(details_url="https://github.com/o/r/actions/runs/9/job/3") + own = check(details_url="https://github.example/o/r/actions/runs/9/job/3") self.assertEqual( gate.check_refusals([own], [], own_run_id=9), ["no check run on the head commit"], @@ -92,8 +93,8 @@ class TestCheckRefusals(unittest.TestCase): class TestRoles(unittest.TestCase): def test_changed_roles_reads_the_role_directory(self): paths = [ - "roles/web-app-nextcloud/meta/services.yml", - "roles/sys-lint/meta/services.yml", + f"roles/web-app-nextcloud/{ROLE_FILE_META_SERVICES}", + f"roles/sys-lint/{ROLE_FILE_META_SERVICES}", "pyproject.toml", "docs/roles/web-app-matomo.md", ] diff --git a/tests/unit/python/cli/contributing/update/merge/test_github.py b/tests/unit/python/cli/contributing/update/merge/test_github.py index d71b9347c9..effd757495 100644 --- a/tests/unit/python/cli/contributing/update/merge/test_github.py +++ b/tests/unit/python/cli/contributing/update/merge/test_github.py @@ -7,6 +7,7 @@ import zipfile from unittest.mock import Mock from cli.contributing.update.merge import github +from utils.roles.mapping import ROLE_FILE_META_MAIN REPO = "infinito-nexus/core" SHA = "b" * 40 @@ -43,8 +44,8 @@ class TestReads(unittest.TestCase): def test_changed_paths_keep_the_old_name_of_a_rename(self): files = [ { - "filename": "roles/web-app-new/meta/main.yml", - "previous_filename": "roles/web-app-old/meta/main.yml", + "filename": f"roles/web-app-new/{ROLE_FILE_META_MAIN}", + "previous_filename": f"roles/web-app-old/{ROLE_FILE_META_MAIN}", }, {"filename": "pyproject.toml", "previous_filename": None}, ] @@ -52,8 +53,8 @@ class TestReads(unittest.TestCase): self.assertEqual( github.changed_paths(gh, REPO, 5), [ - "roles/web-app-new/meta/main.yml", - "roles/web-app-old/meta/main.yml", + f"roles/web-app-new/{ROLE_FILE_META_MAIN}", + f"roles/web-app-old/{ROLE_FILE_META_MAIN}", "pyproject.toml", ], ) diff --git a/tests/unit/python/cli/contributing/update/merge/test_main.py b/tests/unit/python/cli/contributing/update/merge/test_main.py index 0adc182042..4a2522c5ac 100644 --- a/tests/unit/python/cli/contributing/update/merge/test_main.py +++ b/tests/unit/python/cli/contributing/update/merge/test_main.py @@ -11,6 +11,7 @@ from unittest.mock import patch import cli.contributing.update.merge.__main__ as merge_main from cli.contributing.update.merge import github +from utils.roles.mapping import ROLE_FILE_META_SERVICES REPO = "infinito-nexus/core" SHA = "a" * 40 @@ -97,7 +98,7 @@ def green_gh(**overrides): "prs": [pull_request()], "checks": [check()], "statuses": [{"context": "codecov", "state": "success"}], - "files": ["roles/web-app-nextcloud/meta/services.yml"], + "files": [f"roles/web-app-nextcloud/{ROLE_FILE_META_SERVICES}"], "artifacts": { "role-runtimes-web-app-nextcloud": role_runtimes( "web-app-nextcloud", "svc-db-postgres" diff --git a/tests/unit/python/roles/svc-ai-mcp-adapter/files/test_policy.py b/tests/unit/python/roles/svc-ai-mcp-adapter/files/test_policy.py index c283864b2a..6c25e26256 100644 --- a/tests/unit/python/roles/svc-ai-mcp-adapter/files/test_policy.py +++ b/tests/unit/python/roles/svc-ai-mcp-adapter/files/test_policy.py @@ -212,6 +212,17 @@ class TestLimits(unittest.TestCase): self.assertEqual(5, len(policy.truncate_results(contract(), list(range(50))))) +class TestRequestLength(unittest.TestCase): + def test_a_missing_header_announces_no_body(self): + self.assertEqual(policy.request_length(None), 0) + + def test_a_numeric_header_is_the_byte_count(self): + self.assertEqual(policy.request_length("12"), 12) + + def test_a_non_numeric_header_is_negative(self): + self.assertLess(policy.request_length("abc"), 0) + + class TestDeclaredArguments(unittest.TestCase): """A published input schema is enforced, not merely advertised.