Fix Windows path detection in cleanPathWithBase#644
Conversation
Use filepath.IsAbs on the original path before converting to forward slashes. This correctly handles Windows paths like "C:\foo" which filepath.IsAbs recognizes but path.IsAbs (after ToSlash conversion to "C:/foo") does not.
|
Remote paths to windows servers require absolute paths to be encoded as This is not a bug. |
|
I keep thinking I’m missing something here. I’m thinking I need this explained better. This code is only called from within the |
|
The premise that "this code is only called from within the
On Windows, a Go program calling either of these with a native path — e.g. That happens because Agreed that the wire form for SFTP-to-Windows is Happy to add a Windows test that exercises this if that would help. |
|
Yeah, a Windows-specific test would be great. 🤔 It looks like you’re right, there are a few API positions where we’re calling this on a string that users might naturally expect to use a local filepath, rather than a POSIX path. It might be a good idea to instead split these uses into separate |
Summary
Fix
cleanPathWithBaseto usefilepath.IsAbson the original path before converting to forward slashes. This correctly handles Windows paths likeC:\foowhichfilepath.IsAbsrecognizes butpath.IsAbs(afterToSlashconversion toC:/foo) does not.Problem
When using
WithStartDirectoryon Windows, absolute paths likeC:\Users\fooare incorrectly treated as relative paths because:filepath.ToSlash(filepath.Clean("C:\\Users\\foo"))→C:/Users/foopath.IsAbs("C:/Users/foo")→false(only checks for/prefix)Solution
Check
filepath.IsAbs(p)on the original path before theToSlashconversion.filepath.IsAbsis platform-aware and correctly identifies Windows absolute paths.Related
This fix resolves Windows SFTP path handling issues in owenthereal/upterm#440