Skip to content

Commit 455241b

Browse files
authored
Error out on invalid content type (#1617)
Signed-off-by: Bala.FA <[email protected]>
1 parent 942400f commit 455241b

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

api/src/main/java/io/minio/PutObjectArgs.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private Builder setStream(
8686
}
8787

8888
public Builder contentType(String contentType) {
89-
validateNotEmptyString(contentType, "content type");
89+
validateContentType(contentType);
9090
operations.add(args -> args.contentType = contentType);
9191
return this;
9292
}

api/src/main/java/io/minio/PutObjectBaseArgs.java

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.util.Objects;
21+
import okhttp3.MediaType;
2122

2223
/** Base argument class for {@link PutObjectArgs} and {@link UploadObjectArgs}. */
2324
public abstract class PutObjectBaseArgs extends ObjectWriteArgs {
@@ -60,6 +61,14 @@ public boolean preloadData() {
6061
@SuppressWarnings("unchecked") // Its safe to type cast to B as B is inherited by this class
6162
public abstract static class Builder<B extends Builder<B, A>, A extends PutObjectBaseArgs>
6263
extends ObjectWriteArgs.Builder<B, A> {
64+
protected void validateContentType(String contentType) {
65+
validateNotEmptyString(contentType, "content type");
66+
if (MediaType.parse(contentType) == null) {
67+
throw new IllegalArgumentException(
68+
"invalid content type '" + contentType + "' as per RFC 2045");
69+
}
70+
}
71+
6372
private void validateSizes(long objectSize, long partSize) {
6473
if (partSize > 0) {
6574
if (partSize < MIN_MULTIPART_SIZE) {

api/src/main/java/io/minio/S3Base.java

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import okhttp3.Callback;
9393
import okhttp3.Headers;
9494
import okhttp3.HttpUrl;
95+
import okhttp3.MediaType;
9596
import okhttp3.OkHttpClient;
9697
import okhttp3.Request;
9798
import okhttp3.RequestBody;
@@ -518,6 +519,11 @@ protected Request createRequest(
518519
RequestBody requestBody = null;
519520
if (body != null) {
520521
String contentType = (headers != null) ? headers.get("Content-Type") : null;
522+
if (contentType != null && MediaType.parse(contentType) == null) {
523+
throw new IllegalArgumentException(
524+
"invalid content type '" + contentType + "' as per RFC 2045");
525+
}
526+
521527
if (body instanceof PartSource) {
522528
requestBody = new HttpRequestBody((PartSource) body, contentType);
523529
} else {

api/src/main/java/io/minio/UploadObjectArgs.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public Builder filename(String filename) throws IOException {
8484
}
8585

8686
public Builder contentType(String contentType) {
87-
validateNotEmptyString(contentType, "content type");
87+
validateContentType(contentType);
8888
operations.add(args -> args.contentType = contentType);
8989
return this;
9090
}

0 commit comments

Comments
 (0)