Skip to content

Commit

Permalink
Merge pull request #661 from kobotoolbox/658-edit-submission-media
Browse files Browse the repository at this point in the history
Make internal KC calls from editing submissions always use public url
  • Loading branch information
noliveleger authored Oct 26, 2020
2 parents cb3f178 + 44194f9 commit 45906e0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
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 @@ -732,7 +732,7 @@ def form(self, request, format='json', **kwargs):
@detail_route(methods=['GET'])
def enketo(self, request, **kwargs):
self.object = self.get_object()
form_url = _get_form_url(request, self.object.user.username)
form_url = _get_form_url(self.object.user.username)

data = {'message': _("Enketo not properly configured.")}
http_status = status.HTTP_400_BAD_REQUEST
Expand Down
4 changes: 2 additions & 2 deletions onadata/apps/logger/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def enter_data(request, username, id_string):
if not has_edit_permission(xform, owner, request):
return HttpResponseForbidden(_('Not shared.'))

form_url = _get_form_url(request, username, settings.ENKETO_PROTOCOL)
form_url = _get_form_url(username)

try:
url = enketo_url(form_url, xform.id_string)
Expand Down Expand Up @@ -540,7 +540,7 @@ def edit_data(request, username, id_string, data_id):
'username': username,
'id_string': id_string}
) + "#/" + str(instance.id))
form_url = _get_form_url(request, username, settings.ENKETO_PROTOCOL)
form_url = _get_form_url(username)

try:
url = enketo_url(
Expand Down
26 changes: 10 additions & 16 deletions onadata/libs/utils/viewer_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,29 +239,23 @@ def create_attachments_zipfile(attachments, output_file=None):
return output_file


def _get_form_url(request, username, protocol='https'):
def _get_form_url(username):
if settings.TESTING_MODE:
http_host = settings.TEST_HTTP_HOST
http_host = 'http://{}'.format(settings.TEST_HTTP_HOST)
username = settings.TEST_USERNAME
else:
http_host = request.get_host()
# Always use a public url to prevent Enketo SSRF from blocking request
http_host = settings.KOBOCAT_URL

# In case INTERNAL_DOMAIN_NAME is equal to PUBLIC_DOMAIN_NAME,
# configuration doesn't use docker internal network.
# Don't overwrite `protocol.
is_call_internal = settings.KOBOCAT_INTERNAL_HOSTNAME == http_host and \
settings.KOBOCAT_PUBLIC_HOSTNAME != http_host

# Make sure protocol is enforced to `http` when calling `kc` internally
protocol = "http" if is_call_internal else protocol

return '%s://%s/%s' % (protocol, http_host, username)
# Internal requests use the public url, KOBOCAT_URL already has the protocol
return '{http_host}/{username}'.format(
http_host=http_host,
username=username
)


def get_enketo_edit_url(request, instance, return_url):
form_url = _get_form_url(request,
instance.xform.user.username,
settings.ENKETO_PROTOCOL)
form_url = _get_form_url(instance.xform.user.username)
instance_attachments = image_urls_dict(instance)
url = enketo_url(
form_url, instance.xform.id_string, instance_xml=instance.xml,
Expand Down
9 changes: 0 additions & 9 deletions onadata/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@
# All internal communications between containers must be HTTP only.
ENKETO_PROTOCOL = os.environ.get('ENKETO_PROTOCOL', 'https')

# These 2 variables are needed to detect whether the ENKETO_PROTOCOL should overwritten or not.
# See method `_get_form_url` in `onadata/libs/utils/viewer_tools.py`
KOBOCAT_INTERNAL_HOSTNAME = "{}.{}".format(
os.environ.get("KOBOCAT_PUBLIC_SUBDOMAIN", "kc"),
os.environ.get("INTERNAL_DOMAIN_NAME", "docker.internal"))
KOBOCAT_PUBLIC_HOSTNAME = "{}.{}".format(
os.environ.get("KOBOCAT_PUBLIC_SUBDOMAIN", "kc"),
os.environ.get("PUBLIC_DOMAIN_NAME", "kobotoolbox.org"))

# Default value for the `UserProfile.require_auth` attribute. Even though it's
# set in kc_environ, include it here as well to support legacy installations
REQUIRE_AUTHENTICATION_TO_SEE_FORMS_AND_SUBMIT_DATA_DEFAULT = False
Expand Down

0 comments on commit 45906e0

Please sign in to comment.