-
-
Notifications
You must be signed in to change notification settings - Fork 756
Add system(command; args) operator (disabled by default) #2640
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
d181cf8
9a72c0f
6315cfe
da611f7
884c2d8
2c8605f
5ea069a
53abbba
e10e812
b3b4478
6f94991
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # System Operators | ||
|
|
||
| The `system` operator allows you to run an external command and use its output as a value in your expression. | ||
|
|
||
| **Security warning**: The system operator is disabled by default. You must explicitly pass `--enable-system-operator` to use it. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| yq --enable-system-operator '.field = system("command"; "arg1")' | ||
mikefarah marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| The operator takes: | ||
| - A command string (required) | ||
| - An argument or array of arguments separated by `;` (optional) | ||
|
|
||
| The current matched node's value is serialised and piped to the command via stdin. The command's stdout (with trailing newline stripped) is returned as a string. | ||
|
|
||
| ## Disabling the system operator | ||
|
|
||
| The system operator is disabled by default. When disabled, a warning is logged and `null` is returned instead of running the command. | ||
|
|
||
| Use `--enable-system-operator` flag to enable it. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # System Operators | ||
|
|
||
| The `system` operator allows you to run an external command and use its output as a value in your expression. | ||
|
|
||
| **Security warning**: The system operator is disabled by default. You must explicitly pass `--enable-system-operator` to use it. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| yq --enable-system-operator '.field = system("command"; "arg1")' | ||
mikefarah marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| The operator takes: | ||
| - A command string (required) | ||
| - An argument or array of arguments separated by `;` (optional) | ||
|
|
||
| The current matched node's value is serialised and piped to the command via stdin. The command's stdout (with trailing newline stripped) is returned as a string. | ||
|
|
||
| ## Disabling the system operator | ||
|
|
||
| The system operator is disabled by default. When disabled, a warning is logged and `null` is returned instead of running the command. | ||
|
|
||
| Use `--enable-system-operator` flag to enable it. | ||
|
|
||
| ## system operator returns null when disabled | ||
| Use `--enable-system-operator` to enable the system operator. | ||
|
|
||
| Given a sample.yml file of: | ||
| ```yaml | ||
| country: Australia | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '.country = system("/usr/bin/echo"; "test")' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| country: null | ||
| ``` | ||
|
|
||
| ## Run a command with an argument | ||
| Use `--enable-system-operator` to enable the system operator. | ||
|
|
||
| Given a sample.yml file of: | ||
| ```yaml | ||
| country: Australia | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '.country = system("/usr/bin/echo"; "test")' sample.yml | ||
| ``` | ||
mikefarah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| will output | ||
| ```yaml | ||
| country: test | ||
| ``` | ||
|
|
||
| ## Run a command without arguments | ||
| Omit the semicolon and args to run the command with no extra arguments. | ||
|
|
||
| Given a sample.yml file of: | ||
| ```yaml | ||
| a: hello | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '.a = system("/bin/echo")' sample.yml | ||
| ``` | ||
mikefarah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| will output | ||
| ```yaml | ||
| a: "" | ||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package yqlib | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "container/list" | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
| ) | ||
|
|
||
| func systemOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { | ||
| if !ConfiguredSecurityPreferences.EnableSystemOps { | ||
| log.Warning("system operator is disabled, use --enable-system-operator flag to enable") | ||
| results := list.New() | ||
| for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { | ||
| candidate := el.Value.(*CandidateNode) | ||
| results.PushBack(candidate.CreateReplacement(ScalarNode, "!!null", "null")) | ||
| } | ||
| return context.ChildContext(results), nil | ||
| } | ||
|
|
||
| var command string | ||
| var argsExpression *ExpressionNode | ||
|
|
||
| // check if it's a block operator (command; args) or just (command) | ||
| if expressionNode.RHS.Operation.OperationType == blockOpType { | ||
|
||
| block := expressionNode.RHS | ||
| commandNodes, err := d.GetMatchingNodes(context.ReadOnlyClone(), block.LHS) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| if commandNodes.MatchingNodes.Front() == nil { | ||
| return Context{}, fmt.Errorf("system operator: command expression returned no results") | ||
| } | ||
| command = commandNodes.MatchingNodes.Front().Value.(*CandidateNode).Value | ||
| argsExpression = block.RHS | ||
| } else { | ||
| commandNodes, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| if commandNodes.MatchingNodes.Front() == nil { | ||
| return Context{}, fmt.Errorf("system operator: command expression returned no results") | ||
| } | ||
| command = commandNodes.MatchingNodes.Front().Value.(*CandidateNode).Value | ||
| } | ||
|
|
||
| // evaluate args if present | ||
| var args []string | ||
| if argsExpression != nil { | ||
| argsNodes, err := d.GetMatchingNodes(context.ReadOnlyClone(), argsExpression) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| if argsNodes.MatchingNodes.Front() != nil { | ||
| argsNode := argsNodes.MatchingNodes.Front().Value.(*CandidateNode) | ||
| if argsNode.Kind == SequenceNode { | ||
| for _, child := range argsNode.Content { | ||
| args = append(args, child.Value) | ||
| } | ||
| } else if argsNode.Tag != "!!null" { | ||
| args = []string{argsNode.Value} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var results = list.New() | ||
|
|
||
| for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { | ||
| candidate := el.Value.(*CandidateNode) | ||
|
|
||
| var stdin bytes.Buffer | ||
| if candidate.Tag != "!!null" { | ||
| encoded, err := encodeToYamlString(candidate) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| stdin.WriteString(encoded) | ||
| } | ||
mikefarah marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // #nosec G204 - intentional: user must explicitly enable this operator | ||
| cmd := exec.Command(command, args...) | ||
| cmd.Stdin = &stdin | ||
| var stderr bytes.Buffer | ||
| cmd.Stderr = &stderr | ||
|
|
||
| output, err := cmd.Output() | ||
| if err != nil { | ||
| stderrStr := strings.TrimSpace(stderr.String()) | ||
| if stderrStr != "" { | ||
| return Context{}, fmt.Errorf("system command '%v' failed: %w\nstderr: %v", command, err, stderrStr) | ||
| } | ||
| return Context{}, fmt.Errorf("system command '%v' failed: %w", command, err) | ||
| } | ||
|
|
||
| result := strings.TrimRight(string(output), "\n") | ||
mikefarah marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| newNode := candidate.CreateReplacement(ScalarNode, "!!str", result) | ||
| results.PushBack(newNode) | ||
| } | ||
|
|
||
| return context.ChildContext(results), nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package yqlib | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| var systemOperatorDisabledScenarios = []expressionScenario{ | ||
| { | ||
| description: "system operator returns null when disabled", | ||
| subdescription: "Use `--enable-system-operator` to enable the system operator.", | ||
| document: "country: Australia", | ||
| expression: `.country = system("/usr/bin/echo"; "test")`, | ||
| expected: []string{ | ||
| "D0, P[], (!!map)::country: null\n", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var systemOperatorEnabledScenarios = []expressionScenario{ | ||
| { | ||
| description: "Run a command with an argument", | ||
| subdescription: "Use `--enable-system-operator` to enable the system operator.", | ||
| document: "country: Australia", | ||
| expression: `.country = system("/usr/bin/echo"; "test")`, | ||
| expected: []string{ | ||
|
||
| "D0, P[], (!!map)::country: test\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "Run a command without arguments", | ||
| subdescription: "Omit the semicolon and args to run the command with no extra arguments.", | ||
| document: "a: hello", | ||
| expression: `.a = system("/bin/echo")`, | ||
| expected: []string{ | ||
| "D0, P[], (!!map)::a: \"\"\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "Run a command with multiple arguments", | ||
| subdescription: "Pass an array of arguments.", | ||
| skipDoc: true, | ||
| document: "a: hello", | ||
| expression: `.a = system("/bin/echo"; ["foo", "bar"])`, | ||
| expected: []string{ | ||
| "D0, P[], (!!map)::a: foo bar\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "Command failure returns error", | ||
| skipDoc: true, | ||
| document: "a: hello", | ||
| expression: `.a = system("/bin/false")`, | ||
| expectedError: "system command '/bin/false' failed: exit status 1", | ||
| }, | ||
mikefarah marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func TestSystemOperatorDisabledScenarios(t *testing.T) { | ||
| // ensure system operator is disabled | ||
| originalEnableSystemOps := ConfiguredSecurityPreferences.EnableSystemOps | ||
| defer func() { | ||
| ConfiguredSecurityPreferences.EnableSystemOps = originalEnableSystemOps | ||
| }() | ||
|
|
||
| ConfiguredSecurityPreferences.EnableSystemOps = false | ||
|
|
||
| for _, tt := range systemOperatorDisabledScenarios { | ||
| testScenario(t, &tt) | ||
| } | ||
| documentOperatorScenarios(t, "system-operators", systemOperatorDisabledScenarios) | ||
| } | ||
|
|
||
| func TestSystemOperatorEnabledScenarios(t *testing.T) { | ||
| originalEnableSystemOps := ConfiguredSecurityPreferences.EnableSystemOps | ||
| defer func() { | ||
| ConfiguredSecurityPreferences.EnableSystemOps = originalEnableSystemOps | ||
| }() | ||
|
|
||
| ConfiguredSecurityPreferences.EnableSystemOps = true | ||
|
|
||
| for _, tt := range systemOperatorEnabledScenarios { | ||
| testScenario(t, &tt) | ||
| } | ||
| appendOperatorDocumentScenario(t, "system-operators", systemOperatorEnabledScenarios) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| package yqlib | ||
|
|
||
| type SecurityPreferences struct { | ||
| DisableEnvOps bool | ||
| DisableFileOps bool | ||
| DisableEnvOps bool | ||
| DisableFileOps bool | ||
| EnableSystemOps bool | ||
| } | ||
|
Comment on lines
3
to
7
|
||
|
|
||
| var ConfiguredSecurityPreferences = SecurityPreferences{ | ||
| DisableEnvOps: false, | ||
| DisableFileOps: false, | ||
| DisableEnvOps: false, | ||
| DisableFileOps: false, | ||
| EnableSystemOps: false, | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.