Skip to content

Commit

Permalink
Raise ValueError when cookie max-age is not an integer (#2001)
Browse files Browse the repository at this point in the history
* Raise valueerror when cookie max-age is not an integer
  • Loading branch information
ahopkins authored Jan 18, 2021
1 parent 7028eae commit 8f4e0ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sanic/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __setitem__(self, key, value):
if value is not False:
if key.lower() == "max-age":
if not str(value).isdigit():
value = DEFAULT_MAX_AGE
raise ValueError("Cookie max-age must be an integer")
elif key.lower() == "expires":
if not isinstance(value, datetime):
raise TypeError(
Expand Down
19 changes: 18 additions & 1 deletion tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def handler(request):
assert response.cookies["test"] == "pass"


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

Expand Down Expand Up @@ -204,6 +204,23 @@ def handler(request):
assert cookie is None


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

@app.get("/")
def handler(request):
response = text("pass")
response.cookies["test"] = "pass"
response.cookies["test"]["max-age"] = max_age
return response

request, response = app.test_client.get(
"/", cookies=cookies, raw_cookies=True
)
assert response.status == 500


@pytest.mark.parametrize(
"expires", [datetime.utcnow() + timedelta(seconds=60)]
)
Expand Down

0 comments on commit 8f4e0ad

Please sign in to comment.