consent_resource.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import hmac
  16. import logging
  17. from hashlib import sha256
  18. from os import path
  19. from six.moves import http_client
  20. import jinja2
  21. from jinja2 import TemplateNotFound
  22. from twisted.internet import defer
  23. from twisted.web.resource import Resource
  24. from twisted.web.server import NOT_DONE_YET
  25. from synapse.api.errors import NotFoundError, StoreError, SynapseError
  26. from synapse.config import ConfigError
  27. from synapse.http.server import finish_request, wrap_html_request_handler
  28. from synapse.http.servlet import parse_string
  29. from synapse.types import UserID
  30. # language to use for the templates. TODO: figure this out from Accept-Language
  31. TEMPLATE_LANGUAGE = "en"
  32. logger = logging.getLogger(__name__)
  33. # use hmac.compare_digest if we have it (python 2.7.7), else just use equality
  34. if hasattr(hmac, "compare_digest"):
  35. compare_digest = hmac.compare_digest
  36. else:
  37. def compare_digest(a, b):
  38. return a == b
  39. class ConsentResource(Resource):
  40. """A twisted Resource to display a privacy policy and gather consent to it
  41. When accessed via GET, returns the privacy policy via a template.
  42. When accessed via POST, records the user's consent in the database and
  43. displays a success page.
  44. The config should include a template_dir setting which contains templates
  45. for the HTML. The directory should contain one subdirectory per language
  46. (eg, 'en', 'fr'), and each language directory should contain the policy
  47. document (named as '<version>.html') and a success page (success.html).
  48. Both forms take a set of parameters from the browser. For the POST form,
  49. these are normally sent as form parameters (but may be query-params); for
  50. GET requests they must be query params. These are:
  51. u: the complete mxid, or the localpart of the user giving their
  52. consent. Required for both GET (where it is used as an input to the
  53. template) and for POST (where it is used to find the row in the db
  54. to update).
  55. h: hmac_sha256(secret, u), where 'secret' is the privacy_secret in the
  56. config file. If it doesn't match, the request is 403ed.
  57. v: the version of the privacy policy being agreed to.
  58. For GET: optional, and defaults to whatever was set in the config
  59. file. Used to choose the version of the policy to pick from the
  60. templates directory.
  61. For POST: required; gives the value to be recorded in the database
  62. against the user.
  63. """
  64. def __init__(self, hs):
  65. """
  66. Args:
  67. hs (synapse.server.HomeServer): homeserver
  68. """
  69. Resource.__init__(self)
  70. self.hs = hs
  71. self.store = hs.get_datastore()
  72. # this is required by the request_handler wrapper
  73. self.clock = hs.get_clock()
  74. self._default_consent_version = hs.config.user_consent_version
  75. if self._default_consent_version is None:
  76. raise ConfigError(
  77. "Consent resource is enabled but user_consent section is "
  78. "missing in config file.",
  79. )
  80. # daemonize changes the cwd to /, so make the path absolute now.
  81. consent_template_directory = path.abspath(
  82. hs.config.user_consent_template_dir,
  83. )
  84. if not path.isdir(consent_template_directory):
  85. raise ConfigError(
  86. "Could not find template directory '%s'" % (
  87. consent_template_directory,
  88. ),
  89. )
  90. loader = jinja2.FileSystemLoader(consent_template_directory)
  91. self._jinja_env = jinja2.Environment(
  92. loader=loader,
  93. autoescape=jinja2.select_autoescape(['html', 'htm', 'xml']),
  94. )
  95. if hs.config.form_secret is None:
  96. raise ConfigError(
  97. "Consent resource is enabled but form_secret is not set in "
  98. "config file. It should be set to an arbitrary secret string.",
  99. )
  100. self._hmac_secret = hs.config.form_secret.encode("utf-8")
  101. def render_GET(self, request):
  102. self._async_render_GET(request)
  103. return NOT_DONE_YET
  104. @wrap_html_request_handler
  105. @defer.inlineCallbacks
  106. def _async_render_GET(self, request):
  107. """
  108. Args:
  109. request (twisted.web.http.Request):
  110. """
  111. version = parse_string(request, "v", default=self._default_consent_version)
  112. username = parse_string(request, "u", required=False, default="")
  113. userhmac = None
  114. has_consented = False
  115. public_version = username == ""
  116. if not public_version or not self.hs.config.user_consent_at_registration:
  117. userhmac = parse_string(request, "h", required=True, encoding=None)
  118. self._check_hash(username, userhmac)
  119. if username.startswith('@'):
  120. qualified_user_id = username
  121. else:
  122. qualified_user_id = UserID(username, self.hs.hostname).to_string()
  123. u = yield self.store.get_user_by_id(qualified_user_id)
  124. if u is None:
  125. raise NotFoundError("Unknown user")
  126. has_consented = u["consent_version"] == version
  127. try:
  128. self._render_template(
  129. request, "%s.html" % (version,),
  130. user=username, userhmac=userhmac, version=version,
  131. has_consented=has_consented, public_version=public_version,
  132. )
  133. except TemplateNotFound:
  134. raise NotFoundError("Unknown policy version")
  135. def render_POST(self, request):
  136. self._async_render_POST(request)
  137. return NOT_DONE_YET
  138. @wrap_html_request_handler
  139. @defer.inlineCallbacks
  140. def _async_render_POST(self, request):
  141. """
  142. Args:
  143. request (twisted.web.http.Request):
  144. """
  145. version = parse_string(request, "v", required=True)
  146. username = parse_string(request, "u", required=True)
  147. userhmac = parse_string(request, "h", required=True, encoding=None)
  148. self._check_hash(username, userhmac)
  149. if username.startswith('@'):
  150. qualified_user_id = username
  151. else:
  152. qualified_user_id = UserID(username, self.hs.hostname).to_string()
  153. try:
  154. yield self.store.user_set_consent_version(qualified_user_id, version)
  155. except StoreError as e:
  156. if e.code != 404:
  157. raise
  158. raise NotFoundError("Unknown user")
  159. try:
  160. self._render_template(request, "success.html")
  161. except TemplateNotFound:
  162. raise NotFoundError("success.html not found")
  163. def _render_template(self, request, template_name, **template_args):
  164. # get_template checks for ".." so we don't need to worry too much
  165. # about path traversal here.
  166. template_html = self._jinja_env.get_template(
  167. path.join(TEMPLATE_LANGUAGE, template_name)
  168. )
  169. html_bytes = template_html.render(**template_args).encode("utf8")
  170. request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
  171. request.setHeader(b"Content-Length", b"%i" % len(html_bytes))
  172. request.write(html_bytes)
  173. finish_request(request)
  174. def _check_hash(self, userid, userhmac):
  175. """
  176. Args:
  177. userid (unicode):
  178. userhmac (bytes):
  179. Raises:
  180. SynapseError if the hash doesn't match
  181. """
  182. want_mac = hmac.new(
  183. key=self._hmac_secret,
  184. msg=userid.encode('utf-8'),
  185. digestmod=sha256,
  186. ).hexdigest().encode('ascii')
  187. if not compare_digest(want_mac, userhmac):
  188. raise SynapseError(http_client.FORBIDDEN, "HMAC incorrect")