From 92ee21d1366e183d9feaf24745c95c092efdf7a8 Mon Sep 17 00:00:00 2001 From: DenisSinelnikov <142215442+DenisSinelnikov@users.noreply.github.com> Date: Fri, 27 Dec 2024 20:52:10 +0400 Subject: [PATCH] CB-5609. Added validate name for renaming s3 files (#3163) * CB-5609. Added validate name for renaming s3 files * CB-5609. Refactor after review * CB-5609. Refactor after review * CB-5609. Refactor after review * CB-5609. Remove symbol --------- Co-authored-by: Daria Marutkina <125263541+dariamarutkina@users.noreply.github.com> --- .../cloudbeaver/service/fs/impl/WebServiceFS.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/server/bundles/io.cloudbeaver.service.fs/src/io/cloudbeaver/service/fs/impl/WebServiceFS.java b/server/bundles/io.cloudbeaver.service.fs/src/io/cloudbeaver/service/fs/impl/WebServiceFS.java index 187184c780..6dd3d2dcaa 100644 --- a/server/bundles/io.cloudbeaver.service.fs/src/io/cloudbeaver/service/fs/impl/WebServiceFS.java +++ b/server/bundles/io.cloudbeaver.service.fs/src/io/cloudbeaver/service/fs/impl/WebServiceFS.java @@ -36,12 +36,16 @@ import java.nio.file.Path; import java.text.MessageFormat; import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Web file system implementation */ public class WebServiceFS implements DBWServiceFS { + private static final Pattern FORBIDDEN_FILENAME_PATTERN = Pattern.compile("[%#:;№$]"); + @NotNull @Override public FSFileSystem[] getAvailableFileSystems(@NotNull WebSession webSession, @NotNull String projectId) @@ -208,6 +212,7 @@ public FSFile renameFile( @NotNull String nodePath, @NotNull String newName ) throws DBWebException { + validateFilename(newName); try { DBNPathBase node = FSUtils.getNodeByPath(webSession, nodePath); node.rename(webSession.getProgressMonitor(), newName); @@ -276,4 +281,12 @@ public boolean deleteFile( throw new DBWebException("Failed to create folder: " + e.getMessage(), e); } } + + private void validateFilename(@NotNull String filename) throws DBWebException { + Matcher matcher = FORBIDDEN_FILENAME_PATTERN.matcher(filename); + + if (matcher.find()) { + throw new DBWebException(String.format("File %s contains forbidden symbols", filename)); + } + } }