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

Commit ad1ac2a

Browse files
committed
Deny access to additional IP addresses by default.
Including IPv6 with embedded IPv4, IPv4-compatible IPv6, and 6to4 addresses.
1 parent cfcc4bf commit ad1ac2a

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

changelog.d/9240.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deny access to additional IP addresses by default.

docs/sample_config.yaml

+72
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,42 @@ pid_file: DATADIR/homeserver.pid
174174
# - '198.51.100.0/24'
175175
# - '203.0.113.0/24'
176176
# - '224.0.0.0/4'
177+
# - '::127.0.0.0/104'
178+
# - '::10.0.0.0/104'
179+
# - '::172.16.0.0/108'
180+
# - '::192.168.0.0/112'
181+
# - '::100.64.0.0/106'
182+
# - '::192.0.0.0/120'
183+
# - '::169.254.0.0/112'
184+
# - '::198.18.0.0/111'
185+
# - '::192.0.2.0/120'
186+
# - '::198.51.100.0/120'
187+
# - '::203.0.113.0/120'
188+
# - '::224.0.0.0/100'
189+
# - '::ffff:127.0.0.0/104'
190+
# - '::ffff:10.0.0.0/104'
191+
# - '::ffff:172.16.0.0/108'
192+
# - '::ffff:192.168.0.0/112'
193+
# - '::ffff:100.64.0.0/106'
194+
# - '::ffff:192.0.0.0/120'
195+
# - '::ffff:169.254.0.0/112'
196+
# - '::ffff:198.18.0.0/111'
197+
# - '::ffff:192.0.2.0/120'
198+
# - '::ffff:198.51.100.0/120'
199+
# - '::ffff:203.0.113.0/120'
200+
# - '::ffff:224.0.0.0/100'
201+
# - '2002:7f00:0000::/24'
202+
# - '2002:0a00:0000::/24'
203+
# - '2002:ac10:0000::/28'
204+
# - '2002:c0a8:0000::/32'
205+
# - '2002:6440:0000::/26'
206+
# - '2002:c000:0000::/40'
207+
# - '2002:a9fe:0000::/32'
208+
# - '2002:c612:0000::/31'
209+
# - '2002:c000:0200::/40'
210+
# - '2002:c633:6400::/40'
211+
# - '2002:cb00:7100::/40'
212+
# - '2002:e000:0000::/20'
177213
# - '::1/128'
178214
# - 'fe80::/10'
179215
# - 'fc00::/7'
@@ -985,6 +1021,42 @@ media_store_path: "DATADIR/media_store"
9851021
# - '198.51.100.0/24'
9861022
# - '203.0.113.0/24'
9871023
# - '224.0.0.0/4'
1024+
# - '::127.0.0.0/104'
1025+
# - '::10.0.0.0/104'
1026+
# - '::172.16.0.0/108'
1027+
# - '::192.168.0.0/112'
1028+
# - '::100.64.0.0/106'
1029+
# - '::192.0.0.0/120'
1030+
# - '::169.254.0.0/112'
1031+
# - '::198.18.0.0/111'
1032+
# - '::192.0.2.0/120'
1033+
# - '::198.51.100.0/120'
1034+
# - '::203.0.113.0/120'
1035+
# - '::224.0.0.0/100'
1036+
# - '::ffff:127.0.0.0/104'
1037+
# - '::ffff:10.0.0.0/104'
1038+
# - '::ffff:172.16.0.0/108'
1039+
# - '::ffff:192.168.0.0/112'
1040+
# - '::ffff:100.64.0.0/106'
1041+
# - '::ffff:192.0.0.0/120'
1042+
# - '::ffff:169.254.0.0/112'
1043+
# - '::ffff:198.18.0.0/111'
1044+
# - '::ffff:192.0.2.0/120'
1045+
# - '::ffff:198.51.100.0/120'
1046+
# - '::ffff:203.0.113.0/120'
1047+
# - '::ffff:224.0.0.0/100'
1048+
# - '2002:7f00:0000::/24'
1049+
# - '2002:0a00:0000::/24'
1050+
# - '2002:ac10:0000::/28'
1051+
# - '2002:c0a8:0000::/32'
1052+
# - '2002:6440:0000::/26'
1053+
# - '2002:c000:0000::/40'
1054+
# - '2002:a9fe:0000::/32'
1055+
# - '2002:c612:0000::/31'
1056+
# - '2002:c000:0200::/40'
1057+
# - '2002:c633:6400::/40'
1058+
# - '2002:cb00:7100::/40'
1059+
# - '2002:e000:0000::/20'
9881060
# - '::1/128'
9891061
# - 'fe80::/10'
9901062
# - 'fc00::/7'

