Skip to content

Commit dea579a

Browse files
authored
fix(command): parse command ligne with shellwords (#396)
1 parent 0a1fe35 commit dea579a

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Types of changes
1414
- `Fixed` for any bug fixes.
1515
- `Security` in case of vulnerabilities.
1616

17+
## [1.30.1]
18+
19+
- `Fixed` mask `command` split command line on space protected by quote
20+
1721
## [1.30.0]
1822

1923
- `Added` mask `partitions` to handle fields containing different types of values by applying distinct transformations

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
github.com/labstack/gommon v0.4.2 // indirect
5353
github.com/mailru/easyjson v0.7.7 // indirect
5454
github.com/mattn/go-colorable v0.1.13 // indirect
55+
github.com/mattn/go-shellwords v1.0.12
5556
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect
5657
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 // indirect
5758
github.com/mitchellh/copystructure v1.2.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
9898
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
9999
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
100100
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
101+
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
102+
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
101103
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 h1:AMFGa4R4MiIpspGNG7Z948v4n35fFGB3RR3G/ry4FWs=
102104
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
103105
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 h1:+n/aFZefKZp7spd8DFdX7uMikMLXX4oubIzJF4kv/wI=

pkg/command/command.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
package command
1919

2020
import (
21+
"fmt"
2122
"os/exec"
2223
"strings"
2324

2425
"github.com/cgi-fr/pimo/pkg/model"
2526
"github.com/rs/zerolog/log"
27+
28+
"github.com/mattn/go-shellwords"
2629
)
2730

2831
// MaskEngine implements MaskEngine with a console command
@@ -38,7 +41,13 @@ func NewMask(cmd string) MaskEngine {
3841
// Mask delegate mask algorithm to an external program
3942
func (cme MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.Entry, error) {
4043
log.Info().Msg("Mask command")
41-
splitCommand := strings.Split(cme.Cmd, " ")
44+
line := cme.Cmd
45+
parser := shellwords.NewParser()
46+
parser.ParseEnv = true
47+
splitCommand, err := parser.Parse(line)
48+
if err != nil {
49+
return "", fmt.Errorf("failed to parse command %w", err)
50+
}
4251
/* #nosec */
4352
out, err := exec.Command(splitCommand[0], splitCommand[1:]...).Output()
4453

pkg/command/command_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ func TestMaskingShouldReplaceSensitiveValueByCommand(t *testing.T) {
3434
assert.Equal(t, waited, result, "should be Toto")
3535
}
3636

37+
func TestMaskingShouldPreserveSpaceInQuote(t *testing.T) {
38+
nameProgramMasking := NewMask("echo \" Toto \" ")
39+
data := "Benjamin"
40+
result, err := nameProgramMasking.Mask(data)
41+
assert.Equal(t, nil, err, "error should be nil")
42+
waited := " Toto "
43+
assert.NotEqual(t, data, result, "should be masked")
44+
assert.Equal(t, waited, result, "should be Toto with space")
45+
}
46+
3747
func TestMaskingShouldReturnAnErrorInCaseOfWrongCommand(t *testing.T) {
3848
nameCommandMasking := NewMask("WrongCommand")
3949

0 commit comments

Comments
 (0)