Skip to content

Commit

Permalink
Restricted download paths
Browse files Browse the repository at this point in the history
- Updated ImageCommand class
- Updated ImageStorage class
  • Loading branch information
josemmo committed Jan 5, 2024
1 parent 9ea2cdb commit 7cd2664
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public static void listImages(@NotNull CommandSender sender, int page) {

public static void downloadImage(@NotNull CommandSender sender, @NotNull String rawUrl, @NotNull String filename) {
YamipaPlugin plugin = YamipaPlugin.getInstance();
Path basePath = plugin.getStorage().getBasePath();
ImageStorage storage = plugin.getStorage();
Path basePath = storage.getBasePath();

// Resolve destination path
Path destPath;
Expand All @@ -112,6 +113,10 @@ public static void downloadImage(@NotNull CommandSender sender, @NotNull String
sender.sendMessage(ChatColor.RED + "Not a valid destination filename");
return;
}
if (!storage.isPathAllowed(destPath, sender)) {
sender.sendMessage(ChatColor.RED + "Not allowed to download a file here");
return;
}
if (destPath.toFile().exists()) {
sender.sendMessage(ChatColor.RED + "There's already a file with that name");
return;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/josemmo/bukkit/plugin/storage/ImageStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public synchronized int size() {
return response;
}

/**
* Is path allowed
* @param path Path instance
* @param sender Sender instance
* @return Whether sender is allowed to access path
*/
public boolean isPathAllowed(@NotNull Path path, @NotNull CommandSender sender) {
return isPathAllowed(getFilename(path), sender);
}

/**
* Is path allowed
* @param path Path relative to {@link ImageStorage#basePath}
Expand Down

0 comments on commit 7cd2664

Please sign in to comment.