Skip to content

Commit 6311f66

Browse files
committed
Better check for bad local files
1 parent 6cd571a commit 6311f66

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

cloudinary-http42/src/main/java/com/cloudinary/http42/UploaderStrategy.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
8484
}
8585
}
8686

87-
if (file instanceof String && !((String) file).matches("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
88-
file = new File((String) file);
87+
if (file instanceof String && !((String) file).matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
88+
File _file = new File((String) file);
89+
if (!_file.isFile() && !_file.canRead()) {
90+
throw new IOException("File not found or unreadable: " + file);
91+
}
92+
file = _file;
8993
}
9094
String filename = (String) options.get("filename");
9195
if (file instanceof File) {
@@ -98,7 +102,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
98102
} else if (file == null) {
99103
// no-problem
100104
} else {
101-
throw new IOException("Uprecognized file parameter " + file);
105+
throw new IOException("Unrecognized file parameter " + file);
102106
}
103107
postMethod.setEntity(multipart);
104108

cloudinary-http43/src/main/java/com/cloudinary/http43/UploaderStrategy.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
9797
}
9898
}
9999

100-
if (file instanceof String && !((String) file).matches("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
101-
file = new File((String) file);
100+
if (file instanceof String && !((String) file).matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
101+
File _file = new File((String) file);
102+
if (!_file.isFile() && !_file.canRead()) {
103+
throw new IOException("File not found or unreadable: " + file);
104+
}
105+
file = _file;
102106
}
103107
String filename = (String) options.get("filename");
104108
if (file instanceof File) {

cloudinary-http44/src/main/java/com/cloudinary/http44/UploaderStrategy.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
9999
}
100100
}
101101

102-
if (file instanceof String && !((String) file).matches("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
103-
file = new File((String) file);
102+
if (file instanceof String && !((String) file).matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
103+
File _file = new File((String) file);
104+
if (!_file.isFile() && !_file.canRead()) {
105+
throw new IOException("File not found or unreadable: " + file);
106+
}
107+
file = _file;
104108
}
105109
String filename = (String) options.get("filename");
106110
if (file instanceof File) {

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,13 @@ public void testDownloadArchive() throws Exception {
500500
assertEquals(2, files);
501501
}
502502

503-
@Test
504-
public void testUploadInvalidUrl() throws IOException {
505-
Map result = cloudinary.uploader().upload(REMOTE_TEST_IMAGE + "\n", ObjectUtils.asMap("return_error", true));
506-
Map error = (Map) result.get("error");
507-
assertEquals(error.get("http_code"), 404);
503+
public void testUploadInvalidUrl() {
504+
try {
505+
cloudinary.uploader().upload(REMOTE_TEST_IMAGE + "\n", ObjectUtils.asMap("return_error", true));
506+
fail("Expected exception was not thrown");
507+
} catch(IOException e) {
508+
assertEquals(e.getMessage(), "File not found or unreadable: " + REMOTE_TEST_IMAGE + "\n");
509+
}
508510
}
509511

510512
}

0 commit comments

Comments
 (0)