Skip to content

Instantly share code, notes, and snippets.

@tarekziade
Created March 18, 2017 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarekziade/6535011234c45bf8aaee41c6c8436b3d to your computer and use it in GitHub Desktop.
Save tarekziade/6535011234c45bf8aaee41c6c8436b3d to your computer and use it in GitHub Desktop.
White list any 302 locations
from flask import make_response
from urllib.parse import urlparse
# domain:port
SAFE_DOMAINS = ['ziade.org:443']
@app.after_request
def check_redirect(response):
if response.status_code != 302:
return response
url = urlparse(response.location)
netloc = url.netloc
if url.scheme == 'http' and not netloc.endswith(':80'):
netloc += ':80'
if url.scheme == 'https' and not netloc.endswith(':443'):
netloc += ':443'
if netloc not in SAFE_DOMAINS:
# not using abort() here or it'll break the hook
return make_response('Forbidden', 403)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment