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

Commit e78e455

Browse files
authored
Merge pull request #122 from matrix-org/babolivier/avatar_limit
Configurable limits on avatars
2 parents 717ab47 + 5fd09fc commit e78e455

File tree

8 files changed

+349
-219
lines changed

8 files changed

+349
-219
lines changed

changelog.d/11846.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow configuring a maximum file size as well as a list of allowed content types for avatars.

docs/sample_config.yaml

Lines changed: 10 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -484,68 +484,19 @@ limit_remote_rooms:
484484
#
485485
#show_users_in_user_directory: false
486486

487-
# Message retention policy at the server level.
488-
#
489-
# Room admins and mods can define a retention period for their rooms using the
490-
# 'm.room.retention' state event, and server admins can cap this period by setting
491-
# the 'allowed_lifetime_min' and 'allowed_lifetime_max' config options.
487+
# The largest allowed file size for a user avatar. Defaults to no restriction.
492488
#
493-
# If this feature is enabled, Synapse will regularly look for and purge events
494-
# which are older than the room's maximum retention period. Synapse will also
495-
# filter events received over federation so that events that should have been
496-
# purged are ignored and not stored again.
489+
# Note that user avatar changes will not work if this is set without
490+
# using Synapse's media repository.
497491
#
498-
retention:
499-
# The message retention policies feature is disabled by default. Uncomment the
500-
# following line to enable it.
501-
#
502-
#enabled: true
503-
504-
# Default retention policy. If set, Synapse will apply it to rooms that lack the
505-
# 'm.room.retention' state event. Currently, the value of 'min_lifetime' doesn't
506-
# matter much because Synapse doesn't take it into account yet.
507-
#
508-
#default_policy:
509-
# min_lifetime: 1d
510-
# max_lifetime: 1y
511-
512-
# Retention policy limits. If set, a user won't be able to send a
513-
# 'm.room.retention' event which features a 'min_lifetime' or a 'max_lifetime'
514-
# that's not within this range. This is especially useful in closed federations,
515-
# in which server admins can make sure every federating server applies the same
516-
# rules.
517-
#
518-
#allowed_lifetime_min: 1d
519-
#allowed_lifetime_max: 1y
492+
#max_avatar_size: 10M
520493

521-
# Server admins can define the settings of the background jobs purging the
522-
# events which lifetime has expired under the 'purge_jobs' section.
523-
#
524-
# If no configuration is provided, a single job will be set up to delete expired
525-
# events in every room daily.
526-
#
527-
# Each job's configuration defines which range of message lifetimes the job
528-
# takes care of. For example, if 'shortest_max_lifetime' is '2d' and
529-
# 'longest_max_lifetime' is '3d', the job will handle purging expired events in
530-
# rooms whose state defines a 'max_lifetime' that's both higher than 2 days, and
531-
# lower than or equal to 3 days. Both the minimum and the maximum value of a
532-
# range are optional, e.g. a job with no 'shortest_max_lifetime' and a
533-
# 'longest_max_lifetime' of '3d' will handle every room with a retention policy
534-
# which 'max_lifetime' is lower than or equal to three days.
535-
#
536-
# The rationale for this per-job configuration is that some rooms might have a
537-
# retention policy with a low 'max_lifetime', where history needs to be purged
538-
# of outdated messages on a very frequent basis (e.g. every 5min), but not want
539-
# that purge to be performed by a job that's iterating over every room it knows,
540-
# which would be quite heavy on the server.
541-
#
542-
#purge_jobs:
543-
# - shortest_max_lifetime: 1d
544-
# longest_max_lifetime: 3d
545-
# interval: 5m:
546-
# - shortest_max_lifetime: 3d
547-
# longest_max_lifetime: 1y
548-
# interval: 24h
494+
# The MIME types allowed for user avatars. Defaults to no restriction.
495+
#
496+
# Note that user avatar changes will not work if this is set without
497+
# using Synapse's media repository.
498+
#
499+
#allowed_avatar_mimetypes: ["image/png", "image/jpeg", "image/gif"]
549500

