Skip to content

Commit

Permalink
Add back the content-length check as a preliminary check
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerjmchugh committed Dec 30, 2024
1 parent 00770fa commit f4232fa
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ protected String getFilenameFromHeader(final URL fileUrl) {
}
}

protected long getContentLengthFromHeader(final URL fileUrl) {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) fileUrl.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
return connection.getContentLengthLong();
} catch (Exception e) {
log.error("Error retrieving resource content length from header", e);
return -1;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

protected String getFilenameFromUrl(final URL fileUrl) {
String fileName = FilenameUtils.getName(fileUrl.getPath());
if (fileName.contains("?")) {
Expand Down Expand Up @@ -239,6 +256,16 @@ public final MetadataResource putResource(ServiceContext context, String metadat
filename = getFilenameFromUrl(fileUrl);
}

long contentLength = getContentLengthFromHeader(fileUrl);
if (contentLength > maxUploadSize) {
throw new GeonetMaxUploadSizeExceededException("uploadedResourceSizeExceededException")
.withMessageKey("exception.maxUploadSizeExceeded",
new String[]{FileUtil.humanizeFileSize(maxUploadSize)})
.withDescriptionKey("exception.maxUploadSizeExceeded.description",
new String[]{FileUtil.humanizeFileSize(contentLength),
FileUtil.humanizeFileSize(maxUploadSize)});
}

Path tempFilePath = Files.createTempFile("uploaded_resource", null);
try (BoundedInputStream boundedInputStream = new BoundedInputStream(fileUrl.openStream(), maxUploadSize+1)) {
Files.copy(boundedInputStream, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
Expand Down

0 comments on commit f4232fa

Please sign in to comment.