ProxyDetails implements ProxyAddress { public boolean isAllowed(Set<String> allowlist) { if (allowlist.contains("*")) { return true; } return allowlist.contains(host.split(":")[0]); } ProxyDetails(HttpServletRequest httpServletRequest);  ProxyDetails(String pathInfo); boolean isAllowed(Set<String> allowlist); boolean isAllowed(List<Pattern> regexAllowlist); @Override String getFullProxyUrl(); @Override String toString(); String getStringProxyURL(); String getProxyHostAndPort(); String getProxyPath(); String getScheme(); String getHost(); int getPort(); @Override String getUserName(); @Override String getPassword(); String getHostAndPort(); String getPath(); boolean isValid(); static final String USER_PARAM; static final String PWD_PARAM; }
@Test public void testIsAllowed() throws Exception { HttpServletRequest mockReq = mock(HttpServletRequest.class); when(mockReq.getPathInfo()) .thenReturn("/localhost/9000/jolokia/") .thenReturn("/localhost:8181/jolokia/") .thenReturn("/www.myhost.com/jolokia/") .thenReturn("/myhost1.com/jolokia/") .thenReturn("/myhost22.com/jolokia/") .thenReturn("/www.banned.com/jolokia/"); Set<String> allowlist = new HashSet<>(Arrays.asList("localhost", "www.myhost.com")); List<Pattern> regexAllowlist = Collections.singletonList(Pattern.compile("myhost[0-9]+\\.com")); ProxyDetails details1 = new ProxyDetails(mockReq); ProxyDetails details2 = new ProxyDetails(mockReq); ProxyDetails details3 = new ProxyDetails(mockReq); ProxyDetails details4 = new ProxyDetails(mockReq); ProxyDetails details5 = new ProxyDetails(mockReq); ProxyDetails details6 = new ProxyDetails(mockReq); assertTrue("localhost/9000", details1.isAllowed(allowlist)); assertTrue("localhost:8181", details2.isAllowed(allowlist)); assertTrue("www.myhost.com", details3.isAllowed(allowlist)); assertTrue("myhost1.com", details4.isAllowed(regexAllowlist)); assertTrue("myhost22.com", details5.isAllowed(regexAllowlist)); assertFalse("www.banned.com", details6.isAllowed(allowlist)); }
@Test public void testIsAllowedWithAllowAll() throws Exception { HttpServletRequest mockReq = mock(HttpServletRequest.class); when(mockReq.getPathInfo()) .thenReturn("/localhost/9000/jolokia/") .thenReturn("/www.myhost.com/jolokia/") .thenReturn("/www.banned.com/jolokia/"); Set<String> allowlist = new HashSet<>(Arrays.asList("*")); ProxyDetails details1 = new ProxyDetails(mockReq); ProxyDetails details2 = new ProxyDetails(mockReq); ProxyDetails details3 = new ProxyDetails(mockReq); assertTrue("localhost", details1.isAllowed(allowlist)); assertTrue("www.myhost.com", details2.isAllowed(allowlist)); assertTrue("www.banned.com", details3.isAllowed(allowlist)); }