|
29 | 29 | Union,
|
30 | 30 | )
|
31 | 31 |
|
32 |
| -from matrix_common.regex import glob_to_regex |
33 | 32 | from prometheus_client import Counter, Gauge, Histogram
|
34 | 33 |
|
35 |
| -from twisted.internet.abstract import isIPAddress |
36 | 34 | from twisted.python import failure
|
37 | 35 |
|
38 | 36 | from synapse.api.constants import (
|
@@ -1324,75 +1322,13 @@ async def check_server_matches_acl(self, server_name: str, room_id: str) -> None
|
1324 | 1322 | Raises:
|
1325 | 1323 | AuthError if the server does not match the ACL
|
1326 | 1324 | """
|
1327 |
| - acl_event = await self._storage_controllers.state.get_current_state_event( |
1328 |
| - room_id, EventTypes.ServerACL, "" |
| 1325 | + server_acl_evaluator = ( |
| 1326 | + await self._storage_controllers.state.get_server_acl_for_room(room_id) |
1329 | 1327 | )
|
1330 |
| - if not acl_event or server_matches_acl_event(server_name, acl_event): |
1331 |
| - return |
1332 |
| - |
1333 |
| - raise AuthError(code=403, msg="Server is banned from room") |
1334 |
| - |
1335 |
| - |
1336 |
| -def server_matches_acl_event(server_name: str, acl_event: EventBase) -> bool: |
1337 |
| - """Check if the given server is allowed by the ACL event |
1338 |
| -
|
1339 |
| - Args: |
1340 |
| - server_name: name of server, without any port part |
1341 |
| - acl_event: m.room.server_acl event |
1342 |
| -
|
1343 |
| - Returns: |
1344 |
| - True if this server is allowed by the ACLs |
1345 |
| - """ |
1346 |
| - logger.debug("Checking %s against acl %s", server_name, acl_event.content) |
1347 |
| - |
1348 |
| - # first of all, check if literal IPs are blocked, and if so, whether the |
1349 |
| - # server name is a literal IP |
1350 |
| - allow_ip_literals = acl_event.content.get("allow_ip_literals", True) |
1351 |
| - if not isinstance(allow_ip_literals, bool): |
1352 |
| - logger.warning("Ignoring non-bool allow_ip_literals flag") |
1353 |
| - allow_ip_literals = True |
1354 |
| - if not allow_ip_literals: |
1355 |
| - # check for ipv6 literals. These start with '['. |
1356 |
| - if server_name[0] == "[": |
1357 |
| - return False |
1358 |
| - |
1359 |
| - # check for ipv4 literals. We can just lift the routine from twisted. |
1360 |
| - if isIPAddress(server_name): |
1361 |
| - return False |
1362 |
| - |
1363 |
| - # next, check the deny list |
1364 |
| - deny = acl_event.content.get("deny", []) |
1365 |
| - if not isinstance(deny, (list, tuple)): |
1366 |
| - logger.warning("Ignoring non-list deny ACL %s", deny) |
1367 |
| - deny = [] |
1368 |
| - for e in deny: |
1369 |
| - if _acl_entry_matches(server_name, e): |
1370 |
| - # logger.info("%s matched deny rule %s", server_name, e) |
1371 |
| - return False |
1372 |
| - |
1373 |
| - # then the allow list. |
1374 |
| - allow = acl_event.content.get("allow", []) |
1375 |
| - if not isinstance(allow, (list, tuple)): |
1376 |
| - logger.warning("Ignoring non-list allow ACL %s", allow) |
1377 |
| - allow = [] |
1378 |
| - for e in allow: |
1379 |
| - if _acl_entry_matches(server_name, e): |
1380 |
| - # logger.info("%s matched allow rule %s", server_name, e) |
1381 |
| - return True |
1382 |
| - |
1383 |
| - # everything else should be rejected. |
1384 |
| - # logger.info("%s fell through", server_name) |
1385 |
| - return False |
1386 |
| - |
1387 |
| - |
1388 |
| -def _acl_entry_matches(server_name: str, acl_entry: Any) -> bool: |
1389 |
| - if not isinstance(acl_entry, str): |
1390 |
| - logger.warning( |
1391 |
| - "Ignoring non-str ACL entry '%s' (is %s)", acl_entry, type(acl_entry) |
1392 |
| - ) |
1393 |
| - return False |
1394 |
| - regex = glob_to_regex(acl_entry) |
1395 |
| - return bool(regex.match(server_name)) |
| 1328 | + if server_acl_evaluator and not server_acl_evaluator.server_matches_acl_event( |
| 1329 | + server_name |
| 1330 | + ): |
| 1331 | + raise AuthError(code=403, msg="Server is banned from room") |
1396 | 1332 |
|
1397 | 1333 |
|
1398 | 1334 | class FederationHandlerRegistry:
|
|
0 commit comments