Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 5b51e75

Browse files
committed
Optimized DirectoryEntry.java and located a security flaw
1 parent bccd9c9 commit 5b51e75

1 file changed

Lines changed: 17 additions & 21 deletions

File tree

src/ktt/lib/httpserver/handler/DirectoryEntry.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class DirectoryEntry {
5050

5151
if(isFilesPreloaded){
5252
if(!isWalkthrough){
53-
for(final File file : Objects.requireNonNull(directory.listFiles())){
53+
final File[] listFiles = directory.listFiles();
54+
for(final File file : (listFiles == null) ? new File[0] : listFiles){
5455
files.put(
5556
getContext(adapter.getName(file)),
5657
new FileEntry(file, true, adapter)
@@ -61,20 +62,17 @@ class DirectoryEntry {
6162

6263
Files.walk(dirPath).filter(path -> path.toFile().isDirectory()).forEach(path -> {
6364
final File pathFile = path.toFile();
64-
6565
final String rel = dirPath.relativize(path).toString();
66-
final File[] files = pathFile.listFiles();
67-
68-
if(files == null) return;
6966

70-
for(final File file : files){
67+
final File[] listFiles = pathFile.listFiles();
68+
for(final File file : (listFiles == null) ? new File[0] : listFiles){
7169
try{
7270
DirectoryEntry.this.files.put(
7371
getContext(rel + "/" + adapter.getName(file)),
7472
new FileEntry(file, true, adapter)
7573
);
76-
}catch(FileNotFoundException e){
77-
// failed
74+
}catch(final FileNotFoundException ignored){
75+
// #listFiles assume that all files exist, so this exception should never occur unless the user modified the directory mid-read.
7876
}
7977
}
8078
});
@@ -156,26 +154,24 @@ public final File getFile(final String path){
156154
for(final String key : files.keySet())
157155
if(rel.startsWith(key) && key.startsWith(match))
158156
match = key;
159-
if(!match.isEmpty()){
160-
return files.get(match).getFile();
161-
}else{
162-
return null;
163-
}
157+
return !match.isEmpty() ? files.get(match).getFile() : null;
164158
}else{
165159
if(isWalkthrough){
166-
final File parent = new File(directory.getAbsolutePath() + path).getParentFile();
167-
final File target = new File(parent.getAbsolutePath() + path.substring(0,path.lastIndexOf('/')));
168-
return target.exists() ? target : null;
169-
}else{
170-
final File[] files = directory.listFiles(pathname -> !pathname.isDirectory());
160+
final File parent = new File(directory.getAbsolutePath() + path).getParentFile(); // todo // ⚠ security flaw: user may have access to folders outside of directory by using ../
161+
final String name = path.substring(path.lastIndexOf('/'));
162+
final File[] listFiles = parent.listFiles(pathname -> !pathname.isDirectory());
171163

172-
if(files == null) return null;
164+
for(final File file : (listFiles == null) ? new File[0] : listFiles)
165+
if(adapter.getName(file).equalsIgnoreCase(name))
166+
return file;
167+
}else{
168+
final File[] listFiles = directory.listFiles(pathname -> !pathname.isDirectory());
173169

174-
for(final File file : files)
170+
for(final File file : (listFiles == null) ? new File[0] : listFiles)
175171
if(adapter.getName(file).equalsIgnoreCase(path))
176172
return file;
177-
return null;
178173
}
174+
return null;
179175
}
180176
}
181177

0 commit comments

Comments
 (0)