|
| 1 | +package com.dbaagent.service; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | + |
| 6 | +import java.net.Inet4Address; |
| 7 | +import java.net.Inet6Address; |
| 8 | +import java.net.InetAddress; |
| 9 | +import java.net.UnknownHostException; |
| 10 | +import java.util.Locale; |
| 11 | + |
| 12 | +/** |
| 13 | + * Rejects SSH bastion hosts that point back into infrastructure the caller |
| 14 | + * should not reach through DeepSQL — loopback, link-local (including the cloud |
| 15 | + * metadata endpoint), and RFC1918 ranges. |
| 16 | + * |
| 17 | + * The host is resolved and every returned address is checked, so a public |
| 18 | + * hostname whose A record points at 10.x or 169.254.169.254 is still refused. |
| 19 | + */ |
| 20 | +@Component |
| 21 | +@Slf4j |
| 22 | +public class SshHostGuard { |
| 23 | + |
| 24 | + private final SshHostGuardProperties properties; |
| 25 | + |
| 26 | + public SshHostGuard(SshHostGuardProperties properties) { |
| 27 | + this.properties = properties; |
| 28 | + } |
| 29 | + |
| 30 | + public void assertAllowed(String sshHost) { |
| 31 | + if (sshHost == null || sshHost.isBlank()) { |
| 32 | + throw new IllegalArgumentException("SSH host is required"); |
| 33 | + } |
| 34 | + |
| 35 | + String host = normalize(sshHost); |
| 36 | + |
| 37 | + if (!properties.isEnabled() || isExplicitlyAllowed(host)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + InetAddress[] addresses; |
| 42 | + try { |
| 43 | + addresses = InetAddress.getAllByName(host); |
| 44 | + } catch (UnknownHostException e) { |
| 45 | + throw new IllegalArgumentException("SSH host could not be resolved: " + sshHost); |
| 46 | + } |
| 47 | + |
| 48 | + for (InetAddress address : addresses) { |
| 49 | + if (isBlocked(address)) { |
| 50 | + log.warn("Blocked SSH tunnel to restricted host {} (resolved to {})", |
| 51 | + sshHost, address.getHostAddress()); |
| 52 | + throw new IllegalArgumentException( |
| 53 | + "SSH host '" + sshHost + "' resolves to a restricted address (" |
| 54 | + + address.getHostAddress() + "). Add it to " |
| 55 | + + "deepsql.ssh.host-guard.allowed-hosts if this is intentional."); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private boolean isExplicitlyAllowed(String host) { |
| 61 | + for (String allowed : properties.getAllowedHosts()) { |
| 62 | + if (allowed == null || allowed.isBlank()) continue; |
| 63 | + String candidate = normalize(allowed); |
| 64 | + if (candidate.startsWith(".")) { |
| 65 | + if (host.endsWith(candidate)) return true; |
| 66 | + } else if (host.equals(candidate)) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + } |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + private boolean isBlocked(InetAddress address) { |
| 74 | + if (address.isLoopbackAddress() |
| 75 | + || address.isAnyLocalAddress() |
| 76 | + || address.isLinkLocalAddress() |
| 77 | + || address.isSiteLocalAddress() |
| 78 | + || address.isMulticastAddress()) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + if (address instanceof Inet4Address) { |
| 82 | + return isBlockedIpv4(address.getAddress()); |
| 83 | + } |
| 84 | + if (address instanceof Inet6Address v6) { |
| 85 | + // Unique local addresses (fc00::/7) have no isSiteLocalAddress() mapping in Java. |
| 86 | + if ((v6.getAddress()[0] & 0xFE) == 0xFC) return true; |
| 87 | + // IPv4-mapped/compatible forms smuggle a blocked v4 address through a v6 literal. |
| 88 | + byte[] embedded = embeddedIpv4(v6); |
| 89 | + return embedded != null && isBlockedIpv4(embedded); |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + private boolean isBlockedIpv4(byte[] octets) { |
| 95 | + int first = octets[0] & 0xFF; |
| 96 | + int second = octets[1] & 0xFF; |
| 97 | + // 100.64.0.0/10 carrier-grade NAT, used for cloud-internal routing. |
| 98 | + if (first == 100 && second >= 64 && second <= 127) return true; |
| 99 | + // 192.0.0.0/24 IETF protocol assignments. |
| 100 | + if (first == 192 && second == 0 && (octets[2] & 0xFF) == 0) return true; |
| 101 | + // 0.0.0.0/8 "this network". |
| 102 | + return first == 0; |
| 103 | + } |
| 104 | + |
| 105 | + private byte[] embeddedIpv4(Inet6Address address) { |
| 106 | + byte[] bytes = address.getAddress(); |
| 107 | + for (int i = 0; i < 10; i++) { |
| 108 | + if (bytes[i] != 0) return null; |
| 109 | + } |
| 110 | + boolean mapped = (bytes[10] & 0xFF) == 0xFF && (bytes[11] & 0xFF) == 0xFF; |
| 111 | + boolean compat = bytes[10] == 0 && bytes[11] == 0; |
| 112 | + if (!mapped && !compat) return null; |
| 113 | + return new byte[]{bytes[12], bytes[13], bytes[14], bytes[15]}; |
| 114 | + } |
| 115 | + |
| 116 | + private String normalize(String host) { |
| 117 | + String trimmed = host.trim().toLowerCase(Locale.ROOT); |
| 118 | + if (trimmed.startsWith("[") && trimmed.endsWith("]")) { |
| 119 | + trimmed = trimmed.substring(1, trimmed.length() - 1); |
| 120 | + } |
| 121 | + return trimmed; |
| 122 | + } |
| 123 | +} |
0 commit comments