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


##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/artifact/HttpArtifactFetcher.java:
##########
@@ -93,22 +120,26 @@ public File fetch(
                                 + uri
                                 + "'");
             }
-            var validationError =
-                    JarUriValidationUtils.validateJarURI(
-                            currentUri, allowedSchemes, 
disallowRestrictedHosts);
-            if (validationError.isPresent()) {
-                throw new IOException(
-                        "Refusing to fetch artifact from '"
-                                + currentUri
-                                + "': "
-                                + validationError.get());
-            }
+            var pinnedAddress =
+                    uriValidator.validate(currentUri, allowedSchemes, 
disallowRestrictedHosts);
 
             currentUrl = new URL(currentUri);
             if (originalUrl == null) {
                 originalUrl = currentUrl;
             }
-            conn = (HttpURLConnection) currentUrl.openConnection();
+            if (pinnedAddress.isEmpty()) {
+                conn = (HttpURLConnection) currentUrl.openConnection();
+            } else {
+                // Only direct connections are pinned. An HTTP proxy resolves 
the host itself; with
+                // a SOCKS proxy the JDK resolves it locally and the 
connection is not pinned. Only
+                // the selector's first choice is used: the JDK would move on 
to the next one if a
+                // proxy fails, and a direct fallback would connect by 
hostname.
+                var proxy = selectProxy(currentUri);
+                conn =
+                        proxy.type() == Proxy.Type.DIRECT
+                                ? openPinnedConnection(currentUrl, 
pinnedAddress.get())
+                                : (HttpURLConnection) 
currentUrl.openConnection(proxy);

Review Comment:
   Passing the proxy explicitly also bypasses `ProxySelector.connectFailed`, 
not just the unsafe direct fallback. A selector that removes or quarantines 
failed proxies in that callback is never informed, so it can keep returning the 
dead first proxy on every subsequent reconciliation even when a healthy 
alternative is available. Preserve the selector and notify it when connecting 
to the selected real proxy fails, while still preventing unpinned direct 
fallback.



##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/artifact/HttpArtifactFetcher.java:
##########
@@ -264,4 +295,123 @@ 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.getError().isPresent()) {
+            throw new IOException(
+                    "Refusing to fetch artifact from '"
+                            + uri
+                            + "': "
+                            + validation.getError().get());
+        }
+        return validation.getCheckedAddress();
+    }
+
+    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 {
+        // An address from getAllByName() keeps the hostname, which the JDK 
would resolve again
+        // wherever it connects by host string, so connect by the raw IP only.
+        var address = InetAddress.getByAddress(checkedAddress.getAddress());
+        if ("https".equalsIgnoreCase(url.getProtocol())) {
+            // The URL keeps the hostname, so Host, SNI and the certificate 
check still use it; only
+            // the TCP socket goes to the checked address.
+            var conn = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);
+            conn.setSSLSocketFactory(
+                    new PinnedSSLSocketFactory(conn.getSSLSocketFactory(), 
address));
+            return conn;
+        }
+        // Plain HTTP has no socket hook, so the request goes to the address 
as if it were a proxy:
+        // in absolute form, which servers must accept (RFC 9112, section 
3.2.2), with Host still
+        // taken from the URL.
+        var port = url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
+        return (HttpURLConnection)
+                url.openConnection(
+                        new Proxy(Proxy.Type.HTTP, new 
InetSocketAddress(address, port)));
+    }
+
+    /**
+     * Validates one hop of a fetch, throwing if it is rejected, and returns 
the address to connect
+     * to, or empty if the hop is not pinned.
+     */
+    @FunctionalInterface
+    interface UriValidator {
+        Optional<InetAddress> validate(
+                String uri, Collection<String> allowedSchemes, boolean 
disallowRestrictedHosts)
+                throws IOException;
+    }
+
+    // HttpsClient creates the socket with the no-arg createSocket() and 
layers TLS on it with
+    // createSocket(Socket, ...). If layering fails it falls back to 
createSocket(host, port), which
+    // would bypass the checked address, so that and the other overloads throw.
+    private static final class PinnedSSLSocketFactory extends SSLSocketFactory 
{
+        private final SSLSocketFactory delegate;
+        private final InetAddress address;
+
+        PinnedSSLSocketFactory(SSLSocketFactory delegate, InetAddress address) 
{
+            this.delegate = delegate;
+            this.address = address;
+        }
+
+        @Override
+        public Socket createSocket() {
+            return new Socket() {

Review Comment:
   `new Socket()` can consult the default proxy selector again when it 
connects. If the selector returns DIRECT for the HTTPS URL but SOCKS for 
`socket://` destinations, this routes the supposedly direct pinned connection 
through that SOCKS proxy, breaking proxy-bypass rules and potentially making 
the fetch fail. Create the socket with `Proxy.NO_PROXY` so the earlier direct 
selection is honored.



##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/artifact/HttpArtifactFetcher.java:
##########
@@ -264,4 +295,123 @@ 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.getError().isPresent()) {
+            throw new IOException(
+                    "Refusing to fetch artifact from '"
+                            + uri
+                            + "': "
+                            + validation.getError().get());
+        }
+        return validation.getCheckedAddress();
+    }
+
+    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 {
+        // An address from getAllByName() keeps the hostname, which the JDK 
would resolve again
+        // wherever it connects by host string, so connect by the raw IP only.
+        var address = InetAddress.getByAddress(checkedAddress.getAddress());
+        if ("https".equalsIgnoreCase(url.getProtocol())) {
+            // The URL keeps the hostname, so Host, SNI and the certificate 
check still use it; only
+            // the TCP socket goes to the checked address.
+            var conn = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);
+            conn.setSSLSocketFactory(
+                    new PinnedSSLSocketFactory(conn.getSSLSocketFactory(), 
address));
+            return conn;
+        }
+        // Plain HTTP has no socket hook, so the request goes to the address 
as if it were a proxy:
+        // in absolute form, which servers must accept (RFC 9112, section 
3.2.2), with Host still
+        // taken from the URL.
+        var port = url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
+        return (HttpURLConnection)
+                url.openConnection(
+                        new Proxy(Proxy.Type.HTTP, new 
InetSocketAddress(address, port)));

Review Comment:
   For a plain-HTTP jarURI or redirect containing `user:pass@host`, this 
proxy-mode connection sends the userinfo in the absolute-form request target. 
That can expose credentials in the origin's access logs and cause strict 
servers to reject a fetch that previously worked. Rebuild the URL without 
userinfo before opening the pinned HTTP connection, preserving the hostname, 
port, path, and query.



-- 
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