Skip to content

Commit aa4ae84

Browse files
committed
feat(remote): improve input validation and security in remote operations
- Change file permissions from `0o700` to `0o600` in `WriteKey` function - Import `regexp` package in `remote.go` - Add `sanitizeInput` and `isValidInput` functions for input validation - Use sanitized inputs in `RemotePushNamedBranch` function - Add unit tests for `sanitizeInput` function in new `remote_test.go` file Signed-off-by: appleboy <[email protected]>
1 parent ad145ca commit aa4ae84

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

repo/key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func WriteKey(privateKey string) error {
4545
_ = os.WriteFile(
4646
confpath,
4747
[]byte("StrictHostKeyChecking no\n"),
48-
0o700)
48+
0o600)
4949

5050
return os.WriteFile(
5151
privpath,

repo/remote.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package repo
22

33
import (
44
"os/exec"
5+
"regexp"
56
)
67

78
// RemoteRemove drops the defined remote from a git repo.
@@ -43,13 +44,31 @@ func RemotePullRebaseNamedBranch(remote, branch string) *exec.Cmd {
4344
return cmd
4445
}
4546

47+
var validBranchName = regexp.MustCompile(`^[\w\.\-\/]+$`)
48+
49+
func sanitizeInput(input string) string {
50+
if isValidInput(input) {
51+
return input
52+
}
53+
return ""
54+
}
55+
56+
func isValidInput(input string) bool {
57+
return validBranchName.MatchString(input)
58+
}
59+
4660
// RemotePushNamedBranch puchs changes from a local to a remote branch.
4761
func RemotePushNamedBranch(remote, localbranch string, branch string, force bool, followtags bool) *exec.Cmd {
62+
sanitizedRemote := sanitizeInput(remote)
63+
sanitizedLocalBranch := sanitizeInput(localbranch)
64+
sanitizedBranch := sanitizeInput(branch)
65+
4866
cmd := exec.Command(
4967
"git",
5068
"push",
51-
remote,
52-
localbranch+":"+branch)
69+
sanitizedRemote,
70+
sanitizedLocalBranch+":"+sanitizedBranch,
71+
)
5372

5473
if force {
5574
cmd.Args = append(

repo/remote_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package repo
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSanitizeInput(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
input string
11+
want string
12+
}{
13+
{
14+
name: "valid input with alphanumeric characters",
15+
input: "feature-branch",
16+
want: "feature-branch",
17+
},
18+
{
19+
name: "valid input with dots and slashes",
20+
input: "release/1.0.0",
21+
want: "release/1.0.0",
22+
},
23+
{
24+
name: "invalid input with spaces",
25+
input: "invalid branch",
26+
want: "",
27+
},
28+
{
29+
name: "invalid input with special characters",
30+
input: "invalid@branch!",
31+
want: "",
32+
},
33+
{
34+
name: "empty input",
35+
input: "",
36+
want: "",
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
if got := sanitizeInput(tt.input); got != tt.want {
43+
t.Errorf("sanitizeInput() = %v, want %v", got, tt.want)
44+
}
45+
})
46+
}
47+
}

0 commit comments

Comments
 (0)