Skip to content

Commit e5d7aea

Browse files
committed
Fix matching of paths containing ../
1 parent 579e008 commit e5d7aea

4 files changed

Lines changed: 13 additions & 5 deletions

File tree

Sources/Globs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@ func matchGlobs(_ globs: [Glob], in directory: String) -> [URL] {
106106
}
107107
}
108108
}
109-
enumerate(URL(fileURLWithPath: directory))
109+
enumerate(URL(fileURLWithPath: directory).standardized)
110110
return urls
111111
}

Sources/SwiftFormat.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public func enumerateFiles(withInputURL inputURL: URL,
272272

273273
// Process configuration in all directories in specified path.
274274
func gatherOptions(_ options: inout Options, for inputURL: URL, with logger: Logger?) throws {
275-
var directory = URL(fileURLWithPath: inputURL.pathComponents[0])
275+
var directory = URL(fileURLWithPath: inputURL.pathComponents[0]).standardized
276276
for part in inputURL.pathComponents.dropFirst().dropLast() {
277277
directory.appendPathComponent(part)
278278
if shouldSkipFile(directory, with: options) {
@@ -606,12 +606,12 @@ public func lint(
606606

607607
public func expandPath(_ path: String, in directory: String) -> URL {
608608
if path.hasPrefix("/") {
609-
return URL(fileURLWithPath: path)
609+
return URL(fileURLWithPath: path).standardized
610610
}
611611
if path.hasPrefix("~") {
612-
return URL(fileURLWithPath: NSString(string: path).expandingTildeInPath)
612+
return URL(fileURLWithPath: NSString(string: path).expandingTildeInPath).standardized
613613
}
614-
return URL(fileURLWithPath: directory).appendingPathComponent(path)
614+
return URL(fileURLWithPath: directory).appendingPathComponent(path).standardized
615615
}
616616

617617
struct ResourceValues {

Tests/GlobsTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ class GlobsTests: XCTestCase {
123123
XCTAssertEqual(matchGlobs(expandGlobs(path, in: directory.path), in: directory.path).count, 1)
124124
}
125125

126+
func testExpandPathWithDotDot() {
127+
let path = "Tests/BadConfig/../SwiftFormatTests.swift"
128+
let directory = URL(fileURLWithPath: #file)
129+
.deletingLastPathComponent().deletingLastPathComponent()
130+
XCTAssertEqual(matchGlobs(expandGlobs(path, in: directory.path), in: directory.path).count, 1)
131+
}
132+
126133
// MARK: glob regex
127134

128135
func testWildcardRegex() {

Tests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ extension GlobsTests {
191191
("testExpandPathsWithEitherOr", testExpandPathsWithEitherOr),
192192
("testExpandPathWithCharacterClass", testExpandPathWithCharacterClass),
193193
("testExpandPathWithCharacterClassRange", testExpandPathWithCharacterClassRange),
194+
("testExpandPathWithDotDot", testExpandPathWithDotDot),
194195
("testExpandPathWithDoubleWildcardAtEnd", testExpandPathWithDoubleWildcardAtEnd),
195196
("testExpandPathWithEitherOr", testExpandPathWithEitherOr),
196197
("testExpandPathWithEitherOrContainingDot", testExpandPathWithEitherOrContainingDot),

0 commit comments

Comments
 (0)