Skip to content

Commit

Permalink
Merge pull request #3164 from dbeaver/CB-6085-fix-regex-in-web-sql-fi…
Browse files Browse the repository at this point in the history
…le-loader-servlet

Cb 6085 fix regex in web sql file loader servlet
  • Loading branch information
DenisSinelnikov authored Dec 27, 2024
2 parents ca83a1f + 882979e commit 3bcbf34
Showing 1 changed file with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.UUID;

@MultipartConfig
public class WebSQLFileLoaderServlet extends WebServiceServletBase {
Expand All @@ -53,8 +54,6 @@ public class WebSQLFileLoaderServlet extends WebServiceServletBase {

private static final String FILE_ID = "fileId";

private static final String FORBIDDEN_CHARACTERS_FILE_REGEX = "(?U)[$()@ /]+";

private static final Gson gson = new GsonBuilder()
.serializeNulls()
.setPrettyPrinting()
Expand Down Expand Up @@ -89,19 +88,21 @@ protected void processServiceRequest(
Map<String, Object> variables = gson.fromJson(request.getParameter(REQUEST_PARAM_VARIABLES), MAP_STRING_OBJECT_TYPE);

String fileId = JSONUtils.getString(variables, FILE_ID);

if (fileId != null && !fileId.matches(FORBIDDEN_CHARACTERS_FILE_REGEX) && !fileId.startsWith(".")) {
Path file = tempFolder.resolve(fileId);
try {
Files.write(file, request.getPart("fileData").getInputStream().readAllBytes());
} catch (ServletException e) {
log.error(e.getMessage());
throw new DBWebException(e.getMessage());
}
} else {
String illegalCharacters = fileId != null ?
fileId.replaceAll(FORBIDDEN_CHARACTERS_FILE_REGEX, " ").strip() : null;
throw new DBException("Resource path '" + fileId + "' contains illegal characters: " + illegalCharacters);
if (fileId == null) {
throw new DBWebException("File ID not found");
}
try {
// file id must be UUID
UUID.fromString(fileId);
} catch (IllegalArgumentException e) {
throw new DBWebException("File ID is invalid");
}
Path file = tempFolder.resolve(fileId);
try {
Files.write(file, request.getPart("fileData").getInputStream().readAllBytes());
} catch (ServletException e) {
log.error(e.getMessage());
throw new DBWebException(e.getMessage());
}
}
}

0 comments on commit 3bcbf34

Please sign in to comment.