Quoting filesystem path in scp command argument (#28626)

* Quoting filesystem path in scp command argument

* Adding proper shell quoting for scp commands

* Running go fmt

* Using a library for quoting shell commands

* Don't export quoteShell function
This commit is contained in:
Bryan Eastes 2021-06-15 10:04:01 -07:00 committed by GitHub
parent 2ecdf44918
commit 714632206c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -18,6 +18,7 @@ import (
"sync"
"time"
"github.com/apparentlymart/go-shquot/shquot"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/internal/communicator/remote"
"github.com/hashicorp/terraform/internal/provisioners"
@ -419,7 +420,11 @@ func (c *Communicator) Upload(path string, input io.Reader) error {
return scpUploadFile(targetFile, input, w, stdoutR, size)
}
return c.scpSession("scp -vt "+targetDir, scpFunc)
cmd, err := quoteShell([]string{"scp", "-vt", targetDir}, c.connInfo.TargetPlatform)
if err != nil {
return err
}
return c.scpSession(cmd, scpFunc)
}
// UploadScript implementation of communicator.Communicator interface
@ -488,7 +493,11 @@ func (c *Communicator) UploadDir(dst string, src string) error {
return uploadEntries()
}
return c.scpSession("scp -rvt "+dst, scpFunc)
cmd, err := quoteShell([]string{"scp", "-rvt", dst}, c.connInfo.TargetPlatform)
if err != nil {
return err
}
return c.scpSession(cmd, scpFunc)
}
func (c *Communicator) newSession() (session *ssh.Session, err error) {
@ -816,3 +825,15 @@ func (c *bastionConn) Close() error {
c.Conn.Close()
return c.Bastion.Close()
}
func quoteShell(args []string, targetPlatform string) (string, error) {
if targetPlatform == TargetPlatformUnix {
return shquot.POSIXShell(args), nil
}
if targetPlatform == TargetPlatformWindows {
return shquot.WindowsArgv(args), nil
}
return "", fmt.Errorf("Cannot quote shell command, target platform unknown: %s", targetPlatform)
}

View File

@ -642,7 +642,11 @@ func TestAccHugeUploadFile(t *testing.T) {
return scpUploadFile(targetFile, source, w, stdoutR, size)
}
err = c.scpSession("scp -vt "+targetDir, scpFunc)
cmd, err := quoteShell([]string{"scp", "-vt", targetDir}, c.connInfo.TargetPlatform)
if err != nil {
t.Fatal(err)
}
err = c.scpSession(cmd, scpFunc)
if err != nil {
t.Fatal(err)
}