Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command: escape the script string provided to lock #1845

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion command/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *LockCommand) run(args []string, lu **LockUnlock) int {
}
prefix := extra[0]
prefix = strings.TrimPrefix(prefix, "/")
script := strings.Join(extra[1:], " ")
script := strings.Join(escapeScript(extra[1:]), " ")

// Calculate a session name if none provided
if name == "" {
Expand Down Expand Up @@ -440,6 +440,19 @@ func (c *LockCommand) Synopsis() string {
return "Execute a command holding a lock"
}

// escapeScript escapes the given script so it can be safely used afterwards.
func escapeScript(script []string) []string {
for k, v := range script {
// If the script argument is itself composed of multiple strings,
// escape it with double quotes.
parts := strings.SplitN(v, " ", 2)
if len(parts) > 1 {
script[k] = "\"" + v + "\""
}
}
return script
}

// LockUnlock is used to abstract over the differences between
// a lock and a semaphore.
type LockUnlock struct {
Expand Down
83 changes: 68 additions & 15 deletions command/lock_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package command

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
Expand Down Expand Up @@ -43,8 +42,11 @@ func TestLockCommand_Run(t *testing.T) {
ui := new(cli.MockUi)
c := &LockCommand{Ui: ui}
filePath := filepath.Join(a1.dir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "test/prefix", touchCmd}
args := []string{
"-http-addr=" + a1.httpAddr,
"test/prefix",
"touch", filePath,
}

code := c.Run(args)
if code != 0 {
Expand All @@ -66,8 +68,12 @@ func TestLockCommand_Try_Lock(t *testing.T) {
ui := new(cli.MockUi)
c := &LockCommand{Ui: ui}
filePath := filepath.Join(a1.dir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-try=10s", "test/prefix", touchCmd}
args := []string{
"-http-addr=" + a1.httpAddr,
"-try=10s",
"test/prefix",
"touch", filePath,
}

// Run the command.
var lu *LockUnlock
Expand Down Expand Up @@ -98,8 +104,13 @@ func TestLockCommand_Try_Semaphore(t *testing.T) {
ui := new(cli.MockUi)
c := &LockCommand{Ui: ui}
filePath := filepath.Join(a1.dir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "-try=10s", "test/prefix", touchCmd}
args := []string{
"-http-addr=" + a1.httpAddr,
"-n=3",
"-try=10s",
"test/prefix",
"touch", filePath,
}

// Run the command.
var lu *LockUnlock
Expand Down Expand Up @@ -130,8 +141,11 @@ func TestLockCommand_MonitorRetry_Lock_Default(t *testing.T) {
ui := new(cli.MockUi)
c := &LockCommand{Ui: ui}
filePath := filepath.Join(a1.dir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "test/prefix", touchCmd}
args := []string{
"-http-addr=" + a1.httpAddr,
"test/prefix",
"touch", filePath,
}

// Run the command.
var lu *LockUnlock
Expand Down Expand Up @@ -163,8 +177,12 @@ func TestLockCommand_MonitorRetry_Semaphore_Default(t *testing.T) {
ui := new(cli.MockUi)
c := &LockCommand{Ui: ui}
filePath := filepath.Join(a1.dir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "test/prefix", touchCmd}
args := []string{
"-http-addr=" + a1.httpAddr,
"-n=3",
"test/prefix",
"touch", filePath,
}

// Run the command.
var lu *LockUnlock
Expand Down Expand Up @@ -196,8 +214,12 @@ func TestLockCommand_MonitorRetry_Lock_Arg(t *testing.T) {
ui := new(cli.MockUi)
c := &LockCommand{Ui: ui}
filePath := filepath.Join(a1.dir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-monitor-retry=9", "test/prefix", touchCmd}
args := []string{
"-http-addr=" + a1.httpAddr,
"-monitor-retry=9",
"test/prefix",
"touch", filePath,
}

// Run the command.
var lu *LockUnlock
Expand Down Expand Up @@ -229,8 +251,13 @@ func TestLockCommand_MonitorRetry_Semaphore_Arg(t *testing.T) {
ui := new(cli.MockUi)
c := &LockCommand{Ui: ui}
filePath := filepath.Join(a1.dir, "test_touch")
touchCmd := fmt.Sprintf("touch '%s'", filePath)
args := []string{"-http-addr=" + a1.httpAddr, "-n=3", "-monitor-retry=9", "test/prefix", touchCmd}
args := []string{
"-http-addr=" + a1.httpAddr,
"-n=3",
"-monitor-retry=9",
"test/prefix",
"touch", filePath,
}

// Run the command.
var lu *LockUnlock
Expand All @@ -253,3 +280,29 @@ func TestLockCommand_MonitorRetry_Semaphore_Arg(t *testing.T) {
t.Fatalf("bad: %#v", opts)
}
}

func TestLockCommand_Escape_String(t *testing.T) {
a1 := testAgent(t)
defer a1.Shutdown()
waitForLeader(t, a1.httpAddr)

ui := new(cli.MockUi)
c := &LockCommand{Ui: ui}
filePath := filepath.Join(a1.dir, "a b")
args := []string{
"-http-addr=" + a1.httpAddr,
"test/prefix",
"touch", filePath,
}

code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}

// Check for the file
_, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatalf("err: %v", err)
}
}