You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2026/03/17 08:07:06 UTC

(ofbiz-framework) branch trunk updated (3475b96c67 -> 0dcd1faf18)

This is an automated email from the ASF dual-hosted git repository.

jacopoc pushed a change to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


    from 3475b96c67 Fixed: Improve image upload handling by using file extensions for temporary files and ensuring safe copying of sanitized images
     new 23f13adc01 Fixed: Enhance temporary file handling for uploads by using original file extensions and ensuring safe copying of files
     new 0dcd1faf18 Fixed: Enhance temporary file creation for image uploads by using original file extensions and ensuring safe copying to the destination

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/ofbiz/content/data/DataServices.java    | 37 ++++++++++------------
 .../ofbiz/product/imagemanagement/FrameImage.java  | 14 ++++----
 2 files changed, 25 insertions(+), 26 deletions(-)


(ofbiz-framework) 02/02: Fixed: Enhance temporary file creation for image uploads by using original file extensions and ensuring safe copying to the destination

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jacopoc pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git

commit 0dcd1faf182ab0aa43a8939d4a4c84bf5c569af3
Author: Jacopo Cappellato <ja...@gmail.com>
AuthorDate: Tue Mar 17 08:53:06 2026 +0100

    Fixed: Enhance temporary file creation for image uploads by using original file extensions and ensuring safe copying to the destination
---
 .../apache/ofbiz/product/imagemanagement/FrameImage.java   | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java b/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java
index ada6e6f367..3b0883bcd3 100644
--- a/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java
+++ b/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java
@@ -29,10 +29,10 @@ import java.awt.image.BufferedImage;
 import java.awt.image.RenderedImage;
 import java.io.File;
 import java.io.IOException;
-import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
 import java.nio.file.StandardOpenOption;
 import java.util.HashMap;
 import java.util.Locale;
@@ -318,18 +318,20 @@ public class FrameImage {
                 request.setAttribute("_ERROR_MESSAGE_", "There is an existing frame, please select from the existing frame.");
                 return "error";
             }
-            Path tmpFile = Files.createTempFile(null, null);
+            String origName = imageName;
+            int dotIdx = origName.lastIndexOf('.');
+            String fileExt = dotIdx >= 0 ? origName.substring(dotIdx) : null;
+            Path tmpFile = Files.createTempFile(null, fileExt);
             Files.write(tmpFile, imageData.array(), StandardOpenOption.APPEND);
             // Check if a webshell is not uploaded
             if (!org.apache.ofbiz.security.SecuredUpload.isValidFile(tmpFile.toString(), "Image", delegator)) {
                 String errorMessage = UtilProperties.getMessage("SecurityUiLabels", "SupportedFileFormatsIncludingSvg", locale);
+                new File(tmpFile.toString()).deleteOnExit();
                 request.setAttribute("_ERROR_MESSAGE_", errorMessage);
                 return "error";
             }
-            Files.delete(tmpFile);
-            RandomAccessFile out = new RandomAccessFile(file, "rw");
-            out.write(imageData.array());
-            out.close();
+            Files.copy(tmpFile, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
+            new File(tmpFile.toString()).deleteOnExit();
 
             //create dataResource
             Map<String, Object> dataResourceCtx = new HashMap<>();


(ofbiz-framework) 01/02: Fixed: Enhance temporary file handling for uploads by using original file extensions and ensuring safe copying of files

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jacopoc pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git

commit 23f13adc014cbbf1985e2ddeac57df7529b6db19
Author: Jacopo Cappellato <ja...@gmail.com>
AuthorDate: Tue Mar 17 08:41:24 2026 +0100

    Fixed: Enhance temporary file handling for uploads by using original file extensions and ensuring safe copying of files
---
 .../apache/ofbiz/content/data/DataServices.java    | 37 ++++++++++------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java b/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
index 23a7f37669..56a618a18e 100644
--- a/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
+++ b/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
@@ -23,13 +23,13 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
-import java.io.RandomAccessFile;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
 import java.nio.file.StandardOpenOption;
 import java.sql.Timestamp;
 import java.util.Arrays;
@@ -291,21 +291,20 @@ public class DataServices {
             }
         } else if (binData != null) {
             try {
-                Path tempFile = Files.createTempFile(null, null);
+                String origName = file.getName();
+                int dotIdx = origName.lastIndexOf('.');
+                String fileExt = dotIdx >= 0 ? origName.substring(dotIdx) : null;
+                Path tempFile = Files.createTempFile(null, fileExt);
                 Files.write(tempFile, binData.array(), StandardOpenOption.APPEND);
-                // Check if a webshell is not uploaded
-                // TODO I believe the call below to SecuredUpload::isValidFile is now useless because of the same in createFileNoPerm
                 if (!SecuredUpload.isValidFile(tempFile.toString(), "All", delegator)) {
                     String errorMessage = UtilProperties.getMessage("SecurityUiLabels", "SupportedFileFormatsIncludingSvg", locale);
+                    new File(tempFile.toString()).deleteOnExit();
                     return ServiceUtil.returnError(errorMessage);
                 }
-                File tempFileToDelete = new File(tempFile.toString());
-                tempFileToDelete.deleteOnExit();
-                RandomAccessFile out = new RandomAccessFile(file, "rw");
-                out.write(binData.array());
-                out.close();
+                Files.copy(tempFile, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
+                new File(tempFile.toString()).deleteOnExit();
 
-            } catch (FileNotFoundException | ImageReadException e) {
+            } catch (ImageReadException e) {
                 Debug.logError(e, MODULE);
                 return ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, "ContentUnableToOpenFileForWriting",
                         UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));
@@ -501,21 +500,19 @@ public class DataServices {
                 }
             } else if (binData != null) {
                 try {
-                    // Check if a webshell is not uploaded
-                    // TODO I believe the call below to SecuredUpload::isValidFile is now useless because of the same in createFileNoPerm
-                    Path tempFile = Files.createTempFile(null, null);
+                    String origName = file.getName();
+                    int dotIdx = origName.lastIndexOf('.');
+                    String fileExt = dotIdx >= 0 ? origName.substring(dotIdx) : null;
+                    Path tempFile = Files.createTempFile(null, fileExt);
                     Files.write(tempFile, binData.array(), StandardOpenOption.APPEND);
                     if (!SecuredUpload.isValidFile(tempFile.toString(), "Image", delegator)) {
                         String errorMessage = UtilProperties.getMessage("SecurityUiLabels", "SupportedFileFormatsIncludingSvg", locale);
+                        new File(tempFile.toString()).deleteOnExit();
                         return ServiceUtil.returnError(errorMessage);
                     }
-                    File tempFileToDelete = new File(tempFile.toString());
-                    tempFileToDelete.deleteOnExit();
-                    RandomAccessFile out = new RandomAccessFile(file, "rw");
-                    out.setLength(binData.array().length);
-                    out.write(binData.array());
-                    out.close();
-                } catch (FileNotFoundException | ImageReadException e) {
+                    Files.copy(tempFile, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
+                    new File(tempFile.toString()).deleteOnExit();
+                } catch (ImageReadException e) {
                     Debug.logError(e, MODULE);
                     return ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, "ContentUnableToOpenFileForWriting",
                             UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));