from base64 import b64decode, b64encode
import itsdangerous
-from itsdangerous.exc import BadTimeSignature, SignatureExpired
+from itsdangerous.exc import BadSignature
from starlette.datastructures import MutableHeaders, Secret
from starlette.requests import HTTPConnection
data = self.signer.unsign(data, max_age=self.max_age)
scope["session"] = json.loads(b64decode(data))
initial_session_was_empty = False
- except (BadTimeSignature, SignatureExpired):
+ except BadSignature:
scope["session"] = {}
else:
scope["session"] = {}
cookie = response.headers["set-cookie"]
cookie_path = re.search(r"; path=(\S+);", cookie).groups()[0]
assert cookie_path == "/second_app"
+
+
+def test_invalid_session_cookie(test_client_factory):
+ app = create_app()
+ app.add_middleware(SessionMiddleware, secret_key="example")
+ client = test_client_factory(app)
+
+ response = client.post("/update_session", json={"some": "data"})
+ assert response.json() == {"session": {"some": "data"}}
+
+ # we expect it to not raise an exception if we provide a bogus session cookie
+ response = client.get("/view_session", cookies={"session": "invalid"})
+ assert response.json() == {"session": {}}