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/18 15:20:45 UTC

(ofbiz-framework) branch release24.09 updated: Fixed: Ensure safe writing of sanitized images by opening output stream after image processing

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

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


The following commit(s) were added to refs/heads/release24.09 by this push:
     new 1ce54a29b5 Fixed: Ensure safe writing of sanitized images by opening output stream after image processing
1ce54a29b5 is described below

commit 1ce54a29b58914358f1cf199de7de96be29bb981
Author: Jacopo Cappellato <ja...@gmail.com>
AuthorDate: Wed Mar 18 15:20:47 2026 +0100

    Fixed: Ensure safe writing of sanitized images by opening output stream after image processing
    
    (cherry picked from commit aa684ad14090664305a2028d30d66a4d2e887db2)
---
 .../org/apache/ofbiz/security/SecuredUpload.java   | 50 ++++++++++++----------
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/framework/security/src/main/java/org/apache/ofbiz/security/SecuredUpload.java b/framework/security/src/main/java/org/apache/ofbiz/security/SecuredUpload.java
index 96a9af120f..d84e3cca51 100644
--- a/framework/security/src/main/java/org/apache/ofbiz/security/SecuredUpload.java
+++ b/framework/security/src/main/java/org/apache/ofbiz/security/SecuredUpload.java
@@ -570,7 +570,7 @@ public class SecuredUpload {
         boolean safeState = false;
 
         if ((file != null) && file.exists() && file.canRead() && file.canWrite()) {
-            try (OutputStream fos = Files.newOutputStream(file.toPath(), StandardOpenOption.WRITE)) {
+            try {
                 // Get the image format
                 String formatName;
                 ImageInputStream iis = ImageIO.createImageInputStream(file);
@@ -621,29 +621,33 @@ public class SecuredUpload {
                 bg.drawImage(initialSizedImage, 0, 0, null);
                 bg.dispose();
 
-                if (!fallbackOnApacheCommonsImaging) {
-                    ImageIO.write(sanitizedImage, formatName, fos);
-                } else {
-                    ImageParser<?> imageParser;
-                    // Handle only formats for which Apache Commons Imaging can successfully write (YES in Write column of the reference link)
-                    // the image format. See reference link in the class header
-                    switch (formatName) {
-                    case "TIFF":
-                        imageParser = new TiffImageParser();
-                        break;
-                    case "GIF":
-                        imageParser = new GifImageParser();
-                        break;
-                    case "PNG":
-                        imageParser = new PngImageParser();
-                        break;
-                    // case "JPEG":
-                    // imageParser = new JpegImageParser(); // Does not provide imageParser.writeImage used below
-                    // break;
-                    default:
-                        throw new IOException("Format of the original image " + fileName + " is not supported for write operation !");
+                // Open the output stream only after the sanitized image is ready, so that
+                // TRUNCATE_EXISTING does not wipe the file before it has been read.
+                try (OutputStream fos = Files.newOutputStream(file.toPath(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
+                    if (!fallbackOnApacheCommonsImaging) {
+                        ImageIO.write(sanitizedImage, formatName, fos);
+                    } else {
+                        ImageParser<?> imageParser;
+                        // Handle only formats for which Apache Commons Imaging can successfully write (YES in Write column of the reference link)
+                        // the image format. See reference link in the class header
+                        switch (formatName) {
+                        case "TIFF":
+                            imageParser = new TiffImageParser();
+                            break;
+                        case "GIF":
+                            imageParser = new GifImageParser();
+                            break;
+                        case "PNG":
+                            imageParser = new PngImageParser();
+                            break;
+                        // case "JPEG":
+                        // imageParser = new JpegImageParser(); // Does not provide imageParser.writeImage used below
+                        // break;
+                        default:
+                            throw new IOException("Format of the original image " + fileName + " is not supported for write operation !");
+                        }
+                        imageParser.writeImage(sanitizedImage, fos, null);
                     }
-                    imageParser.writeImage(sanitizedImage, fos, null);
                 }
                 // Set state flag
                 safeState = true;