Skip to content
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

fix: Remove post policy v4 client side validation #1210

Merged
merged 4 commits into from
Jan 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -54,7 +52,6 @@ private PostPolicyV4(String url, Map<String, String> fields) {
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
PostFieldsV4.validateFields(fields);

this.url = url;
this.fields = Collections.unmodifiableMap(fields);
Expand Down Expand Up @@ -93,40 +90,12 @@ public Map<String, String> getFields() {
*/
public static final class PostFieldsV4 {
private final Map<String, String> fieldsMap;
private static final List<String> VALID_FIELDS =
Arrays.asList(
"acl",
"bucket",
"cache-control",
"content-disposition",
"content-encoding",
"content-type",
"expires",
"file",
"key",
"policy",
"success_action_redirect",
"success_action_status",
"x-goog-algorithm",
"x-goog-credential",
"x-goog-date",
"x-goog-signature");

private static void validateFields(Map<String, String> fields) {
for (String key : fields.keySet()) {
if (!VALID_FIELDS.contains(key.toLowerCase())
&& !key.startsWith(Builder.CUSTOM_FIELD_PREFIX)) {
throw new IllegalArgumentException("Invalid key: " + key);
}
}
}

private PostFieldsV4(Builder builder) {
this(builder.fieldsMap);
}

private PostFieldsV4(Map<String, String> fields) {
validateFields(fields);
this.fieldsMap = Collections.unmodifiableMap(fields);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,36 +98,12 @@ public void testPostPolicyV4_ofMalformedURL() {
}
}

@Test
public void testPostPolicyV4_ofInvalidField() {
Map<String, String> fields = new HashMap<>(ALL_FIELDS);
fields.put("$file", "file.txt");
try {
PostPolicyV4.of("http://google.com", fields);
fail();
} catch (IllegalArgumentException e) {
assertEquals("Invalid key: $file", e.getMessage());
}
}

@Test
public void testPostFieldsV4_of() {
PostPolicyV4.PostFieldsV4 fields = PostPolicyV4.PostFieldsV4.of(ALL_FIELDS);
assertMapsEquals(ALL_FIELDS, fields.getFieldsMap());
}

@Test
public void testPostFieldsV4_ofInvalidField() {
Map<String, String> map = new HashMap<>();
map.put("$file", "file.txt");
try {
PostPolicyV4.PostFieldsV4.of(map);
fail();
} catch (IllegalArgumentException e) {
assertEquals("Invalid key: $file", e.getMessage());
}
}

@Test
public void testPostPolicyV4_builder() {
PostPolicyV4.PostFieldsV4.Builder builder = PostPolicyV4.PostFieldsV4.newBuilder();
Expand Down