|
23 | 23 |
|
24 | 24 | import attr
|
25 | 25 | import yaml
|
| 26 | +from netaddr import IPSet |
26 | 27 |
|
27 | 28 | from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
|
28 | 29 | from synapse.http.endpoint import parse_and_validate_server_name
|
|
39 | 40 | # in the list.
|
40 | 41 | DEFAULT_BIND_ADDRESSES = ["::", "0.0.0.0"]
|
41 | 42 |
|
| 43 | +DEFAULT_IP_RANGE_BLACKLIST = [ |
| 44 | + # Localhost |
| 45 | + "127.0.0.0/8", |
| 46 | + # Private networks. |
| 47 | + "10.0.0.0/8", |
| 48 | + "172.16.0.0/12", |
| 49 | + "192.168.0.0/16", |
| 50 | + # Carrier grade NAT. |
| 51 | + "100.64.0.0/10", |
| 52 | + # Address registry. |
| 53 | + "192.0.0.0/24", |
| 54 | + # Link-local networks. |
| 55 | + "169.254.0.0/16", |
| 56 | + # Testing networks. |
| 57 | + "198.18.0.0/15", |
| 58 | + "192.0.2.0/24", |
| 59 | + "198.51.100.0/24", |
| 60 | + "203.0.113.0/24", |
| 61 | + # Multicast. |
| 62 | + "224.0.0.0/4", |
| 63 | + # Localhost |
| 64 | + "::1/128", |
| 65 | + # Link-local addresses. |
| 66 | + "fe80::/10", |
| 67 | + # Unique local addresses. |
| 68 | + "fc00::/7", |
| 69 | +] |
| 70 | + |
42 | 71 | DEFAULT_ROOM_VERSION = "6"
|
43 | 72 |
|
44 | 73 | ROOM_COMPLEXITY_TOO_GREAT = (
|
@@ -256,6 +285,38 @@ def read_config(self, config, **kwargs):
|
256 | 285 | # due to resource constraints
|
257 | 286 | self.admin_contact = config.get("admin_contact", None)
|
258 | 287 |
|
| 288 | + ip_range_blacklist = config.get( |
| 289 | + "ip_range_blacklist", DEFAULT_IP_RANGE_BLACKLIST |
| 290 | + ) |
| 291 | + |
| 292 | + # Attempt to create an IPSet from the given ranges |
| 293 | + try: |
| 294 | + self.ip_range_blacklist = IPSet(ip_range_blacklist) |
| 295 | + except Exception as e: |
| 296 | + raise ConfigError("Invalid range(s) provided in ip_range_blacklist.") from e |
| 297 | + # Always blacklist 0.0.0.0, :: |
| 298 | + self.ip_range_blacklist.update(["0.0.0.0", "::"]) |
| 299 | + |
| 300 | + try: |
| 301 | + self.ip_range_whitelist = IPSet(config.get("ip_range_whitelist", ())) |
| 302 | + except Exception as e: |
| 303 | + raise ConfigError("Invalid range(s) provided in ip_range_whitelist.") from e |
| 304 | + |
| 305 | + # The federation_ip_range_blacklist is used for backwards-compatibility |
| 306 | + # and only applies to federation and identity servers. If it is not given, |
| 307 | + # default to ip_range_blacklist. |
| 308 | + federation_ip_range_blacklist = config.get( |
| 309 | + "federation_ip_range_blacklist", ip_range_blacklist |
| 310 | + ) |
| 311 | + try: |
| 312 | + self.federation_ip_range_blacklist = IPSet(federation_ip_range_blacklist) |
| 313 | + except Exception as e: |
| 314 | + raise ConfigError( |
| 315 | + "Invalid range(s) provided in federation_ip_range_blacklist." |
| 316 | + ) from e |
| 317 | + # Always blacklist 0.0.0.0, :: |
| 318 | + self.federation_ip_range_blacklist.update(["0.0.0.0", "::"]) |
| 319 | + |
259 | 320 | if self.public_baseurl is not None:
|
260 | 321 | if self.public_baseurl[-1] != "/":
|
261 | 322 | self.public_baseurl += "/"
|
@@ -561,6 +622,10 @@ def has_tls_listener(self) -> bool:
|
561 | 622 | def generate_config_section(
|
562 | 623 | self, server_name, data_dir_path, open_private_ports, listeners, **kwargs
|
563 | 624 | ):
|
| 625 | + ip_range_blacklist = "\n".join( |
| 626 | + " # - '%s'" % ip for ip in DEFAULT_IP_RANGE_BLACKLIST |
| 627 | + ) |
| 628 | + |
564 | 629 | _, bind_port = parse_and_validate_server_name(server_name)
|
565 | 630 | if bind_port is not None:
|
566 | 631 | unsecure_port = bind_port - 400
|
@@ -752,6 +817,21 @@ def generate_config_section(
|
752 | 817 | #
|
753 | 818 | #enable_search: false
|
754 | 819 |
|
| 820 | + # Prevent outgoing requests from being sent to the following blacklisted IP address |
| 821 | + # CIDR ranges. If this option is not specified then it defaults to private IP |
| 822 | + # address ranges (see the example below). |
| 823 | + # |
| 824 | + # The blacklist applies to the outbound requests for federation, identity servers, |
| 825 | + # push servers, and for checking key validity for third-party invite events. |
| 826 | + # |
| 827 | + # (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly |
| 828 | + # listed here, since they correspond to unroutable addresses.) |
| 829 | + # |
| 830 | + # This option replaces federation_ip_range_blacklist in Synapse v1.25.0. |
| 831 | + # |
| 832 | + #ip_range_blacklist: |
| 833 | +%(ip_range_blacklist)s |
| 834 | +
|
755 | 835 | # List of ports that Synapse should listen on, their purpose and their
|
756 | 836 | # configuration.
|
757 | 837 | #
|
|
0 commit comments