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

Check report_xform permission on enketo URL requests #1602

Merged
merged 3 commits into from
May 23, 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
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ Edit top level requirements in the file `requirements/base.in <requirements/base

pip-compile --output-file requirements/base.pip requirements/base.in

Copy `pre-commit.sh <pre-commit.sh>`_ into `.git/hooks/pre-commit`, it ensures staged python flake8 are in acceptable code style and conventions.

.. code-block:: sh

cp pre-commit.sh .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

**Security Acknowledgments**

We would like to thank the following security researchers for responsibly disclosing security issues:
Expand Down
24 changes: 24 additions & 0 deletions onadata/apps/api/tests/viewsets/test_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,30 @@ def test_enketo_url(self):
data = {"enketo_url": url, "enketo_preview_url": preview_url}
self.assertEqual(response.data, data)

alice_data = {'username': 'alice', 'email': '[email protected]'}
alice_profile = self._create_user_profile(alice_data)
credentials = {
'HTTP_AUTHORIZATION': (
'Token %s' % alice_profile.user.auth_token)
}
request = self.factory.get('/', **credentials)
response = view(request, pk=formid)
# Alice has no permissions to the form hence no access to web form
self.assertEqual(response.status_code, 404)

# Give Alice read-only permissions to the form
ReadOnlyRole.add(alice_profile.user, self.xform)
response = view(request, pk=formid)
# Alice with read-only access should not have access to web form
self.assertEqual(response.status_code, 404)

# Give Alice data-entry permissions
DataEntryRole.add(alice_profile.user, self.xform)
response = view(request, pk=formid)
# Alice with data-entry access should have access to web form
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, data)

def test_get_single_submit_url(self):
with HTTMock(enketo_preview_url_mock, enketo_url_mock,
enketo_single_submission_mock):
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/api/viewsets/xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class XFormViewSet(AnonymousUserPublicFormsMixin,
permission_classes = [XFormPermissions, ]
updatable_fields = set(('description', 'downloadable', 'require_auth',
'shared', 'shared_data', 'title'))
filter_backends = (filters.AnonDjangoObjectPermissionFilter,
filter_backends = (filters.EnketoAnonDjangoObjectPermissionFilter,
filters.TagFilter,
filters.XFormOwnerFilter,
DjangoFilterBackend)
Expand Down
16 changes: 16 additions & 0 deletions onadata/libs/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def filter_queryset(self, request, queryset, view):
.filter_queryset(request, queryset, view)


# pylint: disable=too-few-public-methods
class EnketoAnonDjangoObjectPermissionFilter(AnonDjangoObjectPermissionFilter):
"""EnketoAnonDjangoObjectPermissionFilter

Same as AnonDjangoObjectPermissionFilter but checks 'report_xform'
permission when the view 'enketo' is accessed.
"""

def filter_queryset(self, request, queryset, view):
"""Check report_xform permission when requesting for Enketo URL."""
if view.action == 'enketo':
self.perm_format = '%(app_label)s.report_%(model_name)s' # noqa pylint: disable=W0201
return super(EnketoAnonDjangoObjectPermissionFilter, self)\
.filter_queryset(request, queryset, view)


class XFormListObjectPermissionFilter(AnonDjangoObjectPermissionFilter):
perm_format = '%(app_label)s.report_%(model_name)s'

Expand Down
4 changes: 4 additions & 0 deletions pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
#
# flake8 check
exec git diff --staged --name-only | grep -E '\.py$' | xargs flake8 --exclude=migrations -