From c0a03c86eb2c4398449c908f94ecb2295ac96c2d Mon Sep 17 00:00:00 2001 From: Sobuno Date: Thu, 4 Jun 2026 21:54:55 +0200 Subject: [PATCH] fix: Strip leading and trailing whitespace for HostOs header Leading and trailing whitespace in any request header value will prevent requests from being sent when using HTTP 1.1 as `h11` package's normalize_and_validate function (https://github.com/python-hyper/h11/blob/62c5068c971579d61fa1b55373390e12f25fd856/h11/_headers.py#L152) validates the header using a regex that explicitly disallows trailing whitespace. `h2`, used for HTTP 2 traffic, trims the values itself. The HostOs header can end up with leading whitespace if `platform.system()` returns an empty string (which per Python's documentation is possible: "An empty string is returned if the value cannot be determined.") The HostOs header can end up with trailing whitespace in two cases: * If `platform.version()` returns an empty string (which per Python's documentation is possible: "An empty string is returned if the value cannot be determined.") * If `platform.version()` returns a string that ends with a whitespace character. The latter case can happen on e.g. a Linux platform where `platform.version()` usually returns the kernel version. The `uname` struct fields are 65 characters (as seen here: https://github.com/torvalds/linux/blob/9154c4af7829b6f82712b4d1a2a720adddacdb8d/include/uapi/linux/utsname.h#L25) - A kernel version longer than 65 characters may end up being with a whitespace as the last character. --- src/msgraph_core/middleware/telemetry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/msgraph_core/middleware/telemetry.py b/src/msgraph_core/middleware/telemetry.py index 1f6a604b..681d9e51 100644 --- a/src/msgraph_core/middleware/telemetry.py +++ b/src/msgraph_core/middleware/telemetry.py @@ -115,7 +115,7 @@ def _add_host_os_header(self, request) -> None: """ system = platform.system() version = platform.version() - host_os = f'{system} {version}' + host_os = f'{system} {version}'.strip() request.headers.update({'HostOs': host_os}) def _add_runtime_environment_header(self, request) -> None: