2018-05-10 19:17:11 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-05-10 19:17:11 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import hmac
|
|
|
|
import logging
|
2018-07-09 02:09:20 -04:00
|
|
|
from hashlib import sha256
|
2020-06-16 08:51:47 -04:00
|
|
|
from http import HTTPStatus
|
2018-05-10 19:17:11 -04:00
|
|
|
from os import path
|
2021-09-15 08:45:32 -04:00
|
|
|
from typing import TYPE_CHECKING, Any, Dict, List
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2018-05-10 19:17:11 -04:00
|
|
|
import jinja2
|
|
|
|
from jinja2 import TemplateNotFound
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2021-09-15 08:45:32 -04:00
|
|
|
from twisted.web.server import Request
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.api.errors import NotFoundError, StoreError, SynapseError
|
2018-05-10 19:17:11 -04:00
|
|
|
from synapse.config import ConfigError
|
2020-07-03 14:02:19 -04:00
|
|
|
from synapse.http.server import DirectServeHtmlResource, respond_with_html
|
2021-06-08 08:30:48 -04:00
|
|
|
from synapse.http.servlet import parse_bytes_from_args, parse_string
|
2018-05-10 19:17:11 -04:00
|
|
|
from synapse.types import UserID
|
|
|
|
|
2021-09-15 08:45:32 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2018-05-10 19:17:11 -04:00
|
|
|
# language to use for the templates. TODO: figure this out from Accept-Language
|
|
|
|
TEMPLATE_LANGUAGE = "en"
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-07-03 14:02:19 -04:00
|
|
|
class ConsentResource(DirectServeHtmlResource):
|
2018-05-10 19:17:11 -04:00
|
|
|
"""A twisted Resource to display a privacy policy and gather consent to it
|
|
|
|
|
|
|
|
When accessed via GET, returns the privacy policy via a template.
|
|
|
|
|
|
|
|
When accessed via POST, records the user's consent in the database and
|
|
|
|
displays a success page.
|
|
|
|
|
|
|
|
The config should include a template_dir setting which contains templates
|
|
|
|
for the HTML. The directory should contain one subdirectory per language
|
|
|
|
(eg, 'en', 'fr'), and each language directory should contain the policy
|
|
|
|
document (named as '<version>.html') and a success page (success.html).
|
|
|
|
|
|
|
|
Both forms take a set of parameters from the browser. For the POST form,
|
|
|
|
these are normally sent as form parameters (but may be query-params); for
|
|
|
|
GET requests they must be query params. These are:
|
|
|
|
|
|
|
|
u: the complete mxid, or the localpart of the user giving their
|
|
|
|
consent. Required for both GET (where it is used as an input to the
|
|
|
|
template) and for POST (where it is used to find the row in the db
|
|
|
|
to update).
|
|
|
|
|
|
|
|
h: hmac_sha256(secret, u), where 'secret' is the privacy_secret in the
|
|
|
|
config file. If it doesn't match, the request is 403ed.
|
|
|
|
|
|
|
|
v: the version of the privacy policy being agreed to.
|
|
|
|
|
|
|
|
For GET: optional, and defaults to whatever was set in the config
|
|
|
|
file. Used to choose the version of the policy to pick from the
|
|
|
|
templates directory.
|
|
|
|
|
|
|
|
For POST: required; gives the value to be recorded in the database
|
|
|
|
against the user.
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-09-15 08:45:32 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2019-06-29 03:06:55 -04:00
|
|
|
super().__init__()
|
2018-05-10 19:17:11 -04:00
|
|
|
|
|
|
|
self.hs = hs
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2019-02-20 02:47:31 -05:00
|
|
|
self.registration_handler = hs.get_registration_handler()
|
2018-05-10 19:17:11 -04:00
|
|
|
|
|
|
|
# this is required by the request_handler wrapper
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
|
2021-09-23 07:13:34 -04:00
|
|
|
# Consent must be configured to create this resource.
|
|
|
|
default_consent_version = hs.config.consent.user_consent_version
|
|
|
|
consent_template_directory = hs.config.consent.user_consent_template_dir
|
|
|
|
if default_consent_version is None or consent_template_directory is None:
|
2018-05-10 19:17:11 -04:00
|
|
|
raise ConfigError(
|
|
|
|
"Consent resource is enabled but user_consent section is "
|
|
|
|
"missing in config file."
|
|
|
|
)
|
2021-09-23 07:13:34 -04:00
|
|
|
self._default_consent_version = default_consent_version
|
2018-05-10 19:17:11 -04:00
|
|
|
|
2021-02-01 10:52:50 -05:00
|
|
|
# TODO: switch to synapse.util.templates.build_jinja_env
|
2018-05-10 19:17:11 -04:00
|
|
|
loader = jinja2.FileSystemLoader(consent_template_directory)
|
2018-05-22 09:18:53 -04:00
|
|
|
self._jinja_env = jinja2.Environment(
|
|
|
|
loader=loader, autoescape=jinja2.select_autoescape(["html", "htm", "xml"])
|
|
|
|
)
|
2018-05-10 19:17:11 -04:00
|
|
|
|
2021-09-23 12:03:01 -04:00
|
|
|
if hs.config.key.form_secret is None:
|
2018-05-10 19:17:11 -04:00
|
|
|
raise ConfigError(
|
|
|
|
"Consent resource is enabled but form_secret is not set in "
|
|
|
|
"config file. It should be set to an arbitrary secret string."
|
|
|
|
)
|
|
|
|
|
2021-09-23 12:03:01 -04:00
|
|
|
self._hmac_secret = hs.config.key.form_secret.encode("utf-8")
|
2018-05-10 19:17:11 -04:00
|
|
|
|
2021-09-15 08:45:32 -04:00
|
|
|
async def _async_render_GET(self, request: Request) -> None:
|
2018-10-12 19:54:28 -04:00
|
|
|
version = parse_string(request, "v", default=self._default_consent_version)
|
2021-07-21 09:47:56 -04:00
|
|
|
username = parse_string(request, "u", default="")
|
2018-10-03 19:50:11 -04:00
|
|
|
userhmac = None
|
|
|
|
has_consented = False
|
2018-11-01 18:47:05 -04:00
|
|
|
public_version = username == ""
|
2018-11-14 05:44:46 -05:00
|
|
|
if not public_version:
|
2021-09-15 08:45:32 -04:00
|
|
|
args: Dict[bytes, List[bytes]] = request.args # type: ignore
|
2021-06-08 08:30:48 -04:00
|
|
|
userhmac_bytes = parse_bytes_from_args(args, "h", required=True)
|
2018-05-10 19:17:11 -04:00
|
|
|
|
2018-11-14 05:21:07 -05:00
|
|
|
self._check_hash(username, userhmac_bytes)
|
2018-05-22 09:50:22 -04:00
|
|
|
|
2018-10-03 19:50:11 -04:00
|
|
|
if username.startswith("@"):
|
|
|
|
qualified_user_id = username
|
|
|
|
else:
|
|
|
|
qualified_user_id = UserID(username, self.hs.hostname).to_string()
|
|
|
|
|
2020-08-07 09:44:48 -04:00
|
|
|
u = await self.store.get_user_by_id(qualified_user_id)
|
2018-10-03 19:50:11 -04:00
|
|
|
if u is None:
|
|
|
|
raise NotFoundError("Unknown user")
|
2018-11-14 05:21:07 -05:00
|
|
|
|
2023-09-14 07:46:30 -04:00
|
|
|
has_consented = u.consent_version == version
|
2018-11-14 05:21:07 -05:00
|
|
|
userhmac = userhmac_bytes.decode("ascii")
|
2018-05-22 09:50:22 -04:00
|
|
|
|
2018-05-10 19:17:11 -04:00
|
|
|
try:
|
|
|
|
self._render_template(
|
|
|
|
request,
|
|
|
|
"%s.html" % (version,),
|
2018-11-08 06:03:08 -05:00
|
|
|
user=username,
|
2018-11-14 05:21:07 -05:00
|
|
|
userhmac=userhmac,
|
2018-11-08 06:03:08 -05:00
|
|
|
version=version,
|
2018-11-14 05:21:07 -05:00
|
|
|
has_consented=has_consented,
|
|
|
|
public_version=public_version,
|
2018-05-10 19:17:11 -04:00
|
|
|
)
|
|
|
|
except TemplateNotFound:
|
|
|
|
raise NotFoundError("Unknown policy version")
|
|
|
|
|
2021-09-15 08:45:32 -04:00
|
|
|
async def _async_render_POST(self, request: Request) -> None:
|
2018-05-10 19:17:11 -04:00
|
|
|
version = parse_string(request, "v", required=True)
|
|
|
|
username = parse_string(request, "u", required=True)
|
2021-09-15 08:45:32 -04:00
|
|
|
args: Dict[bytes, List[bytes]] = request.args # type: ignore
|
2021-06-08 08:30:48 -04:00
|
|
|
userhmac = parse_bytes_from_args(args, "h", required=True)
|
2018-05-10 19:17:11 -04:00
|
|
|
|
|
|
|
self._check_hash(username, userhmac)
|
|
|
|
|
|
|
|
if username.startswith("@"):
|
|
|
|
qualified_user_id = username
|
|
|
|
else:
|
|
|
|
qualified_user_id = UserID(username, self.hs.hostname).to_string()
|
|
|
|
|
|
|
|
try:
|
2019-06-29 03:06:55 -04:00
|
|
|
await self.store.user_set_consent_version(qualified_user_id, version)
|
2018-05-10 19:17:11 -04:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code != 404:
|
|
|
|
raise
|
|
|
|
raise NotFoundError("Unknown user")
|
2019-06-29 03:06:55 -04:00
|
|
|
await self.registration_handler.post_consent_actions(qualified_user_id)
|
2018-05-10 19:17:11 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
self._render_template(request, "success.html")
|
|
|
|
except TemplateNotFound:
|
|
|
|
raise NotFoundError("success.html not found")
|
|
|
|
|
2021-09-15 08:45:32 -04:00
|
|
|
def _render_template(
|
|
|
|
self, request: Request, template_name: str, **template_args: Any
|
|
|
|
) -> None:
|
2018-05-10 19:17:11 -04:00
|
|
|
# get_template checks for ".." so we don't need to worry too much
|
|
|
|
# about path traversal here.
|
|
|
|
template_html = self._jinja_env.get_template(
|
|
|
|
path.join(TEMPLATE_LANGUAGE, template_name)
|
|
|
|
)
|
2020-07-01 09:10:23 -04:00
|
|
|
html = template_html.render(**template_args)
|
|
|
|
respond_with_html(request, 200, html)
|
2018-05-10 19:17:11 -04:00
|
|
|
|
2021-09-15 08:45:32 -04:00
|
|
|
def _check_hash(self, userid: str, userhmac: bytes) -> None:
|
2018-08-21 17:41:41 -04:00
|
|
|
"""
|
|
|
|
Args:
|
2021-09-15 08:45:32 -04:00
|
|
|
userid:
|
|
|
|
userhmac:
|
2018-08-21 17:41:41 -04:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
SynapseError if the hash doesn't match
|
|
|
|
|
|
|
|
"""
|
2018-05-10 19:17:11 -04:00
|
|
|
want_mac = (
|
|
|
|
hmac.new(
|
2018-08-21 17:41:41 -04:00
|
|
|
key=self._hmac_secret, msg=userid.encode("utf-8"), digestmod=sha256
|
2018-11-05 13:53:44 -05:00
|
|
|
)
|
|
|
|
.hexdigest()
|
|
|
|
.encode("ascii")
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2018-05-10 19:17:11 -04:00
|
|
|
|
2021-04-27 08:13:07 -04:00
|
|
|
if not hmac.compare_digest(want_mac, userhmac):
|
2020-06-16 08:51:47 -04:00
|
|
|
raise SynapseError(HTTPStatus.FORBIDDEN, "HMAC incorrect")
|