Package com.atlassian.bamboo.file
Class SafeFileOperationFactory<T>
java.lang.Object
com.atlassian.bamboo.file.SafeFileOperationFactory<T>
- Direct Known Subclasses:
SafeFileFactory,SafePathFactory
Factory for creating "file operations" with validation against path traversal vulnerabilities.
"File operation" is an object of any type that offers file-related operations, such as creating a file,
reading a file, etc.
It ensures that the file paths used in these operations are safe and within a secure directory.
This class is designed to be extended for specific file operations, see SafeFileFactory and SafePathFactory.
- Since:
- 12.0.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal Tcreate()Creates a file operation in a secure directory validating the path against path traversal vulnerabilities.final TcreateWithoutValidation(@NotNull SafeFileOperationFactory.NoValidationReason reason) Creates a file operation without validating the path.protected abstract Tget()protected abstract StringtoPath()Returns the path to the file.withSecureDirectory(File secureDirectory) Sets the secure directory path where the file is expected to be accessed.withSecureDirectory(@NotNull String secureDirectory) Sets the secure directory path where the file is expected to be accessed.withSecureDirectory(Path secureDirectory) Sets the secure directory path where the file is expected to be accessed.
-
Constructor Details
-
SafeFileOperationFactory
public SafeFileOperationFactory()
-
-
Method Details
-
withSecureDirectory
Sets the secure directory path where the file is expected to be accessed.If the path used to create the file operation is not within this secure directory, create() will throw an IllegalArgumentException.
- Parameters:
secureDirectory- the secure directory path where the file is expected to be accessed
-
withSecureDirectory
Sets the secure directory path where the file is expected to be accessed.If the path used to create the file operation is not within this secure directory, create() will throw an IllegalArgumentException.
- Parameters:
secureDirectory- the secure directory where the file is expected to be accessed
-
withSecureDirectory
Sets the secure directory path where the file is expected to be accessed.If the path used to create the file operation is not within this secure directory, create() will throw an IllegalArgumentException.
- Parameters:
secureDirectory- the secure directory where the file is expected to be accessed
-
create
Creates a file operation in a secure directory validating the path against path traversal vulnerabilities.withSecureDirectory(String)has to be called before!- Returns:
- a file operation instance that is created in the secure directory
- Throws:
IllegalArgumentException- when the path contains path traversal constructs or is outside the secure directory
-
createWithoutValidation
public final T createWithoutValidation(@NotNull @NotNull SafeFileOperationFactory.NoValidationReason reason) Creates a file operation without validating the path.It's forbidden to use this method if it's possible that a path to the file or directory we want to operate on can contain user input
- Parameters:
reason- the reason for skipping validation, e.g. if the path is hard-coded or immutable- Returns:
- a file operation instance that is created without the validation
-
toPath
Returns the path to the file. This should be the file to which the file operator give the access. e.g. for java.io.File it would be java.io.File#getPath()- Returns:
- the path as a string
-
get
-