Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into hs/hacked-together…
Browse files Browse the repository at this point in the history
…-event-cache
  • Loading branch information
Half-Shot committed Mar 4, 2021
2 parents 4a32600 + 33a02f0 commit 316db51
Show file tree
Hide file tree
Showing 89 changed files with 894 additions and 413 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
*.egg
*.egg-info
*.lock
*.pyc
*.py[cod]
*.snap
*.tac
_trial_temp/
_trial_temp*/
/out
.DS_Store
__pycache__/

# stuff that is likely to exist when you run a server locally
/*.db
Expand Down
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Synapse 1.xx.0
==============

Note that synapse now expects an `X-Forwarded-Proto` header when used with a reverse proxy. Please see [UPGRADE.rst](UPGRADE.rst#upgrading-to-v1290) for more details on this change.


Synapse 1.28.0 (2021-02-25)
===========================

Expand Down
20 changes: 20 additions & 0 deletions UPGRADE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ for example:
wget https://packages.matrix.org/debian/pool/main/m/matrix-synapse-py3/matrix-synapse-py3_1.3.0+stretch1_amd64.deb
dpkg -i matrix-synapse-py3_1.3.0+stretch1_amd64.deb
Upgrading to v1.29.0
====================

Requirement for X-Forwarded-Proto header
----------------------------------------

When using Synapse with a reverse proxy (in particular, when using the
`x_forwarded` option on an HTTP listener), Synapse now expects to receive an
`X-Forwarded-Proto` header on incoming HTTP requests. If it is not set, Synapse
will log a warning on each received request.

To avoid the warning, administrators using a reverse proxy should ensure that
the reverse proxy sets `X-Forwarded-Proto` header to `https` or `http` to
indicate the protocol used by the client. See the [reverse proxy
documentation](docs/reverse_proxy.md), where the example configurations have
been updated to show how to set this header.

(Users of `Caddy <https://caddyserver.com/>`_ are unaffected, since we believe it
sets `X-Forwarded-Proto` by default.)

Upgrading to v1.27.0
====================

Expand Down
1 change: 0 additions & 1 deletion changelog.d/9358.misc

This file was deleted.

1 change: 1 addition & 0 deletions changelog.d/9372.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `no_proxy` and `NO_PROXY` environment variables are now respected in proxied HTTP clients with the lowercase form taking precedence if both are present. Additionally, the lowercase `https_proxy` environment variable is now respected in proxied HTTP clients on top of existing support for the uppercase `HTTPS_PROXY` form and takes precedence if both are present. Contributed by Timothy Leung.
1 change: 1 addition & 0 deletions changelog.d/9436.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug in single sign-on which could cause a "No session cookie found" error.
2 changes: 1 addition & 1 deletion changelog.d/9472.feature
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add support for `X-Forwarded-Proto` header when using a reverse proxy. Administrators using a reverse proxy should ensure this header is set to avoid warnings. See [docs/workers.md](docs/workers.md) for example configurations.
Add support for `X-Forwarded-Proto` header when using a reverse proxy.
1 change: 1 addition & 0 deletions changelog.d/9497.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug where the media repository could leak file descriptors while previewing media.
1 change: 1 addition & 0 deletions changelog.d/9498.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Properly purge the event chain cover index when purging history.
1 change: 1 addition & 0 deletions changelog.d/9501.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for `X-Forwarded-Proto` header when using a reverse proxy.
1 change: 1 addition & 0 deletions changelog.d/9502.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow python to generate bytecode for synapse.
1 change: 1 addition & 0 deletions changelog.d/9503.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix missing chain cover index due to a schema delta not being applied correctly. Only affected servers that ran development versions.
1 change: 1 addition & 0 deletions changelog.d/9506.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug introduced in v1.25.0 where `/_synapse/admin/join/` would fail when given a room alias.
1 change: 1 addition & 0 deletions changelog.d/9512.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for `X-Forwarded-Proto` header when using a reverse proxy.
1 change: 1 addition & 0 deletions changelog.d/9515.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incorrect type hints.
1 change: 1 addition & 0 deletions changelog.d/9516.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where users' pushers were not all deleted when they deactivated their account.
1 change: 1 addition & 0 deletions changelog.d/9518.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incorrect type hints.
1 change: 1 addition & 0 deletions changelog.d/9519.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to device and event report admin API.
1 change: 1 addition & 0 deletions changelog.d/9521.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to user admin API.
1 change: 1 addition & 0 deletions changelog.d/9529.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bump the versions of mypy and mypy-zope used for static type checking.
1 change: 1 addition & 0 deletions changelog.d/9530.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent presence background jobs from running when presence is disabled.
1 change: 1 addition & 0 deletions changelog.d/9536.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix deleting pushers when using sharded pushers.
1 change: 1 addition & 0 deletions changelog.d/9537.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix rare edge case that caused a background update to fail if the server had rejected an event that had duplicate auth events.
1 change: 1 addition & 0 deletions changelog.d/9539.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for `X-Forwarded-Proto` header when using a reverse proxy.
6 changes: 3 additions & 3 deletions debian/build_virtualenv
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ trap "rm -r $tmpdir" EXIT
cp -r tests "$tmpdir"

PYTHONPATH="$tmpdir" \
"${TARGET_PYTHON}" -B -m twisted.trial --reporter=text -j2 tests
"${TARGET_PYTHON}" -m twisted.trial --reporter=text -j2 tests

# build the config file
"${TARGET_PYTHON}" -B "${VIRTUALENV_DIR}/bin/generate_config" \
"${TARGET_PYTHON}" "${VIRTUALENV_DIR}/bin/generate_config" \
--config-dir="/etc/matrix-synapse" \
--data-dir="/var/lib/matrix-synapse" |
perl -pe '
Expand All @@ -87,7 +87,7 @@ PYTHONPATH="$tmpdir" \
' > "${PACKAGE_BUILD_DIR}/etc/matrix-synapse/homeserver.yaml"

# build the log config file
"${TARGET_PYTHON}" -B "${VIRTUALENV_DIR}/bin/generate_log_config" \
"${TARGET_PYTHON}" "${VIRTUALENV_DIR}/bin/generate_log_config" \
--output-file="${PACKAGE_BUILD_DIR}/etc/matrix-synapse/log.yaml"

# add a dependency on the right version of python to substvars.
Expand Down
7 changes: 7 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
matrix-synapse-py3 (1.29.0) UNRELEASED; urgency=medium

[ Jonathan de Jong ]
* Remove the python -B flag (don't generate bytecode) in scripts and documentation.

-- Synapse Packaging team <packages@matrix.org> Fri, 26 Feb 2021 14:41:31 +0100

matrix-synapse-py3 (1.28.0) stable; urgency=medium

* New synapse release 1.28.0.
Expand Down
2 changes: 1 addition & 1 deletion debian/synctl.1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Configuration file may be generated as follows:
.
.nf

$ python \-B \-m synapse\.app\.homeserver \-c config\.yaml \-\-generate\-config \-\-server\-name=<server name>
$ python \-m synapse\.app\.homeserver \-c config\.yaml \-\-generate\-config \-\-server\-name=<server name>
.
.fi
.
Expand Down
2 changes: 1 addition & 1 deletion debian/synctl.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ process.

Configuration file may be generated as follows:

$ python -B -m synapse.app.homeserver -c config.yaml --generate-config --server-name=<server name>
$ python -m synapse.app.homeserver -c config.yaml --generate-config --server-name=<server name>

## ENVIRONMENT

Expand Down
2 changes: 2 additions & 0 deletions docs/reverse_proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ server {
proxy_pass http://localhost:8008;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
# Nginx by default only allows file uploads up to 1M in size
# Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
client_max_body_size 50M;
Expand Down
2 changes: 2 additions & 0 deletions scripts/synapse_port_db
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ from synapse.storage.databases.main.events_bg_updates import (
from synapse.storage.databases.main.media_repository import (
MediaRepositoryBackgroundUpdateStore,
)
from synapse.storage.databases.main.pusher import PusherWorkerStore
from synapse.storage.databases.main.registration import (
RegistrationBackgroundUpdateStore,
find_max_generated_user_id_localpart,
Expand Down Expand Up @@ -178,6 +179,7 @@ class Store(
UserDirectoryBackgroundUpdateStore,
EndToEndKeyBackgroundStore,
StatsStore,
PusherWorkerStore,
):
def execute(self, f, *args, **kwargs):
return self.db_pool.runInteraction(f.__name__, f, *args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def exec_file(path_segments):
"flake8",
]

CONDITIONAL_REQUIREMENTS["mypy"] = ["mypy==0.790", "mypy-zope==0.2.8"]
CONDITIONAL_REQUIREMENTS["mypy"] = ["mypy==0.812", "mypy-zope==0.2.11"]

# Dependencies which are exclusively required by unit test code. This is
# NOT a list of all modules that are necessary to run the unit tests.
Expand Down
2 changes: 0 additions & 2 deletions synapse/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

from synapse import python_dependencies # noqa: E402

sys.dont_write_bytecode = True

logger = logging.getLogger(__name__)

try:
Expand Down
15 changes: 13 additions & 2 deletions synapse/app/generic_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from twisted.internet import address
from twisted.web.resource import IResource
from twisted.web.server import Request

import synapse
import synapse.events
Expand Down Expand Up @@ -190,7 +191,7 @@ def __init__(self, hs):
self.http_client = hs.get_simple_http_client()
self.main_uri = hs.config.worker_main_http_uri

async def on_POST(self, request, device_id):
async def on_POST(self, request: Request, device_id: Optional[str]):
requester = await self.auth.get_user_by_req(request, allow_guest=True)
user_id = requester.user.to_string()
body = parse_json_object_from_request(request)
Expand Down Expand Up @@ -223,10 +224,12 @@ async def on_POST(self, request, device_id):
header: request.requestHeaders.getRawHeaders(header, [])
for header in (b"Authorization", b"User-Agent")
}
# Add the previous hop the the X-Forwarded-For header.
# Add the previous hop to the X-Forwarded-For header.
x_forwarded_for = request.requestHeaders.getRawHeaders(
b"X-Forwarded-For", []
)
# we use request.client here, since we want the previous hop, not the
# original client (as returned by request.getClientAddress()).
if isinstance(request.client, (address.IPv4Address, address.IPv6Address)):
previous_host = request.client.host.encode("ascii")
# If the header exists, add to the comma-separated list of the first
Expand All @@ -239,6 +242,14 @@ async def on_POST(self, request, device_id):
x_forwarded_for = [previous_host]
headers[b"X-Forwarded-For"] = x_forwarded_for

# Replicate the original X-Forwarded-Proto header. Note that
# XForwardedForRequest overrides isSecure() to give us the original protocol
# used by the client, as opposed to the protocol used by our upstream proxy
# - which is what we want here.
headers[b"X-Forwarded-Proto"] = [
b"https" if request.isSecure() else b"http"
]

try:
result = await self.http_client.post_json_get_json(
self.main_uri + request.uri.decode("ascii"), body, headers=headers
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import bcrypt
import pymacaroons

from twisted.web.http import Request
from twisted.web.server import Request

from synapse.api.constants import LoginType
from synapse.api.errors import (
Expand Down Expand Up @@ -481,7 +481,7 @@ async def check_ui_auth(
sid = authdict["session"]

# Convert the URI and method to strings.
uri = request.uri.decode("utf-8")
uri = request.uri.decode("utf-8") # type: ignore
method = request.method.decode("utf-8")

# If there's no session ID, create a new session.
Expand Down
31 changes: 17 additions & 14 deletions synapse/handlers/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,25 @@ def __init__(self, hs: "HomeServer"):

self.external_sync_linearizer = Linearizer(name="external_sync_linearizer")

# Start a LoopingCall in 30s that fires every 5s.
# The initial delay is to allow disconnected clients a chance to
# reconnect before we treat them as offline.
def run_timeout_handler():
return run_as_background_process(
"handle_presence_timeouts", self._handle_timeouts
)

self.clock.call_later(30, self.clock.looping_call, run_timeout_handler, 5000)
if self._presence_enabled:
# Start a LoopingCall in 30s that fires every 5s.
# The initial delay is to allow disconnected clients a chance to
# reconnect before we treat them as offline.
def run_timeout_handler():
return run_as_background_process(
"handle_presence_timeouts", self._handle_timeouts
)

def run_persister():
return run_as_background_process(
"persist_presence_changes", self._persist_unpersisted_changes
self.clock.call_later(
30, self.clock.looping_call, run_timeout_handler, 5000
)

self.clock.call_later(60, self.clock.looping_call, run_persister, 60 * 1000)
def run_persister():
return run_as_background_process(
"persist_presence_changes", self._persist_unpersisted_changes
)

self.clock.call_later(60, self.clock.looping_call, run_persister, 60 * 1000)

LaterGauge(
"synapse_handlers_presence_wheel_timer_size",
Expand All @@ -299,7 +302,7 @@ def run_persister():
)

# Used to handle sending of presence to newly joined users/servers
if hs.config.use_presence:
if self._presence_enabled:
self.notifier.add_replication_callback(self.notify_new_event)

# Presence is best effort and quickly heals itself, so lets just always
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import attr
from typing_extensions import NoReturn, Protocol

from twisted.web.http import Request
from twisted.web.iweb import IRequest
from twisted.web.server import Request

from synapse.api.constants import LoginType
from synapse.api.errors import Codes, NotFoundError, RedirectException, SynapseError
Expand Down
3 changes: 1 addition & 2 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,8 @@ async def wait_for_sync_for_user(
user_id = sync_config.user.to_string()
await self.auth.check_auth_blocking(requester=requester)

res = await self.response_cache.wrap_conditional(
res = await self.response_cache.wrap(
sync_config.request_key,
lambda result: since_token != result.next_batch,
self._wait_for_sync_for_user,
sync_config,
since_token,
Expand Down
37 changes: 36 additions & 1 deletion synapse/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from typing import Union

from twisted.internet import task
from twisted.internet import address, task
from twisted.web.client import FileBodyProducer
from twisted.web.iweb import IRequest

Expand Down Expand Up @@ -53,6 +54,40 @@ def stopProducing(self):
pass


def get_request_uri(request: IRequest) -> bytes:
"""Return the full URI that was requested by the client"""
return b"%s://%s%s" % (
b"https" if request.isSecure() else b"http",
_get_requested_host(request),
# despite its name, "request.uri" is only the path and query-string.
request.uri,
)


def _get_requested_host(request: IRequest) -> bytes:
hostname = request.getHeader(b"host")
if hostname:
return hostname

# no Host header, use the address/port that the request arrived on
host = request.getHost() # type: Union[address.IPv4Address, address.IPv6Address]

hostname = host.host.encode("ascii")

if request.isSecure() and host.port == 443:
# default port for https
return hostname

if not request.isSecure() and host.port == 80:
# default port for http
return hostname

return b"%s:%i" % (
hostname,
host.port,
)


def get_request_user_agent(request: IRequest, default: str = "") -> str:
"""Return the last User-Agent header, or the given default."""
# There could be raw utf-8 bytes in the User-Agent header.
Expand Down
Loading

0 comments on commit 316db51

Please sign in to comment.