-
Field Summary
Modifier and TypeFieldDescriptionstatic final Pattern
These patterns can be used in path strings to access files higher in the directory structure. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkFileIsWithinDirectory
(String path, String directoryPath) This method is to provide assurance that the file specified by the path parameter is within the directory specified by directoryPath.static void
Checks that the path string avoids bad patterns and meets our whitelist for acceptable characters.void
Deletes a directory, deleting everything inside it recursively afterwards.void
makeDirectory
(Path directory) Creates a directory if it doesn't already exist.byte[]
readBinaryFile
(String path) Read a binary file, return as a byte arrayreadTextFile
(String path) Read a text file from the given path, return as a string.static Path
safeResolve
(String parentDirectory, String path) This helper method will ensure that the requested path is within the parent directory and using safe charactersvoid
writeString
(Path path, String content) Write a string to a path on disk.
-
Field Details
-
badFilePathPatterns
These patterns can be used in path strings to access files higher in the directory structure. We disallow this, as a security precaution.- 1st Alternative
//
- This prevents going to the root directory - 2nd Alternative
..
- prevents going up a directory - 3rd Alternative
:
- prevents certain special paths, like "C:" or "file://" - 4th Alternative
^/
- prevents starting with a slash, meaning the root, but allows intermediate slashes. - 5th Alternative
:^\
- prevents starting with a backslash, meaning the root, but allows intermediate backslashes.
- 1st Alternative
-
-
Constructor Details
-
FileUtils
-
-
Method Details
-
writeString
Write a string to a path on disk.
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)
first against the path to ensure it uses valid characters and prevent it escaping the expected directory. -
deleteDirectoryRecursivelyIfExists
Deletes a directory, deleting everything inside it recursively afterwards. A more dangerous method than many others, take care.
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)
first against the path to ensure it uses valid characters and prevent it escaping the expected directory. -
makeDirectory
Creates a directory if it doesn't already exist.
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)
first against the path to ensure it uses valid characters and prevent it escaping the expected directory.If the directory does exist, the program will simply skip building it, and mention it in the logs.
-
readBinaryFile
Read a binary file, return as a byte array
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)
first against the path to ensure it uses valid characters and prevent it escaping the expected directory.If there is an error, this will return an empty byte array.
-
readTextFile
Read a text file from the given path, return as a string.
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)
first against the path to ensure it uses valid characters and prevent it escaping the expected directory.If there is an error, this will return an empty string.
-
checkFileIsWithinDirectory
This method is to provide assurance that the file specified by the path parameter is within the directory specified by directoryPath. Use this for any code that reads from files where the user provides untrusted input.- Throws:
InvariantException
- if the file is not within the directory
-
checkForBadFilePatterns
Checks that the path string avoids bad patterns and meets our whitelist for acceptable characters.- Throws:
InvariantException
- if there are any issues with the path string, such as being an empty string, containing known bad patterns frombadFilePathPatterns
, or including characters other than the set of characters we will allow for filenames. It is a simple set of ascii characters - alphanumerics, underscore, dash, period, forward and backward slash.
-
safeResolve
This helper method will ensure that the requested path is within the parent directory and using safe characters
-