Get URL to redirect to and ensure that it is local.

def get_safe_redirect_target(arg='next'):
    """Get URL to redirect to and ensure that it is local."""
    for target in request.args.get(arg), request.referrer:
        if not target:
            continue
        if is_local_url(target):
            return target
    return None