Skip to content

Conversation

@avidal
Copy link
Collaborator

@avidal avidal commented Jul 9, 2025

This patch replaces git-go with calls directly to the git CLI. While it's not ideal to depend on a subprocess, the fact is that using git-go required a lot of lines of code for simple operations, and often disagreed with git on a local repository (which wasn't reproducible outside of a CI environment).

The git CLI commands in-use in this patch are all "plumbing" (versus "porcelain") commands which generally have stable output: they are designed explicitly for machine consumption.

@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Jul 9, 2025

✅ Code Quality    ⚠️ Code Vulnerabilities    ✅ Libraries

⚠️ Warnings

🛡️ 2 Code vulnerabilities detected

High: go-security/command-injection Avoid command injection View rule
git.go:128
High: go-security/command-injection Avoid command injection View rule
git.go:71

ℹ️ Info

🛠️ No new code quality issues
📚 No new vulnerable libraries detected

This comment will be updated automatically if new data arrives.

🔗 Commit SHA: 0b6d801 | Was this helpful? Give us feedback!

// Returns the files changed in the given commit, along with their contents
// Deleted files will have an empty value
func (r *Repository) changedFiles(commit string) (map[string][]byte, error) {
cmd := exec.Command("git", "diff-tree", "--no-commit-id", "--name-status", "-r", commit)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 High: Code Vulnerability

Check command call and ensure there is no unsanitized data used. The variable `commit` may need to be validated (...read more)

In Go, the exec.Command function is used to run external commands. Using this function carelessly can lead to command injection vulnerabilities. Carefully review the data flow that leads to a command execution and ensures no data can be injected by a third-party.

Command injection occurs when untrusted input is passed directly to a system shell, allowing an attacker to execute arbitrary commands. This can result in unauthorized access to the system, data leaks, or other security breaches.

Avoid executing commands constructed using user-provided data, or if you must, always validate and sanitize user inputs before passing them to exec.Command.

How to remediate?

Either remove the user-controlled data, filter the potential command with a list of allowed command or sanitize the command before execution.

View in Datadog  Leave us feedback  Documentation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit hashes are validated in the calling function.


diff, err := ptree.Diff(tree)
func (r *Repository) catfile(commit string) ([]string, string, string, error) {
cmd := exec.Command("git", "cat-file", "commit", commit)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 High: Code Vulnerability

Check command call and ensure there is no unsanitized data used. The variable `commit` may need to be validated (...read more)

In Go, the exec.Command function is used to run external commands. Using this function carelessly can lead to command injection vulnerabilities. Carefully review the data flow that leads to a command execution and ensures no data can be injected by a third-party.

Command injection occurs when untrusted input is passed directly to a system shell, allowing an attacker to execute arbitrary commands. This can result in unauthorized access to the system, data leaks, or other security breaches.

Avoid executing commands constructed using user-provided data, or if you must, always validate and sanitize user inputs before passing them to exec.Command.

How to remediate?

Either remove the user-controlled data, filter the potential command with a list of allowed command or sanitize the command before execution.

View in Datadog  Leave us feedback  Documentation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit hashes are validated in the calling function.

This patch replaces `git-go` with calls directly to the `git` CLI. While
it's not ideal to depend on a subprocess, the fact is that using
`git-go` required a lot of lines of code for simple operations, and
often disagreed with `git` on a local repository (which wasn't
reproducible outside of a CI environment).

The `git` CLI commands in-use in this patch are all "plumbing" (versus
"porcelain") commands which generally have stable output: they are
designed explicitly for machine consumption.
// Returns the files changed in the given commit, along with their contents
// Deleted files will have an empty value
func (r *Repository) changedFiles(commit string) (map[string][]byte, error) {
cmd := exec.Command("git", "diff-tree", "--no-commit-id", "--name-status", "-r", commit)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 High: Code Vulnerability

Check command call and ensure there is no unsanitized data used. The variable `commit` may need to be validated (...read more)

In Go, the exec.Command function is used to run external commands. Using this function carelessly can lead to command injection vulnerabilities. Carefully review the data flow that leads to a command execution and ensures no data can be injected by a third-party.

Command injection occurs when untrusted input is passed directly to a system shell, allowing an attacker to execute arbitrary commands. This can result in unauthorized access to the system, data leaks, or other security breaches.

Avoid executing commands constructed using user-provided data, or if you must, always validate and sanitize user inputs before passing them to exec.Command.

How to remediate?

Either remove the user-controlled data, filter the potential command with a list of allowed command or sanitize the command before execution.

View in Datadog  Leave us feedback  Documentation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit hashes are validated in the calling function.


diff, err := ptree.Diff(tree)
func (r *Repository) catfile(commit string) ([]string, string, string, error) {
cmd := exec.Command("git", "cat-file", "commit", commit)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 High: Code Vulnerability

Check command call and ensure there is no unsanitized data used. The variable `commit` may need to be validated (...read more)

In Go, the exec.Command function is used to run external commands. Using this function carelessly can lead to command injection vulnerabilities. Carefully review the data flow that leads to a command execution and ensures no data can be injected by a third-party.

Command injection occurs when untrusted input is passed directly to a system shell, allowing an attacker to execute arbitrary commands. This can result in unauthorized access to the system, data leaks, or other security breaches.

Avoid executing commands constructed using user-provided data, or if you must, always validate and sanitize user inputs before passing them to exec.Command.

How to remediate?

Either remove the user-controlled data, filter the potential command with a list of allowed command or sanitize the command before execution.

View in Datadog  Leave us feedback  Documentation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit hashes are validated in the calling function.

@avidal avidal merged commit 68fb327 into main Jul 9, 2025
7 checks passed
@avidal avidal deleted the use-git-cli branch July 9, 2025 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants