Repository navigation
Validate Git branch arguments before command execution #62378
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
Changes from all commits
f70cf86
cd15e3a
34d131b
5243801
bec2637
8eac820
5c74d04
815cf63
f157bb9
14e96ab
13589e0
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 |
|---|---|---|
|
|
@@ -27,6 +27,16 @@ func isSafeGitRevisionArg(ref string) bool { | |
| return ref != "" && !strings.HasPrefix(ref, "-") && !containsControlCharacters(ref) | ||
| } | ||
|
|
||
| func validateGitBranchArg(branchName, label string) error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] 💡 Suggested testfunc TestValidateGitBranchArg(t *testing.T) {
t.Parallel()
err := validateGitBranchArg("--upload-pack=evil", "branch name")
assert.ErrorContains(t, err, "invalid branch name")
err = validateGitBranchArg("", "branch name")
assert.Error(t, err)
}This complements the existing @copilot please address this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| if branchName == "" { | ||
| return fmt.Errorf("invalid %s: must not be empty", label) | ||
| } | ||
| if !isSafeGitRevisionArg(branchName) { | ||
| return fmt.Errorf("invalid %s %q: must not start with '-' or contain control characters", label, branchName) | ||
| } | ||
|
Copilot marked this conversation as resolved.
Resolved
|
||
| return nil | ||
| } | ||
|
|
||
| // validateRelPathForGit rejects relative paths that could be misinterpreted as | ||
| // a git CLI flag (a leading "-") or that escape the repository root via path | ||
| // traversal (a leading ".." path segment after cleaning), before the path is | ||
|
|
@@ -534,6 +544,9 @@ func getCurrentBranchIn(dir string) (string, error) { | |
|
|
||
| // createAndSwitchBranch creates a new branch and switches to it | ||
| func createAndSwitchBranch(branchName string, verbose bool) error { | ||
| if err := validateGitBranchArg(branchName, "branch name"); err != nil { | ||
| return err | ||
| } | ||
| console.LogVerbose(verbose, "Creating and switching to branch: "+branchName) | ||
|
|
||
| cmd := exec.Command("git", "checkout", "-b", branchName) | ||
|
|
@@ -546,6 +559,9 @@ func createAndSwitchBranch(branchName string, verbose bool) error { | |
|
|
||
| // switchBranch switches to the specified branch | ||
| func switchBranch(branchName string, verbose bool) error { | ||
| if err := validateGitBranchArg(branchName, "branch name"); err != nil { | ||
| return err | ||
| } | ||
| console.LogVerbose(verbose, "Switching to branch: "+branchName) | ||
|
|
||
| cmd := exec.Command("git", "checkout", branchName) | ||
|
|
@@ -572,6 +588,9 @@ func commitChanges(message string, verbose bool) error { | |
|
|
||
| // pushBranch pushes the specified branch to origin | ||
| func pushBranch(branchName string, verbose bool) error { | ||
| if err := validateGitBranchArg(branchName, "branch name"); err != nil { | ||
| return err | ||
| } | ||
| console.LogVerbose(verbose, "Pushing branch: "+branchName) | ||
|
|
||
| cmd := exec.Command("git", "push", "-u", "origin", branchName) | ||
|
|
||
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.
pkg/cli/git.go:L30: yagni:
validateGitBranchArgwrapper adds a one-off abstraction used only forisSafeGitRevisionArg. CallisSafeGitRevisionArginline at each callsite with contextual error text until shared semantics diverge.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.
Keeping the wrapper:
validateGitBranchArgis called from 5 sites across 3 files (git.go,pr_command.go,add_interactive_git.go), not a single one-off usage, so the shared helper avoids duplicating the error-message format at each callsite.