From 5ec79130464736958fb348314a01207183124e47 Mon Sep 17 00:00:00 2001 From: Madeline Miller Date: Sun, 21 May 2023 21:49:16 +1000 Subject: [PATCH] Fix symlink check --- .../src/main/java/com/sk89q/worldedit/WorldEdit.java | 4 +++- .../internal/schematic/backends/PollingSchematicsBackend.java | 3 +-- .../worldedit/internal/util/RecursiveDirectoryWatcher.java | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java index b9f6d495e7..c803df8d74 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -407,8 +407,10 @@ private File getSafeFileWithExtension(File dir, String filename, String extensio return new File(dir, filename); } + private static final java.util.regex.Pattern SAFE_FILENAME_REGEX = java.util.regex.Pattern.compile("^[A-Za-z0-9_\\- \\./\\\\'\\$@~!%\\^\\*\\(\\)\\[\\]\\+\\{\\},\\?]+\\.[A-Za-z0-9]+$"); + private boolean checkFilename(String filename) { - return filename.matches("^[A-Za-z0-9_\\- \\./\\\\'\\$@~!%\\^\\*\\(\\)\\[\\]\\+\\{\\},\\?]+\\.[A-Za-z0-9]+$"); + return SAFE_FILENAME_REGEX.matcher(filename).matches(); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/schematic/backends/PollingSchematicsBackend.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/schematic/backends/PollingSchematicsBackend.java index 05904a6dfd..d8dc827973 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/schematic/backends/PollingSchematicsBackend.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/schematic/backends/PollingSchematicsBackend.java @@ -33,7 +33,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; -import java.util.concurrent.locks.ReentrantReadWriteLock; /** * A backend that scans the folder tree, then caches the result for a certain amount of time. @@ -67,7 +66,7 @@ private List scanFolder(Path root) { try (DirectoryStream stream = Files.newDirectoryStream(root)) { for (Path path : stream) { - path = WorldEdit.getInstance().getSafeOpenFile(null, schematicRoot.toFile(), path.toString(), null).toPath(); + path = WorldEdit.getInstance().getSafeOpenFile(null, schematicRoot.toFile(), schematicRoot.relativize(path).toString(), null).toPath(); if (Files.isDirectory(path)) { pathList.addAll(scanFolder(path)); } else { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/util/RecursiveDirectoryWatcher.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/util/RecursiveDirectoryWatcher.java index 27eb7b55e6..c50ba193ea 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/util/RecursiveDirectoryWatcher.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/util/RecursiveDirectoryWatcher.java @@ -117,7 +117,7 @@ private void triggerInitialEvents(Path root) throws IOException, FilenameExcepti Path schematicRoot = WorldEdit.getInstance().getSchematicsManager().getRoot(); eventConsumer.accept(new DirectoryCreatedEvent(root)); for (Path path : Files.newDirectoryStream(root)) { - path = WorldEdit.getInstance().getSafeOpenFile(null, schematicRoot.toFile(), path.toString(), null).toPath(); + path = WorldEdit.getInstance().getSafeOpenFile(null, schematicRoot.toFile(), schematicRoot.relativize(path).toString(), null).toPath(); if (Files.isDirectory(path)) { triggerInitialEvents(path); } else { @@ -165,7 +165,7 @@ public void start(Consumer eventConsumer) { path = parentPath.resolve(path); if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) { - path = WorldEdit.getInstance().getSafeOpenFile(null, schematicRoot.toFile(), path.toString(), null).toPath(); + path = WorldEdit.getInstance().getSafeOpenFile(null, schematicRoot.toFile(), schematicRoot.relativize(path).toString(), null).toPath(); if (Files.isDirectory(path)) { // new subfolder created, create watch for it try {