Package eu.europa.esig.dss.asic.common
Class SecureContainerHandler
java.lang.Object
eu.europa.esig.dss.asic.common.SecureContainerHandler
- All Implemented Interfaces:
ZipContainerHandler
The default implementation of
ZipContainerHandler
, providing
utilities to prevent a denial of service attacks, such as zip-bombing-
Constructor Summary
ConstructorDescriptionDefault constructor instantiating handler with default configuration -
Method Summary
Modifier and TypeMethodDescriptionprotected void
buildZip
(List<DSSDocument> containerEntries, Date creationTime, String zipComment, ZipOutputStream zos) This method stores allcontainerEntries
in a given order to aZipOutputStream
with the given parameterscreateZipArchive
(List<DSSDocument> containerEntries, Date creationTime, String zipComment) Creates a ZIP-Archive with the givencontainerEntries
extractContainerContent
(DSSDocument zipArchive) Extracts a list ofDSSDocument
from the given ZIP-archiveextractEntryNames
(DSSDocument zipArchive) Returns a list of ZIP archive entry namesprotected ZipEntry
getZipEntry
(DSSDocument entry, Date creationTime) Creates a newZipEntry
for the givenDSSDocument
atcreationTime
protected DSSResourcesHandler
This method instantiates a newDSSResourcesFactory
protected void
secureCopy
(InputStream is, OutputStream os, long allowedSize) Reads and copies InputStream in a secure way to OutputStream.protected void
secureSkip
(InputStream is, long allowedSize) This method allows skipping securely InputStream without caching the contentvoid
setExtractComments
(boolean extractComments) Sets whether comments of ZIP entries shall be extracted.void
setMaxAllowedFilesAmount
(int maxAllowedFilesAmount) Sets the maximum allowed amount of files inside a container Default : 1000void
setMaxCompressionRatio
(long maxCompressionRatio) Sets the maximum allowed compression ratio If the container compression ratio exceeds the value, an exception is being thrown Default : 100void
setMaxMalformedFiles
(int maxMalformedFiles) Sets the maximum allowed amount of malformed files Default : 100void
setResourcesHandlerBuilder
(DSSResourcesHandlerBuilder resourcesHandlerBuilder) SetsDSSResourcesFactoryBuilder
to be used for aDSSResourcesHandler
creation in internal methods.void
setThreshold
(long threshold) Sets the maximum allowed threshold after exceeding each the security checks are enforced Default : 1000000 (1 MB)
-
Constructor Details
-
SecureContainerHandler
public SecureContainerHandler()Default constructor instantiating handler with default configuration
-
-
Method Details
-
setThreshold
public void setThreshold(long threshold) Sets the maximum allowed threshold after exceeding each the security checks are enforced Default : 1000000 (1 MB)- Parameters:
threshold
- in bytes
-
setMaxCompressionRatio
public void setMaxCompressionRatio(long maxCompressionRatio) Sets the maximum allowed compression ratio If the container compression ratio exceeds the value, an exception is being thrown Default : 100- Parameters:
maxCompressionRatio
- the maximum compression ratio
-
setMaxAllowedFilesAmount
public void setMaxAllowedFilesAmount(int maxAllowedFilesAmount) Sets the maximum allowed amount of files inside a container Default : 1000- Parameters:
maxAllowedFilesAmount
- the maximum number of allowed files
-
setMaxMalformedFiles
public void setMaxMalformedFiles(int maxMalformedFiles) Sets the maximum allowed amount of malformed files Default : 100- Parameters:
maxMalformedFiles
- the maximum number of malformed files
-
setExtractComments
public void setExtractComments(boolean extractComments) Sets whether comments of ZIP entries shall be extracted. Enabling of the feature can be useful when editing an existing archive, in order to preserve the existing data (i.e. comments). When enabled, slightly decreases the performance (about 10% forextractContainerContent(zipArchive)
method). Reason : All ZIP entries from a ZIP archive are extracted usingjava.util.zip.ZipInputStream
, that is not able to extract comments for entries. In order to extract comments, the archive shall be read again usingjava.util.zip.ZipFile
. For more information about limitations please see<a href="https://stackoverflow.com/a/70848140">the link</a>
. Default : false (not extracted)- Parameters:
extractComments
- whether comments shall be extracted
-
setResourcesHandlerBuilder
SetsDSSResourcesFactoryBuilder
to be used for aDSSResourcesHandler
creation in internal methods.DSSResourcesHandler
defines a way to operate with OutputStreams and createDSSDocument
s. Default :eu.europa.esig.dss.signature.resources.InMemoryResourcesHandler
. Works with data in memory.- Parameters:
resourcesHandlerBuilder
-DSSResourcesHandlerBuilder
-
extractContainerContent
Description copied from interface:ZipContainerHandler
Extracts a list ofDSSDocument
from the given ZIP-archive- Specified by:
extractContainerContent
in interfaceZipContainerHandler
- Parameters:
zipArchive
-DSSDocument
- Returns:
- a list of
DSSDocument
s
-
extractEntryNames
Description copied from interface:ZipContainerHandler
Returns a list of ZIP archive entry names- Specified by:
extractEntryNames
in interfaceZipContainerHandler
- Parameters:
zipArchive
-DSSDocument
- Returns:
- a list of
String
entry names
-
createZipArchive
public DSSDocument createZipArchive(List<DSSDocument> containerEntries, Date creationTime, String zipComment) Description copied from interface:ZipContainerHandler
Creates a ZIP-Archive with the givencontainerEntries
- Specified by:
createZipArchive
in interfaceZipContainerHandler
- Parameters:
containerEntries
- a list ofDSSDocument
s to embed into the new container instancecreationTime
- (Optional)Date
defined time of an archive creation, will be set for all embedded files. If null, the local current time will be usedzipComment
- (Optional)String
defined a zipComment- Returns:
DSSDocument
ZIP-Archive
-
instantiateResourcesHandler
This method instantiates a newDSSResourcesFactory
- Returns:
DSSResourcesHandler
- Throws:
IOException
- if an error occurs on DSSResourcesHandler instantiation
-
buildZip
protected void buildZip(List<DSSDocument> containerEntries, Date creationTime, String zipComment, ZipOutputStream zos) throws IOException This method stores allcontainerEntries
in a given order to aZipOutputStream
with the given parameters- Parameters:
containerEntries
- a list ofDSSDocument
s to storecreationTime
-Date
ZIP archive creation timezipComment
-String
zip comment (optional)zos
-ZipOutputStream
to consume the ZIP entries- Throws:
IOException
- in case an error occurs onZipOutputStream
update
-
getZipEntry
Creates a newZipEntry
for the givenDSSDocument
atcreationTime
- Parameters:
entry
-DSSDocument
to be placed within a ZIP containercreationTime
-Date
the creation time of ZIP container- Returns:
ZipEntry
-
secureCopy
Reads and copies InputStream in a secure way to OutputStream. Detects "ZipBombing" (large files inside a zip container) depending on the provided container size- Parameters:
is
-InputStream
of fileos
-OutputStream
where save file to.allowedSize
- defines an allowed size of the ZIP container entries, if -1 skips the validation- Throws:
IOException
- if an exception occurs
-
secureSkip
This method allows skipping securely InputStream without caching the content- Parameters:
is
-InputStream
to skipallowedSize
- the maximum allowed size of the extracted content- Throws:
IOException
- if an exception occurs
-