|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package net.sourceforge.plantuml.security; |
|
|
|
import java.awt.image.BufferedImage; |
|
import java.io.ByteArrayInputStream; |
|
import java.io.ByteArrayOutputStream; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.OutputStream; |
|
import java.net.HttpURLConnection; |
|
import java.net.MalformedURLException; |
|
import java.net.Proxy; |
|
import java.net.URL; |
|
import java.net.URLConnection; |
|
import java.nio.charset.Charset; |
|
import java.nio.charset.StandardCharsets; |
|
import java.util.Arrays; |
|
import java.util.Collections; |
|
import java.util.List; |
|
import java.util.Locale; |
|
import java.util.Map; |
|
import java.util.Objects; |
|
import java.util.concurrent.Callable; |
|
import java.util.concurrent.ConcurrentHashMap; |
|
import java.util.concurrent.ExecutorService; |
|
import java.util.concurrent.Executors; |
|
import java.util.concurrent.Future; |
|
import java.util.concurrent.ThreadFactory; |
|
import java.util.concurrent.TimeUnit; |
|
import java.util.regex.Matcher; |
|
import java.util.regex.Pattern; |
|
|
|
import javax.net.ssl.HttpsURLConnection; |
|
import javax.swing.ImageIcon; |
|
|
|
import net.sourceforge.plantuml.StringUtils; |
|
import net.sourceforge.plantuml.security.authentication.SecurityAccessInterceptor; |
|
import net.sourceforge.plantuml.security.authentication.SecurityAuthentication; |
|
import net.sourceforge.plantuml.security.authentication.SecurityCredentials; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SURL { |
|
|
|
|
|
|
|
|
|
public static final String WITHOUT_AUTHENTICATION = SecurityUtils.NO_CREDENTIALS; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Pattern PATTERN_USERINFO = Pattern.compile("(^https?://)([-_0-9a-zA-Z]+@)([^@]*)"); |
|
|
|
private static final ExecutorService EXE = Executors.newCachedThreadPool(new ThreadFactory() { |
|
public Thread newThread(Runnable r) { |
|
final Thread t = Executors.defaultThreadFactory().newThread(r); |
|
t.setDaemon(true); |
|
return t; |
|
} |
|
}); |
|
|
|
private static final Map<String, Long> BAD_HOSTS = new ConcurrentHashMap<String, Long>(); |
|
|
|
|
|
|
|
|
|
private final URL internal; |
|
|
|
|
|
|
|
|
|
private final String securityIdentifier; |
|
|
|
private SURL(URL url, String securityIdentifier) { |
|
this.internal = Objects.requireNonNull(url); |
|
this.securityIdentifier = Objects.requireNonNull(securityIdentifier); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static SURL create(String url) { |
|
if (url == null) |
|
return null; |
|
|
|
if (url.startsWith("http://") || url.startsWith("https://")) |
|
try { |
|
return create(new URL(url)); |
|
} catch (MalformedURLException e) { |
|
e.printStackTrace(); |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static SURL create(URL url) throws MalformedURLException { |
|
if (url == null) |
|
throw new MalformedURLException("URL cannot be null"); |
|
|
|
final String credentialId = url.getUserInfo(); |
|
|
|
if (credentialId == null || credentialId.indexOf(':') > 0) |
|
|
|
|
|
return new SURL(url, WITHOUT_AUTHENTICATION); |
|
else if (SecurityUtils.existsSecurityCredentials(credentialId)) |
|
|
|
return new SURL(removeUserInfo(url), credentialId); |
|
else |
|
return new SURL(url, WITHOUT_AUTHENTICATION); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static SURL createWithoutUser(URL url) throws MalformedURLException { |
|
return new SURL(removeUserInfo(url), WITHOUT_AUTHENTICATION); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void resetBadHosts() { |
|
BAD_HOSTS.clear(); |
|
} |
|
|
|
@Override |
|
public String toString() { |
|
return internal.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
private boolean isUrlOk() { |
|
if (SecurityUtils.getSecurityProfile() == SecurityProfile.SANDBOX) |
|
|
|
return false; |
|
|
|
if (SecurityUtils.getSecurityProfile() == SecurityProfile.LEGACY) |
|
return true; |
|
|
|
if (SecurityUtils.getSecurityProfile() == SecurityProfile.UNSECURE) |
|
|
|
return true; |
|
|
|
if (isInUrlAllowList()) |
|
return true; |
|
|
|
if (SecurityUtils.getSecurityProfile() == SecurityProfile.INTERNET) { |
|
if (forbiddenURL(cleanPath(internal.toString()))) |
|
return false; |
|
|
|
final int port = internal.getPort(); |
|
|
|
return port == 80 || port == 443 || port == -1; |
|
} |
|
return false; |
|
} |
|
|
|
private boolean forbiddenURL(String full) { |
|
if (full.matches("^https?://[.0-9]+/.*")) |
|
return true; |
|
if (full.matches("^https?://[^.]+/.*")) |
|
return true; |
|
return false; |
|
} |
|
|
|
private boolean isInUrlAllowList() { |
|
final String full = cleanPath(internal.toString()); |
|
for (String allow : getUrlAllowList()) |
|
if (full.startsWith(cleanPath(allow))) |
|
return true; |
|
|
|
return false; |
|
} |
|
|
|
private String cleanPath(String path) { |
|
|
|
|
|
path = removeUserInfoFromUrlPath(path); |
|
path = path.trim().toLowerCase(Locale.US); |
|
|
|
path = path.replace(":80/", ""); |
|
path = path.replace(":443/", ""); |
|
return path; |
|
} |
|
|
|
private List<String> getUrlAllowList() { |
|
final String env = SecurityUtils.getenv(SecurityUtils.ALLOWLIST_URL); |
|
if (env == null) |
|
return Collections.emptyList(); |
|
|
|
return Arrays.asList(StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(env).split(";")); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] getBytes() { |
|
if (isUrlOk() == false) |
|
return null; |
|
|
|
final SecurityCredentials credentials = SecurityUtils.loadSecurityCredentials(securityIdentifier); |
|
final SecurityAuthentication authentication = SecurityUtils.getAuthenticationManager(credentials) |
|
.create(credentials); |
|
try { |
|
final String host = internal.getHost(); |
|
final Long bad = BAD_HOSTS.get(host); |
|
if (bad != null) { |
|
if ((System.currentTimeMillis() - bad) < 1000L * 60) |
|
return null; |
|
BAD_HOSTS.remove(host); |
|
} |
|
|
|
try { |
|
final Future<byte[]> result = EXE |
|
.submit(requestWithGetAndResponse(internal, credentials.getProxy(), authentication, null)); |
|
final byte[] data = result.get(SecurityUtils.getSecurityProfile().getTimeout(), TimeUnit.MILLISECONDS); |
|
if (data != null) |
|
return data; |
|
|
|
} catch (Exception e) { |
|
System.err.println("issue " + host + " " + e); |
|
} |
|
|
|
BAD_HOSTS.put(host, System.currentTimeMillis()); |
|
return null; |
|
} finally { |
|
|
|
credentials.eraseCredentials(); |
|
authentication.eraseCredentials(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private byte[] getBytes(Proxy proxy, SecurityAuthentication authentication, Map<String, Object> headers) { |
|
if (isUrlOk() == false) |
|
return null; |
|
|
|
final Future<byte[]> result = EXE.submit(requestWithGetAndResponse(internal, proxy, authentication, headers)); |
|
|
|
try { |
|
return result.get(SecurityUtils.getSecurityProfile().getTimeout(), TimeUnit.MILLISECONDS); |
|
} catch (Exception e) { |
|
System.err.println("SURL response issue to " + internal.getHost() + " " + e); |
|
return null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] getBytesOnPost(Proxy proxy, SecurityAuthentication authentication, String data, |
|
Map<String, Object> headers) { |
|
if (isUrlOk() == false) |
|
return null; |
|
|
|
final Future<byte[]> result = EXE |
|
.submit(requestWithPostAndResponse(internal, proxy, authentication, data, headers)); |
|
|
|
try { |
|
return result.get(SecurityUtils.getSecurityProfile().getTimeout(), TimeUnit.MILLISECONDS); |
|
} catch (Exception e) { |
|
System.err.println("SURL response issue to " + internal.getHost() + " " + e); |
|
return null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Callable<byte[]> requestWithGetAndResponse(final URL url, final Proxy proxy, |
|
final SecurityAuthentication authentication, final Map<String, Object> headers) { |
|
return new Callable<byte[]>() { |
|
|
|
private HttpURLConnection openConnection(final URL url) throws IOException { |
|
|
|
final URLConnection connection = proxy == null ? url.openConnection() : url.openConnection(proxy); |
|
if (connection == null) |
|
return null; |
|
|
|
final HttpURLConnection http = (HttpURLConnection) connection; |
|
|
|
applyEndpointAccessAuthentication(http, authentication); |
|
applyAdditionalHeaders(http, headers); |
|
return http; |
|
} |
|
|
|
public byte[] call() throws IOException { |
|
HttpURLConnection http = openConnection(url); |
|
final int responseCode = http.getResponseCode(); |
|
|
|
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP |
|
|| responseCode == HttpURLConnection.HTTP_MOVED_PERM) { |
|
final String newUrl = http.getHeaderField("Location"); |
|
http = openConnection(new URL(newUrl)); |
|
} |
|
|
|
return retrieveResponseAsBytes(http); |
|
} |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Callable<byte[]> requestWithPostAndResponse(final URL url, final Proxy proxy, |
|
final SecurityAuthentication authentication, final String data, final Map<String, Object> headers) { |
|
return new Callable<byte[]>() { |
|
public byte[] call() throws IOException { |
|
|
|
final URLConnection connection = proxy == null ? url.openConnection() : url.openConnection(proxy); |
|
if (connection == null) |
|
return null; |
|
|
|
final boolean withContent = StringUtils.isNotEmpty(data); |
|
|
|
final HttpURLConnection http = (HttpURLConnection) connection; |
|
http.setRequestMethod("POST"); |
|
if (withContent) |
|
http.setDoOutput(true); |
|
|
|
applyEndpointAccessAuthentication(http, authentication); |
|
applyAdditionalHeaders(http, headers); |
|
|
|
final Charset charSet = extractCharset(http.getRequestProperty("Content-Type")); |
|
|
|
if (withContent) |
|
sendRequestAsBytes(http, data.getBytes(charSet != null ? charSet : StandardCharsets.UTF_8)); |
|
|
|
return retrieveResponseAsBytes(http); |
|
} |
|
}; |
|
} |
|
|
|
private static Charset extractCharset(String contentType) { |
|
if (StringUtils.isEmpty(contentType)) |
|
return null; |
|
|
|
final Matcher matcher = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)").matcher(contentType); |
|
if (matcher.find()) |
|
try { |
|
return Charset.forName(matcher.group(1)); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static byte[] retrieveResponseAsBytes(HttpURLConnection connection) throws IOException { |
|
final int responseCode = connection.getResponseCode(); |
|
if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) { |
|
try (InputStream input = connection.getInputStream()) { |
|
return retrieveData(input); |
|
} |
|
} else { |
|
try (InputStream error = connection.getErrorStream()) { |
|
final byte[] bytes = retrieveData(error); |
|
throw new IOException( |
|
"HTTP error " + responseCode + " with " + new String(bytes, StandardCharsets.UTF_8)); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static byte[] retrieveData(InputStream input) throws IOException { |
|
final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
final byte[] buffer = new byte[1024]; |
|
int read; |
|
while ((read = input.read(buffer)) > 0) { |
|
out.write(buffer, 0, read); |
|
} |
|
out.close(); |
|
return out.toByteArray(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void sendRequestAsBytes(HttpURLConnection connection, byte[] data) throws IOException { |
|
connection.setFixedLengthStreamingMode(data.length); |
|
try (OutputStream os = connection.getOutputStream()) { |
|
os.write(data); |
|
} |
|
} |
|
|
|
public InputStream openStream() { |
|
if (isUrlOk()) { |
|
final byte[] data = getBytes(); |
|
if (data != null) |
|
return new ByteArrayInputStream(data); |
|
|
|
} |
|
return null; |
|
} |
|
|
|
public BufferedImage readRasterImageFromURL() { |
|
if (isUrlOk()) |
|
try { |
|
final byte[] bytes = getBytes(); |
|
if (bytes == null || bytes.length == 0) |
|
return null; |
|
final ImageIcon tmp = new ImageIcon(bytes); |
|
return SecurityUtils.readRasterImage(tmp); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isAuthorizationConfigured() { |
|
return WITHOUT_AUTHENTICATION.equals(securityIdentifier) == false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void applyEndpointAccessAuthentication(URLConnection http, SecurityAuthentication authentication) { |
|
if (authentication.isPublic()) |
|
|
|
return; |
|
|
|
if (http instanceof HttpsURLConnection || SecurityUtils.isNonSSLAuthenticationAllowed()) { |
|
SecurityAccessInterceptor accessInterceptor = SecurityUtils.getAccessInterceptor(authentication); |
|
accessInterceptor.apply(authentication, http); |
|
} else { |
|
|
|
|
|
throw new IllegalStateException( |
|
"The transport of authentication data over an unencrypted http connection is not allowed"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void applyAdditionalHeaders(URLConnection http, Map<String, Object> headers) { |
|
if (headers == null || headers.isEmpty()) |
|
return; |
|
|
|
for (Map.Entry<String, Object> header : headers.entrySet()) { |
|
final Object value = header.getValue(); |
|
if (value instanceof String) |
|
http.setRequestProperty(header.getKey(), (String) value); |
|
else if (value instanceof List) |
|
for (Object item : (List<?>) value) |
|
if (item != null) |
|
http.addRequestProperty(header.getKey(), item.toString()); |
|
|
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static URL removeUserInfo(URL url) throws MalformedURLException { |
|
return new URL(removeUserInfoFromUrlPath(url.toExternalForm())); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String removeUserInfoFromUrlPath(String url) { |
|
|
|
final Matcher matcher = PATTERN_USERINFO.matcher(url); |
|
if (matcher.find()) |
|
return matcher.replaceFirst("$1$3"); |
|
|
|
return url; |
|
} |
|
} |
|
|