Skip to content

Commit

Permalink
Error out on invalid content type
Browse files Browse the repository at this point in the history
Fixes #1616

Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana committed Jan 16, 2025
1 parent 942400f commit 35ebccc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/io/minio/PutObjectArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private Builder setStream(
}

public Builder contentType(String contentType) {
validateNotEmptyString(contentType, "content type");
validateContentType(contentType);
operations.add(args -> args.contentType = contentType);
return this;
}
Expand Down
8 changes: 8 additions & 0 deletions api/src/main/java/io/minio/PutObjectBaseArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.util.Objects;
import okhttp3.MediaType;

/** Base argument class for {@link PutObjectArgs} and {@link UploadObjectArgs}. */
public abstract class PutObjectBaseArgs extends ObjectWriteArgs {
Expand Down Expand Up @@ -60,6 +61,13 @@ public boolean preloadData() {
@SuppressWarnings("unchecked") // Its safe to type cast to B as B is inherited by this class
public abstract static class Builder<B extends Builder<B, A>, A extends PutObjectBaseArgs>
extends ObjectWriteArgs.Builder<B, A> {
protected void validateContentType(String contentType) {
validateNotEmptyString(contentType, "content type");
if (MediaType.parse(contentType) == null) {
throw new IllegalArgumentException("invalid content type as per RFC 2045");
}
}

private void validateSizes(long objectSize, long partSize) {
if (partSize > 0) {
if (partSize < MIN_MULTIPART_SIZE) {
Expand Down
5 changes: 5 additions & 0 deletions api/src/main/java/io/minio/S3Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
Expand Down Expand Up @@ -518,6 +519,10 @@ protected Request createRequest(
RequestBody requestBody = null;
if (body != null) {
String contentType = (headers != null) ? headers.get("Content-Type") : null;
if (contentType != null && MediaType.parse(contentType) == null) {
throw new IllegalArgumentException("invalid content type as per RFC 2045");
}

if (body instanceof PartSource) {
requestBody = new HttpRequestBody((PartSource) body, contentType);
} else {
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/io/minio/UploadObjectArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Builder filename(String filename) throws IOException {
}

public Builder contentType(String contentType) {
validateNotEmptyString(contentType, "content type");
validateContentType(contentType);
operations.add(args -> args.contentType = contentType);
return this;
}
Expand Down

0 comments on commit 35ebccc

Please sign in to comment.