github-advanced-security[bot] commented on code in PR #4420: URL: https://github.com/apache/streampark/pull/4420#discussion_r3540603913
########## 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); Review Comment: ## SonarCloud / I/O function calls should not be vulnerable to path injection attacks <!--SONAR_ISSUE_KEY:AZ8_Sj8wKYGzFWtirlZ3-->Change this code to not construct the path from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AZ8_Sj8wKYGzFWtirlZ3&open=AZ8_Sj8wKYGzFWtirlZ3&pullRequest=4420">SonarQube Cloud</a></p> [Show more details](https://github.com/apache/streampark/security/code-scanning/249) ########## 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: ## SonarCloud / I/O function calls should not be vulnerable to path injection attacks <!--SONAR_ISSUE_KEY:AZ8_Sj8wKYGzFWtirlZ2-->Change this code to not construct the path from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AZ8_Sj8wKYGzFWtirlZ2&open=AZ8_Sj8wKYGzFWtirlZ2&pullRequest=4420">SonarQube Cloud</a></p> [Show more details](https://github.com/apache/streampark/security/code-scanning/248) ########## 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); Review Comment: ## SonarCloud / I/O function calls should not be vulnerable to path injection attacks <!--SONAR_ISSUE_KEY:AZ8_Sj8wKYGzFWtirlZ1-->Change this code to not construct the path from user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AZ8_Sj8wKYGzFWtirlZ1&open=AZ8_Sj8wKYGzFWtirlZ1&pullRequest=4420">SonarQube Cloud</a></p> [Show more details](https://github.com/apache/streampark/security/code-scanning/247) -- 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]
