JAL-3353 Change to build.gradle to use the file:// enabled getdown-launcher-local...
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / util / HostWhitelist.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.util;
7
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.util.List;
11
12 import com.threerings.getdown.data.Build;
13
14 /**
15  * Optional support for compiling a URL host whitelist into the Getdown JAR.
16  * Useful if you're on the paranoid end of the security spectrum.
17  *
18  * @see Build#hostWhitelist()
19  */
20 public final class HostWhitelist
21 {
22     /**
23      * Verifies that the specified URL should be accessible, per the built-in host whitelist.
24      * See {@link Build#hostWhitelist()} and {@link #verify(List,URL)}.
25      */
26     public static URL verify (URL url) throws MalformedURLException
27     {
28         
29       
30         return verify(Build.hostWhitelist(), url);
31     }
32
33     /**
34      * Verifies that the specified URL should be accessible, per the supplied host whitelist.
35      * If the URL should not be accessible, this method throws a {@link MalformedURLException}.
36      * If the URL should be accessible, this method simply returns the {@link URL} passed in.
37      */
38     public static URL verify (List<String> hosts, URL url) throws MalformedURLException
39     {
40         if (url == null || hosts.isEmpty()) {
41             // either there is no URL to validate or no whitelist was configured
42             return url;
43         }
44
45         String urlHost = url.getHost();
46         String protocol = url.getProtocol();
47         
48         if (ALLOW_LOCATOR_FILE_PROTOCOL && protocol.equals("file") && urlHost.equals("")) {
49           return url;
50         }
51         
52         for (String host : hosts) {
53             String regex = host.replace(".", "\\.").replace("*", ".*");
54             if (urlHost.matches(regex)) {
55                 return url;
56             }
57         }
58
59         throw new MalformedURLException(
60             "The host for the specified URL (" + url + ") is not in the host whitelist: " + hosts);
61     }
62     private static final boolean ALLOW_LOCATOR_FILE_PROTOCOL = Build.allowLocatorFileProtocol();
63 }