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 AdminGroupSchema CSRF protection #9333

Merged
merged 1 commit into from
Feb 12, 2025
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
6 changes: 5 additions & 1 deletion h/schemas/forms/admin/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def group_organization_select_widget(_node, kwargs):

class AdminGroupSchema(CSRFSchema):
def __init__(self, *args):
super().__init__(validator=username_validator, *args) # noqa: B026
super().__init__(*args)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The *args looks like it's unused: we never instantiate AdminGroupSchema() with any arguments. But in fact Colander itself does instantiate further AdminGroupSchema instances for us, with arguments.


group_type = colander.SchemaNode(
colander.String(),
Expand Down Expand Up @@ -217,3 +217,7 @@ def __init__(self, *args):
widget=SequenceWidget(add_subitem_text_template=_("Add member")),
missing=None,
)

def validator(self, node, value):
super().validator(node, value)
username_validator(node, value)
13 changes: 13 additions & 0 deletions tests/unit/h/schemas/forms/admin/group_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import colander
import pytest
from pyramid.exceptions import BadCSRFToken

from h.models.group import (
GROUP_DESCRIPTION_MAX_LENGTH,
Expand All @@ -17,6 +18,18 @@ class TestAdminGroupSchema:
def test_it_allows_with_valid_data(self, group_data, bound_schema):
bound_schema.deserialize(group_data)

def test_it_raises_if_csrf_token_missing(self, group_data, bound_schema):
del bound_schema.bindings["request"].headers["X-CSRF-Token"]

with pytest.raises(BadCSRFToken):
bound_schema.deserialize(group_data)

def test_it_raises_if_csrf_token_wrong(self, group_data, bound_schema):
bound_schema.bindings["request"].headers["X-CSRF-Token"] = "foobar"

with pytest.raises(BadCSRFToken):
bound_schema.deserialize(group_data)
Comment on lines +21 to +31
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default Pyramid will read the CSRF token from either a csrf_token form field or an X-CSRF-Token header. In practice our pages use the form field. But our tests use the header (via the pyramid_csrf_request fixture).


def test_it_raises_if_name_too_short(self, group_data, bound_schema):
too_short_name = "a" * (GROUP_NAME_MIN_LENGTH - 1)
group_data["name"] = too_short_name
Expand Down
Loading