site.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. # Copyright 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import contextlib
  15. import logging
  16. import time
  17. from typing import TYPE_CHECKING, Any, Generator, Optional, Tuple, Union
  18. import attr
  19. from zope.interface import implementer
  20. from twisted.internet.defer import Deferred
  21. from twisted.internet.interfaces import IAddress, IReactorTime
  22. from twisted.python.failure import Failure
  23. from twisted.web.http import HTTPChannel
  24. from twisted.web.resource import IResource, Resource
  25. from twisted.web.server import Request, Site
  26. from synapse.config.server import ListenerConfig
  27. from synapse.http import get_request_user_agent, redact_uri
  28. from synapse.http.request_metrics import RequestMetrics, requests_counter
  29. from synapse.logging.context import (
  30. ContextRequest,
  31. LoggingContext,
  32. PreserveLoggingContext,
  33. )
  34. from synapse.types import Requester
  35. if TYPE_CHECKING:
  36. import opentracing
  37. logger = logging.getLogger(__name__)
  38. _next_request_seq = 0
  39. class SynapseRequest(Request):
  40. """Class which encapsulates an HTTP request to synapse.
  41. All of the requests processed in synapse are of this type.
  42. It extends twisted's twisted.web.server.Request, and adds:
  43. * Unique request ID
  44. * A log context associated with the request
  45. * Redaction of access_token query-params in __repr__
  46. * Logging at start and end
  47. * Metrics to record CPU, wallclock and DB time by endpoint.
  48. * A limit to the size of request which will be accepted
  49. It also provides a method `processing`, which returns a context manager. If this
  50. method is called, the request won't be logged until the context manager is closed;
  51. this is useful for asynchronous request handlers which may go on processing the
  52. request even after the client has disconnected.
  53. Attributes:
  54. logcontext: the log context for this request
  55. """
  56. def __init__(
  57. self,
  58. channel: HTTPChannel,
  59. site: "SynapseSite",
  60. *args: Any,
  61. max_request_body_size: int = 1024,
  62. **kw: Any,
  63. ):
  64. super().__init__(channel, *args, **kw)
  65. self._max_request_body_size = max_request_body_size
  66. self.synapse_site = site
  67. self.reactor = site.reactor
  68. self._channel = channel # this is used by the tests
  69. self.start_time = 0.0
  70. # The requester, if authenticated. For federation requests this is the
  71. # server name, for client requests this is the Requester object.
  72. self._requester: Optional[Union[Requester, str]] = None
  73. # An opentracing span for this request. Will be closed when the request is
  74. # completely processed.
  75. self._opentracing_span: "Optional[opentracing.Span]" = None
  76. # we can't yet create the logcontext, as we don't know the method.
  77. self.logcontext: Optional[LoggingContext] = None
  78. # The `Deferred` to cancel if the client disconnects early and
  79. # `is_render_cancellable` is set. Expected to be set by `Resource.render`.
  80. self.render_deferred: Optional["Deferred[None]"] = None
  81. # A boolean indicating whether `render_deferred` should be cancelled if the
  82. # client disconnects early. Expected to be set by the coroutine started by
  83. # `Resource.render`, if rendering is asynchronous.
  84. self.is_render_cancellable = False
  85. global _next_request_seq
  86. self.request_seq = _next_request_seq
  87. _next_request_seq += 1
  88. # whether an asynchronous request handler has called processing()
  89. self._is_processing = False
  90. # the time when the asynchronous request handler completed its processing
  91. self._processing_finished_time: Optional[float] = None
  92. # what time we finished sending the response to the client (or the connection
  93. # dropped)
  94. self.finish_time: Optional[float] = None
  95. def __repr__(self) -> str:
  96. # We overwrite this so that we don't log ``access_token``
  97. return "<%s at 0x%x method=%r uri=%r clientproto=%r site=%r>" % (
  98. self.__class__.__name__,
  99. id(self),
  100. self.get_method(),
  101. self.get_redacted_uri(),
  102. self.clientproto.decode("ascii", errors="replace"),
  103. self.synapse_site.site_tag,
  104. )
  105. def handleContentChunk(self, data: bytes) -> None:
  106. # we should have a `content` by now.
  107. assert self.content, "handleContentChunk() called before gotLength()"
  108. if self.content.tell() + len(data) > self._max_request_body_size:
  109. logger.warning(
  110. "Aborting connection from %s because the request exceeds maximum size: %s %s",
  111. self.client,
  112. self.get_method(),
  113. self.get_redacted_uri(),
  114. )
  115. self.transport.abortConnection()
  116. return
  117. super().handleContentChunk(data)
  118. @property
  119. def requester(self) -> Optional[Union[Requester, str]]:
  120. return self._requester
  121. @requester.setter
  122. def requester(self, value: Union[Requester, str]) -> None:
  123. # Store the requester, and update some properties based on it.
  124. # This should only be called once.
  125. assert self._requester is None
  126. self._requester = value
  127. # A logging context should exist by now (and have a ContextRequest).
  128. assert self.logcontext is not None
  129. assert self.logcontext.request is not None
  130. (
  131. requester,
  132. authenticated_entity,
  133. ) = self.get_authenticated_entity()
  134. self.logcontext.request.requester = requester
  135. # If there's no authenticated entity, it was the requester.
  136. self.logcontext.request.authenticated_entity = authenticated_entity or requester
  137. def set_opentracing_span(self, span: "opentracing.Span") -> None:
  138. """attach an opentracing span to this request
  139. Doing so will cause the span to be closed when we finish processing the request
  140. """
  141. self._opentracing_span = span
  142. def get_request_id(self) -> str:
  143. return "%s-%i" % (self.get_method(), self.request_seq)
  144. def get_redacted_uri(self) -> str:
  145. """Gets the redacted URI associated with the request (or placeholder if the URI
  146. has not yet been received).
  147. Note: This is necessary as the placeholder value in twisted is str
  148. rather than bytes, so we need to sanitise `self.uri`.
  149. Returns:
  150. The redacted URI as a string.
  151. """
  152. uri: Union[bytes, str] = self.uri
  153. if isinstance(uri, bytes):
  154. uri = uri.decode("ascii", errors="replace")
  155. return redact_uri(uri)
  156. def get_method(self) -> str:
  157. """Gets the method associated with the request (or placeholder if method
  158. has not yet been received).
  159. Note: This is necessary as the placeholder value in twisted is str
  160. rather than bytes, so we need to sanitise `self.method`.
  161. Returns:
  162. The request method as a string.
  163. """
  164. method: Union[bytes, str] = self.method
  165. if isinstance(method, bytes):
  166. return self.method.decode("ascii")
  167. return method
  168. def get_authenticated_entity(self) -> Tuple[Optional[str], Optional[str]]:
  169. """
  170. Get the "authenticated" entity of the request, which might be the user
  171. performing the action, or a user being puppeted by a server admin.
  172. Returns:
  173. A tuple:
  174. The first item is a string representing the user making the request.
  175. The second item is a string or None representing the user who
  176. authenticated when making this request. See
  177. Requester.authenticated_entity.
  178. """
  179. # Convert the requester into a string that we can log
  180. if isinstance(self._requester, str):
  181. return self._requester, None
  182. elif isinstance(self._requester, Requester):
  183. requester = self._requester.user.to_string()
  184. authenticated_entity = self._requester.authenticated_entity
  185. # If this is a request where the target user doesn't match the user who
  186. # authenticated (e.g. and admin is puppetting a user) then we return both.
  187. if self._requester.user.to_string() != authenticated_entity:
  188. return requester, authenticated_entity
  189. return requester, None
  190. elif self._requester is not None:
  191. # This shouldn't happen, but we log it so we don't lose information
  192. # and can see that we're doing something wrong.
  193. return repr(self._requester), None # type: ignore[unreachable]
  194. return None, None
  195. def render(self, resrc: Resource) -> None:
  196. # this is called once a Resource has been found to serve the request; in our
  197. # case the Resource in question will normally be a JsonResource.
  198. # create a LogContext for this request
  199. request_id = self.get_request_id()
  200. self.logcontext = LoggingContext(
  201. request_id,
  202. request=ContextRequest(
  203. request_id=request_id,
  204. ip_address=self.getClientAddress().host,
  205. site_tag=self.synapse_site.site_tag,
  206. # The requester is going to be unknown at this point.
  207. requester=None,
  208. authenticated_entity=None,
  209. method=self.get_method(),
  210. url=self.get_redacted_uri(),
  211. protocol=self.clientproto.decode("ascii", errors="replace"),
  212. user_agent=get_request_user_agent(self),
  213. ),
  214. )
  215. # override the Server header which is set by twisted
  216. self.setHeader("Server", self.synapse_site.server_version_string)
  217. with PreserveLoggingContext(self.logcontext):
  218. # we start the request metrics timer here with an initial stab
  219. # at the servlet name. For most requests that name will be
  220. # JsonResource (or a subclass), and JsonResource._async_render
  221. # will update it once it picks a servlet.
  222. servlet_name = resrc.__class__.__name__
  223. self._started_processing(servlet_name)
  224. Request.render(self, resrc)
  225. # record the arrival of the request *after*
  226. # dispatching to the handler, so that the handler
  227. # can update the servlet name in the request
  228. # metrics
  229. requests_counter.labels(self.get_method(), self.request_metrics.name).inc()
  230. @contextlib.contextmanager
  231. def processing(self) -> Generator[None, None, None]:
  232. """Record the fact that we are processing this request.
  233. Returns a context manager; the correct way to use this is:
  234. async def handle_request(request):
  235. with request.processing("FooServlet"):
  236. await really_handle_the_request()
  237. Once the context manager is closed, the completion of the request will be logged,
  238. and the various metrics will be updated.
  239. """
  240. if self._is_processing:
  241. raise RuntimeError("Request is already processing")
  242. self._is_processing = True
  243. try:
  244. yield
  245. except Exception:
  246. # this should already have been caught, and sent back to the client as a 500.
  247. logger.exception(
  248. "Asynchronous message handler raised an uncaught exception"
  249. )
  250. finally:
  251. # the request handler has finished its work and either sent the whole response
  252. # back, or handed over responsibility to a Producer.
  253. self._processing_finished_time = time.time()
  254. self._is_processing = False
  255. if self._opentracing_span:
  256. self._opentracing_span.log_kv({"event": "finished processing"})
  257. # if we've already sent the response, log it now; otherwise, we wait for the
  258. # response to be sent.
  259. if self.finish_time is not None:
  260. self._finished_processing()
  261. def finish(self) -> None:
  262. """Called when all response data has been written to this Request.
  263. Overrides twisted.web.server.Request.finish to record the finish time and do
  264. logging.
  265. """
  266. self.finish_time = time.time()
  267. Request.finish(self)
  268. if self._opentracing_span:
  269. self._opentracing_span.log_kv({"event": "response sent"})
  270. if not self._is_processing:
  271. assert self.logcontext is not None
  272. with PreserveLoggingContext(self.logcontext):
  273. self._finished_processing()
  274. def connectionLost(self, reason: Union[Failure, Exception]) -> None:
  275. """Called when the client connection is closed before the response is written.
  276. Overrides twisted.web.server.Request.connectionLost to record the finish time and
  277. do logging.
  278. """
  279. # There is a bug in Twisted where reason is not wrapped in a Failure object
  280. # Detect this and wrap it manually as a workaround
  281. # More information: https://github.com/matrix-org/synapse/issues/7441
  282. if not isinstance(reason, Failure):
  283. reason = Failure(reason)
  284. self.finish_time = time.time()
  285. Request.connectionLost(self, reason)
  286. if self.logcontext is None:
  287. logger.info(
  288. "Connection from %s lost before request headers were read", self.client
  289. )
  290. return
  291. # we only get here if the connection to the client drops before we send
  292. # the response.
  293. #
  294. # It's useful to log it here so that we can get an idea of when
  295. # the client disconnects.
  296. with PreserveLoggingContext(self.logcontext):
  297. logger.info("Connection from client lost before response was sent")
  298. if self._opentracing_span:
  299. self._opentracing_span.log_kv(
  300. {"event": "client connection lost", "reason": str(reason.value)}
  301. )
  302. if self._is_processing:
  303. if self.is_render_cancellable:
  304. if self.render_deferred is not None:
  305. # Throw a cancellation into the request processing, in the hope
  306. # that it will finish up sooner than it normally would.
  307. # The `self.processing()` context manager will call
  308. # `_finished_processing()` when done.
  309. with PreserveLoggingContext():
  310. self.render_deferred.cancel()
  311. else:
  312. logger.error(
  313. "Connection from client lost, but have no Deferred to "
  314. "cancel even though the request is marked as cancellable."
  315. )
  316. else:
  317. self._finished_processing()
  318. def _started_processing(self, servlet_name: str) -> None:
  319. """Record the fact that we are processing this request.
  320. This will log the request's arrival. Once the request completes,
  321. be sure to call finished_processing.
  322. Args:
  323. servlet_name (str): the name of the servlet which will be
  324. processing this request. This is used in the metrics.
  325. It is possible to update this afterwards by updating
  326. self.request_metrics.name.
  327. """
  328. self.start_time = time.time()
  329. self.request_metrics = RequestMetrics()
  330. self.request_metrics.start(
  331. self.start_time, name=servlet_name, method=self.get_method()
  332. )
  333. self.synapse_site.access_logger.debug(
  334. "%s - %s - Received request: %s %s",
  335. self.getClientAddress().host,
  336. self.synapse_site.site_tag,
  337. self.get_method(),
  338. self.get_redacted_uri(),
  339. )
  340. def _finished_processing(self) -> None:
  341. """Log the completion of this request and update the metrics"""
  342. assert self.logcontext is not None
  343. assert self.finish_time is not None
  344. usage = self.logcontext.get_resource_usage()
  345. if self._processing_finished_time is None:
  346. # we completed the request without anything calling processing()
  347. self._processing_finished_time = time.time()
  348. # the time between receiving the request and the request handler finishing
  349. processing_time = self._processing_finished_time - self.start_time
  350. # the time between the request handler finishing and the response being sent
  351. # to the client (nb may be negative)
  352. response_send_time = self.finish_time - self._processing_finished_time
  353. user_agent = get_request_user_agent(self, "-")
  354. # int(self.code) looks redundant, because self.code is already an int.
  355. # But self.code might be an HTTPStatus (which inherits from int)---which has
  356. # a different string representation. So ensure we really have an integer.
  357. code = str(int(self.code))
  358. if not self.finished:
  359. # we didn't send the full response before we gave up (presumably because
  360. # the connection dropped)
  361. code += "!"
  362. log_level = logging.INFO if self._should_log_request() else logging.DEBUG
  363. # If this is a request where the target user doesn't match the user who
  364. # authenticated (e.g. and admin is puppetting a user) then we log both.
  365. requester, authenticated_entity = self.get_authenticated_entity()
  366. if authenticated_entity:
  367. requester = f"{authenticated_entity}|{requester}"
  368. self.synapse_site.access_logger.log(
  369. log_level,
  370. "%s - %s - {%s}"
  371. " Processed request: %.3fsec/%.3fsec (%.3fsec, %.3fsec) (%.3fsec/%.3fsec/%d)"
  372. ' %sB %s "%s %s %s" "%s" [%d dbevts]',
  373. self.getClientAddress().host,
  374. self.synapse_site.site_tag,
  375. requester,
  376. processing_time,
  377. response_send_time,
  378. usage.ru_utime,
  379. usage.ru_stime,
  380. usage.db_sched_duration_sec,
  381. usage.db_txn_duration_sec,
  382. int(usage.db_txn_count),
  383. self.sentLength,
  384. code,
  385. self.get_method(),
  386. self.get_redacted_uri(),
  387. self.clientproto.decode("ascii", errors="replace"),
  388. user_agent,
  389. usage.evt_db_fetch_count,
  390. )
  391. # complete the opentracing span, if any.
  392. if self._opentracing_span:
  393. self._opentracing_span.finish()
  394. try:
  395. self.request_metrics.stop(self.finish_time, self.code, self.sentLength)
  396. except Exception as e:
  397. logger.warning("Failed to stop metrics: %r", e)
  398. def _should_log_request(self) -> bool:
  399. """Whether we should log at INFO that we processed the request."""
  400. if self.path == b"/health":
  401. return False
  402. if self.method == b"OPTIONS":
  403. return False
  404. return True
  405. class XForwardedForRequest(SynapseRequest):
  406. """Request object which honours proxy headers
  407. Extends SynapseRequest to replace getClientIP, getClientAddress, and isSecure with
  408. information from request headers.
  409. """
  410. # the client IP and ssl flag, as extracted from the headers.
  411. _forwarded_for: "Optional[_XForwardedForAddress]" = None
  412. _forwarded_https: bool = False
  413. def requestReceived(self, command: bytes, path: bytes, version: bytes) -> None:
  414. # this method is called by the Channel once the full request has been
  415. # received, to dispatch the request to a resource.
  416. # We can use it to set the IP address and protocol according to the
  417. # headers.
  418. self._process_forwarded_headers()
  419. return super().requestReceived(command, path, version)
  420. def _process_forwarded_headers(self) -> None:
  421. headers = self.requestHeaders.getRawHeaders(b"x-forwarded-for")
  422. if not headers:
  423. return
  424. # for now, we just use the first x-forwarded-for header. Really, we ought
  425. # to start from the client IP address, and check whether it is trusted; if it
  426. # is, work backwards through the headers until we find an untrusted address.
  427. # see https://github.com/matrix-org/synapse/issues/9471
  428. self._forwarded_for = _XForwardedForAddress(
  429. headers[0].split(b",")[0].strip().decode("ascii")
  430. )
  431. # if we got an x-forwarded-for header, also look for an x-forwarded-proto header
  432. header = self.getHeader(b"x-forwarded-proto")
  433. if header is not None:
  434. self._forwarded_https = header.lower() == b"https"
  435. else:
  436. # this is done largely for backwards-compatibility so that people that
  437. # haven't set an x-forwarded-proto header don't get a redirect loop.
  438. logger.warning(
  439. "forwarded request lacks an x-forwarded-proto header: assuming https"
  440. )
  441. self._forwarded_https = True
  442. def isSecure(self) -> bool:
  443. if self._forwarded_https:
  444. return True
  445. return super().isSecure()
  446. def getClientIP(self) -> str:
  447. """
  448. Return the IP address of the client who submitted this request.
  449. This method is deprecated. Use getClientAddress() instead.
  450. """
  451. if self._forwarded_for is not None:
  452. return self._forwarded_for.host
  453. return super().getClientIP()
  454. def getClientAddress(self) -> IAddress:
  455. """
  456. Return the address of the client who submitted this request.
  457. """
  458. if self._forwarded_for is not None:
  459. return self._forwarded_for
  460. return super().getClientAddress()
  461. @implementer(IAddress)
  462. @attr.s(frozen=True, slots=True, auto_attribs=True)
  463. class _XForwardedForAddress:
  464. host: str
  465. class SynapseSite(Site):
  466. """
  467. Synapse-specific twisted http Site
  468. This does two main things.
  469. First, it replaces the requestFactory in use so that we build SynapseRequests
  470. instead of regular t.w.server.Requests. All of the constructor params are really
  471. just parameters for SynapseRequest.
  472. Second, it inhibits the log() method called by Request.finish, since SynapseRequest
  473. does its own logging.
  474. """
  475. def __init__(
  476. self,
  477. logger_name: str,
  478. site_tag: str,
  479. config: ListenerConfig,
  480. resource: IResource,
  481. server_version_string: str,
  482. max_request_body_size: int,
  483. reactor: IReactorTime,
  484. ):
  485. """
  486. Args:
  487. logger_name: The name of the logger to use for access logs.
  488. site_tag: A tag to use for this site - mostly in access logs.
  489. config: Configuration for the HTTP listener corresponding to this site
  490. resource: The base of the resource tree to be used for serving requests on
  491. this site
  492. server_version_string: A string to present for the Server header
  493. max_request_body_size: Maximum request body length to allow before
  494. dropping the connection
  495. reactor: reactor to be used to manage connection timeouts
  496. """
  497. Site.__init__(self, resource, reactor=reactor)
  498. self.site_tag = site_tag
  499. self.reactor = reactor
  500. assert config.http_options is not None
  501. proxied = config.http_options.x_forwarded
  502. request_class = XForwardedForRequest if proxied else SynapseRequest
  503. def request_factory(channel: HTTPChannel, queued: bool) -> Request:
  504. return request_class(
  505. channel,
  506. self,
  507. max_request_body_size=max_request_body_size,
  508. queued=queued,
  509. )
  510. self.requestFactory = request_factory # type: ignore
  511. self.access_logger = logging.getLogger(logger_name)
  512. self.server_version_string = server_version_string.encode("ascii")
  513. def log(self, request: SynapseRequest) -> None:
  514. pass