Commit 84164aac authored by Jacob Vosmaer's avatar Jacob Vosmaer

Remove regexp dependency

Change in behavior: we now accept requests with path '/'. This
should not be a problem; upstream GitLab (the 'auth backend') still
decides if Git access to the path is OK. We are still guarding
against traversal.
parent 1de27beb
...@@ -24,14 +24,13 @@ import ( ...@@ -24,14 +24,13 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"regexp"
"strings" "strings"
"syscall" "syscall"
) )
type gitService struct { type gitService struct {
method string method string
regexp *regexp.Regexp suffix string
handleFunc func(gitEnv, string, string, http.ResponseWriter, *http.Request) handleFunc func(gitEnv, string, string, http.ResponseWriter, *http.Request)
rpc string rpc string
} }
...@@ -42,7 +41,6 @@ type gitEnv struct { ...@@ -42,7 +41,6 @@ type gitEnv struct {
var Version string var Version string
var httpClient = &http.Client{} var httpClient = &http.Client{}
var pathTraversal = regexp.MustCompile(`/\.\./`)
// Command-line options // Command-line options
var repoRoot string var repoRoot string
...@@ -53,9 +51,9 @@ var listenUmask = flag.Int("listenUmask", 022, "Umask for Unix socket, default: ...@@ -53,9 +51,9 @@ var listenUmask = flag.Int("listenUmask", 022, "Umask for Unix socket, default:
var authBackend = flag.String("authBackend", "http://localhost:8080", "Authentication/authorization backend") var authBackend = flag.String("authBackend", "http://localhost:8080", "Authentication/authorization backend")
var gitServices = [...]gitService{ var gitServices = [...]gitService{
gitService{"GET", regexp.MustCompile(`\A(/..*)/info/refs\z`), handleGetInfoRefs, ""}, gitService{"GET", "/info/refs", handleGetInfoRefs, ""},
gitService{"POST", regexp.MustCompile(`\A(/..*)/git-upload-pack\z`), handlePostRPC, "git-upload-pack"}, gitService{"POST", "/git-upload-pack", handlePostRPC, "git-upload-pack"},
gitService{"POST", regexp.MustCompile(`\A(/..*)/git-receive-pack\z`), handlePostRPC, "git-receive-pack"}, gitService{"POST", "/git-receive-pack", handlePostRPC, "git-receive-pack"},
} }
func main() { func main() {
...@@ -99,7 +97,6 @@ func main() { ...@@ -99,7 +97,6 @@ func main() {
func gitHandler(w http.ResponseWriter, r *http.Request) { func gitHandler(w http.ResponseWriter, r *http.Request) {
var env gitEnv var env gitEnv
var pathMatch []string
var g gitService var g gitService
log.Print(r.Method, " ", r.URL) log.Print(r.Method, " ", r.URL)
...@@ -107,8 +104,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) { ...@@ -107,8 +104,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
// Look for a matching Git service // Look for a matching Git service
foundService := false foundService := false
for _, g = range gitServices { for _, g = range gitServices {
pathMatch = g.regexp.FindStringSubmatch(r.URL.Path) if r.Method == g.method && strings.HasSuffix(r.URL.Path, g.suffix) {
if r.Method == g.method && pathMatch != nil {
foundService = true foundService = true
break break
} }
...@@ -155,7 +151,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) { ...@@ -155,7 +151,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
authResponse.Body.Close() authResponse.Body.Close()
// Validate the path to the Git repository // Validate the path to the Git repository
foundPath := pathMatch[1] foundPath := strings.TrimSuffix(r.URL.Path, g.suffix)
if !validPath(foundPath) { if !validPath(foundPath) {
http.Error(w, "Not Found", 404) http.Error(w, "Not Found", 404)
return return
...@@ -165,7 +161,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) { ...@@ -165,7 +161,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
} }
func validPath(p string) bool { func validPath(p string) bool {
if pathTraversal.MatchString(p) { if strings.Contains(p, "/../") {
log.Printf("path traversal detected in %s", p) log.Printf("path traversal detected in %s", p)
return false return false
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment