Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Detected non-static command inside Command. Audit the input to 'exec.Command'. If unverified user data can reach this call site, this is a code injection vulnerability. A malicious actor can inject a malicious script to execute arbitrary code.
Ignore this finding from dangerous-exec-command.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semgrep Assistant suggests the following fix: Use
exec.CommandContext
with static commands and separate arguments to prevent command injection.View step-by-step instructions
fmt.Sprintf
to construct the command string. Instead, split the command and its arguments into separate strings to prevent injection.exec.CommandContext
with a static command and pass arguments separately. For example, replacecommand := fmt.Sprintf("sh -c %s", input)
withcommand := "sh"
andargs := []string{"-c", input}
.exec.CommandContext
call to include the arguments:cmd := exec.CommandContext(ctx, command, args...)
.input
to ensure it does not contain malicious content. For example, you can use a whitelist of allowed commands or patterns.golang.org/x/net/context
package for more advanced context handling if needed.This code change should be a good starting point:
Leave feedback with a 👍 / 👎. Save a memory with
/semgrep remember <your custom instructions>
.