Skip to content

Commit

Permalink
Change AbsolutePath to CanonicalPath (#1901)
Browse files Browse the repository at this point in the history
I am not 100% sure if this would fix BentoBoxWorld/BSkyBlock#451

However, searching google I found that others had a similar issue with the absolute path and they changed it to canonical. 

As I was not able to reproduce the main reported issue, I do not know if this fixes it.
  • Loading branch information
BONNe authored Jan 2, 2022
1 parent 7088a27 commit d1eb175
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Blueprint loadBlueprint(String fileName) throws IOException {
plugin.logError(LOAD_ERROR + zipFile.getName());
throw new IOException(LOAD_ERROR + zipFile.getName());
}
unzip(zipFile.getAbsolutePath());
unzip(zipFile.getCanonicalPath());
File file = new File(blueprintFolder, BlueprintsManager.sanitizeFileName(fileName));
if (!file.exists()) {
plugin.logError(LOAD_ERROR + file.getName());
Expand Down Expand Up @@ -194,7 +194,7 @@ private void unzip(final String zipFilePath) throws IOException {
if (!entry.isDirectory()) {
unzipFiles(zipInputStream, filePath);
} else {
if (!filePath.startsWith(blueprintFolder.getAbsolutePath())) {
if (!filePath.startsWith(blueprintFolder.getCanonicalPath())) {
throw new IOException("Entry is outside of the target directory");
}
Files.createDirectories(filePath);
Expand All @@ -207,10 +207,10 @@ private void unzip(final String zipFilePath) throws IOException {
}

private void unzipFiles(final ZipInputStream zipInputStream, final Path unzipFilePath) throws IOException {
if (!unzipFilePath.toAbsolutePath().toString().startsWith(blueprintFolder.getAbsolutePath())) {
if (!unzipFilePath.toFile().getCanonicalPath().startsWith(blueprintFolder.getCanonicalPath())) {
throw new IOException("Entry is outside of the target directory");
}
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(unzipFilePath.toAbsolutePath().toString()))) {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(unzipFilePath.toFile().getCanonicalPath()))) {
byte[] bytesIn = new byte[1024];
int read;
while ((read = zipInputStream.read(bytesIn)) != -1) {
Expand All @@ -220,7 +220,7 @@ private void unzipFiles(final ZipInputStream zipInputStream, final Path unzipFil
}

private void zip(File targetFile) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + BlueprintsManager.BLUEPRINT_SUFFIX))) {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getCanonicalPath() + BlueprintsManager.BLUEPRINT_SUFFIX))) {
zipOutputStream.putNextEntry(new ZipEntry(targetFile.getName()));
try (FileInputStream inputStream = new FileInputStream(targetFile)) {
final byte[] buffer = new byte[1024];
Expand Down

0 comments on commit d1eb175

Please sign in to comment.