Skip to content

Commit 106ec65

Browse files
committed
limit request size, zipbomb
1 parent a99e622 commit 106ec65

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Run <code><strong>src login <i>SOURCEGRAPH-URL</i></strong></code> to authentica
119119

120120
- `SRC_ENDPOINT`: the URL to your Sourcegraph instance (such as `https://sourcegraph.example.com`)
121121
- `SRC_ACCESS_TOKEN`: your Sourcegraph access token (on your Sourcegraph instance, click your user menu in the top right, then select **Settings > Access tokens** to create one)
122+
- `SRC_MAX_REQUESTSIZE_MB`: maximum request body size in MB for git operations (default: 10)
122123

123124
For convenience, you can add these environment variables persistently.
124125

internal/servegit/gitservice.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ import (
1717
"github.com/sourcegraph/sourcegraph/lib/errors"
1818
)
1919

20+
const defaultMaxFileSizeMB = 10
21+
22+
func maxRequestSize() int64 {
23+
if v := os.Getenv("SRC_MAX_REQUESTSIZE_MB"); v != "" {
24+
if mb, err := strconv.ParseInt(v, 10, 64); err == nil && mb > 0 {
25+
return mb * 1024 * 1024
26+
}
27+
}
28+
return defaultMaxFileSizeMB * 1024 * 1024
29+
}
30+
2031
var uploadPackArgs = []string{
2132
// Partial clones/fetches
2233
"-c", "uploadpack.allowFilter=true",
@@ -109,10 +120,9 @@ func (s *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
109120
return
110121
}
111122

112-
body := r.Body
123+
body := io.NopCloser(io.LimitReader(r.Body, maxRequestSize()))
113124
defer body.Close()
114125

115-
// TODO(@evict) max filereader
116126
if r.Header.Get("Content-Encoding") == "gzip" {
117127
gzipReader, err := gzip.NewReader(body)
118128
if err != nil {
@@ -121,7 +131,7 @@ func (s *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
121131
}
122132
defer gzipReader.Close()
123133

124-
body = gzipReader
134+
body = io.NopCloser(io.LimitReader(gzipReader, maxRequestSize()))
125135
}
126136

127137
// err is set if we fail to run command or have an unexpected svc. It is

0 commit comments

Comments
 (0)