-
-
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 all commits
d181cf8
9a72c0f
6315cfe
da611f7
884c2d8
2c8605f
5ea069a
53abbba
e10e812
b3b4478
6f94991
62d28d5
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,27 @@ | ||
| # 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 `--security-enable-system-operator` to use it. | ||
|
|
||
| **Note:** When enabled, the system operator can replicate the functionality of `env` and `load` | ||
| operators via external commands. Enabling it effectively overrides `--security-disable-env-ops` | ||
| and `--security-disable-file-ops`. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| yq --security-enable-system-operator --null-input '.field = system("command"; "arg1")' | ||
| ``` | ||
|
|
||
| The operator takes: | ||
| - A command string (required) | ||
| - An argument (or an array of arguments), separated from the command 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, an error is returned instead of running the command, consistent with `--security-disable-env-ops` and `--security-disable-file-ops`. | ||
|
|
||
| Use `--security-enable-system-operator` flag to enable it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # 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 `--security-enable-system-operator` to use it. | ||
|
|
||
| **Note:** When enabled, the system operator can replicate the functionality of `env` and `load` | ||
| operators via external commands. Enabling it effectively overrides `--security-disable-env-ops` | ||
| and `--security-disable-file-ops`. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| yq --security-enable-system-operator --null-input '.field = system("command"; "arg1")' | ||
| ``` | ||
|
|
||
| The operator takes: | ||
| - A command string (required) | ||
| - An argument (or an array of arguments), separated from the command 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, an error is returned instead of running the command, consistent with `--security-disable-env-ops` and `--security-disable-file-ops`. | ||
|
|
||
| Use `--security-enable-system-operator` flag to enable it. | ||
|
|
||
| ## system operator returns error when disabled | ||
| Use `--security-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 | ||
| ```bash | ||
| Error: system operations are disabled, use --security-enable-system-operator to enable | ||
| ``` | ||
|
|
||
| ## Run a command with an argument | ||
| Use `--security-enable-system-operator` to enable the system operator. | ||
|
|
||
| Given a sample.yml file of: | ||
| ```yaml | ||
| country: Australia | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq --security-enable-system-operator '.country = system("/usr/bin/echo"; "test")' sample.yml | ||
| ``` | ||
| 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 --security-enable-system-operator '.a = system("/usr/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,146 @@ | ||
| package yqlib | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "container/list" | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
| ) | ||
|
|
||
| func resolveSystemArgs(argsNode *CandidateNode) ([]string, error) { | ||
| if argsNode == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if argsNode.Kind == SequenceNode { | ||
| args := make([]string, 0, len(argsNode.Content)) | ||
| for _, child := range argsNode.Content { | ||
| // Only non-null scalar children are valid arguments. | ||
| if child == nil { | ||
| continue | ||
| } | ||
| if child.Kind != ScalarNode || child.Tag == "!!null" { | ||
| return nil, fmt.Errorf("system operator: argument must be a non-null scalar; got kind=%v tag=%v", child.Kind, child.Tag) | ||
| } | ||
| args = append(args, child.Value) | ||
| } | ||
| if len(args) == 0 { | ||
| return nil, nil | ||
| } | ||
| return args, nil | ||
| } | ||
|
|
||
| // Single-argument case: only accept a non-null scalar node. | ||
| if argsNode.Tag == "!!null" { | ||
| return nil, nil | ||
| } | ||
| if argsNode.Kind != ScalarNode { | ||
| return nil, fmt.Errorf("system operator: args must be a non-null scalar or sequence of non-null scalars; got kind=%v tag=%v", argsNode.Kind, argsNode.Tag) | ||
| } | ||
| return []string{argsNode.Value}, nil | ||
| } | ||
|
|
||
| func resolveCommandNode(commandNodes Context) (string, error) { | ||
| if commandNodes.MatchingNodes.Front() == nil { | ||
| return "", fmt.Errorf("system operator: command expression returned no results") | ||
| } | ||
| if commandNodes.MatchingNodes.Len() > 1 { | ||
| log.Debugf("system operator: command expression returned %d results, using first", commandNodes.MatchingNodes.Len()) | ||
| } | ||
| cmdNode := commandNodes.MatchingNodes.Front().Value.(*CandidateNode) | ||
| if cmdNode.Kind != ScalarNode || cmdNode.guessTagFromCustomType() != "!!str" { | ||
| return "", fmt.Errorf("system operator: command must be a string scalar") | ||
| } | ||
mikefarah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if cmdNode.Value == "" { | ||
| return "", fmt.Errorf("system operator: command must be a non-empty string") | ||
| } | ||
| return cmdNode.Value, nil | ||
| } | ||
|
|
||
| func systemOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { | ||
| if !ConfiguredSecurityPreferences.EnableSystemOps { | ||
| return Context{}, fmt.Errorf("system operations are disabled, use --security-enable-system-operator to enable") | ||
| } | ||
|
|
||
| // determine at parse time whether we have (command; args) or just (command) | ||
| hasArgs := expressionNode.RHS.Operation.OperationType == blockOpType | ||
|
|
||
| var results = list.New() | ||
|
|
||
| for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { | ||
| candidate := el.Value.(*CandidateNode) | ||
| nodeContext := context.SingleReadonlyChildContext(candidate) | ||
|
|
||
| var command string | ||
| var args []string | ||
|
|
||
| if hasArgs { | ||
| block := expressionNode.RHS | ||
| commandNodes, err := d.GetMatchingNodes(nodeContext, block.LHS) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| command, err = resolveCommandNode(commandNodes) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
|
|
||
| argsNodes, err := d.GetMatchingNodes(nodeContext, block.RHS) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| if argsNodes.MatchingNodes.Len() > 1 { | ||
| log.Debugf("system operator: args expression returned %d results, using first", argsNodes.MatchingNodes.Len()) | ||
| } | ||
| if argsNodes.MatchingNodes.Front() != nil { | ||
| args, err = resolveSystemArgs(argsNodes.MatchingNodes.Front().Value.(*CandidateNode)) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| } | ||
| } else { | ||
| commandNodes, err := d.GetMatchingNodes(nodeContext, expressionNode.RHS) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| command, err = resolveCommandNode(commandNodes) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| } | ||
|
|
||
| var stdin bytes.Buffer | ||
| encoded, err := encodeToYamlString(candidate) | ||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
| stdin.WriteString(encoded) | ||
|
|
||
| // #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 := string(output) | ||
| if strings.HasSuffix(result, "\r\n") { | ||
| result = result[:len(result)-2] | ||
| } else if strings.HasSuffix(result, "\n") { | ||
| result = result[:len(result)-1] | ||
| } | ||
|
Comment on lines
+135
to
+140
|
||
| newNode := candidate.CreateReplacement(ScalarNode, "!!str", result) | ||
| results.PushBack(newNode) | ||
| } | ||
|
|
||
| return context.ChildContext(results), nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.