550501
# How long to keep redacted events in unredacted form in the database. After
551502
# this period redacted events get replaced with their redacted form in the DB.
@@ -1040,30 +991,6 @@ media_store_path: "DATADIR/media_store"
1040991
#
1041992
#max_upload_size: 50M
1042993

1043-
# The largest allowed size for a user avatar. If not defined, no
1044-
# restriction will be imposed.
1045-
#
1046-
# Note that this only applies when an avatar is changed globally.
1047-
# Per-room avatar changes are not affected. See allow_per_room_profiles
1048-
# for disabling that functionality.
1049-
#
1050-
# Note that user avatar changes will not work if this is set without
1051-
# using Synapse's local media repo.
1052-
#
1053-
#max_avatar_size: 10M
1054-
1055-
# Allow mimetypes for a user avatar. If not defined, no restriction will
1056-
# be imposed.
1057-
#
1058-
# Note that this only applies when an avatar is changed globally.
1059-
# Per-room avatar changes are not affected. See allow_per_room_profiles
1060-
# for disabling that functionality.
1061-
#
1062-
# Note that user avatar changes will not work if this is set without
1063-
# using Synapse's local media repo.
1064-
#
1065-
#allowed_avatar_mimetypes: ["image/png", "image/jpeg", "image/gif"]
1066-
1067994
# Maximum number of pixels that will be thumbnailed
1068995
#
1069996
#max_image_pixels: 32M

synapse/config/repository.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ def read_config(self, config, **kwargs):
112112
self.max_image_pixels = self.parse_size(config.get("max_image_pixels", "32M"))
113113
self.max_spider_size = self.parse_size(config.get("max_spider_size", "10M"))
114114

115-
self.max_avatar_size = config.get("max_avatar_size")
116-
if self.max_avatar_size:
117-
self.max_avatar_size = self.parse_size(self.max_avatar_size)
118-
119-
self.allowed_avatar_mimetypes = config.get("allowed_avatar_mimetypes", [])
120-
121115
self.media_store_path = self.ensure_directory(
122116
config.get("media_store_path", "media_store")
123117
)
@@ -272,30 +266,6 @@ def generate_config_section(self, data_dir_path, **kwargs):
272266
#
273267
#max_upload_size: 50M
274268
275-
# The largest allowed size for a user avatar. If not defined, no
276-
# restriction will be imposed.
277-
#
278-
# Note that this only applies when an avatar is changed globally.
279-
# Per-room avatar changes are not affected. See allow_per_room_profiles
280-
# for disabling that functionality.
281-
#
282-
# Note that user avatar changes will not work if this is set without
283-
# using Synapse's local media repo.
284-
#
285-
#max_avatar_size: 10M
286-
287-
# Allow mimetypes for a user avatar. If not defined, no restriction will
288-
# be imposed.
289-
#
290-
# Note that this only applies when an avatar is changed globally.
291-
# Per-room avatar changes are not affected. See allow_per_room_profiles
292-
# for disabling that functionality.
293-
#
294-
# Note that user avatar changes will not work if this is set without
295-
# using Synapse's local media repo.
296-
#
297-
#allowed_avatar_mimetypes: ["image/png", "image/jpeg", "image/gif"]
298-
299269
# Maximum number of pixels that will be thumbnailed
300270
#
301271
#max_image_pixels: 32M

synapse/config/server.py

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,19 @@ def read_config(self, config, **kwargs):
488488
# events with profile information that differ from the target's global profile.
489489
self.allow_per_room_profiles = config.get("allow_per_room_profiles", True)
490490

