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

Improve MIME type checks #4610

Merged
merged 1 commit into from
Jan 10, 2024
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
11 changes: 9 additions & 2 deletions src/main/java/mil/dds/anet/database/AttachmentDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public int updateInternal(Attachment obj) {
+ "UPDATE \"attachments\" SET \"mimeType\" = :mimeType, \"fileName\" = :fileName, "
+ "\"description\" = :description, \"classification\" = :classification, "
+ "\"caption\" = :caption, \"updatedAt\" = :updatedAt WHERE uuid = :uuid")
.bindBean(obj).bind("updatedAt", DaoUtils.asLocalDateTime(obj.getUpdatedAt()))
.bind("updatedAt", DaoUtils.asLocalDateTime(obj.getUpdatedAt())).execute();
.bindBean(obj).bind("updatedAt", DaoUtils.asLocalDateTime(obj.getUpdatedAt())).execute();
}

@Override
Expand All @@ -105,6 +104,14 @@ public int deleteInternal(String uuid) {
.bind("uuid", uuid).mapTo(Integer.class).one();
}

@InTransaction
public int updateMimeType(Attachment obj) {
return getDbHandle()
.createUpdate("/* updateAttachmentMimeType */ "
+ "UPDATE \"attachments\" SET \"mimeType\" = :mimeType WHERE uuid = :uuid")
.bindBean(obj).execute();
}

public interface AttachmentContent {
@SqlUpdate("UPDATE attachments SET content = :content WHERE uuid = :uuid")
void updateContent(@Bind("uuid") String uuid, @Bind("content") InputStream content);
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/mil/dds/anet/resources/AttachmentResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,23 @@ private InputStream checkMimeType(final Attachment attachment,
return attachmentContent;
}
if (!detectedMimeType.equals(attachment.getMimeType())) {
logger.error(
"Attachment content upload rejected for attachment {} (\"{}\"): "
+ "stated mimeType \"{}\" differs from detected mimeType \"{}\"",
attachment.getUuid(), attachment.getFileName(), attachment.getMimeType(),
detectedMimeType);
throw new WebApplicationException("Attachment content does not match the MIME type",
Status.BAD_REQUEST);
if (getAllowedMimeTypes().contains(detectedMimeType)) {
logger.info(
"Attachment content upload for attachment {} (\"{}\"): "
+ "updated stated mimeType \"{}\" to detected mimeType \"{}\"",
attachment.getUuid(), attachment.getFileName(), attachment.getMimeType(),
detectedMimeType);
attachment.setMimeType(detectedMimeType);
dao.updateMimeType(attachment);
} else {
logger.error(
"Attachment content upload rejected for attachment {} (\"{}\"): "
+ "stated mimeType \"{}\" differs from detected mimeType \"{}\"",
attachment.getUuid(), attachment.getFileName(), attachment.getMimeType(),
detectedMimeType);
throw new WebApplicationException("Attachment content does not match the MIME type",
Status.BAD_REQUEST);
}
}
return tikaInputStream;
}
Expand Down
Loading