shangeyao commented on code in PR #4419:
URL: https://github.com/apache/streampark/pull/4419#discussion_r3540613875


##########
streampark-common/src/main/java/org/apache/streampark/common/util/SafePathUtils.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.streampark.common.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/** Path resolution helpers for user-supplied file locations. */
+public final class SafePathUtils {
+
+    private SafePathUtils() {}
+
+    public static Path resolveConfigPath(String filename) {
+        validateConfigFilename(filename);
+        Path base = Paths.get("").toAbsolutePath().normalize();
+        Path resolved = base.resolve(filename).normalize();
+        if (!Paths.get(filename).isAbsolute() && !resolved.startsWith(base)) {
+            throw new IllegalArgumentException("invalid file path: " + 
filename);
+        }
+        return resolved;
+    }
+
+    public static InputStream openConfigFile(String filename) throws 
IOException {
+        validateConfigFilename(filename);
+        Path base = Paths.get("").toAbsolutePath().normalize();
+        Path resolved = base.resolve(filename).normalize();
+        if (!Paths.get(filename).isAbsolute() && !resolved.startsWith(base)) {
+            throw new IOException("invalid file path: " + filename);
+        }
+        return Files.newInputStream(resolved);
+    }
+
+    public static String readConfigFile(String filename) throws IOException {
+        validateConfigFilename(filename);
+        Path base = Paths.get("").toAbsolutePath().normalize();
+        Path resolved = base.resolve(filename).normalize();
+        if (!Paths.get(filename).isAbsolute() && !resolved.startsWith(base)) {
+            throw new IOException("invalid file path: " + filename);
+        }
+        return Files.readString(resolved, StandardCharsets.UTF_8);
+    }
+
+    public static Path resolveJarPath(java.net.URL jar) throws IOException {
+        try {
+            String location = jar.toString();
+            if (location.contains("..")) {
+                throw new IOException("JAR file path is invalid " + jar);
+            }
+            Path base = Paths.get("").toAbsolutePath().normalize();
+            return base.resolve(Paths.get(jar.toURI())).normalize();
+        } catch (Exception e) {
+            throw new IOException("JAR file path is invalid " + jar, e);
+        }
+    }
+
+    public static InputStream openJarFile(java.net.URL jar) throws IOException 
{
+        String location = jar.toString();
+        if (location.contains("..")) {
+            throw new IOException("JAR file path is invalid " + jar);
+        }
+        Path base = Paths.get("").toAbsolutePath().normalize();
+        Path resolved = base.resolve(Paths.get(jar.toURI())).normalize();
+        return Files.newInputStream(resolved);

Review Comment:
   Fixed in `752fa8a68`: `SafePathUtils` co-locates `Path.resolve()` validation 
with file I/O.



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