Skip to content

Commit 1026835

Browse files
authored
Fix base64 url validation (accept parameters). (#165)
1 parent 6dc1961 commit 1026835

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

cloudinary-core/src/main/java/com/cloudinary/utils/StringUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public static String read(InputStream in) throws IOException {
209209
}
210210

211211
public static boolean isRemoteUrl(String file) {
212-
return file.matches("ftp:.*|https?:.*|s3:.*|gs:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)");
212+
return file.matches("ftp:.*|https?:.*|s3:.*|gs:.*|data:([\\w-]+/[\\w-]+)?(;[\\w-]+=[\\w-]+)*;base64,([a-zA-Z0-9/+\n=]+)");
213213
}
214214

215215
/**

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.cloudinary.utils.ObjectUtils.asArray;
1919
import static com.cloudinary.utils.ObjectUtils.asMap;
20+
import static com.cloudinary.utils.StringUtils.isRemoteUrl;
2021
import static org.hamcrest.Matchers.*;
2122
import static org.junit.Assert.*;
2223
import static org.junit.Assume.assumeNotNull;
@@ -121,6 +122,28 @@ public void testUpload() throws IOException {
121122
assertEquals(result.get("signature"), expected_signature);
122123
}
123124

125+
@Test
126+
public void testIsRemoteUrl() {
127+
String[] urls = new String[]{
128+
"ftp://ftp.cloudinary.com/images/old_logo.png",
129+
"http://cloudinary.com/images/old_logo.png",
130+
"https://cloudinary.com/images/old_logo.png",
131+
"s3://s3-us-west-2.amazonaws.com/cloudinary/images/old_logo.png",
132+
"gs://cloudinary/images/old_logo.png",
133+
"data:image/gif;charset=utf8;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
134+
"data:image/gif;param1=value1;param2=value2;base64," +
135+
"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"};
136+
137+
for (String url : urls) {
138+
assertTrue(isRemoteUrl(url));
139+
}
140+
141+
String[] invalidUrls = new String[]{"adsadasdasdasd", " ", ""};
142+
143+
for (String url : invalidUrls) {
144+
assertFalse(isRemoteUrl(url));
145+
}
146+
}
124147

125148
@Test
126149
public void testUploadUrl() throws IOException {

0 commit comments

Comments
 (0)