Skip to content

Commit bf7d11c

Browse files
committed
limit default filesize
1 parent f72949a commit bf7d11c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
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_FILESIZE_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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ import (
1616
"github.com/sourcegraph/sourcegraph/lib/errors"
1717
)
1818

19+
const defaultMaxFileSizeMB = 10
20+
21+
func maxFileSize() int64 {
22+
if v := os.Getenv("SRC_MAX_FILESIZE_MB"); v != "" {
23+
if mb, err := strconv.ParseInt(v, 10, 64); err == nil && mb > 0 {
24+
return mb * 1024 * 1024
25+
}
26+
}
27+
return defaultMaxFileSizeMB * 1024 * 1024
28+
}
29+
1930
var uploadPackArgs = []string{
2031
// Partial clones/fetches
2132
"-c", "uploadpack.allowFilter=true",
@@ -108,7 +119,6 @@ func (s *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
108119
body := r.Body
109120
defer body.Close()
110121

111-
// TODO(@evict) max filereader
112122
if r.Header.Get("Content-Encoding") == "gzip" {
113123
gzipReader, err := gzip.NewReader(body)
114124
if err != nil {
@@ -117,7 +127,7 @@ func (s *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
117127
}
118128
defer gzipReader.Close()
119129

120-
body = gzipReader
130+
body = io.NopCloser(io.LimitReader(gzipReader, maxFileSize()))
121131
}
122132

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

0 commit comments

Comments
 (0)