491+
# The maximum size an avatar can have, in bytes.
492+
self.max_avatar_size = config.get("max_avatar_size")
493+
if self.max_avatar_size is not None:
494+
self.max_avatar_size = self.parse_size(self.max_avatar_size)
495+
496+
# The MIME types allowed for an avatar.
497+
self.allowed_avatar_mimetypes = config.get("allowed_avatar_mimetypes")
498+
if self.allowed_avatar_mimetypes and not isinstance(
499+
self.allowed_avatar_mimetypes,
500+
list,
501+
):
502+
raise ConfigError("allowed_avatar_mimetypes must be a list")
503+
491504
# Whether to show the users on this homeserver in the user directory. Defaults to
492505
# True.
493506
self.show_users_in_user_directory = config.get(
@@ -1177,68 +1190,19 @@ def generate_config_section(
11771190
#
11781191
#show_users_in_user_directory: false
11791192
1180-
# Message retention policy at the server level.
1193+
# The largest allowed file size for a user avatar. Defaults to no restriction.
11811194
#
1182-
# Room admins and mods can define a retention period for their rooms using the
1183-
# 'm.room.retention' state event, and server admins can cap this period by setting
1184-
# the 'allowed_lifetime_min' and 'allowed_lifetime_max' config options.
1195+
# Note that user avatar changes will not work if this is set without
1196+
# using Synapse's media repository.
11851197
#
1186-
# If this feature is enabled, Synapse will regularly look for and purge events
1187-
# which are older than the room's maximum retention period. Synapse will also
1188-
# filter events received over federation so that events that should have been
1189-
# purged are ignored and not stored again.
1190-
#
1191-
retention:
1192-
# The message retention policies feature is disabled by default. Uncomment the
1193-
# following line to enable it.
1194-
#
1195-
#enabled: true
1196-
1197-
# Default retention policy. If set, Synapse will apply it to rooms that lack the
1198-
# 'm.room.retention' state event. Currently, the value of 'min_lifetime' doesn't
1199-
# matter much because Synapse doesn't take it into account yet.
1200-
#
1201-
#default_policy:
1202-
# min_lifetime: 1d
1203-
# max_lifetime: 1y
1204-
1205-
# Retention policy limits. If set, a user won't be able to send a
1206-
# 'm.room.retention' event which features a 'min_lifetime' or a 'max_lifetime'
1207-
# that's not within this range. This is especially useful in closed federations,
1208-
# in which server admins can make sure every federating server applies the same
1209-
# rules.
1210-
#
1211-
#allowed_lifetime_min: 1d
1212-
#allowed_lifetime_max: 1y
1198+
#max_avatar_size: 10M
12131199
1214-
# Server admins can define the settings of the background jobs purging the
1215-
# events which lifetime has expired under the 'purge_jobs' section.
1216-
#
1217-
# If no configuration is provided, a single job will be set up to delete expired
1218-
# events in every room daily.
1219-
#
1220-
# Each job's configuration defines which range of message lifetimes the job
1221-
# takes care of. For example, if 'shortest_max_lifetime' is '2d' and
1222-
# 'longest_max_lifetime' is '3d', the job will handle purging expired events in
1223-
# rooms whose state defines a 'max_lifetime' that's both higher than 2 days, and
1224-
# lower than or equal to 3 days. Both the minimum and the maximum value of a
1225-
# range are optional, e.g. a job with no 'shortest_max_lifetime' and a
1226-
# 'longest_max_lifetime' of '3d' will handle every room with a retention policy
1227-
# which 'max_lifetime' is lower than or equal to three days.
1228-
#
1229-
# The rationale for this per-job configuration is that some rooms might have a
1230-
# retention policy with a low 'max_lifetime', where history needs to be purged
1231-
# of outdated messages on a very frequent basis (e.g. every 5min), but not want
1232-
# that purge to be performed by a job that's iterating over every room it knows,
1233-
# which would be quite heavy on the server.
1234-
#
1235-
#purge_jobs:
1236-
# - shortest_max_lifetime: 1d
1237-
# longest_max_lifetime: 3d
1238-
# interval: 5m:
1239-
# - shortest_max_lifetime: 3d
1240-
# longest_max_lifetime: 1y
1241-
# interval: 24h
1200+
# The MIME types allowed for user avatars. Defaults to no restriction.
1201+
#
1202+
# Note that user avatar changes will not work if this is set without
1203+
# using Synapse's media repository.
1204+
#
1205+
#allowed_avatar_mimetypes: ["image/png", "image/jpeg", "image/gif"]
12421206
12431207
# How long to keep redacted events in unredacted form in the database. After
12441208
# this period redacted events get replaced with their redacted form in the DB.

0 commit comments

Comments
 (0)