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

Fix code scanning alert - Failure to use secure cookies #176

Closed
Closed
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
3 changes: 3 additions & 0 deletions fsd_utils/locale_selector/set_lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def set_language_cookie(locale: str, response: Response):
current_app.config["COOKIE_DOMAIN"]
),
max_age=86400 * 30, # 30 days
secure=True,
samesite="Lax",
httponly=True,
)

def __init__(self, app):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_set_lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,31 @@ def test_set_lang(flask_test_client):
response_cookie = response.headers.get("Set-Cookie")
assert response_cookie is not None, "No cookie set for language"
assert response_cookie.split(";")[0] == ("language" + "=cy")
assert (
"Secure" in response_cookie
), "Secure attribute not set for language cookie"
assert (
"SameSite=Lax" in response_cookie
), "SameSite attribute not set to Lax for language cookie"
assert (
"HttpOnly" in response_cookie
), "HttpOnly attribute not set for language cookie"


def test_set_language_cookie_attributes(flask_test_client):
mock_app = Mock()
set_lang = LanguageSelector(mock_app)
with flask_test_client.application.test_request_context():
response = flask_test_client.response_class()
set_lang.set_language_cookie("cy", response)
response_cookie = response.headers.get("Set-Cookie")
assert response_cookie is not None, "No cookie set for language"
assert (
"Secure" in response_cookie
), "Secure attribute not set for language cookie"
assert (
"SameSite=Lax" in response_cookie
), "SameSite attribute not set to Lax for language cookie"
assert (
"HttpOnly" in response_cookie
), "HttpOnly attribute not set for language cookie"