From b115a99907618d7ff04e8d9323b2c66ce40c970a Mon Sep 17 00:00:00 2001 From: l-jonas <43265000+l-jonas@users.noreply.github.com> Date: Sun, 9 Dec 2018 12:00:40 +0100 Subject: [PATCH] Path validation (#122) * Remove isTrimmed path validation rule * Include exact reason in path validation exceptions --- .../syncthing/java/core/utils/PathUtils.kt | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/syncthing-core/src/main/kotlin/net/syncthing/java/core/utils/PathUtils.kt b/syncthing-core/src/main/kotlin/net/syncthing/java/core/utils/PathUtils.kt index 9967005..4f6371b 100644 --- a/syncthing-core/src/main/kotlin/net/syncthing/java/core/utils/PathUtils.kt +++ b/syncthing-core/src/main/kotlin/net/syncthing/java/core/utils/PathUtils.kt @@ -34,42 +34,57 @@ object PathUtils { return pathSegments.contains(PARENT_PATH) or pathSegments.contains(CURRENT_PATH) } - private fun isTrimmed(value: String) = value.trim() == value private fun containsWindowsPathSeparator(path: String) = path.contains(PATH_SEPARATOR_WIN) - private fun startsWithPathSeperator(path: String) = path.startsWith(PATH_SEPARATOR) - private fun isValidPath(path: String) = (!containsRelativeElements(path)) and - (!containsWindowsPathSeparator(path)) and - path.isNotEmpty() and - (!startsWithPathSeperator(path)) and - isTrimmed(path) + private fun startsWithPathSeparator(path: String) = path.startsWith(PATH_SEPARATOR) private fun containsPathSeparator(file: String) = file.contains(PATH_SEPARATOR) or file.contains(PATH_SEPARATOR_WIN) - private fun isFilenameValid(file: String) = file.isNotBlank() and - (!containsPathSeparator(file)) and - isTrimmed(file) private fun assertPathValid(path: String) { - if (!isValidPath(path)) { + fun throwException(reason: String) { throw ExceptionDetailException( - IllegalArgumentException("provided path is invalid"), + IllegalArgumentException("provided path is invalid because it $reason"), ExceptionDetails( component = "PathUtils", - details = "processed path: $path" + details = "processed path: \"$path\"" ) ) } + + if (containsRelativeElements(path)) { + throwException("contains relative path elements") + } + + if (containsWindowsPathSeparator(path)) { + throwException("contains windows path separators") + } + + if (path.isEmpty()) { + throwException("is empty") + } + + if (startsWithPathSeparator(path)) { + throwException("starts with a path separator") + } } private fun assertFilenameValid(filename: String) { - if (!isFilenameValid(filename)) { + fun throwException(reason: String) { throw ExceptionDetailException( - IllegalArgumentException("provided filename is invalid"), + IllegalArgumentException("provided filename is invalid because the filename $reason"), ExceptionDetails( component = "PathUtils", - details = "processed filename: $filename" + details = "processed filename: \"$filename\"" ) ) } + + if (filename.isBlank()) { + throwException("is blank") + } + + if (containsPathSeparator(filename)) { + throwException("contains a path separator") + } } fun isParent(path: String): Boolean {