1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-05 01:26:21 +00:00
calibre-web/cps/redirect.py

29 lines
801 B
Python
Raw Normal View History

# http://flask.pocoo.org/snippets/62/
2017-07-14 04:28:00 +00:00
try:
from urllib.parse import urlparse, urljoin
except ImportError:
from urlparse import urlparse, urljoin
from flask import request, url_for, redirect
def is_safe_url(target):
ref_url = urlparse(request.host_url)
test_url = urlparse(urljoin(request.host_url, target))
return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc
def get_redirect_target():
for target in request.values.get('next'), request.referrer:
if not target:
continue
if is_safe_url(target):
return target
def redirect_back(endpoint, **values):
target = request.form['next']
if not target or not is_safe_url(target):
target = url_for(endpoint, **values)
return redirect(target)