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

Add checks when uploading files/thumbnails #1016

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -51,6 +51,8 @@ public abstract class AbstractRequestMapper {
protected static final String BOUNDARY = "boundary";
protected static final Pattern PATTERN_NAME = Pattern.compile("name=\"([^\"]+)\"");
protected static final Pattern PATTERN_CONTENT_TYPE = Pattern.compile(HttpConstants.HEADER_CONTENT_TYPE + ": ([^\n^\r]+)");
protected static final String FILENAME = "fileName";
protected static final String FILE = "file";

protected final ServiceContext serviceContext;
protected final HttpJsonApiDeserializer deserializer;
Expand Down Expand Up @@ -181,6 +183,7 @@ protected <T> T parseBody(HttpRequest httpRequest, Class<T> type) throws Invalid
*/
protected Map<String, TypedInMemoryFile> parseMultiPartBody(HttpRequest httpRequest, MediaType contentType) throws InvalidRequestException {
Ensure.requireNonNull(httpRequest, "httpRequest must be non-null");
Ensure.require(contentType.subtype().equals("form-data"), "contentType must be form-data");
Map<String, TypedInMemoryFile> map = new HashMap<>();
try {
MultipartStream multipartStream = new MultipartStream(
Expand All @@ -191,14 +194,14 @@ protected Map<String, TypedInMemoryFile> parseMultiPartBody(HttpRequest httpRequ
ByteArrayOutputStream output = new ByteArrayOutputStream();
String multipartHeaders = multipartStream.readHeaders();
multipartStream.readBodyData(output);
if (Objects.equals(headerMatcher(PATTERN_NAME, multipartHeaders), "fileName")) {
map.put("fileName", new TypedInMemoryFile.Builder()
if (Objects.equals(headerMatcher(PATTERN_NAME, multipartHeaders), FILENAME)) {
map.put(FILENAME, new TypedInMemoryFile.Builder()
.content(output.toByteArray())
.contentType(MediaType.PLAIN_TEXT_UTF_8.toString())
.build());
}
else {
map.put("file", new TypedInMemoryFile.Builder()
map.put(FILE, new TypedInMemoryFile.Builder()
.content(output.toByteArray())
.contentType(headerMatcher(PATTERN_CONTENT_TYPE, multipartHeaders))
.build());
Expand All @@ -209,6 +212,8 @@ protected Map<String, TypedInMemoryFile> parseMultiPartBody(HttpRequest httpRequ
catch (IOException e) {
throw new InvalidRequestException(MSG_ERROR_PARSING_BODY, e);
}
Ensure.requireNonNull(map.get(FILE));
Ensure.requireNonNull(map.get(FILENAME));
return map;
}

Expand Down
Loading