diff --git a/src/Paket.Core/Dependencies/ModuleResolver.fs b/src/Paket.Core/Dependencies/ModuleResolver.fs index 20f74264bb..96bba83b51 100644 --- a/src/Paket.Core/Dependencies/ModuleResolver.fs +++ b/src/Paket.Core/Dependencies/ModuleResolver.fs @@ -16,6 +16,14 @@ type Origin = let internal computeFilePath(owner,project,root,groupName:GroupName,name : string) = let path = normalizePath (name.TrimStart('/')) + // A malformed dependency spec (e.g. a missing version after "~>") can cause the + // "file name" part to be parsed as "~>", which normalizePath then expands into an + // absolute path rooted at the user's home directory. Path.Combine silently discards + // the preceding (root-relative) segments whenever a later segment is rooted, which + // would otherwise make the computed destination point outside of the intended + // paket-files directory - see https://github.com/fsprojects/Paket/issues/4306 + if Path.IsPathRooted path then + failwithf "Invalid remote file specification '%s' for %s/%s - the resolved file name must not be an absolute path" name owner project let owner = if String.IsNullOrWhiteSpace owner then "localhost" else owner let dir = if groupName = Constants.MainDependencyGroup then diff --git a/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs b/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs index 282429af77..221ae1f78f 100644 --- a/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs +++ b/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs @@ -386,6 +386,21 @@ let ``should read github source file from config without quotes``() = PackagePath = None AuthKey = Some "github" }] +[] +let ``should not compute a file path outside paket-files for a malformed github source spec``() = + // Regression test for https://github.com/fsprojects/Paket/issues/4306 : + // "github myorg/repo:main ~>" (a missing version after "~>") used to be parsed + // such that the "~>" ended up as the remote file's Name, which normalizePath then + // expanded into an absolute path rooted at the user's home directory. This made + // Path.Combine discard the paket-files root entirely, so a subsequent CleanDir + // wiped out the user's home directory instead of a paket-files subfolder. + let config = """github myorg/repo:main ~>""" + let dependencies = DependenciesFile.FromSource(config) + let remoteFile = dependencies.Groups.[Constants.MainDependencyGroup].RemoteFiles.Head + remoteFile.Name |> shouldEqual "~>" + (fun () -> remoteFile.FilePath("/some/root", Constants.MainDependencyGroup) |> ignore) + |> shouldFail + [] let ``should read github source file from config with quotes``() = let config = """github fsharp/FAKE:master "src/app/FAKE/Cli.fs"