-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
134 lines (123 loc) · 3.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
)
func main() {
// find the next git after the first one found
os.Args[0] = FindGit(os.Getenv("PATH"))
if os.Args[0] == "" {
fmt.Fprintln(os.Stderr, "Failed to find git in the path")
os.Exit(1)
return
}
if IsRewriteAllowed(os.Args[1:]) {
// Change any ssh or git url arguments to https
for i, arg := range os.Args[1:] {
os.Args[i+1] = Scrub(arg)
}
}
// Run the scrubbed git command
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
switch v := err.(type) {
case *exec.ExitError:
os.Exit(v.ExitCode())
case nil:
os.Exit(0)
default:
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(2)
}
}
var allowedCommands = []string{"clone", "fetch", "ls-remote"}
// IsRewriteAllowed returns true if it is safe to rewrite arguments. Some commands
// such as config would break if rewritten, like when using insteadOf.
func IsRewriteAllowed(args []string) bool {
for _, arg := range args {
if strings.HasPrefix(arg, "-") {
continue
}
for _, allowed := range allowedCommands {
if arg == allowed {
return true
}
}
return false
}
return false
}
// FindGit finds the second git executable on the path, the first being this one.
func FindGit(envPath string) string {
paths := strings.Split(envPath, string(os.PathListSeparator))
var shimPath string
for _, path := range paths {
dirEntry, err := os.ReadDir(path)
if err != nil {
continue
}
for _, dirOrFile := range dirEntry {
if dirOrFile.IsDir() {
continue
}
if dirOrFile.Name() == "git" {
if shimPath == "" {
shimPath = path
break
}
if shimPath != path {
return strings.Join([]string{path, "git"}, string(os.PathSeparator))
}
}
}
}
return ""
}
var scpUrl = regexp.MustCompile(`^(?P<user>\S+?)@(?P<host>[a-zA-Z\d-]+(\.[a-zA-Z\d-]+)+\.?):(?P<path>.*?/.*?)$`)
var allowedSchemes = []string{"git", "ssh", "git+ssh"}
// Scrub rewrites arguments that look like URLs to have the HTTPS protocol.
func Scrub(argument string) string {
u, err := url.ParseRequestURI(argument)
if err == nil && u.Host != "" && contains(allowedSchemes, u.Scheme) {
u.Scheme = "https"
// Clear the user if there is no password, since the URL is usually ssh://[email protected].
// The username is required to tell the server you're doing Git operations, but not needed for HTTPS.
if _, isSet := u.User.Password(); !isSet {
u.User = nil
}
return u.String()
}
if scpUrl.MatchString(argument) {
newUrl := "https://" + strings.Replace(argument, ":", "/", 1)
u, err = url.ParseRequestURI(newUrl)
if err != nil {
// new URI isn't valid
return argument
}
if u.Host != scpUrl.FindStringSubmatch(argument)[2] {
// host changed, possible attack
return argument
}
// Clear the user if there is no password, since the URL is usually [email protected].
// The username is required to tell the server you're doing Git operations, but not needed for HTTPS.
if _, isSet := u.User.Password(); !isSet {
u.User = nil
}
return u.String()
}
return argument
}
func contains(haystack []string, needle string) bool {
for _, hay := range haystack {
if hay == needle {
return true
}
}
return false
}