mirror of
https://github.com/appleboy/drone-ssh.git
synced 2025-05-09 18:23:21 +08:00
improve: shell escaping, allow for whitespace and single quotes (#108)
This commit is contained in:
parent
f92f762c9d
commit
7f4cb1c1d0
@ -43,6 +43,10 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func escapeArg(arg string) string {
|
||||||
|
return "'" + strings.Replace(arg, "'", `'\''`, -1) + "'"
|
||||||
|
}
|
||||||
|
|
||||||
func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
|
func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
|
||||||
// Create MakeConfig instance with remote username, server address and path to private key.
|
// Create MakeConfig instance with remote username, server address and path to private key.
|
||||||
ssh := &easyssh.MakeConfig{
|
ssh := &easyssh.MakeConfig{
|
||||||
@ -72,8 +76,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
|
|||||||
for _, key := range p.Config.Envs {
|
for _, key := range p.Config.Envs {
|
||||||
key = strings.ToUpper(key)
|
key = strings.ToUpper(key)
|
||||||
val := os.Getenv(key)
|
val := os.Getenv(key)
|
||||||
val = strings.Replace(val, " ", "", -1)
|
env = append(env, key+"="+escapeArg(val))
|
||||||
env = append(env, key+"='"+val+"'")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Config.Script = append(env, p.Config.Script...)
|
p.Config.Script = append(env, p.Config.Script...)
|
||||||
|
@ -232,7 +232,7 @@ func TestSSHCommandExitCodeError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSetENV(t *testing.T) {
|
func TestSetENV(t *testing.T) {
|
||||||
os.Setenv("FOO", "1)")
|
os.Setenv("FOO", `' 1) '`)
|
||||||
plugin := Plugin{
|
plugin := Plugin{
|
||||||
Config: Config{
|
Config: Config{
|
||||||
Host: []string{"localhost"},
|
Host: []string{"localhost"},
|
||||||
|
Loading…
Reference in New Issue
Block a user