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

enforce integer for max-age cookie #1457

Merged
merged 3 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions sanic/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import string


DEFAULT_MAX_AGE = 0

# ------------------------------------------------------------ #
# SimpleCookie
# ------------------------------------------------------------ #
Expand Down Expand Up @@ -103,6 +105,9 @@ def __setitem__(self, key, value):
if key not in self._keys:
raise KeyError("Unknown cookie property")
if value is not False:
if key.lower() == "max-age":
if not str(value).isdigit():
value = DEFAULT_MAX_AGE
r0fls marked this conversation as resolved.
Show resolved Hide resolved
return super().__setitem__(key, value)

def encode(self, encoding):
Expand Down
10 changes: 7 additions & 3 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from http.cookies import SimpleCookie
from sanic.response import text
import pytest
from sanic.cookies import Cookie
from sanic.cookies import Cookie, DEFAULT_MAX_AGE

# ------------------------------------------------------------ #
# GET
Expand Down Expand Up @@ -138,7 +138,7 @@ def handler(request):
assert response.cookies["test"].value == "pass"


@pytest.mark.parametrize("max_age", ["0", 30, "30"])
@pytest.mark.parametrize("max_age", ["0", 30, 30.0, 30.1, "30", "test"])
def test_cookie_max_age(app, max_age):
cookies = {"test": "wait"}

Expand All @@ -153,7 +153,11 @@ def handler(request):
assert response.status == 200

assert response.cookies["test"].value == "pass"
assert response.cookies["test"]["max-age"] == str(max_age)

if str(max_age).isdigit() and int(max_age) == float(max_age):
assert response.cookies["test"]["max-age"] == str(max_age)
else:
assert response.cookies["test"]["max-age"] == str(DEFAULT_MAX_AGE)


@pytest.mark.parametrize(
Expand Down