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

fix(command): parse command ligne with shellwords #396

Merged
merged 1 commit into from
Feb 7, 2025
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Types of changes
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [1.30.1]

- `Fixed` mask `command` split command line on space protected by quote

## [1.30.0]

- `Added` mask `partitions` to handle fields containing different types of values by applying distinct transformations
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ require (
github.com/labstack/gommon v0.4.2 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-shellwords v1.0.12
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 h1:AMFGa4R4MiIpspGNG7Z948v4n35fFGB3RR3G/ry4FWs=
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 h1:+n/aFZefKZp7spd8DFdX7uMikMLXX4oubIzJF4kv/wI=
Expand Down
11 changes: 10 additions & 1 deletion pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package command

import (
"fmt"
"os/exec"
"strings"

"github.com/cgi-fr/pimo/pkg/model"
"github.com/rs/zerolog/log"

"github.com/mattn/go-shellwords"
)

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

Expand Down
10 changes: 10 additions & 0 deletions pkg/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func TestMaskingShouldReplaceSensitiveValueByCommand(t *testing.T) {
assert.Equal(t, waited, result, "should be Toto")
}

func TestMaskingShouldPreserveSpaceInQuote(t *testing.T) {
nameProgramMasking := NewMask("echo \" Toto \" ")
data := "Benjamin"
result, err := nameProgramMasking.Mask(data)
assert.Equal(t, nil, err, "error should be nil")
waited := " Toto "
assert.NotEqual(t, data, result, "should be masked")
assert.Equal(t, waited, result, "should be Toto with space")
}

func TestMaskingShouldReturnAnErrorInCaseOfWrongCommand(t *testing.T) {
nameCommandMasking := NewMask("WrongCommand")

Expand Down
Loading