Skip to content

Commit 358bfbf

Browse files
michalkcloudinaynitzanj
authored andcommitted
Add validation for CLOUDINARY_URL scheme (#185)
1 parent e94c23b commit 358bfbf

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

cloudinary-core/src/main/java/com/cloudinary/Configuration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ public static Configuration from(String cloudinaryUrl) {
186186
static protected Map parseConfigUrl(String cloudinaryUrl) {
187187
Map params = new HashMap();
188188
URI cloudinaryUri = URI.create(cloudinaryUrl);
189+
if (cloudinaryUri.getScheme() == null || !cloudinaryUri.getScheme().equalsIgnoreCase("cloudinary")){
190+
throw new IllegalArgumentException("Invalid CLOUDINARY_URL scheme. Expecting to start with 'cloudinary://'");
191+
}
189192
params.put("cloud_name", cloudinaryUri.getHost());
190193
if (cloudinaryUri.getUserInfo() != null) {
191194
String[] creds = cloudinaryUri.getUserInfo().split(":");
@@ -427,4 +430,4 @@ public Builder from(Configuration other) {
427430
return this;
428431
}
429432
}
430-
}
433+
}

cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,24 @@ public void testConfiguration() throws IllegalAccessException {
12301230
assertFieldsEqual(config, copy);
12311231
}
12321232

1233+
@Test
1234+
public void testCloudinaryUrlValidScheme() {
1235+
String cloudinaryUrl = "cloudinary://123456789012345:ALKJdjklLJAjhkKJ45hBK92baj3@test";
1236+
Configuration.from(cloudinaryUrl);
1237+
}
1238+
1239+
@Test(expected = IllegalArgumentException.class)
1240+
public void testCloudinaryUrlInvalidScheme() {
1241+
String cloudinaryUrl = "https://123456789012345:ALKJdjklLJAjhkKJ45hBK92baj3@test";
1242+
Configuration.from(cloudinaryUrl);
1243+
}
1244+
1245+
@Test(expected = IllegalArgumentException.class)
1246+
public void testCloudinaryUrlEmptyScheme() {
1247+
String cloudinaryUrl = " ";
1248+
Configuration.from(cloudinaryUrl);
1249+
}
1250+
12331251
private void assertFieldsEqual(Object a, Object b) throws IllegalAccessException {
12341252
assertEquals("Two objects must be the same class", a.getClass(), b.getClass());
12351253
Field[] fields = a.getClass().getFields();

0 commit comments

Comments
 (0)