repository.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014, 2015 matrix.org
  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. from ._base import Config
  16. from collections import namedtuple
  17. import sys
  18. ThumbnailRequirement = namedtuple(
  19. "ThumbnailRequirement", ["width", "height", "method", "media_type"]
  20. )
  21. def parse_thumbnail_requirements(thumbnail_sizes):
  22. """ Takes a list of dictionaries with "width", "height", and "method" keys
  23. and creates a map from image media types to the thumbnail size, thumbnailing
  24. method, and thumbnail media type to precalculate
  25. Args:
  26. thumbnail_sizes(list): List of dicts with "width", "height", and
  27. "method" keys
  28. Returns:
  29. Dictionary mapping from media type string to list of
  30. ThumbnailRequirement tuples.
  31. """
  32. requirements = {}
  33. for size in thumbnail_sizes:
  34. width = size["width"]
  35. height = size["height"]
  36. method = size["method"]
  37. jpeg_thumbnail = ThumbnailRequirement(width, height, method, "image/jpeg")
  38. png_thumbnail = ThumbnailRequirement(width, height, method, "image/png")
  39. requirements.setdefault("image/jpeg", []).append(jpeg_thumbnail)
  40. requirements.setdefault("image/gif", []).append(png_thumbnail)
  41. requirements.setdefault("image/png", []).append(png_thumbnail)
  42. return {
  43. media_type: tuple(thumbnails)
  44. for media_type, thumbnails in requirements.items()
  45. }
  46. class ContentRepositoryConfig(Config):
  47. def read_config(self, config):
  48. self.max_upload_size = self.parse_size(config["max_upload_size"])
  49. self.max_image_pixels = self.parse_size(config["max_image_pixels"])
  50. self.max_spider_size = self.parse_size(config["max_spider_size"])
  51. self.media_store_path = self.ensure_directory(config["media_store_path"])
  52. self.uploads_path = self.ensure_directory(config["uploads_path"])
  53. self.dynamic_thumbnails = config["dynamic_thumbnails"]
  54. self.thumbnail_requirements = parse_thumbnail_requirements(
  55. config["thumbnail_sizes"]
  56. )
  57. self.url_preview_enabled = config["url_preview_enabled"]
  58. if self.url_preview_enabled:
  59. try:
  60. from netaddr import IPSet
  61. if "url_preview_ip_range_blacklist" in config:
  62. self.url_preview_ip_range_blacklist = IPSet(
  63. config["url_preview_ip_range_blacklist"]
  64. )
  65. if "url_preview_url_blacklist" in config:
  66. self.url_preview_url_blacklist = config["url_preview_url_blacklist"]
  67. except ImportError:
  68. sys.stderr.write("\nmissing netaddr dep - disabling preview_url API\n")
  69. def default_config(self, **kwargs):
  70. media_store = self.default_path("media_store")
  71. uploads_path = self.default_path("uploads")
  72. return """
  73. # Directory where uploaded images and attachments are stored.
  74. media_store_path: "%(media_store)s"
  75. # Directory where in-progress uploads are stored.
  76. uploads_path: "%(uploads_path)s"
  77. # The largest allowed upload size in bytes
  78. max_upload_size: "10M"
  79. # Maximum number of pixels that will be thumbnailed
  80. max_image_pixels: "32M"
  81. # Whether to generate new thumbnails on the fly to precisely match
  82. # the resolution requested by the client. If true then whenever
  83. # a new resolution is requested by the client the server will
  84. # generate a new thumbnail. If false the server will pick a thumbnail
  85. # from a precalculated list.
  86. dynamic_thumbnails: false
  87. # List of thumbnail to precalculate when an image is uploaded.
  88. thumbnail_sizes:
  89. - width: 32
  90. height: 32
  91. method: crop
  92. - width: 96
  93. height: 96
  94. method: crop
  95. - width: 320
  96. height: 240
  97. method: scale
  98. - width: 640
  99. height: 480
  100. method: scale
  101. - width: 800
  102. height: 600
  103. method: scale
  104. # Is the preview URL API enabled? If enabled, you *must* specify
  105. # an explicit url_preview_ip_range_blacklist of IPs that the spider is
  106. # denied from accessing.
  107. url_preview_enabled: False
  108. # List of IP address CIDR ranges that the URL preview spider is denied
  109. # from accessing. There are no defaults: you must explicitly
  110. # specify a list for URL previewing to work. You should specify any
  111. # internal services in your network that you do not want synapse to try
  112. # to connect to, otherwise anyone in any Matrix room could cause your
  113. # synapse to issue arbitrary GET requests to your internal services,
  114. # causing serious security issues.
  115. #
  116. # url_preview_ip_range_blacklist:
  117. # - '127.0.0.0/8'
  118. # - '10.0.0.0/8'
  119. # - '172.16.0.0/12'
  120. # - '192.168.0.0/16'
  121. # Optional list of URL matches that the URL preview spider is
  122. # denied from accessing. You should use url_preview_ip_range_blacklist
  123. # in preference to this, otherwise someone could define a public DNS
  124. # entry that points to a private IP address and circumvent the blacklist.
  125. # This is more useful if you know there is an entire shape of URL that
  126. # you know that will never want synapse to try to spider.
  127. #
  128. # Each list entry is a dictionary of url component attributes as returned
  129. # by urlparse.urlsplit as applied to the absolute form of the URL. See
  130. # https://docs.python.org/2/library/urlparse.html#urlparse.urlsplit
  131. # The values of the dictionary are treated as an filename match pattern
  132. # applied to that component of URLs, unless they start with a ^ in which
  133. # case they are treated as a regular expression match. If all the
  134. # specified component matches for a given list item succeed, the URL is
  135. # blacklisted.
  136. #
  137. # url_preview_url_blacklist:
  138. # # blacklist any URL with a username in its URI
  139. # - username: '*'
  140. #
  141. # # blacklist all *.google.com URLs
  142. # - netloc: 'google.com'
  143. # - netloc: '*.google.com'
  144. #
  145. # # blacklist all plain HTTP URLs
  146. # - scheme: 'http'
  147. #
  148. # # blacklist http(s)://www.acme.com/foo
  149. # - netloc: 'www.acme.com'
  150. # path: '/foo'
  151. #
  152. # # blacklist any URL with a literal IPv4 address
  153. # - netloc: '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
  154. # The largest allowed URL preview spidering size in bytes
  155. max_spider_size: "10M"
  156. """ % locals()