synapse/config/server.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import attr
2525
import yaml
26-
from netaddr import IPSet
26+
from netaddr import IPNetwork, IPSet
2727

2828
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
2929
from synapse.util.stringutils import parse_and_validate_server_name
@@ -40,7 +40,24 @@
4040
# in the list.
4141
DEFAULT_BIND_ADDRESSES = ["::", "0.0.0.0"]
4242

43-
DEFAULT_IP_RANGE_BLACKLIST = [
43+
44+
def _6to4(network_str: str) -> str:
45+
"""Convert an IPv4 network into a 6to4 IPv6 network per RFC 3056."""
46+
network = IPNetwork(network_str)
47+
# 6to4 networks have a prefix of 2002, the first IPv4 address in the network
48+
# needs to be hex-encoded as the next 32 bits. Calculate the new prefix by
49+
# adding 16 (the additional bits from the 2002: prefix).
50+
hex_network = hex(network.first)[2:]
51+
hex_network = ("0" * (8 - len(hex_network))) + hex_network
52+
return "2002:%s:%s::/%d" % (
53+
hex_network[:4],
54+
hex_network[4:],
55+
16 + network.prefixlen,
56+
)
57+
58+
59+
# Start with IPv4 ranges that are considered private / unroutable / don't make sense.
60+
DEFAULT_IPV4_RANGE_BLACKLIST = [
4461
# Localhost
4562
"127.0.0.0/8",
4663
# Private networks.
@@ -60,13 +77,32 @@
6077
"203.0.113.0/24",
6178
# Multicast.
6279
"224.0.0.0/4",
80+
]
81+
82+
# IPv6 contains all of the IPv4 address space, see RFC 4291, section 2.5.5.
83+
# IPv6 also has a deprecated transition mechanism (6to4) which is not supposed
84+
# to be used for private IPv4 space, see RFC 3056, section 2.
85+
DEFAULT_IPV6_RANGE_BLACKLIST = (
86+
[
87+
str(IPNetwork(ip).ipv6(ipv4_compatible=True))
88+
for ip in DEFAULT_IPV4_RANGE_BLACKLIST
89+
]
90+
+ [
91+
str(IPNetwork(ip).ipv6(ipv4_compatible=False))
92+
for ip in DEFAULT_IPV4_RANGE_BLACKLIST
93+
]
94+
+ [_6to4(ip) for ip in DEFAULT_IPV4_RANGE_BLACKLIST]
95+
)
96+
# Add IPv6 ranges that are considered private / unroutable / don't make sense.
97+
DEFAULT_IPV6_RANGE_BLACKLIST += [
6398
# Localhost
6499
"::1/128",
65100
# Link-local addresses.
66101
"fe80::/10",
67102
# Unique local addresses.
68103
"fc00::/7",
69104
]
105+
DEFAULT_IP_RANGE_BLACKLIST = DEFAULT_IPV4_RANGE_BLACKLIST + DEFAULT_IPV6_RANGE_BLACKLIST
70106

71107
DEFAULT_ROOM_VERSION = "6"
72108

0 commit comments

Comments
 (0)