Skip to content

Fix base64 url validation (accept parameters). #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public static String read(InputStream in) throws IOException {
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.cloudinary.utils.ObjectUtils.asArray;
import static com.cloudinary.utils.ObjectUtils.asMap;
import static com.cloudinary.utils.StringUtils.isRemoteUrl;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeNotNull;
Expand Down Expand Up @@ -121,6 +122,28 @@ public void testUpload() throws IOException {
assertEquals(result.get("signature"), expected_signature);
}

@Test
public void testIsRemoteUrl() {
String[] urls = new String[]{
"ftp://ftp.cloudinary.com/images/old_logo.png",
"http://cloudinary.com/images/old_logo.png",
"https://cloudinary.com/images/old_logo.png",
"s3://s3-us-west-2.amazonaws.com/cloudinary/images/old_logo.png",
"gs://cloudinary/images/old_logo.png",
"data:image/gif;charset=utf8;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
"data:image/gif;param1=value1;param2=value2;base64," +
"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"};

for (String url : urls) {
assertTrue(isRemoteUrl(url));
}

String[] invalidUrls = new String[]{"adsadasdasdasd", " ", ""};

for (String url : invalidUrls) {
assertFalse(isRemoteUrl(url));
}
}

@Test
public void testUploadUrl() throws IOException {
Expand Down