Skip to content

Commit 9f20495

Browse files
committed
Fix empty filename not checked (#178)
* Fix empty file name not checked Signed-off-by: Milen Pivchev <milen.pivchev@gmail.com> * trim whitespace Signed-off-by: Milen Pivchev <milen.pivchev@gmail.com> --------- Signed-off-by: Milen Pivchev <milen.pivchev@gmail.com>
1 parent 264bcf0 commit 9f20495

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Sources/NextcloudKit/Utils/FileNameValidator.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public final class FileNameValidator: Sendable {
1010
private let forbiddenFileNameCharacters: [String]
1111
private let forbiddenFileNameExtensions: [String]
1212

13+
public func fileEmptyNameError() -> NKError {
14+
NKError(errorCode: NSURLErrorCannotCreateFile, errorDescription: NSLocalizedString("_file_name_empty_", value: "File name cannot be empty.", comment: ""))
15+
}
16+
1317
public func fileWithSpaceError() -> NKError {
1418
NKError(errorCode: NSURLErrorCannotCreateFile, errorDescription: NSLocalizedString("_file_name_validator_error_space_", value: "Name must not contain spaces at the beginning or end.", comment: ""))
1519
}
@@ -40,6 +44,10 @@ public final class FileNameValidator: Sendable {
4044
}
4145

4246
public func checkFileName(_ filename: String) -> NKError? {
47+
if filename.trimmingCharacters(in: .whitespaces).isEmpty {
48+
return fileEmptyNameError()
49+
}
50+
4351
if let regex = try? NSRegularExpression(pattern: "[\(forbiddenFileNameCharacters.joined())]"), let invalidCharacterError = checkInvalidCharacters(string: filename, regex: regex) {
4452
return invalidCharacterError
4553
}

Tests/NextcloudKitUnitTests/FileNameValidatorUnitTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,18 @@ class FileNameValidatorUnitTests: XCTestCase {
130130
let result = fileNameValidator.checkFolderPath(folderPath)
131131
XCTAssertFalse(result)
132132
}
133+
134+
func testFileNameEmpty() {
135+
let folderPath = "/A1/Aaaww/W/ "
136+
let filePaths = ["", " ", " ", " "]
137+
138+
let result = fileNameValidator.checkFolderPath(folderPath)
139+
XCTAssertFalse(result)
140+
141+
filePaths.forEach { path in
142+
let result = fileNameValidator.checkFileName(path)
143+
144+
XCTAssertNotNil(result?.errorDescription)
145+
}
146+
}
133147
}

0 commit comments

Comments
 (0)