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

1990 validate input fields on put form endpoint requests #1991

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
47 changes: 47 additions & 0 deletions onadata/apps/api/tests/viewsets/test_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from django.http import HttpResponseRedirect
from django.test.utils import override_settings
from django.utils.dateparse import parse_datetime
from django.utils.html import conditional_escape
from django.utils.timezone import utc
from django_digest.test import DigestAuth
from httmock import HTTMock
Expand Down Expand Up @@ -2687,6 +2688,52 @@ def test_update_xform_dropbox_url(self, mock_urlopen):
self.assertEquals(self.xform.version, "212121211")
self.assertEquals(form_id, self.xform.pk)

def test_update_xform_using_put_with_invalid_input(self):
with HTTMock(enketo_mock):
self._publish_xls_form_to_project()
form_id = self.xform.pk

unsanitized_html_str = "<h1>HTML Injection testing</h1>"
version = unsanitized_html_str
view = XFormViewSet.as_view({
'put': 'update',
})

put_data = {
'uuid': 'ae631e898bd34ced91d2a309d8b72das',
'description': unsanitized_html_str,
'downloadable': False,
'owner': 'http://testserver/api/v1/users/{0}'.
format(self.user),
'created_by':
'http://testserver/api/v1/users/{0}'.format(self.user),
'public': False,
'public_data': False,
'project': 'http://testserver/api/v1/projects/{0}'.format(
self.xform.project.pk),
'title': 'Transport Form',
'version': unsanitized_html_str
}

# trigger error is form version is invalid
with self.assertRaises(XLSFormError):
request = self.factory.put('/', data=put_data, **self.extra)
response = view(request, pk=form_id)

put_data['version'] = self.xform.version

request = self.factory.put('/', data=put_data, **self.extra)
response = view(request, pk=form_id)
self.assertEqual(response.status_code, 200, response.data)

self.xform.refresh_from_db()

# check that description has been sanitized
self.assertEquals(
conditional_escape(unsanitized_html_str),
self.xform.description
)

def test_update_xform_using_put(self):
with HTTMock(enketo_mock):
self._publish_xls_form_to_project()
Expand Down
8 changes: 8 additions & 0 deletions onadata/apps/logger/models/xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

from onadata.apps.logger.xform_instance_parser import (XLSFormError,
clean_and_parse_xml)
from django.utils.html import conditional_escape
from onadata.libs.models.base_model import BaseModel
from onadata.libs.utils.cache_tools import (
IS_ORG, PROJ_BASE_FORMS_CACHE, PROJ_FORMS_CACHE,
Expand Down Expand Up @@ -901,6 +902,13 @@ def save(self, *args, **kwargs):
'in settings sheet or reduce the file name if you do'
' not have a settings sheets.' % self.MAX_ID_LENGTH))

if contains_xml_invalid_char(self.version):
raise XLSFormError(
_("Version shouldn't have any invalid "
"characters ('>' '&' '<')"))

self.description = conditional_escape(self.description)

super(XForm, self).save(*args, **kwargs)

def __str__(self):
Expand Down