fix as per review

This commit is contained in:
Nicola Murino 2021-04-27 18:06:39 +02:00
parent db2d41339c
commit 0b43101c5b
4 changed files with 13 additions and 13 deletions

View File

@ -469,7 +469,7 @@ func (fs *root) Realpath(p string) string {
if fs.startDirectory == "" || fs.startDirectory == "/" { if fs.startDirectory == "" || fs.startDirectory == "/" {
return cleanPath(p) return cleanPath(p)
} }
return cleanPathWithBase(p, fs.startDirectory) return cleanPathWithBase(fs.startDirectory, p)
} }
// In memory file-system-y thing that the Hanlders live on // In memory file-system-y thing that the Hanlders live on

View File

@ -86,13 +86,13 @@ type LstatFileLister interface {
Lstat(*Request) (ListerAt, error) Lstat(*Request) (ListerAt, error)
} }
// RealpathFileLister is a FileLister that implements the Realpath method. // RealPathFileLister is a FileLister that implements the Realpath method.
// We use "/" as start directory for relative paths, implementing this // We use "/" as start directory for relative paths, implementing this
// interface you can customize the start directory. // interface you can customize the start directory.
// You have to return an absolute POSIX path. // You have to return an absolute POSIX path.
type RealpathFileLister interface { type RealPathFileLister interface {
FileLister FileLister
Realpath(string) string RealPath(string) string
} }
// ListerAt does for file lists what io.ReaderAt does for files. // ListerAt does for file lists what io.ReaderAt does for files.

View File

@ -199,8 +199,8 @@ func (rs *RequestServer) packetWorker(
rpkt = statusFromError(pkt.ID, rs.closeRequest(handle)) rpkt = statusFromError(pkt.ID, rs.closeRequest(handle))
case *sshFxpRealpathPacket: case *sshFxpRealpathPacket:
var realPath string var realPath string
if realPather, ok := rs.Handlers.FileList.(RealpathFileLister); ok { if realPather, ok := rs.Handlers.FileList.(RealPathFileLister); ok {
realPath = realPather.Realpath(pkt.getPath()) realPath = realPather.RealPath(pkt.getPath())
} else { } else {
realPath = cleanPath(pkt.getPath()) realPath = cleanPath(pkt.getPath())
} }
@ -284,10 +284,10 @@ func cleanPacketPath(pkt *sshFxpRealpathPacket, realPath string) responsePacket
// Makes sure we have a clean POSIX (/) absolute path to work with // Makes sure we have a clean POSIX (/) absolute path to work with
func cleanPath(p string) string { func cleanPath(p string) string {
return cleanPathWithBase(p, "/") return cleanPathWithBase("/", p)
} }
func cleanPathWithBase(p, base string) string { func cleanPathWithBase(base, p string) string {
p = filepath.ToSlash(p) p = filepath.ToSlash(p)
if !path.IsAbs(p) { if !path.IsAbs(p) {
return path.Join(base, p) return path.Join(base, p)

View File

@ -816,15 +816,15 @@ func TestRealPath(t *testing.T) {
} }
p := root.Realpath(".") p := root.Realpath(".")
require.Equal(t, root.startDirectory, p) assert.Equal(t, root.startDirectory, p)
p = root.Realpath("/") p = root.Realpath("/")
require.Equal(t, "/", p) assert.Equal(t, "/", p)
p = root.Realpath("..") p = root.Realpath("..")
require.Equal(t, "/", p) assert.Equal(t, "/", p)
p = root.Realpath("../../..") p = root.Realpath("../../..")
require.Equal(t, "/", p) assert.Equal(t, "/", p)
p = root.Realpath("relpath") p = root.Realpath("relpath")
require.Equal(t, path.Join(root.startDirectory, "relpath"), p) assert.Equal(t, path.Join(root.startDirectory, "relpath"), p)
} }
func TestCleanPath(t *testing.T) { func TestCleanPath(t *testing.T) {