Copilot commented on code in PR #11354:
URL: https://github.com/apache/gravitino/pull/11354#discussion_r3378346336
##########
core/src/main/java/org/apache/gravitino/job/JobManager.java:
##########
@@ -808,6 +854,10 @@ static String fetchFileFromUri(String uri, File
stagingDir, int timeoutInMs) {
case "http":
case "https":
case "ftp":
+ RemoteUriValidator.validate(
+ fileUri,
+ blockUnsafeAddressForRemoteUri,
+ String.format("'%s' to false",
Configs.JOB_REMOTE_URI_BLOCK_UNSAFE_ADDRESS_KEY));
FileUtils.copyURLToFile(fileUri.toURL(), destFile, timeoutInMs,
timeoutInMs);
break;
Review Comment:
`fetchFileFromUri()` validates the hostname and then calls
`FileUtils.copyURLToFile(fileUri.toURL(), ...)`, which will typically perform
its own DNS resolution again. This leaves a DNS-rebinding/TOCTOU gap: a
hostname can resolve to a public IP during validation and then rebind to a
private/loopback IP when the connection is opened.
##########
common/src/main/java/org/apache/gravitino/utils/RemoteUriValidator.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.utils;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.URI;
+
+/** Validates remote URI hosts before server-side downloads. */
+public final class RemoteUriValidator {
+
+ private RemoteUriValidator() {}
+
+ /**
+ * Resolves the host in the given URI and rejects unsafe addresses when
blocking is enabled.
+ *
+ * @param uri The remote URI to validate.
+ * @param blockUnsafeAddress Whether unsafe addresses should be blocked.
+ * @param blockUnsafeAddressHint The configuration hint that disables unsafe
address blocking.
+ * @throws IOException If host resolution fails.
+ * @throws IllegalArgumentException If the URI has no host or resolves to an
unsafe address.
+ */
+ public static void validate(URI uri, boolean blockUnsafeAddress, String
blockUnsafeAddressHint)
+ throws IOException {
+ String host = uri.getHost();
+ if (host == null) {
+ throw new IllegalArgumentException("URI has no host: " + uri);
+ }
+
+ if (!blockUnsafeAddress) {
+ return;
+ }
+
+ InetAddress[] addresses = InetAddress.getAllByName(host);
+ for (InetAddress address : addresses) {
+ if (isUnsafeAddress(address)) {
+ throw new IllegalArgumentException(
+ String.format(
+ "URI '%s' resolves to blocked address %s from the Gravitino
server side. "
+ + "Access to local, private, link-local, multicast,
unspecified, and cloud "
+ + "metadata addresses is disabled by default to prevent
SSRF. If this URI is "
+ + "trusted and this access is required, set %s.",
+ uri, address.getHostAddress(), blockUnsafeAddressHint));
+ }
Review Comment:
`RemoteUriValidator.validate()` performs a DNS lookup and then callers open
a fresh connection via `FileUtils.copyURLToFile(uri.toURL(), ...)`, which will
resolve DNS again. This creates a TOCTOU/DNS-rebinding window where an attacker
can pass validation and then rebind the hostname to a blocked address before
the actual connection, bypassing the SSRF protection.
--
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]