dalelane commented on code in PR #1188:
URL: 
https://github.com/apache/flink-kubernetes-operator/pull/1188#discussion_r4800229617


##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/artifact/HttpArtifactFetcher.java:
##########
@@ -264,4 +293,112 @@ private static boolean isRedirect(int status) {
                 || status == 307 // Temporary Redirect
                 || status == 308; // Permanent Redirect
     }
+
+    @VisibleForTesting
+    static Optional<InetAddress> validateHop(
+            String uri, Collection<String> allowedSchemes, boolean 
disallowRestrictedHosts)
+            throws IOException {
+        var validation =
+                JarUriValidationUtils.validateAndResolve(
+                        uri, allowedSchemes, disallowRestrictedHosts);
+        if (validation.error() != null) {
+            throw new IOException(
+                    "Refusing to fetch artifact from '" + uri + "': " + 
validation.error());
+        }
+        return Optional.ofNullable(validation.checkedAddress());
+    }
+
+    private static Proxy selectProxy(String uri) {
+        var selector = ProxySelector.getDefault();
+        var proxies = selector == null ? List.<Proxy>of() : 
selector.select(URI.create(uri));
+        return proxies.isEmpty() ? Proxy.NO_PROXY : proxies.get(0);
+    }
+
+    private static HttpURLConnection openPinnedConnection(URL url, InetAddress 
checkedAddress)
+            throws IOException {
+        // Drop the hostname so the JDK cannot resolve it again.
+        var address = InetAddress.getByAddress(checkedAddress.getAddress());
+        if ("https".equalsIgnoreCase(url.getProtocol())) {
+            var conn = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);
+            conn.setSSLSocketFactory(
+                    new PinnedSSLSocketFactory(conn.getSSLSocketFactory(), 
address));
+            return conn;
+        }
+        // Plain HTTP has no socket hook, so treat the address as a proxy 
(absolute-form request,

Review Comment:
   I'm still thinking about this one :) 
   
   I asked Claude to make me a simple recreate, and I used my normal goto for 
quick-and-dirty HTTP servers (`python3 -m http.server`)
   
   ```java
   public class PinnedFetchCheck {
       public static void main(String[] args) throws Exception {
           int port = Integer.parseInt(args[0]);
           URL url = URI.create("http://localhost:"; + port + 
"/job.jar").toURL();
   
           HttpURLConnection normal = (HttpURLConnection) url.openConnection();
           System.out.println("normal connection: " + normal.getResponseCode());
   
           InetAddress checked = InetAddress.getByAddress(new byte[] {127, 0, 
0, 1});
           Proxy proxy = new Proxy(Proxy.Type.HTTP, new 
InetSocketAddress(checked, port));
           HttpURLConnection pinned = (HttpURLConnection) 
url.openConnection(proxy);
           System.out.println("pinned connection: " + pinned.getResponseCode());
       }
   }
   ```
   
   ```sh
   mkdir www && echo hello > www/job.jar
   (cd www && python3 -m http.server 8000 --bind 127.0.0.1) &
   java PinnedFetchCheck.java 8000
   ```
   
   The results were:
   ```
   normal connection: 200
   pinned connection: 404
   ```
   
   I'm not saying I think there are many people hosting jars in a python 
http.server, but the point is that this may trip up some older or simpler HTTP 
servers. Realistically, someone who is enabling plain http fetching is probably 
not super security conscious. Speculating, I wonder if they'd more likely value 
the ability to fetch from simple servers over the security value - so maybe we 
make this optional? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to