From 46b14f409e7856db199d18fef993e6eeb9e5aed5 Mon Sep 17 00:00:00 2001 From: Mika Wahlroos Date: Tue, 11 Mar 2014 13:48:09 +0200 Subject: [PATCH] [#1419] Move URL locality checking to helpers This allows the function to be also used for similar purposes outside the user controller. Also rename the function to be more descriptive in this general setting. --- ckan/controllers/user.py | 16 ++-------------- ckan/lib/helpers.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/ckan/controllers/user.py b/ckan/controllers/user.py index 2e764230834..a1160c18f0a 100644 --- a/ckan/controllers/user.py +++ b/ckan/controllers/user.py @@ -1,6 +1,5 @@ import logging from urllib import quote -from urlparse import urlparse from pylons import config @@ -356,7 +355,7 @@ def login(self, error=None): def logged_in(self): # redirect if needed came_from = request.params.get('came_from', '') - if self._sane_came_from(came_from): + if h.url_is_local(came_from): return h.redirect_to(str(came_from)) if c.user: @@ -392,7 +391,7 @@ def logout(self): def logged_out(self): # redirect if needed came_from = request.params.get('came_from', '') - if self._sane_came_from(came_from): + if h.url_is_local(came_from): return h.redirect_to(str(came_from)) h.redirect_to(controller='user', action='logged_out_page') @@ -689,14 +688,3 @@ def unfollow(self, id): or e.error_dict) h.flash_error(error_message) h.redirect_to(controller='user', action='read', id=id) - - def _sane_came_from(self, url): - '''Returns True if came_from is local''' - if not url or (len(url) >= 2 and url.startswith('//')): - return False - parsed = urlparse(url) - if parsed.scheme: - domain = urlparse(h.url_for('/', qualified=True)).netloc - if domain != parsed.netloc: - return False - return True diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index b83499de3b7..f5e44bca0f0 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -12,6 +12,7 @@ import urllib import pprint import copy +import urlparse from urllib import urlencode from paste.deploy.converters import asbool @@ -227,6 +228,18 @@ def _add_i18n_to_url(url_to_amend, **kw): return url +def url_is_local(url): + '''Returns True if url is local''' + if not url or (len(url) >= 2 and url.startswith('//')): + return False + parsed = urlparse.urlparse(url) + if parsed.scheme: + domain = urlparse.urlparse(url_for('/', qualified=True)).netloc + if domain != parsed.netloc: + return False + return True + + def full_current_url(): ''' Returns the fully qualified current url (eg http://...) useful for sharing etc '''