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
@@ -25,10 +25,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
@@ -7,14 +7,16 @@
 from starlite import Response, get, post
 from starlite.config.compression import CompressionConfig
 from starlite.config.logging import LoggingConfig, StructLoggingConfig
+from starlite.connection import Request
 from starlite.datastructures import Cookie
 from starlite.middleware.logging import LoggingMiddlewareConfig
-from starlite.status_codes import HTTP_200_OK
+from starlite.status_codes import HTTP_200_OK, HTTP_201_CREATED
 from starlite.testing import create_test_client
 
 if TYPE_CHECKING:
     from _pytest.logging import LogCaptureFixture
 
+    from starlite.middleware.session.server_side import ServerSideSessionConfig
     from starlite.types.callable_types import GetLogger
 
 
@@ -210,3 +212,33 @@ def test_logging_middleware_log_fields(get_logger: "GetLogger", caplog: "LogCapt
 
         assert caplog.messages[0] == "HTTP Request: path=/"
         assert caplog.messages[1] == "HTTP Response: status_code=200"
+
+
+def test_logging_middleware_with_session_middleware(session_backend_config_memory: "ServerSideSessionConfig") -> 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()
+
+    with create_test_client(
+        [set_session, get_session],
+        logging_config=LoggingConfig(),
+        middleware=[logging_middleware_config.middleware, session_backend_config_memory.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