Skip to content

Commit bbd02b8

Browse files
Copilotcubap
andcommitted
Address code review: revert to try-with-resources, validate image extensions, clarify JPG-only support
Co-authored-by: cubap <1119165+cubap@users.noreply.github.com>
1 parent 90785fb commit bbd02b8

2 files changed

Lines changed: 18 additions & 27 deletions

File tree

src/java/ImageUpload/FileUpload.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
import static java.lang.Integer.parseInt;
2525
import java.sql.Connection;
2626
import java.util.*;
27-
import java.util.logging.Logger;
28-
import static java.util.logging.Logger.getLogger;
29-
import static java.util.logging.Level.WARNING;
3027
import javax.servlet.Servlet;
3128
import javax.servlet.ServletException;
3229
import javax.servlet.http.*;
@@ -45,8 +42,6 @@
4542
* @author obi1one
4643
*/
4744
public class FileUpload extends HttpServlet implements Servlet {
48-
49-
private static final Logger LOG = getLogger(FileUpload.class.getName());
5045

5146
/**
5247
*
@@ -109,10 +104,8 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
109104
upload.setProgressListener(listener);
110105

111106
session.setAttribute("LISTENER", listener);
112-
113-
Connection conn = null;
114-
try {
115-
conn = getDBConnection();
107+
108+
try (Connection conn = getDBConnection()) {
116109
conn.setAutoCommit(false);
117110
User thisUser = new User(uid);
118111
String city = req.getParameter("city");
@@ -146,23 +139,8 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
146139
conn.rollback();
147140
}
148141
} catch (Exception ex) {
149-
if (conn != null) {
150-
try {
151-
conn.rollback();
152-
} catch (Exception rollbackEx) {
153-
// Log rollback failure but throw original exception
154-
ex.addSuppressed(rollbackEx);
155-
}
156-
}
157142
reportInternalError(resp, ex);
158143
} finally {
159-
if (conn != null) {
160-
try {
161-
conn.close();
162-
} catch (Exception closeEx) {
163-
LOG.log(WARNING, "Failed to close database connection", closeEx);
164-
}
165-
}
166144
session.setAttribute("LISTENER", null);
167145
}
168146
} else {

src/java/ImageUpload/UserImageCollection.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,17 @@ private static String sanitizeFilename(String filename) {
316316
String extension = "";
317317
int lastDotIndex = filename.lastIndexOf('.');
318318
if (lastDotIndex > 0 && lastDotIndex < filename.length() - 1) {
319-
name = filename.substring(0, lastDotIndex);
320-
extension = filename.substring(lastDotIndex);
319+
String potentialExtension = filename.substring(lastDotIndex).toLowerCase();
320+
// Validate extension is a known image type (only .jpg supported currently)
321+
if (potentialExtension.equals(".jpg") || potentialExtension.equals(".jpeg")) {
322+
name = filename.substring(0, lastDotIndex);
323+
extension = filename.substring(lastDotIndex);
324+
} else {
325+
// If not a valid image extension, treat the whole filename as name
326+
// This prevents files like "document.pdf" from being treated as images
327+
name = filename;
328+
extension = "";
329+
}
321330
}
322331

323332
// Handle directory separators (keep them)
@@ -340,7 +349,10 @@ private static String sanitizeFilename(String filename) {
340349

341350
/**
342351
* Check that the image is actually a valid jpg image by loading it as a
343-
* BufferedImage BH TODO strip invalid characters!
352+
* BufferedImage and re-saving it as JPG.
353+
*
354+
* NOTE: This system only supports JPG format for private uploads as documented
355+
* in the upload UI. Images are always scaled to max 2000px height and saved as JPG.
344356
*/
345357
private static Boolean validateImage(File f) {
346358
try {
@@ -357,6 +369,7 @@ private static Boolean validateImage(File f) {
357369
return false;
358370
}
359371

372+
// Scale image to max 2000px height and save as JPG (system only supports JPG)
360373
img = scale(img, 2000);
361374
write(img, "jpg", f);
362375
} catch (Exception e) {

0 commit comments

Comments
 (0)