Skip to content

Commit

Permalink
do not allow write snapshot file outside of parent dir (sofastack#490)
Browse files Browse the repository at this point in the history
* do not allow write snapshot file outside of parent dir

* do not allow write snapshot file outside of parent dir

* do not allow write snapshot file outside of parent dir
  • Loading branch information
fengjiachun authored Jul 21, 2020
1 parent 31a9d71 commit 5ca7134
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ void copyFile(final String fileName) throws IOException, InterruptedException {
LOG.info("Skipped downloading {}", fileName);
return;
}
if (!checkFile(fileName)) {
return;
}
final String filePath = this.writer.getPath() + File.separator + fileName;
final Path subPath = Paths.get(filePath);
if (!subPath.equals(subPath.getParent()) && !subPath.getParent().getFileName().toString().equals(".")) {
Expand Down Expand Up @@ -181,6 +184,28 @@ void copyFile(final String fileName) throws IOException, InterruptedException {
}
}

private boolean checkFile(final String fileName) {
try {
final String parentCanonicalPath = Paths.get(this.writer.getPath()).toFile().getCanonicalPath();
final Path filePath = Paths.get(parentCanonicalPath, fileName);
final File file = filePath.toFile();
final String fileAbsolutePath = file.getAbsolutePath();
final String fileCanonicalPath = file.getCanonicalPath();
if (!fileAbsolutePath.equals(fileCanonicalPath)) {
LOG.error("File[{}] are not allowed to be created outside of directory[{}].", fileAbsolutePath,
fileCanonicalPath);
setError(RaftError.EIO, "File[%s] are not allowed to be created outside of directory.",
fileAbsolutePath, fileCanonicalPath);
return false;
}
} catch (final IOException e) {
LOG.error("Failed to check file: {}, writer path: {}.", fileName, this.writer.getPath(), e);
setError(RaftError.EIO, "Failed to check file: {}, writer path: {}.", fileName, this.writer.getPath());
return false;
}
return true;
}

private void loadMetaTable() throws InterruptedException {
final ByteBufferCollector metaBuf = ByteBufferCollector.allocate(0);
Session session = null;
Expand Down

0 comments on commit 5ca7134

Please sign in to comment.