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

Error out on invalid content type #1617

Merged
merged 1 commit into from
Jan 16, 2025
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
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
9 changes: 9 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,14 @@ 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 '" + contentType + "' as per RFC 2045");
}
}

private void validateSizes(long objectSize, long partSize) {
if (partSize > 0) {
if (partSize < MIN_MULTIPART_SIZE) {
Expand Down
6 changes: 6 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,11 @@ 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 '" + contentType + "' 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
Loading