diff --git a/runtimes/v1/azure_functions_runtime_v1/bindings/datumdef.py b/runtimes/v1/azure_functions_runtime_v1/bindings/datumdef.py index a4a11cc8..b52b74e8 100644 --- a/runtimes/v1/azure_functions_runtime_v1/bindings/datumdef.py +++ b/runtimes/v1/azure_functions_runtime_v1/bindings/datumdef.py @@ -166,11 +166,11 @@ def parse_to_rpc_http_cookie_list(cookies: Optional[List[SimpleCookie]], protos) protos.RpcHttpCookie(name=name, value=cookie_entity.value, domain=to_nullable_string( - cookie_entity['domain'], + cookie_entity['domain'] or None, 'cookie.domain', protos), path=to_nullable_string( - cookie_entity['path'], + cookie_entity['path'] or None, 'cookie.path', protos), expires=to_nullable_timestamp( diff --git a/runtimes/v1/tests/unittests/test_datumdef.py b/runtimes/v1/tests/unittests/test_datumdef.py index ee1e4a93..73b85a87 100644 --- a/runtimes/v1/tests/unittests/test_datumdef.py +++ b/runtimes/v1/tests/unittests/test_datumdef.py @@ -167,6 +167,30 @@ def test_parse_to_rpc_http_cookie_list_valid(self): self.assertEqual(cookie1, rpc_cookies[0]) self.assertEqual(cookie2, rpc_cookies[1]) + def test_parse_to_rpc_http_cookie_list_no_domain(self): + # Regression test for Azure/azure-functions-python-worker#1794: + # a cookie without a Domain (e.g. `test="hello//world"; Path=/; + # SameSite=lax`) must keep its value intact and must not gain an + # empty `domain=` attribute, so domain is sent as None. + cookies = SimpleCookie('test="hello//world"; Path=/; SameSite=lax') + + cookie = protos.RpcHttpCookie(name="test", + value="hello//world", + domain=None, + path=to_nullable_string( + "/", "cookie.path", protos), + expires=None, + secure=to_nullable_bool( + False, 'cookie.secure', protos), + http_only=to_nullable_bool( + False, 'cookie.httpOnly', protos), + same_site=parse_cookie_attr_same_site( + {"samesite": "Lax"}, protos), + max_age=None) + + rpc_cookies = parse_to_rpc_http_cookie_list([cookies], protos) + self.assertEqual(cookie, rpc_cookies[0]) + def test_parse_to_rpc_http_cookie_list_no_cookie(self): datum = Datum( type='http', diff --git a/runtimes/v2/azure_functions_runtime/bindings/datumdef.py b/runtimes/v2/azure_functions_runtime/bindings/datumdef.py index d4dbc31c..721dafea 100644 --- a/runtimes/v2/azure_functions_runtime/bindings/datumdef.py +++ b/runtimes/v2/azure_functions_runtime/bindings/datumdef.py @@ -165,11 +165,11 @@ def parse_to_rpc_http_cookie_list(cookies: Optional[List[SimpleCookie]], protos) protos.RpcHttpCookie(name=name, value=cookie_entity.value, domain=to_nullable_string( - cookie_entity['domain'], + cookie_entity['domain'] or None, 'cookie.domain', protos), path=to_nullable_string( - cookie_entity['path'], + cookie_entity['path'] or None, 'cookie.path', protos), expires=to_nullable_timestamp( diff --git a/runtimes/v2/tests/unittests/test_datumdef.py b/runtimes/v2/tests/unittests/test_datumdef.py index 8a8d2372..52a2038a 100644 --- a/runtimes/v2/tests/unittests/test_datumdef.py +++ b/runtimes/v2/tests/unittests/test_datumdef.py @@ -167,6 +167,30 @@ def test_parse_to_rpc_http_cookie_list_valid(self): self.assertEqual(cookie1, rpc_cookies[0]) self.assertEqual(cookie2, rpc_cookies[1]) + def test_parse_to_rpc_http_cookie_list_no_domain(self): + # Regression test for Azure/azure-functions-python-worker#1794: + # a cookie without a Domain (e.g. `test="hello//world"; Path=/; + # SameSite=lax`) must keep its value intact and must not gain an + # empty `domain=` attribute, so domain is sent as None. + cookies = SimpleCookie('test="hello//world"; Path=/; SameSite=lax') + + cookie = protos.RpcHttpCookie(name="test", + value="hello//world", + domain=None, + path=to_nullable_string( + "/", "cookie.path", protos), + expires=None, + secure=to_nullable_bool( + False, 'cookie.secure', protos), + http_only=to_nullable_bool( + False, 'cookie.httpOnly', protos), + same_site=parse_cookie_attr_same_site( + {"samesite": "Lax"}, protos), + max_age=None) + + rpc_cookies = parse_to_rpc_http_cookie_list([cookies], protos) + self.assertEqual(cookie, rpc_cookies[0]) + def test_parse_to_rpc_http_cookie_list_no_cookie(self): datum = Datum( type='http', diff --git a/workers/azure_functions_worker/bindings/datumdef.py b/workers/azure_functions_worker/bindings/datumdef.py index 0e2a9c53..7e231a1e 100644 --- a/workers/azure_functions_worker/bindings/datumdef.py +++ b/workers/azure_functions_worker/bindings/datumdef.py @@ -241,10 +241,11 @@ def parse_to_rpc_http_cookie_list(cookies: Optional[List[SimpleCookie]]): protos.RpcHttpCookie(name=name, value=cookie_entity.value, domain=to_nullable_string( - cookie_entity['domain'], + cookie_entity['domain'] or None, 'cookie.domain'), path=to_nullable_string( - cookie_entity['path'], 'cookie.path'), + cookie_entity['path'] or None, + 'cookie.path'), expires=to_nullable_timestamp( parse_cookie_attr_expires( cookie_entity), 'cookie.expires'), diff --git a/workers/r2p2/bridge/bridge.py b/workers/r2p2/bridge/bridge.py index a0a8dedd..e5415256 100644 --- a/workers/r2p2/bridge/bridge.py +++ b/workers/r2p2/bridge/bridge.py @@ -839,7 +839,8 @@ def _flatten_cookies(cookies): without any Python protobuf on the native path. Mirrors the runtime's ``parse_to_rpc_http_cookie_list`` semantics: ``domain`` - and ``path`` are always emitted (possibly empty), ``secure``/``http_only`` are + and ``path`` are emitted when set (``None`` when unset, so the host does + not render an empty ``domain=``/``path=`` attribute), ``secure``/``http_only`` are always booleans, ``same_site`` is pre-resolved to the RpcHttpCookie.SameSite enum integer (None=0, Lax=1, Strict=2, 'none'->ExplicitNone=3), ``expires`` is epoch seconds (or None) and ``max_age`` is a float (or None). @@ -878,8 +879,8 @@ def _flatten_cookies(cookies): out.append({ 'name': name, 'value': morsel.value, - 'domain': morsel['domain'] or '', - 'path': morsel['path'] or '', + 'domain': morsel['domain'] or None, + 'path': morsel['path'] or None, 'secure': bool(morsel['secure']), 'http_only': bool(morsel['httponly']), 'same_site': same_site, diff --git a/workers/r2p2/src/convert.rs b/workers/r2p2/src/convert.rs index 128d6939..a372e7be 100644 --- a/workers/r2p2/src/convert.rs +++ b/workers/r2p2/src/convert.rs @@ -169,6 +169,22 @@ fn http_out_from_dict(py: Python<'_>, d: &Bound<'_, PyAny>) -> Result { }) } +/// Map an optional cookie string (`domain`/`path`) to the `NullableString` +/// the Host expects: Python `None` or an empty string stays `None`, so the +/// Host leaves the attribute unset instead of rendering `domain=`/`path=`. +fn optional_nullable_string(obj: Bound<'_, PyAny>) -> Result> { + if obj.is_none() { + return Ok(None); + } + let s: String = obj.extract()?; + if s.is_empty() { + return Ok(None); + } + Ok(Some(NullableString { + string: Some(nullable_string::String::Value(s)), + })) +} + /// Build the `RpcHttpCookie` list from the flat cookie dicts the bridge emits. /// /// The bridge (bridge.py `_flatten_cookies`) pre-computes every field as a plain @@ -185,8 +201,11 @@ fn http_cookies_from_list(list_obj: &Bound<'_, PyAny>) -> Result) -> Result