|
| 1 | +import ipaddress |
| 2 | +from typing import List, Optional |
| 3 | + |
| 4 | +from .cidr_helpers import IPNetwork, try_parse_cidr_string |
| 5 | +from .exceptions import AntiSSRFException |
| 6 | + |
| 7 | + |
| 8 | +class AntiSSRFPolicy: |
| 9 | + def __init__(self, use_defaults: bool = True): |
| 10 | + self.AllowedAddresses: List[IPNetwork] = [] |
| 11 | + self.DeniedAddresses: List[IPNetwork] = [] |
| 12 | + self.DeniedHeaders: List[str] = [] |
| 13 | + self.RequiredHeaders: List[str] = [] |
| 14 | + self.AllowPlainTextHttp: bool = False |
| 15 | + self.AddXFFHeader: bool = True |
| 16 | + self.DenyAllUnspecifiedIPs: bool = False |
| 17 | + |
| 18 | + if use_defaults: |
| 19 | + self._set_defaults() |
| 20 | + |
| 21 | + def add_allowed_addresses(self, networks: List[str]) -> bool: |
| 22 | + for network in networks: |
| 23 | + outnet = try_parse_cidr_string(network) |
| 24 | + self.AllowedAddresses.append(outnet) |
| 25 | + return True |
| 26 | + |
| 27 | + def add_denied_addresses(self, networks: List[str]) -> bool: |
| 28 | + if self.DenyAllUnspecifiedIPs: |
| 29 | + raise AntiSSRFException("Can't add denied networks when * is already supplied") |
| 30 | + if not networks: |
| 31 | + raise AntiSSRFException("Bad networks parameter") |
| 32 | + if len(networks) == 1 and networks[0] == "*": |
| 33 | + if len(self.DeniedAddresses) > 0: |
| 34 | + raise AntiSSRFException("Can't add * when deny list already has entries") |
| 35 | + self.DenyAllUnspecifiedIPs = True |
| 36 | + return True |
| 37 | + else: |
| 38 | + for network in networks: |
| 39 | + outnet = try_parse_cidr_string(network) |
| 40 | + self.DeniedAddresses.append(outnet) |
| 41 | + return True |
| 42 | + |
| 43 | + def add_denied_headers(self, denied_headers: Optional[List[str]]) -> None: |
| 44 | + if denied_headers: |
| 45 | + self.DeniedHeaders.extend(denied_headers) |
| 46 | + |
| 47 | + def add_required_headers(self, required_headers: Optional[List[str]]) -> None: |
| 48 | + if required_headers: |
| 49 | + self.RequiredHeaders.extend(required_headers) |
| 50 | + |
| 51 | + def set_allow_plain_text_http(self, allow_plain_text_http: bool = False) -> None: |
| 52 | + self.AllowPlainTextHttp = allow_plain_text_http |
| 53 | + |
| 54 | + def add_xff(self, add_xff: bool = True) -> None: |
| 55 | + self.AddXFFHeader = add_xff |
| 56 | + |
| 57 | + # IP Addresses in Deny List can be IPv4, IPv6 or IPv4 mapped to IPv6 |
| 58 | + # Accordingly, to check if an input address from DNS resolution is to be denied, we should: |
| 59 | + # 1. Check if the input IP is an IPv4 address, and then check if it is present in deny list |
| 60 | + # as a pure IPv4 or an IPv4 mapped to IPv6 format |
| 61 | + # 2. Check if the input IP is an IPv6 address, and then check if it is present in deny list |
| 62 | + # as a pure IPv6 address. This includes addresses in IPv4 mapped to IPv6 format |
| 63 | + # 3. Check if the input IP is an IPv4 mapped to IPv6, then check if it is present in the |
| 64 | + # deny list as an IPv4 mapped IPv6 address, then convert it to IPv4 and check if it is |
| 65 | + # present in the deny list as a pure IPv4 address |
| 66 | + # |
| 67 | + # For example, 169.254.169.254, if present in the deny list, should deny DNS resolved |
| 68 | + # addresses 169.254.169.254 and ::ffff:a9fe:a9fe |
| 69 | + # Likewise ::ffff:a9fe:a9fe, if present in the deny list, should deny DNS resolved |
| 70 | + # addresses ::ffff:a9fe:a9fe and 169.254.169.254 |
| 71 | + # |
| 72 | + # Such case-by-case comparisons leads to a lot of branches in code leading to |
| 73 | + # sphagettification and also makes code difficult to follow and maintain |
| 74 | + # Furthermore, the complexity gets compounded if one adds an allow list to the mix |
| 75 | + # |
| 76 | + # To make things easier and efficient, we convert every IPv4 address to IPv6 across the |
| 77 | + # deny list, allow list and also the input DNS resolved addresses |
| 78 | + # The CIDR helper class is accordingly written |
| 79 | + # |
| 80 | + # As IPv6 is the future anyway, this also makes the code future proof |
| 81 | + def is_network_connection_allowed(self, dns_resolved_ip_addresses: List[str]) -> bool: |
| 82 | + for ip_str in dns_resolved_ip_addresses: |
| 83 | + ip_address = ipaddress.ip_address(ip_str) |
| 84 | + ipv6_address = ( |
| 85 | + ip_address |
| 86 | + if isinstance(ip_address, ipaddress.IPv6Address) |
| 87 | + else ipaddress.IPv6Address(f"::ffff:{ip_address}") |
| 88 | + ) |
| 89 | + |
| 90 | + if self.DenyAllUnspecifiedIPs: |
| 91 | + # If the address is not in an allow list, it's not allowed. |
| 92 | + if not self._networks_contain_address(self.AllowedAddresses, ipv6_address): |
| 93 | + return False |
| 94 | + elif self.DeniedAddresses: |
| 95 | + # If address is in deny list and not in allow list, it's not allowed. |
| 96 | + if self._networks_contain_address( |
| 97 | + self.DeniedAddresses, ipv6_address |
| 98 | + ) and not self._networks_contain_address(self.AllowedAddresses, ipv6_address): |
| 99 | + return False |
| 100 | + # No IP addresses returned by DNS resolution were denied |
| 101 | + return True |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def _networks_contain_address(networks: List[IPNetwork], address: ipaddress.IPv6Address) -> bool: |
| 105 | + for network in networks: |
| 106 | + if network.contains(address): |
| 107 | + return True |
| 108 | + return False |
| 109 | + |
| 110 | + def is_http_request_allowed(self, scheme: str, headers: dict) -> bool: |
| 111 | + if scheme.lower() == "http" and not self.AllowPlainTextHttp: |
| 112 | + return False |
| 113 | + |
| 114 | + if self.AddXFFHeader: |
| 115 | + if "X-Forwarded-For" not in headers: |
| 116 | + headers["X-Forwarded-For"] = "true" |
| 117 | + |
| 118 | + if self.DeniedHeaders: |
| 119 | + for header in self.DeniedHeaders: |
| 120 | + if header in headers: |
| 121 | + return False |
| 122 | + |
| 123 | + if self.RequiredHeaders: |
| 124 | + for header in self.RequiredHeaders: |
| 125 | + if header not in headers: |
| 126 | + return False |
| 127 | + |
| 128 | + return True |
| 129 | + |
| 130 | + def _set_defaults(self): |
| 131 | + self.AllowedAddresses = [] |
| 132 | + self.DeniedAddresses = [] |
| 133 | + self.RequiredHeaders = [] |
| 134 | + self.DeniedHeaders = [] |
| 135 | + self.AllowPlainTextHttp = False |
| 136 | + self.DenyAllUnspecifiedIPs = False |
| 137 | + self.AddXFFHeader = True |
| 138 | + |
| 139 | + self.add_denied_addresses( |
| 140 | + [ |
| 141 | + # ==== IPv4 ==== # |
| 142 | + "255.255.255.255/32", |
| 143 | + "168.63.129.16/32", # Not nonroutable, |
| 144 | + # but this is the WireServer IP we should block. |
| 145 | + "192.0.0.0/24", |
| 146 | + "192.0.2.0/24", |
| 147 | + "192.88.99.0/24", |
| 148 | + "198.51.100.0/24", |
| 149 | + "203.0.113.0/24", |
| 150 | + "169.254.0.0/16", |
| 151 | + "192.168.0.0/16", |
| 152 | + "198.18.0.0/15", |
| 153 | + "172.16.0.0/12", |
| 154 | + "100.64.0.0/10", # IANA-Reserved |
| 155 | + "0.0.0.0/8", |
| 156 | + "10.0.0.0/8", |
| 157 | + "127.0.0.0/8", |
| 158 | + "25.0.0.0/8", # GNS Core |
| 159 | + "224.0.0.0/4", |
| 160 | + "240.0.0.0/4", |
| 161 | + # ==== IPv6 ==== # |
| 162 | + "::1/128", # Localhost |
| 163 | + "FC00::/7", # Unique-local |
| 164 | + "fe80::/10", # Link-local |
| 165 | + "fec0::/10", # Site-local |
| 166 | + "2001::/32", # Teredo |
| 167 | + ] |
| 168 | + ) |
| 169 | + self.DenyAllUnspecifiedIPs = False |
| 170 | + |
| 171 | + # Deprecated method, for backward compatibility only |
| 172 | + def set_defaults(self): |
| 173 | + self._set_defaults() |
0 commit comments