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

fixed bug where csrf_token was not recognized for register json api #926

Merged
merged 2 commits into from
Feb 17, 2024
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
1 change: 1 addition & 0 deletions flask_security/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def logout():


@anonymous_user_required
@unauth_csrf(fall_through=True)
def register() -> "ResponseValue":
"""View function which handles a registration request."""

Expand Down
35 changes: 35 additions & 0 deletions tests/test_csrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,38 @@ def test_remember_login_csrf_cookie(app, client):
assert not client.get_cookie("remember_token")
assert not client.get_cookie("session")
assert not client.get_cookie("XSRF-Token")


@pytest.mark.csrf(csrfprotect=True)
@pytest.mark.registerable()
def test_json_register_csrf_with_ignore_unauth_set_to_false(app, client):
"""
Test that you are able to register a user when using the JSON api
and the CSRF_IGNORE_UNAUTH_ENDPOINTS is set to False.
"""
app.config["WTF_CSRF_ENABLED"] = True
app.config["WTF_CSRF_CHECK_DEFAULT"] = False
app.config["CSRF_IGNORE_UNAUTH_ENDPOINTS"] = False
app.config["SECURITY_CSRF_HEADER"] = "X-CSRF-Token"
CSRFProtect(app)

csrf_token = client.get("/login", headers={"Accept": "application/json"}).json[
"response"
]["csrf_token"]

email = "eg@testuser.com"
data = {"email": email, "password": "password"}

response = client.post(
"/register", json=data, headers={"Content-Type": "application/json"}
)
assert response.status_code == 400
assert response.json["response"]["errors"][0] == "The CSRF token is missing."

response = client.post(
"/register",
json=data,
headers={"Content-Type": "application/json", "X-CSRF-Token": csrf_token},
)
assert response.status_code == 200
assert response.json["response"]["user"]["email"] == email
Loading