Class FilenameUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final char[]Reserved characters -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcalculateExtension(Metadata metadata, String defaultValue) Calculate the extension based on theHttpHeaders.CONTENT_TYPEvalue.static StringThis is a duplication of the algorithm and functionality available in commons io FilenameUtils.static StringgetSanitizedEmbeddedFileName(Metadata metadata, String defaultExtension, int maxLength) static StringgetSanitizedEmbeddedFilePath(Metadata metadata, String defaultExtension, int maxLength) This tries to sanitize dangerous user generated embedded file paths.static StringgetSuffixFromPath(String path) This includes the period, e.g. ".pdf".static StringScans the given file name for reserved characters on different OSs and file systems and returns a sanitized version of the name with the reserved chars replaced by their hexadecimal value.static PathresolveWithin(Path dir, String name) Resolvesnameagainstdirand returns the result only if it stays withindir.
-
Field Details
-
RESERVED_FILENAME_CHARACTERS
public static final char[] RESERVED_FILENAME_CHARACTERSReserved characters
-
-
Constructor Details
-
FilenameUtils
public FilenameUtils()
-
-
Method Details
-
normalize
Scans the given file name for reserved characters on different OSs and file systems and returns a sanitized version of the name with the reserved chars replaced by their hexadecimal value.For example
why?.zipwill be converted intowhy%3F.zip- Parameters:
name- the file name to be normalized - NOT NULL- Returns:
- the normalized file name
- Throws:
IllegalArgumentException- if name is null
-
getName
This is a duplication of the algorithm and functionality available in commons io FilenameUtils. If Java's File were able handle Windows file paths correctly in linux, we wouldn't need this.The goal of this is to get a filename from a path. The package parsers and some other embedded doc extractors could put anything into TikaCoreProperties.RESOURCE_NAME_KEY.
If a careless client used that filename as if it were a filename and not a path when writing embedded files, bad things could happen. Consider: "../../../my_ppt.ppt".
Consider using this in combination with
normalize(String).- Parameters:
path- path to strip- Returns:
- empty string or a filename, never null
-
getSuffixFromPath
-
getSanitizedEmbeddedFileName
-
getSanitizedEmbeddedFilePath
public static String getSanitizedEmbeddedFilePath(Metadata metadata, String defaultExtension, int maxLength) This tries to sanitize dangerous user generated embedded file paths. If trusting these paths for writing files, users should run checks to make sure that the generated file path does not zipslip out of the target directory.- Parameters:
metadata-defaultExtension-maxLength-- Returns:
-
resolveWithin
Resolvesnameagainstdirand returns the result only if it stays withindir.The result is
normalizedbefore it is checked. This is the load-bearing step: without it, resolving a name such as../xyields the literal pathdir/../x, whose path elements still begin withdir, so thePath.startsWith(Path)check below would wrongly accept it. Normalizing first collapses the..so the check sees the real location.Containment is tested with
Path.startsWith(Path)(element by element) rather than by comparing path strings, so a sibling whose name merely shares a textual prefix withdir(for example/a/bcagainst/a/b) is correctly treated as being outsidedir.- Parameters:
dir- the directory the resolved path must stay withinname- the child name to resolve againstdir; may contain relative segments such as..- Returns:
- the resolved, normalized path, guaranteed to start with the normalized
dir - Throws:
IOException- ifnameresolves to a location outsidedir
-
calculateExtension
Calculate the extension based on theHttpHeaders.CONTENT_TYPEvalue. On parse exception or null value, return the default value.- Parameters:
metadata-defaultValue-- Returns:
- the extension based on the mime type, including the initial "."
-