2016-07-03 16:48:04 +02:00
|
|
|
|
2016-12-27 07:09:35 +01:00
|
|
|
from six import binary_type
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, AnyStr, Callable, Dict, Iterable, List, MutableMapping, Optional, Text
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2013-02-11 23:33:47 +01:00
|
|
|
from django.conf import settings
|
2017-01-30 05:17:48 +01:00
|
|
|
from django.core.exceptions import DisallowedHost
|
2016-05-25 15:02:02 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2017-05-18 11:56:03 +02:00
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
2016-05-25 15:02:02 +02:00
|
|
|
|
2017-07-21 02:19:52 +02:00
|
|
|
from zerver.lib.response import json_error, json_response_from_error
|
2017-07-25 03:30:13 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError, ErrorCode
|
2013-02-11 23:33:47 +01:00
|
|
|
from django.db import connection
|
2017-05-26 01:22:19 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
|
2016-07-19 14:35:08 +02:00
|
|
|
from zerver.lib.utils import statsd, get_subdomain
|
2013-09-30 17:53:46 +02:00
|
|
|
from zerver.lib.queue import queue_json_publish
|
2016-03-31 03:24:05 +02:00
|
|
|
from zerver.lib.cache import get_remote_cache_time, get_remote_cache_requests
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.bugdown import get_bugdown_time, get_bugdown_requests
|
2017-01-04 05:30:48 +01:00
|
|
|
from zerver.models import flush_per_request_caches, get_realm
|
2017-07-20 00:38:39 +02:00
|
|
|
from zerver.lib.exceptions import RateLimited
|
2015-01-16 05:59:20 +01:00
|
|
|
from django.contrib.sessions.middleware import SessionMiddleware
|
2013-12-17 22:18:13 +01:00
|
|
|
from django.views.csrf import csrf_failure as html_csrf_failure
|
2015-01-16 05:59:20 +01:00
|
|
|
from django.utils.cache import patch_vary_headers
|
|
|
|
from django.utils.http import cookie_date
|
2017-03-16 14:08:42 +01:00
|
|
|
from django.shortcuts import redirect, render
|
2012-12-19 20:19:46 +01:00
|
|
|
|
2012-10-16 23:52:10 +02:00
|
|
|
import logging
|
2012-11-14 21:00:26 +01:00
|
|
|
import time
|
2013-11-18 18:55:19 +01:00
|
|
|
import cProfile
|
2013-12-17 22:50:49 +01:00
|
|
|
import traceback
|
2012-10-16 23:52:10 +02:00
|
|
|
|
2013-08-06 22:51:47 +02:00
|
|
|
logger = logging.getLogger('zulip.requests')
|
2012-10-16 23:52:10 +02:00
|
|
|
|
2013-11-08 21:40:44 +01:00
|
|
|
def record_request_stop_data(log_data):
|
2016-07-03 16:48:04 +02:00
|
|
|
# type: (MutableMapping[str, Any]) -> None
|
2013-11-08 21:40:44 +01:00
|
|
|
log_data['time_stopped'] = time.time()
|
2016-03-31 03:23:21 +02:00
|
|
|
log_data['remote_cache_time_stopped'] = get_remote_cache_time()
|
2016-03-31 03:24:05 +02:00
|
|
|
log_data['remote_cache_requests_stopped'] = get_remote_cache_requests()
|
2013-11-08 21:40:44 +01:00
|
|
|
log_data['bugdown_time_stopped'] = get_bugdown_time()
|
|
|
|
log_data['bugdown_requests_stopped'] = get_bugdown_requests()
|
2013-11-18 18:55:19 +01:00
|
|
|
if settings.PROFILE_ALL_REQUESTS:
|
|
|
|
log_data["prof"].disable()
|
2013-11-08 21:40:44 +01:00
|
|
|
|
2013-04-23 19:36:50 +02:00
|
|
|
def async_request_stop(request):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (HttpRequest) -> None
|
2013-11-08 21:40:44 +01:00
|
|
|
record_request_stop_data(request._log_data)
|
|
|
|
|
|
|
|
def record_request_restart_data(log_data):
|
2016-07-03 16:48:04 +02:00
|
|
|
# type: (MutableMapping[str, Any]) -> None
|
2013-11-18 18:55:19 +01:00
|
|
|
if settings.PROFILE_ALL_REQUESTS:
|
|
|
|
log_data["prof"].enable()
|
2013-11-08 21:40:44 +01:00
|
|
|
log_data['time_restarted'] = time.time()
|
2016-03-31 03:23:21 +02:00
|
|
|
log_data['remote_cache_time_restarted'] = get_remote_cache_time()
|
2016-03-31 03:24:05 +02:00
|
|
|
log_data['remote_cache_requests_restarted'] = get_remote_cache_requests()
|
2013-11-08 21:40:44 +01:00
|
|
|
log_data['bugdown_time_restarted'] = get_bugdown_time()
|
|
|
|
log_data['bugdown_requests_restarted'] = get_bugdown_requests()
|
2013-04-23 19:36:50 +02:00
|
|
|
|
|
|
|
def async_request_restart(request):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (HttpRequest) -> None
|
2013-12-12 18:59:02 +01:00
|
|
|
if "time_restarted" in request._log_data:
|
|
|
|
# Don't destroy data when being called from
|
|
|
|
# finish_current_handler
|
|
|
|
return
|
2013-11-08 21:40:44 +01:00
|
|
|
record_request_restart_data(request._log_data)
|
|
|
|
|
|
|
|
def record_request_start_data(log_data):
|
2016-07-03 16:48:04 +02:00
|
|
|
# type: (MutableMapping[str, Any]) -> None
|
2013-11-18 18:55:19 +01:00
|
|
|
if settings.PROFILE_ALL_REQUESTS:
|
|
|
|
log_data["prof"] = cProfile.Profile()
|
|
|
|
log_data["prof"].enable()
|
|
|
|
|
2013-11-08 21:40:44 +01:00
|
|
|
log_data['time_started'] = time.time()
|
2016-03-31 03:23:21 +02:00
|
|
|
log_data['remote_cache_time_start'] = get_remote_cache_time()
|
2016-03-31 03:24:05 +02:00
|
|
|
log_data['remote_cache_requests_start'] = get_remote_cache_requests()
|
2013-11-08 21:40:44 +01:00
|
|
|
log_data['bugdown_time_start'] = get_bugdown_time()
|
|
|
|
log_data['bugdown_requests_start'] = get_bugdown_requests()
|
|
|
|
|
2013-11-08 23:11:37 +01:00
|
|
|
def timedelta_ms(timedelta):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (float) -> float
|
2013-11-08 23:11:37 +01:00
|
|
|
return timedelta * 1000
|
2013-11-08 21:40:44 +01:00
|
|
|
|
2013-11-08 23:11:37 +01:00
|
|
|
def format_timedelta(timedelta):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (float) -> str
|
2013-11-08 23:11:37 +01:00
|
|
|
if (timedelta >= 1):
|
|
|
|
return "%.1fs" % (timedelta)
|
|
|
|
return "%.0fms" % (timedelta_ms(timedelta),)
|
2013-11-08 21:40:44 +01:00
|
|
|
|
2013-12-26 15:01:46 +01:00
|
|
|
def is_slow_query(time_delta, path):
|
2016-12-27 07:09:35 +01:00
|
|
|
# type: (float, Text) -> bool
|
2013-12-26 15:16:49 +01:00
|
|
|
if time_delta < 1.2:
|
2013-12-26 15:13:00 +01:00
|
|
|
return False
|
|
|
|
is_exempt = \
|
|
|
|
path in ["/activity", "/json/report_error",
|
|
|
|
"/api/v1/deployments/report_error"] \
|
|
|
|
or path.startswith("/realm_activity/") \
|
|
|
|
or path.startswith("/user_activity/")
|
|
|
|
if is_exempt:
|
2013-12-26 15:23:18 +01:00
|
|
|
return time_delta >= 5
|
2013-12-26 15:20:59 +01:00
|
|
|
if 'webathena_kerberos' in path:
|
|
|
|
return time_delta >= 10
|
2013-12-26 15:13:00 +01:00
|
|
|
return True
|
2013-12-26 15:01:46 +01:00
|
|
|
|
2013-11-08 23:11:37 +01:00
|
|
|
def write_log_line(log_data, path, method, remote_ip, email, client_name,
|
2015-08-22 23:18:31 +02:00
|
|
|
status_code=200, error_content=None, error_content_iter=None):
|
2016-12-27 07:09:35 +01:00
|
|
|
# type: (MutableMapping[str, Any], Text, str, str, Text, Text, int, Optional[AnyStr], Optional[Iterable[AnyStr]]) -> None
|
2015-08-22 23:38:01 +02:00
|
|
|
assert error_content is None or error_content_iter is None
|
2015-08-22 23:18:31 +02:00
|
|
|
if error_content is not None:
|
|
|
|
error_content_iter = (error_content,)
|
|
|
|
|
2017-02-11 05:26:24 +01:00
|
|
|
# For statsd timer name
|
2013-11-08 21:40:44 +01:00
|
|
|
if path == '/':
|
2016-01-26 03:07:01 +01:00
|
|
|
statsd_path = u'webreq'
|
2013-11-08 21:40:44 +01:00
|
|
|
else:
|
|
|
|
statsd_path = u"webreq.%s" % (path[1:].replace('/', '.'),)
|
|
|
|
# Remove non-ascii chars from path (there should be none, if there are it's
|
|
|
|
# because someone manually entered a nonexistant path), as UTF-8 chars make
|
|
|
|
# statsd sad when it sends the key name over the socket
|
2016-01-24 05:18:35 +01:00
|
|
|
statsd_path = statsd_path.encode('ascii', errors='ignore').decode("ascii")
|
2015-08-21 08:52:48 +02:00
|
|
|
blacklisted_requests = ['do_confirm', 'send_confirm',
|
2014-01-16 23:20:33 +01:00
|
|
|
'eventslast_event_id', 'webreq.content', 'avatar', 'user_uploads',
|
2014-02-03 19:58:25 +01:00
|
|
|
'password.reset', 'static', 'json.bots', 'json.users', 'json.streams',
|
|
|
|
'accounts.unsubscribe', 'apple-touch-icon', 'emoji', 'json.bots',
|
|
|
|
'upload_file', 'realm_activity', 'user_activity']
|
2013-11-08 21:40:44 +01:00
|
|
|
suppress_statsd = any((blacklisted in statsd_path for blacklisted in blacklisted_requests))
|
|
|
|
|
|
|
|
time_delta = -1
|
|
|
|
# A time duration of -1 means the StartLogRequests middleware
|
|
|
|
# didn't run for some reason
|
|
|
|
optional_orig_delta = ""
|
|
|
|
if 'time_started' in log_data:
|
|
|
|
time_delta = time.time() - log_data['time_started']
|
|
|
|
if 'time_stopped' in log_data:
|
|
|
|
orig_time_delta = time_delta
|
|
|
|
time_delta = ((log_data['time_stopped'] - log_data['time_started']) +
|
|
|
|
(time.time() - log_data['time_restarted']))
|
|
|
|
optional_orig_delta = " (lp: %s)" % (format_timedelta(orig_time_delta),)
|
2016-03-31 03:28:24 +02:00
|
|
|
remote_cache_output = ""
|
2016-03-31 03:23:21 +02:00
|
|
|
if 'remote_cache_time_start' in log_data:
|
|
|
|
remote_cache_time_delta = get_remote_cache_time() - log_data['remote_cache_time_start']
|
2016-03-31 03:26:47 +02:00
|
|
|
remote_cache_count_delta = get_remote_cache_requests() - log_data['remote_cache_requests_start']
|
2016-03-31 03:24:05 +02:00
|
|
|
if 'remote_cache_requests_stopped' in log_data:
|
2013-11-08 21:40:44 +01:00
|
|
|
# (now - restarted) + (stopped - start) = (now - start) + (stopped - restarted)
|
2016-03-31 03:23:21 +02:00
|
|
|
remote_cache_time_delta += (log_data['remote_cache_time_stopped'] -
|
2016-12-03 00:04:17 +01:00
|
|
|
log_data['remote_cache_time_restarted'])
|
2016-03-31 03:26:47 +02:00
|
|
|
remote_cache_count_delta += (log_data['remote_cache_requests_stopped'] -
|
2016-12-03 00:04:17 +01:00
|
|
|
log_data['remote_cache_requests_restarted'])
|
2013-11-08 21:40:44 +01:00
|
|
|
|
2016-03-31 03:23:21 +02:00
|
|
|
if (remote_cache_time_delta > 0.005):
|
2016-03-31 03:28:24 +02:00
|
|
|
remote_cache_output = " (mem: %s/%s)" % (format_timedelta(remote_cache_time_delta),
|
|
|
|
remote_cache_count_delta)
|
2013-11-08 21:40:44 +01:00
|
|
|
|
|
|
|
if not suppress_statsd:
|
2016-03-31 03:33:30 +02:00
|
|
|
statsd.timing("%s.remote_cache.time" % (statsd_path,), timedelta_ms(remote_cache_time_delta))
|
|
|
|
statsd.incr("%s.remote_cache.querycount" % (statsd_path,), remote_cache_count_delta)
|
2013-11-08 21:40:44 +01:00
|
|
|
|
2013-11-18 19:34:37 +01:00
|
|
|
startup_output = ""
|
|
|
|
if 'startup_time_delta' in log_data and log_data["startup_time_delta"] > 0.005:
|
|
|
|
startup_output = " (+start: %s)" % (format_timedelta(log_data["startup_time_delta"]))
|
|
|
|
|
2013-11-08 21:40:44 +01:00
|
|
|
bugdown_output = ""
|
|
|
|
if 'bugdown_time_start' in log_data:
|
|
|
|
bugdown_time_delta = get_bugdown_time() - log_data['bugdown_time_start']
|
|
|
|
bugdown_count_delta = get_bugdown_requests() - log_data['bugdown_requests_start']
|
|
|
|
if 'bugdown_requests_stopped' in log_data:
|
|
|
|
# (now - restarted) + (stopped - start) = (now - start) + (stopped - restarted)
|
|
|
|
bugdown_time_delta += (log_data['bugdown_time_stopped'] -
|
|
|
|
log_data['bugdown_time_restarted'])
|
|
|
|
bugdown_count_delta += (log_data['bugdown_requests_stopped'] -
|
|
|
|
log_data['bugdown_requests_restarted'])
|
|
|
|
|
|
|
|
if (bugdown_time_delta > 0.005):
|
|
|
|
bugdown_output = " (md: %s/%s)" % (format_timedelta(bugdown_time_delta),
|
|
|
|
bugdown_count_delta)
|
|
|
|
|
|
|
|
if not suppress_statsd:
|
|
|
|
statsd.timing("%s.markdown.time" % (statsd_path,), timedelta_ms(bugdown_time_delta))
|
|
|
|
statsd.incr("%s.markdown.count" % (statsd_path,), bugdown_count_delta)
|
|
|
|
|
|
|
|
# Get the amount of time spent doing database queries
|
|
|
|
db_time_output = ""
|
2014-01-07 22:20:29 +01:00
|
|
|
queries = connection.connection.queries if connection.connection is not None else []
|
|
|
|
if len(queries) > 0:
|
|
|
|
query_time = sum(float(query.get('time', 0)) for query in queries)
|
2013-11-08 21:40:44 +01:00
|
|
|
db_time_output = " (db: %s/%sq)" % (format_timedelta(query_time),
|
2014-01-07 22:20:29 +01:00
|
|
|
len(queries))
|
2013-11-08 21:40:44 +01:00
|
|
|
|
|
|
|
if not suppress_statsd:
|
|
|
|
# Log ms, db ms, and num queries to statsd
|
|
|
|
statsd.timing("%s.dbtime" % (statsd_path,), timedelta_ms(query_time))
|
2016-05-04 23:16:27 +02:00
|
|
|
statsd.incr("%s.dbq" % (statsd_path,), len(queries))
|
2013-11-08 21:40:44 +01:00
|
|
|
statsd.timing("%s.total" % (statsd_path,), timedelta_ms(time_delta))
|
|
|
|
|
|
|
|
if 'extra' in log_data:
|
|
|
|
extra_request_data = " %s" % (log_data['extra'],)
|
|
|
|
else:
|
|
|
|
extra_request_data = ""
|
|
|
|
logger_client = "(%s via %s)" % (email, client_name)
|
2016-11-30 14:17:35 +01:00
|
|
|
logger_timing = ('%5s%s%s%s%s%s %s' %
|
2013-11-08 21:40:44 +01:00
|
|
|
(format_timedelta(time_delta), optional_orig_delta,
|
2016-03-31 03:28:24 +02:00
|
|
|
remote_cache_output, bugdown_output,
|
2016-11-30 14:17:35 +01:00
|
|
|
db_time_output, startup_output, path))
|
|
|
|
logger_line = ('%-15s %-7s %3d %s%s %s' %
|
|
|
|
(remote_ip, method, status_code,
|
|
|
|
logger_timing, extra_request_data, logger_client))
|
2015-12-14 06:54:38 +01:00
|
|
|
if (status_code in [200, 304] and method == "GET" and path.startswith("/static")):
|
|
|
|
logger.debug(logger_line)
|
|
|
|
else:
|
|
|
|
logger.info(logger_line)
|
2013-11-08 21:40:44 +01:00
|
|
|
|
2013-12-26 15:01:46 +01:00
|
|
|
if (is_slow_query(time_delta, path)):
|
2016-07-13 03:19:44 +02:00
|
|
|
queue_json_publish("slow_queries", "%s (%s)" % (logger_line, email), lambda e: None)
|
2013-11-08 21:40:44 +01:00
|
|
|
|
2013-11-18 18:55:19 +01:00
|
|
|
if settings.PROFILE_ALL_REQUESTS:
|
|
|
|
log_data["prof"].disable()
|
|
|
|
profile_path = "/tmp/profile.data.%s.%s" % (path.split("/")[-1], int(time_delta * 1000),)
|
|
|
|
log_data["prof"].dump_stats(profile_path)
|
|
|
|
|
2013-11-08 21:40:44 +01:00
|
|
|
# Log some additional data whenever we return certain 40x errors
|
|
|
|
if 400 <= status_code < 500 and status_code not in [401, 404, 405]:
|
2017-02-11 05:26:24 +01:00
|
|
|
assert error_content_iter is not None
|
2016-07-10 00:15:36 +02:00
|
|
|
error_content_list = list(error_content_iter)
|
|
|
|
if error_content_list:
|
|
|
|
error_data = u''
|
2016-12-27 07:09:35 +01:00
|
|
|
elif isinstance(error_content_list[0], Text):
|
2016-07-10 00:15:36 +02:00
|
|
|
error_data = u''.join(error_content_list)
|
|
|
|
elif isinstance(error_content_list[0], binary_type):
|
|
|
|
error_data = repr(b''.join(error_content_list))
|
|
|
|
if len(error_data) > 100:
|
|
|
|
error_data = u"[content more than 100 characters]"
|
|
|
|
logger.info('status=%3d, data=%s, uid=%s' % (status_code, error_data, email))
|
2013-04-23 19:36:50 +02:00
|
|
|
|
2017-05-18 11:56:03 +02:00
|
|
|
class LogRequests(MiddlewareMixin):
|
2013-11-18 19:34:37 +01:00
|
|
|
# We primarily are doing logging using the process_view hook, but
|
|
|
|
# for some views, process_view isn't run, so we call the start
|
|
|
|
# method here too
|
2012-11-14 21:00:26 +01:00
|
|
|
def process_request(self, request):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (HttpRequest) -> None
|
2013-11-08 20:18:02 +01:00
|
|
|
request._log_data = dict()
|
2013-11-08 21:40:44 +01:00
|
|
|
record_request_start_data(request._log_data)
|
2014-01-07 22:20:29 +01:00
|
|
|
if connection.connection is not None:
|
|
|
|
connection.connection.queries = []
|
2012-11-14 21:00:26 +01:00
|
|
|
|
2013-11-18 19:34:37 +01:00
|
|
|
def process_view(self, request, view_func, args, kwargs):
|
2017-02-19 03:39:27 +01:00
|
|
|
# type: (HttpRequest, Callable[..., HttpResponse], List[str], Dict[str, Any]) -> None
|
2013-11-18 19:34:37 +01:00
|
|
|
# process_request was already run; we save the initialization
|
|
|
|
# time (i.e. the time between receiving the request and
|
|
|
|
# figuring out which view function to call, which is primarily
|
|
|
|
# importing modules on the first start)
|
|
|
|
request._log_data["startup_time_delta"] = time.time() - request._log_data["time_started"]
|
|
|
|
# And then completely reset our tracking to only cover work
|
|
|
|
# done as part of this request
|
|
|
|
record_request_start_data(request._log_data)
|
2014-01-07 22:20:29 +01:00
|
|
|
if connection.connection is not None:
|
|
|
|
connection.connection.queries = []
|
2013-11-18 19:34:37 +01:00
|
|
|
|
2012-10-16 23:52:10 +02:00
|
|
|
def process_response(self, request, response):
|
2017-05-24 03:50:56 +02:00
|
|
|
# type: (HttpRequest, StreamingHttpResponse) -> StreamingHttpResponse
|
2012-10-16 23:52:10 +02:00
|
|
|
# The reverse proxy might have sent us the real external IP
|
|
|
|
remote_ip = request.META.get('HTTP_X_REAL_IP')
|
|
|
|
if remote_ip is None:
|
|
|
|
remote_ip = request.META['REMOTE_ADDR']
|
|
|
|
|
2013-03-15 18:57:58 +01:00
|
|
|
# Get the requestor's email address and client, if available.
|
2013-02-11 23:15:34 +01:00
|
|
|
try:
|
|
|
|
email = request._email
|
|
|
|
except Exception:
|
|
|
|
email = "unauth"
|
2013-03-15 18:57:58 +01:00
|
|
|
try:
|
2013-03-26 19:40:28 +01:00
|
|
|
client = request.client.name
|
2013-03-15 18:57:58 +01:00
|
|
|
except Exception:
|
|
|
|
client = "?"
|
2013-02-11 23:15:34 +01:00
|
|
|
|
2015-08-19 22:20:27 +02:00
|
|
|
if response.streaming:
|
|
|
|
content_iter = response.streaming_content
|
2015-08-22 23:18:31 +02:00
|
|
|
content = None
|
2015-08-19 22:20:27 +02:00
|
|
|
else:
|
2015-08-22 23:18:31 +02:00
|
|
|
content = response.content
|
2015-08-22 23:38:01 +02:00
|
|
|
content_iter = None
|
2015-08-19 22:20:27 +02:00
|
|
|
|
2013-11-08 21:40:44 +01:00
|
|
|
write_log_line(request._log_data, request.path, request.method,
|
2015-08-22 23:18:31 +02:00
|
|
|
remote_ip, email, client, status_code=response.status_code,
|
|
|
|
error_content=content, error_content_iter=content_iter)
|
2012-10-16 23:52:10 +02:00
|
|
|
return response
|
2012-12-19 20:19:46 +01:00
|
|
|
|
2017-05-18 11:56:03 +02:00
|
|
|
class JsonErrorHandler(MiddlewareMixin):
|
2012-12-19 20:19:46 +01:00
|
|
|
def process_exception(self, request, exception):
|
2017-07-20 00:19:42 +02:00
|
|
|
# type: (HttpRequest, Exception) -> Optional[HttpResponse]
|
|
|
|
if isinstance(exception, JsonableError):
|
2017-07-21 02:19:52 +02:00
|
|
|
return json_response_from_error(exception)
|
2013-12-17 22:50:49 +01:00
|
|
|
if request.error_format == "JSON":
|
|
|
|
logging.error(traceback.format_exc())
|
2016-05-25 15:02:02 +02:00
|
|
|
return json_error(_("Internal server error"), status=500)
|
2012-12-19 20:19:46 +01:00
|
|
|
return None
|
2013-02-12 17:26:12 +01:00
|
|
|
|
2017-05-18 11:56:03 +02:00
|
|
|
class TagRequests(MiddlewareMixin):
|
2013-12-17 22:18:13 +01:00
|
|
|
def process_view(self, request, view_func, args, kwargs):
|
2017-02-19 03:39:27 +01:00
|
|
|
# type: (HttpRequest, Callable[..., HttpResponse], List[str], Dict[str, Any]) -> None
|
2013-12-17 22:18:13 +01:00
|
|
|
self.process_request(request)
|
2016-11-29 07:22:02 +01:00
|
|
|
|
2013-12-17 22:18:13 +01:00
|
|
|
def process_request(self, request):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (HttpRequest) -> None
|
2013-12-17 22:18:13 +01:00
|
|
|
if request.path.startswith("/api/") or request.path.startswith("/json/"):
|
|
|
|
request.error_format = "JSON"
|
|
|
|
else:
|
|
|
|
request.error_format = "HTML"
|
|
|
|
|
2017-07-25 03:30:13 +02:00
|
|
|
class CsrfFailureError(JsonableError):
|
|
|
|
http_status_code = 403
|
|
|
|
code = ErrorCode.CSRF_FAILED
|
|
|
|
data_fields = ['reason']
|
|
|
|
|
|
|
|
def __init__(self, reason):
|
|
|
|
# type: (Text) -> None
|
|
|
|
self.reason = reason # type: Text
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format():
|
2017-08-11 00:31:28 +02:00
|
|
|
# type: () -> Text
|
2017-07-25 03:30:13 +02:00
|
|
|
return _("CSRF Error: {reason}")
|
|
|
|
|
2013-12-17 22:18:13 +01:00
|
|
|
def csrf_failure(request, reason=""):
|
2017-08-11 00:31:28 +02:00
|
|
|
# type: (HttpRequest, Text) -> HttpResponse
|
2013-12-17 22:18:13 +01:00
|
|
|
if request.error_format == "JSON":
|
2017-07-25 03:30:13 +02:00
|
|
|
return json_response_from_error(CsrfFailureError(reason))
|
2013-12-17 22:18:13 +01:00
|
|
|
else:
|
|
|
|
return html_csrf_failure(request, reason)
|
|
|
|
|
2017-05-18 11:56:03 +02:00
|
|
|
class RateLimitMiddleware(MiddlewareMixin):
|
2013-05-29 23:58:07 +02:00
|
|
|
def process_response(self, request, response):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (HttpRequest, HttpResponse) -> HttpResponse
|
2013-05-29 23:58:07 +02:00
|
|
|
if not settings.RATE_LIMITING:
|
|
|
|
return response
|
|
|
|
|
2017-07-31 08:08:47 +02:00
|
|
|
from zerver.lib.rate_limiter import max_api_calls, RateLimitedUser
|
2013-05-29 23:58:07 +02:00
|
|
|
# Add X-RateLimit-*** headers
|
|
|
|
if hasattr(request, '_ratelimit_applied_limits'):
|
2017-07-31 08:08:47 +02:00
|
|
|
entity = RateLimitedUser(request.user)
|
|
|
|
response['X-RateLimit-Limit'] = str(max_api_calls(entity))
|
2013-05-29 23:58:07 +02:00
|
|
|
if hasattr(request, '_ratelimit_secs_to_freedom'):
|
2017-05-24 03:43:40 +02:00
|
|
|
response['X-RateLimit-Reset'] = str(int(time.time() + request._ratelimit_secs_to_freedom))
|
2013-05-29 23:58:07 +02:00
|
|
|
if hasattr(request, '_ratelimit_remaining'):
|
2017-05-24 03:43:40 +02:00
|
|
|
response['X-RateLimit-Remaining'] = str(request._ratelimit_remaining)
|
2013-05-29 23:58:07 +02:00
|
|
|
return response
|
|
|
|
|
|
|
|
def process_exception(self, request, exception):
|
2017-05-24 02:44:30 +02:00
|
|
|
# type: (HttpRequest, Exception) -> Optional[HttpResponse]
|
2015-11-01 17:10:16 +01:00
|
|
|
if isinstance(exception, RateLimited):
|
2017-05-22 20:12:59 +02:00
|
|
|
resp = json_error(
|
|
|
|
_("API usage exceeded rate limit"),
|
|
|
|
data={'retry-after': request._ratelimit_secs_to_freedom},
|
|
|
|
status=429
|
|
|
|
)
|
2014-03-07 16:47:30 +01:00
|
|
|
resp['Retry-After'] = request._ratelimit_secs_to_freedom
|
2013-05-29 23:58:07 +02:00
|
|
|
return resp
|
2017-08-02 17:21:46 +02:00
|
|
|
return None
|
2013-11-08 21:13:34 +01:00
|
|
|
|
2017-05-18 11:56:03 +02:00
|
|
|
class FlushDisplayRecipientCache(MiddlewareMixin):
|
2013-11-08 21:13:34 +01:00
|
|
|
def process_response(self, request, response):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (HttpRequest, HttpResponse) -> HttpResponse
|
2013-12-18 23:00:14 +01:00
|
|
|
# We flush the per-request caches after every request, so they
|
|
|
|
# are not shared at all between requests.
|
|
|
|
flush_per_request_caches()
|
2013-11-08 21:13:34 +01:00
|
|
|
return response
|
2015-01-16 05:59:20 +01:00
|
|
|
|
|
|
|
class SessionHostDomainMiddleware(SessionMiddleware):
|
|
|
|
def process_response(self, request, response):
|
2016-06-05 02:23:03 +02:00
|
|
|
# type: (HttpRequest, HttpResponse) -> HttpResponse
|
2017-01-30 05:17:48 +01:00
|
|
|
try:
|
|
|
|
request.get_host()
|
|
|
|
except DisallowedHost:
|
|
|
|
# If we get a DisallowedHost exception trying to access
|
|
|
|
# the host, (1) the request is failed anyway and so the
|
|
|
|
# below code will do nothing, and (2) the below will
|
|
|
|
# trigger a recursive exception, breaking things, so we
|
|
|
|
# just return here.
|
|
|
|
return response
|
|
|
|
|
2017-10-02 08:32:09 +02:00
|
|
|
if (not request.path.startswith("/static/") and not request.path.startswith("/api/") and
|
|
|
|
not request.path.startswith("/json/")):
|
|
|
|
subdomain = get_subdomain(request)
|
|
|
|
if (request.get_host() == "127.0.0.1:9991" or request.get_host() == "localhost:9991"):
|
|
|
|
return redirect("%s%s" % (settings.EXTERNAL_URI_SCHEME,
|
|
|
|
settings.EXTERNAL_HOST))
|
|
|
|
if subdomain != "":
|
|
|
|
realm = get_realm(subdomain)
|
|
|
|
if (realm is None):
|
|
|
|
return render(request, "zerver/invalid_realm.html")
|
2015-01-16 05:59:20 +01:00
|
|
|
"""
|
|
|
|
If request.session was modified, or if the configuration is to save the
|
|
|
|
session every time, save the changes and set a session cookie.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
accessed = request.session.accessed
|
|
|
|
modified = request.session.modified
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if accessed:
|
|
|
|
patch_vary_headers(response, ('Cookie',))
|
|
|
|
if modified or settings.SESSION_SAVE_EVERY_REQUEST:
|
|
|
|
if request.session.get_expire_at_browser_close():
|
|
|
|
max_age = None
|
|
|
|
expires = None
|
|
|
|
else:
|
|
|
|
max_age = request.session.get_expiry_age()
|
|
|
|
expires_time = time.time() + max_age
|
|
|
|
expires = cookie_date(expires_time)
|
|
|
|
# Save the session data and refresh the client cookie.
|
|
|
|
# Skip session save for 500 responses, refs #3881.
|
|
|
|
if response.status_code != 500:
|
|
|
|
request.session.save()
|
2015-01-16 06:03:20 +01:00
|
|
|
host = request.get_host().split(':')[0]
|
2016-07-19 14:35:08 +02:00
|
|
|
|
|
|
|
# The subdomains feature overrides the
|
|
|
|
# SESSION_COOKIE_DOMAIN setting, since the setting
|
|
|
|
# is a fixed value and with subdomains enabled,
|
|
|
|
# the session cookie domain has to vary with the
|
|
|
|
# subdomain.
|
2017-10-02 08:32:09 +02:00
|
|
|
session_cookie_domain = host
|
2015-01-16 05:59:20 +01:00
|
|
|
response.set_cookie(settings.SESSION_COOKIE_NAME,
|
2016-12-03 00:04:17 +01:00
|
|
|
request.session.session_key, max_age=max_age,
|
|
|
|
expires=expires, domain=session_cookie_domain,
|
|
|
|
path=settings.SESSION_COOKIE_PATH,
|
|
|
|
secure=settings.SESSION_COOKIE_SECURE or None,
|
|
|
|
httponly=settings.SESSION_COOKIE_HTTPONLY or None)
|
2015-01-16 05:59:20 +01:00
|
|
|
return response
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2017-05-18 11:56:03 +02:00
|
|
|
class SetRemoteAddrFromForwardedFor(MiddlewareMixin):
|
2017-01-30 23:19:38 +01:00
|
|
|
"""
|
|
|
|
Middleware that sets REMOTE_ADDR based on the HTTP_X_FORWARDED_FOR.
|
|
|
|
|
|
|
|
This middleware replicates Django's former SetRemoteAddrFromForwardedFor middleware.
|
|
|
|
Because Zulip sits behind a NGINX reverse proxy, if the HTTP_X_FORWARDED_FOR
|
|
|
|
is set in the request, then it has properly been set by NGINX.
|
|
|
|
Therefore HTTP_X_FORWARDED_FOR's value is trusted.
|
|
|
|
"""
|
|
|
|
def process_request(self, request):
|
|
|
|
# type: (HttpRequest) -> None
|
|
|
|
try:
|
|
|
|
real_ip = request.META['HTTP_X_FORWARDED_FOR']
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
# HTTP_X_FORWARDED_FOR can be a comma-separated list of IPs.
|
|
|
|
# For NGINX reverse proxy servers, the client's IP will be the first one.
|
|
|
|
real_ip = real_ip.split(",")[0].strip()
|
|
|
|
request.META['REMOTE_ADDR'] = real_ip
|