Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotate samesite parameter on set_cookie #1590

Merged
merged 6 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion starlette/middleware/sessions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import sys
import typing
from base64 import b64decode, b64encode

Expand All @@ -9,6 +10,11 @@
from starlette.requests import HTTPConnection
from starlette.types import ASGIApp, Message, Receive, Scope, Send

if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal
else: # pragma: no cover
from typing_extensions import Literal


class SessionMiddleware:
def __init__(
Expand All @@ -18,7 +24,7 @@ def __init__(
session_cookie: str = "session",
max_age: typing.Optional[int] = 14 * 24 * 60 * 60, # 14 days, in seconds
path: str = "/",
same_site: str = "lax",
same_site: Literal["lax", "strict", "none"] = "lax",
https_only: bool = False,
) -> None:
self.app = app
Expand Down
9 changes: 7 additions & 2 deletions starlette/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
from starlette.datastructures import URL, MutableHeaders
from starlette.types import Receive, Scope, Send

if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal
else: # pragma: no cover
from typing_extensions import Literal

# Workaround for adding samesite support to pre 3.8 python
http.cookies.Morsel._reserved["samesite"] = "SameSite" # type: ignore

Expand Down Expand Up @@ -105,7 +110,7 @@ def set_cookie(
domain: typing.Optional[str] = None,
secure: bool = False,
httponly: bool = False,
samesite: str = "lax",
samesite: typing.Optional[Literal["lax", "strict", "none"]] = "lax",
) -> None:
cookie: http.cookies.BaseCookie = http.cookies.SimpleCookie()
cookie[key] = value
Expand Down Expand Up @@ -138,7 +143,7 @@ def delete_cookie(
domain: typing.Optional[str] = None,
secure: bool = False,
httponly: bool = False,
samesite: str = "lax",
samesite: typing.Optional[Literal["lax", "strict", "none"]] = "lax",
) -> None:
self.set_cookie(
key,
Expand Down