-
-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce upload size limit on putResourceFromUrl api (#8562)
* Check content-length of resource at url * Remove unnecessary throw * Fallback to InputStream.available() if content length is not found or smaller * Fallback maxUploadSize for unit tests * Fix default value always used * Remove content-length check as it cannot be trusted * Download file to a temp file to check size * Add back the content-length check as a preliminary check * Stream file instead of using temp file * Rollback for JCloud * Fix rollback for JCloud * Fix abstract store and implement jcloud rollback * Fix typo and remove unused import * Fix unit tests * Fix tests (mock response code) * Fix unit test, refactor, and add docs * Fix exception handling and use bounded input stream instead of custom input stream * Improvements * Add documentation * Rename exception * Remove unneeded changes * Update docs * Fix comment Co-authored-by: Ian <[email protected]> * Update exception handling * Update jcloud exception handling and comments * Add file header * Add comment * Fix whitespace --------- Co-authored-by: Ian <[email protected]>
- Loading branch information
1 parent
d46fa20
commit 8b2a00d
Showing
12 changed files
with
212 additions
and
40 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
core/src/main/java/org/fao/geonet/api/exception/InputStreamLimitExceededException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//============================================================================= | ||
//=== Copyright (C) 2001-2025 Food and Agriculture Organization of the | ||
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP) | ||
//=== and United Nations Environment Programme (UNEP) | ||
//=== | ||
//=== This library is free software; you can redistribute it and/or | ||
//=== modify it under the terms of the GNU Lesser General Public | ||
//=== License as published by the Free Software Foundation; either | ||
//=== version 2.1 of the License, or (at your option) any later version. | ||
//=== | ||
//=== This library is distributed in the hope that it will be useful, | ||
//=== but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
//=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
//=== Lesser General Public License for more details. | ||
//=== | ||
//=== You should have received a copy of the GNU Lesser General Public | ||
//=== License along with this library; if not, write to the Free Software | ||
//=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
//=== | ||
//=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2, | ||
//=== Rome - Italy. email: [email protected] | ||
//============================================================================== | ||
|
||
package org.fao.geonet.api.exception; | ||
|
||
import org.springframework.web.multipart.MaxUploadSizeExceededException; | ||
|
||
/** | ||
* Custom exception to be thrown when the size of a remote file to be uploaded to the store exceeds the maximum upload size. | ||
*/ | ||
public class InputStreamLimitExceededException extends MaxUploadSizeExceededException { | ||
private final long remoteFileSize; | ||
|
||
/** | ||
* Create a new InputStreamLimitExceededException with an unknown remote file size. | ||
* | ||
* @param maxUploadSize the maximum upload size allowed | ||
*/ | ||
public InputStreamLimitExceededException(long maxUploadSize) { | ||
this(maxUploadSize, -1L); | ||
} | ||
|
||
/** | ||
* Create a new InputStreamLimitExceededException with a known remote file size. | ||
* | ||
* @param maxUploadSize the maximum upload size allowed | ||
* @param remoteFileSize the size of the remote file | ||
*/ | ||
public InputStreamLimitExceededException(long maxUploadSize, long remoteFileSize) { | ||
super(maxUploadSize); | ||
this.remoteFileSize = remoteFileSize; | ||
} | ||
|
||
/** | ||
* Get the size of the remote file. | ||
* | ||
* @return the size of the remote file or -1 if the size is unknown | ||
*/ | ||
public long getRemoteFileSize() { | ||
return this.remoteFileSize; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/src/main/java/org/fao/geonet/util/LimitedInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//============================================================================= | ||
//=== Copyright (C) 2001-2025 Food and Agriculture Organization of the | ||
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP) | ||
//=== and United Nations Environment Programme (UNEP) | ||
//=== | ||
//=== This library is free software; you can redistribute it and/or | ||
//=== modify it under the terms of the GNU Lesser General Public | ||
//=== License as published by the Free Software Foundation; either | ||
//=== version 2.1 of the License, or (at your option) any later version. | ||
//=== | ||
//=== This library is distributed in the hope that it will be useful, | ||
//=== but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
//=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
//=== Lesser General Public License for more details. | ||
//=== | ||
//=== You should have received a copy of the GNU Lesser General Public | ||
//=== License along with this library; if not, write to the Free Software | ||
//=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
//=== | ||
//=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2, | ||
//=== Rome - Italy. email: [email protected] | ||
//============================================================================== | ||
|
||
package org.fao.geonet.util; | ||
|
||
import org.fao.geonet.api.exception.InputStreamLimitExceededException; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Implementation of {@link org.apache.commons.fileupload.util.LimitedInputStream} that throws a | ||
* {@link InputStreamLimitExceededException} when the configured limit is exceeded. | ||
*/ | ||
public class LimitedInputStream extends org.apache.commons.fileupload.util.LimitedInputStream { | ||
|
||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param inputStream The input stream, which shall be limited. | ||
* @param pSizeMax The limit; no more than this number of bytes | ||
* shall be returned by the source stream. | ||
*/ | ||
public LimitedInputStream(InputStream inputStream, long pSizeMax) { | ||
super(inputStream, pSizeMax); | ||
} | ||
|
||
@Override | ||
protected void raiseError(long pSizeMax, long pCount) throws IOException { | ||
throw new InputStreamLimitExceededException(pSizeMax); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.