Skip to content

Commit 272b72d

Browse files
authored
Fix crash with absolute path to binary artifact (#3737)
The code in `Workspace.parseArtifacts()` assumes prior validation of paths even though it is running before any such validation. rdar://83059393
1 parent d82f52a commit 272b72d

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Sources/Workspace/Workspace.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,8 +1777,7 @@ extension Workspace {
17771777
for target in manifest.targets where target.type == .binary {
17781778
if let path = target.path {
17791779
// TODO: find a better way to get the base path (not via the manifest)
1780-
// the target path is validated earlier to be within the package directory
1781-
let absolutePath = manifest.path.parentDirectory.appending(RelativePath(path))
1780+
let absolutePath = try manifest.path.parentDirectory.appending(RelativePath(validating: path))
17821781
localArtifacts.append(
17831782
.local(
17841783
packageRef: packageReference,

Tests/WorkspaceTests/WorkspaceTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7020,6 +7020,45 @@ final class WorkspaceTests: XCTestCase {
70207020
}
70217021
}
70227022
}
7023+
7024+
func testBinaryArtifactsInvalidPath() throws {
7025+
let fs = localFileSystem
7026+
try testWithTemporaryDirectory { path in
7027+
let foo = path.appending(component: "foo")
7028+
7029+
try fs.writeFileContents(foo.appending(component: "Package.swift")) {
7030+
$0 <<<
7031+
"""
7032+
// swift-tools-version:5.3
7033+
import PackageDescription
7034+
let package = Package(
7035+
name: "Best",
7036+
targets: [
7037+
.binaryTarget(name: "best", path: "/best.xcframework")
7038+
]
7039+
)
7040+
"""
7041+
}
7042+
7043+
let manifestLoader = ManifestLoader(toolchain: ToolchainConfiguration.default)
7044+
let sandbox = path.appending(component: "ws")
7045+
let workspace = try Workspace(
7046+
fileSystem: fs,
7047+
location: .init(forRootPackage: sandbox, fileSystem: fs),
7048+
customManifestLoader: manifestLoader,
7049+
delegate: MockWorkspaceDelegate()
7050+
)
7051+
7052+
do {
7053+
let diagnostics = DiagnosticsEngine()
7054+
try workspace.resolve(root: .init(packages: [foo]), diagnostics: diagnostics)
7055+
} catch {
7056+
XCTAssertEqual(error.localizedDescription, "invalid relative path '/best.xcframework'; relative path should not begin with '/' or '~'")
7057+
return
7058+
}
7059+
XCTFail("unexpected success")
7060+
}
7061+
}
70237062
}
70247063

70257064
struct DummyError: LocalizedError, Equatable {

0 commit comments

Comments
 (0)