site.py 25 KB

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