25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

686 lines
26 KiB

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