|
| 1 | +package io.swagger.v3.parser.urlresolver; |
| 2 | + |
| 3 | +import io.swagger.v3.parser.urlresolver.exceptions.HostDeniedException; |
| 4 | +import io.swagger.v3.parser.urlresolver.matchers.UrlPatternMatcher; |
| 5 | +import io.swagger.v3.parser.urlresolver.models.ResolvedUrl; |
| 6 | +import io.swagger.v3.parser.urlresolver.utils.NetUtils; |
| 7 | + |
| 8 | +import java.net.InetAddress; |
| 9 | +import java.net.MalformedURLException; |
| 10 | +import java.net.URL; |
| 11 | +import java.net.UnknownHostException; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class PermittedUrlsChecker { |
| 16 | + |
| 17 | + protected final UrlPatternMatcher allowlistMatcher; |
| 18 | + protected final UrlPatternMatcher denylistMatcher; |
| 19 | + |
| 20 | + public PermittedUrlsChecker() { |
| 21 | + this.allowlistMatcher = new UrlPatternMatcher(Collections.emptyList()); |
| 22 | + this.denylistMatcher = new UrlPatternMatcher(Collections.emptyList()); |
| 23 | + } |
| 24 | + |
| 25 | + public PermittedUrlsChecker(List<String> allowlist, List<String> denylist) { |
| 26 | + if(allowlist != null) { |
| 27 | + this.allowlistMatcher = new UrlPatternMatcher(allowlist); |
| 28 | + } else { |
| 29 | + this.allowlistMatcher = new UrlPatternMatcher(Collections.emptyList()); |
| 30 | + } |
| 31 | + |
| 32 | + if(denylist != null) { |
| 33 | + this.denylistMatcher = new UrlPatternMatcher(denylist); |
| 34 | + } else { |
| 35 | + this.denylistMatcher = new UrlPatternMatcher(Collections.emptyList()); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public ResolvedUrl verify(String url) throws HostDeniedException { |
| 40 | + URL parsed; |
| 41 | + |
| 42 | + try { |
| 43 | + parsed = new URL(url); |
| 44 | + } catch (MalformedURLException e) { |
| 45 | + throw new HostDeniedException(String.format("Failed to parse URL. URL [%s]", url), e); |
| 46 | + } |
| 47 | + |
| 48 | + if (!parsed.getProtocol().equals("http") && !parsed.getProtocol().equals("https")) { |
| 49 | + throw new HostDeniedException(String.format("URL does not use a supported protocol. URL [%s]", url)); |
| 50 | + } |
| 51 | + |
| 52 | + String hostname; |
| 53 | + try { |
| 54 | + hostname = NetUtils.getHostFromUrl(url); |
| 55 | + } catch (MalformedURLException e) { |
| 56 | + throw new HostDeniedException(String.format("Failed to get hostname from URL. URL [%s]", url), e); |
| 57 | + } |
| 58 | + |
| 59 | + if (this.allowlistMatcher.matches(url)) { |
| 60 | + return new ResolvedUrl(url, hostname); |
| 61 | + } |
| 62 | + |
| 63 | + if (this.denylistMatcher.matches(url)) { |
| 64 | + throw new HostDeniedException(String.format("URL is part of the explicit denylist. URL [%s]", url)); |
| 65 | + } |
| 66 | + |
| 67 | + InetAddress ip; |
| 68 | + try { |
| 69 | + ip = NetUtils.getHostByName(hostname); |
| 70 | + } catch (UnknownHostException e) { |
| 71 | + throw new HostDeniedException( |
| 72 | + String.format("Failed to resolve IP from hostname. Hostname [%s]", hostname), e); |
| 73 | + } |
| 74 | + |
| 75 | + String urlWithIp; |
| 76 | + try { |
| 77 | + urlWithIp = NetUtils.setHost(url, ip.getHostAddress()); |
| 78 | + } catch (MalformedURLException e) { |
| 79 | + throw new HostDeniedException( |
| 80 | + String.format("Failed to create new URL with IP. IP [%s] URL [%s]", ip.getHostAddress(), url), e); |
| 81 | + } |
| 82 | + |
| 83 | + if (this.allowlistMatcher.matches(urlWithIp)) { |
| 84 | + return new ResolvedUrl(urlWithIp, hostname); |
| 85 | + } |
| 86 | + |
| 87 | + if (isRestrictedIpRange(ip)) { |
| 88 | + throw new HostDeniedException(String.format("IP is restricted. URL [%s]", urlWithIp)); |
| 89 | + } |
| 90 | + |
| 91 | + if (this.denylistMatcher.matches(urlWithIp)) { |
| 92 | + throw new HostDeniedException(String.format("IP is part of the explicit denylist. URL [%s]", urlWithIp)); |
| 93 | + } |
| 94 | + |
| 95 | + return new ResolvedUrl(urlWithIp, hostname); |
| 96 | + } |
| 97 | + |
| 98 | + protected boolean isRestrictedIpRange(InetAddress ip) { |
| 99 | + return ip.isLinkLocalAddress() |
| 100 | + || ip.isSiteLocalAddress() |
| 101 | + || ip.isLoopbackAddress() |
| 102 | + || ip.isAnyLocalAddress() |
| 103 | + || NetUtils.isUniqueLocalAddress(ip) |
| 104 | + || NetUtils.isNAT64Address(ip); |
| 105 | + } |
| 106 | +} |
0 commit comments