You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Accessing paths controlled by users can allow an attacker to access unexpected resources. This can result in sensitive information being revealed or deleted, or an attacker being able to influence behavior by modifying unexpected files. Paths that are naively constructed from data controlled by a user may be absolute paths, or may contain unexpected special characters such as "..". Such a path could point anywhere on the file system.
POC
In this a file name is read from a java.net.Socket and then used to access a file and send it back over the socket. However, a malicious user could enter a file name anywhere on the file system, such as "/etc/passwd" or "../../../etc/passwd".
publicvoidsendUserFile(Socketsock, Stringuser) {
BufferedReaderfilenameReader = newBufferedReader(
newInputStreamReader(sock.getInputStream(), "UTF-8"));
Stringfilename = filenameReader.readLine();
// BAD: read from a file without checking its pathBufferedReaderfileReader = newBufferedReader(newFileReader(filename));
StringfileLine = fileReader.readLine();
while(fileLine != null) {
sock.getOutputStream().write(fileLine.getBytes());
fileLine = fileReader.readLine();
}
}
If the input should only be a file name, you can check that it doesn't contain any path separators or ".." sequences.
publicvoidsendUserFileGood(Socketsock, Stringuser) {
BufferedReaderfilenameReader = newBufferedReader(
newInputStreamReader(sock.getInputStream(), "UTF-8"));
Stringfilename = filenameReader.readLine();
// GOOD: ensure that the filename has no path separators or parent directory referencesif (filename.contains("..") || filename.contains("/") || filename.contains("\\")) {
thrownewIllegalArgumentException("Invalid filename");
}
BufferedReaderfileReader = newBufferedReader(newFileReader(filename));
StringfileLine = fileReader.readLine();
while(fileLine != null) {
sock.getOutputStream().write(fileLine.getBytes());
fileLine = fileReader.readLine();
}
}
If the input should be within a specific directory, you can check that the resolved path is still contained within that directory.
publicvoidsendUserFileGood(Socketsock, Stringuser) {
BufferedReaderfilenameReader = newBufferedReader(
newInputStreamReader(sock.getInputStream(), "UTF-8"));
Stringfilename = filenameReader.readLine();
PathpublicFolder = Paths.get("/home/" + user + "/public").normalize().toAbsolutePath();
PathfilePath = publicFolder.resolve(filename).normalize().toAbsolutePath();
// GOOD: ensure that the path stays within the public folderif (!filePath.startsWith(publicFolder + File.separator)) {
thrownewIllegalArgumentException("Invalid filename");
}
BufferedReaderfileReader = newBufferedReader(newFileReader(filePath.toString()));
StringfileLine = fileReader.readLine();
while(fileLine != null) {
sock.getOutputStream().write(fileLine.getBytes());
fileLine = fileReader.readLine();
}
}
h2o-3/h2o-extensions/xgboost/src/main/java/hex/tree/xgboost/remote/RemoteXGBoostUploadServlet.java
Line 27 in e6a314b
h2o-3/h2o-extensions/xgboost/src/main/java/hex/tree/xgboost/remote/RemoteXGBoostUploadServlet.java
Lines 22 to 30 in e6a314b
Accessing paths controlled by users can allow an attacker to access unexpected resources. This can result in sensitive information being revealed or deleted, or an attacker being able to influence behavior by modifying unexpected files. Paths that are naively constructed from data controlled by a user may be absolute paths, or may contain unexpected special characters such as "..". Such a path could point anywhere on the file system.
POC
In this a file name is read from a
java.net.Socket
and then used to access a file and send it back over the socket. However, a malicious user could enter a file name anywhere on the file system, such as "/etc/passwd" or "../../../etc/passwd".If the input should only be a file name, you can check that it doesn't contain any path separators or ".." sequences.
If the input should be within a specific directory, you can check that the resolved path is still contained within that directory.
References
Path Traversal
CWE-22
CWE-23
CWE-36
CWE-73
The text was updated successfully, but these errors were encountered: