Skip to content

Commit

Permalink
[#1419] Move URL locality checking to helpers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mwahlroos authored and nigelb committed Jun 26, 2014
1 parent 87a2c3d commit 46b14f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
16 changes: 2 additions & 14 deletions ckan/controllers/user.py
@@ -1,6 +1,5 @@
import logging
from urllib import quote
from urlparse import urlparse

from pylons import config

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -12,6 +12,7 @@
import urllib
import pprint
import copy
import urlparse
from urllib import urlencode

from paste.deploy.converters import asbool
Expand Down Expand Up @@ -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 '''
Expand Down

0 comments on commit 46b14f4

Please sign in to comment.