You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

205 lines
7.5 KiB

  1. # Copyright 2018 New Vector 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 hmac
  15. import logging
  16. from hashlib import sha256
  17. from http import HTTPStatus
  18. from os import path
  19. from typing import TYPE_CHECKING, Any, Dict, List
  20. import jinja2
  21. from jinja2 import TemplateNotFound
  22. from twisted.web.server import Request
  23. from synapse.api.errors import NotFoundError, StoreError, SynapseError
  24. from synapse.config import ConfigError
  25. from synapse.http.server import DirectServeHtmlResource, respond_with_html
  26. from synapse.http.servlet import parse_bytes_from_args, parse_string
  27. from synapse.types import UserID
  28. if TYPE_CHECKING:
  29. from synapse.server import HomeServer
  30. # language to use for the templates. TODO: figure this out from Accept-Language
  31. TEMPLATE_LANGUAGE = "en"
  32. logger = logging.getLogger(__name__)
  33. class ConsentResource(DirectServeHtmlResource):
  34. """A twisted Resource to display a privacy policy and gather consent to it
  35. When accessed via GET, returns the privacy policy via a template.
  36. When accessed via POST, records the user's consent in the database and
  37. displays a success page.
  38. The config should include a template_dir setting which contains templates
  39. for the HTML. The directory should contain one subdirectory per language
  40. (eg, 'en', 'fr'), and each language directory should contain the policy
  41. document (named as '<version>.html') and a success page (success.html).
  42. Both forms take a set of parameters from the browser. For the POST form,
  43. these are normally sent as form parameters (but may be query-params); for
  44. GET requests they must be query params. These are:
  45. u: the complete mxid, or the localpart of the user giving their
  46. consent. Required for both GET (where it is used as an input to the
  47. template) and for POST (where it is used to find the row in the db
  48. to update).
  49. h: hmac_sha256(secret, u), where 'secret' is the privacy_secret in the
  50. config file. If it doesn't match, the request is 403ed.
  51. v: the version of the privacy policy being agreed to.
  52. For GET: optional, and defaults to whatever was set in the config
  53. file. Used to choose the version of the policy to pick from the
  54. templates directory.
  55. For POST: required; gives the value to be recorded in the database
  56. against the user.
  57. """
  58. def __init__(self, hs: "HomeServer"):
  59. super().__init__()
  60. self.hs = hs
  61. self.store = hs.get_datastores().main
  62. self.registration_handler = hs.get_registration_handler()
  63. # this is required by the request_handler wrapper
  64. self.clock = hs.get_clock()
  65. # Consent must be configured to create this resource.
  66. default_consent_version = hs.config.consent.user_consent_version
  67. consent_template_directory = hs.config.consent.user_consent_template_dir
  68. if default_consent_version is None or consent_template_directory is None:
  69. raise ConfigError(
  70. "Consent resource is enabled but user_consent section is "
  71. "missing in config file."
  72. )
  73. self._default_consent_version = default_consent_version
  74. # TODO: switch to synapse.util.templates.build_jinja_env
  75. loader = jinja2.FileSystemLoader(consent_template_directory)
  76. self._jinja_env = jinja2.Environment(
  77. loader=loader, autoescape=jinja2.select_autoescape(["html", "htm", "xml"])
  78. )
  79. if hs.config.key.form_secret is None:
  80. raise ConfigError(
  81. "Consent resource is enabled but form_secret is not set in "
  82. "config file. It should be set to an arbitrary secret string."
  83. )
  84. self._hmac_secret = hs.config.key.form_secret.encode("utf-8")
  85. async def _async_render_GET(self, request: Request) -> None:
  86. version = parse_string(request, "v", default=self._default_consent_version)
  87. username = parse_string(request, "u", default="")
  88. userhmac = None
  89. has_consented = False
  90. public_version = username == ""
  91. if not public_version:
  92. args: Dict[bytes, List[bytes]] = request.args # type: ignore
  93. userhmac_bytes = parse_bytes_from_args(args, "h", required=True)
  94. self._check_hash(username, userhmac_bytes)
  95. if username.startswith("@"):
  96. qualified_user_id = username
  97. else:
  98. qualified_user_id = UserID(username, self.hs.hostname).to_string()
  99. u = await self.store.get_user_by_id(qualified_user_id)
  100. if u is None:
  101. raise NotFoundError("Unknown user")
  102. has_consented = u.consent_version == version
  103. userhmac = userhmac_bytes.decode("ascii")
  104. try:
  105. self._render_template(
  106. request,
  107. "%s.html" % (version,),
  108. user=username,
  109. userhmac=userhmac,
  110. version=version,
  111. has_consented=has_consented,
  112. public_version=public_version,
  113. )
  114. except TemplateNotFound:
  115. raise NotFoundError("Unknown policy version")
  116. async def _async_render_POST(self, request: Request) -> None:
  117. version = parse_string(request, "v", required=True)
  118. username = parse_string(request, "u", required=True)
  119. args: Dict[bytes, List[bytes]] = request.args # type: ignore
  120. userhmac = parse_bytes_from_args(args, "h", required=True)
  121. self._check_hash(username, userhmac)
  122. if username.startswith("@"):
  123. qualified_user_id = username
  124. else:
  125. qualified_user_id = UserID(username, self.hs.hostname).to_string()
  126. try:
  127. await self.store.user_set_consent_version(qualified_user_id, version)
  128. except StoreError as e:
  129. if e.code != 404:
  130. raise
  131. raise NotFoundError("Unknown user")
  132. await self.registration_handler.post_consent_actions(qualified_user_id)
  133. try:
  134. self._render_template(request, "success.html")
  135. except TemplateNotFound:
  136. raise NotFoundError("success.html not found")
  137. def _render_template(
  138. self, request: Request, template_name: str, **template_args: Any
  139. ) -> None:
  140. # get_template checks for ".." so we don't need to worry too much
  141. # about path traversal here.
  142. template_html = self._jinja_env.get_template(
  143. path.join(TEMPLATE_LANGUAGE, template_name)
  144. )
  145. html = template_html.render(**template_args)
  146. respond_with_html(request, 200, html)
  147. def _check_hash(self, userid: str, userhmac: bytes) -> None:
  148. """
  149. Args:
  150. userid:
  151. userhmac:
  152. Raises:
  153. SynapseError if the hash doesn't match
  154. """
  155. want_mac = (
  156. hmac.new(
  157. key=self._hmac_secret, msg=userid.encode("utf-8"), digestmod=sha256
  158. )
  159. .hexdigest()
  160. .encode("ascii")
  161. )
  162. if not hmac.compare_digest(want_mac, userhmac):
  163. raise SynapseError(HTTPStatus.FORBIDDEN, "HMAC incorrect")