Repository: litestar-org/litestar

Issue:
Bug: LoggingMiddleware is sending obfuscated session id to client
**Describe the bug**
When using the logging middleware and session middleware together, the logging middleware's cookie obfuscation is overwriting the session name with "*****" and that name is being pushed down to the client.

The initial set-cookie has the correct session id but subsequent requests do not.

**To Reproduce**
I created a test function in tests/middleware/test_logging_middleware.py which I believe confirms the bug:

```python
def test_logging_with_session_middleware() -> None:
    @post("/")
    async def set_session(request: Request) -> None:
        request.set_session({"hello": "world"})

    @get("/")
    async def get_session() -> None:
        pass

    logging_middleware_config = LoggingMiddlewareConfig()
    session_config = MemoryBackendConfig()

    with create_test_client(
        [set_session, get_session],
        logging_config=LoggingConfig(),
        middleware=[logging_middleware_config.middleware, session_config.middleware],
    ) as client:
        response = client.post("/")
        assert response.status_code == HTTP_201_CREATED
        assert len(client.cookies.get("session", "")) == 64

        response = client.get("/")
        assert response.status_code == HTTP_200_OK
        assert len(client.cookies.get("session", "")) == 64
```

The test results in the following exception:
```
>           assert len(client.cookies.get("session", "")) == 64
E           AssertionError: assert 5 == 64
E            +  where 5 = len('*****')
E            +    where '*****' = <bound method Cookies.get of <Cookies[<Cookie session=***** for testserver.local />]>>('session', '')
E            +      where <bound method Cookies.get of <Cookies[<Cookie session=***** for testserver.local />]>> = <Cookies[<Cookie session=***** for testserver.local />]>.get
E            +        where <Cookies[<Cookie session=***** for testserver.local />]> = <starlite.testing.client.sync_client.TestClient object at 0x7f4cbf7bea40>.cookies
```

**Additional Context**
Starlite version: 1.51.4


Discussion:

Resolving patch:
diff --git a/starlite/utils/extractors.py b/starlite/utils/extractors.py
--- a/starlite/utils/extractors.py
+++ b/starlite/utils/extractors.py
@@ -35,10 +35,7 @@ def obfuscate(values: Dict[str, Any], fields_to_obfuscate: Set[str]) -> Dict[str
     Returns:
         A dictionary with obfuscated strings
     """
-    for key in values:
-        if key.lower() in fields_to_obfuscate:
-            values[key] = "*****"
-    return values
+    return {key: "*****" if key.lower() in fields_to_obfuscate else value for key, value in values.items()}
 
 
 RequestExtractorField = Literal[


Test patch:
diff --git a/tests/middleware/test_logging_middleware.py b/tests/middleware/test_logging_middleware.py
--- a/tests/middleware/test_logging_middleware.py
+++ b/tests/middleware/test_logging_middleware.py
@@ -6,8 +6,10 @@
 
 from starlite import Cookie, LoggingConfig, Response, StructLoggingConfig, get, post
 from starlite.config.compression import CompressionConfig
+from starlite.connection import Request
 from starlite.middleware import LoggingMiddlewareConfig
-from starlite.status_codes import HTTP_200_OK
+from starlite.middleware.session.memory_backend import MemoryBackendConfig
+from starlite.status_codes import HTTP_200_OK, HTTP_201_CREATED
 from starlite.testing import create_test_client
 
 if TYPE_CHECKING:
@@ -190,3 +192,34 @@ async def hello_world_handler() -> Dict[str, str]:
         response = client.get("/")
         assert response.status_code == HTTP_200_OK
         assert len(caplog.messages) == 2
+
+
+def test_logging_middleware_with_session_middleware() -> None:
+    # https://github.com/starlite-api/starlite/issues/1228
+
+    @post("/")
+    async def set_session(request: Request) -> None:
+        request.set_session({"hello": "world"})
+
+    @get("/")
+    async def get_session() -> None:
+        pass
+
+    logging_middleware_config = LoggingMiddlewareConfig()
+    session_config = MemoryBackendConfig()
+
+    with create_test_client(
+        [set_session, get_session],
+        logging_config=LoggingConfig(),
+        middleware=[logging_middleware_config.middleware, session_config.middleware],
+    ) as client:
+        response = client.post("/")
+        assert response.status_code == HTTP_201_CREATED
+        assert "session" in client.cookies
+        assert client.cookies["session"] != "*****"
+        session_id = client.cookies["session"]
+
+        response = client.get("/")
+        assert response.status_code == HTTP_200_OK
+        assert "session" in client.cookies
+        assert client.cookies["session"] == session_id