Skip to content

Commit

Permalink
Merge pull request #1602 Check report_xform permission on enketo URL …
Browse files Browse the repository at this point in the history
…requests
  • Loading branch information
ukanga authored May 23, 2019
2 parents 371042b + e24ea13 commit aac447f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
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 -

0 comments on commit aac447f

Please sign in to comment.