Package de.xima.fc.security.malware
Class ExtensionCheck
- java.lang.Object
-
- de.xima.fc.security.malware.ExtensionCheck
-
public class ExtensionCheck extends Object
Helper class to verify a file's extension against its content.
-
-
Constructor Summary
Constructors Constructor Description ExtensionCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ExtensionCheck
setAllowUnknownTypes(boolean allowUnknownTypes)
Sets whether unknown types (undetected by the ContentTypeUtils or unknown to the context mime mapper) should be allowed or rejected.ExtensionCheck
setContextMimeMapper(Function<String,String> contextMimeMapper)
Sets the mandatory MIME mapper used for comparing the MIME type detected by the ContentTypeUtils to the one suggested by the 'context' for the provided file name, e.g.ExtensionCheck
setMimeNormalizer(IMimeNormalizer mimeNormalizer)
Sets an optional, use case dependent MIME type normalizer to handle different MIME types describing the same content (e.g.ExtensionCheckReport
verify(String fileName, InputStream inputStream)
Compares the MIME type detected by Tika (by examining the streams content) with the one suggested by the provided context MIME mapper.
-
-
-
Method Detail
-
setContextMimeMapper
public ExtensionCheck setContextMimeMapper(Function<String,String> contextMimeMapper)
Sets the mandatory MIME mapper used for comparing the MIME type detected by the ContentTypeUtils to the one suggested by the 'context' for the provided file name, e.g. by ServletContext.getMimeType(String).- Parameters:
contextMimeMapper
- AFunction
that takes a file name and returns the MIME type corresponding to the file name's extension- Returns:
- this
-
setMimeNormalizer
public ExtensionCheck setMimeNormalizer(IMimeNormalizer mimeNormalizer)
Sets an optional, use case dependent MIME type normalizer to handle different MIME types describing the same content (e.g. 'application/xml', 'text/xml' and 'application/xhtml+xml' may be mapped to 'text/plain').- Parameters:
mimeNormalizer
- the MIME normalizer to use.- Returns:
- this
-
setAllowUnknownTypes
public ExtensionCheck setAllowUnknownTypes(boolean allowUnknownTypes)
Sets whether unknown types (undetected by the ContentTypeUtils or unknown to the context mime mapper) should be allowed or rejected.- Parameters:
allowUnknownTypes
- the flag to set (if true, unknown types are allowed, if false not)- Returns:
- this
-
verify
public ExtensionCheckReport verify(String fileName, InputStream inputStream)
Compares the MIME type detected by Tika (by examining the streams content) with the one suggested by the provided context MIME mapper.- If no MIME normalizer has been set,
DFLT_NORMALIZER
will be used. - If the MIME normalizer has been explicitly set to
null
, no normalization will be done.
- Parameters:
fileName
- the name of the file (used to request the MIME type corresponding to the file's extension from the context MIME mapper)inputStream
- the input stream to detect the file's actual content type- Returns:
- a report object describing the result of this content type verification.
- If no MIME normalizer has been set,
-
-