Skip to content

Commit cf9b6c2

Browse files
KN4CK3Rzeripathlafrikslunnytechknowlogick
authored
Close file on invalid range (Addition to #15166) (#15268) (#15308)
* Close file on invalid range. * Close on seek error Signed-off-by: Andrew Thornton <[email protected]> * Moved 'Seek' into server. * io.ReadSeekCloser is only available in Go 1.16 Co-authored-by: Andrew Thornton <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: Andrew Thornton <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent a8c6a4a commit cf9b6c2

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

modules/lfs/content_store.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,13 @@ type ContentStore struct {
4444
}
4545

4646
// Get takes a Meta object and retrieves the content from the store, returning
47-
// it as an io.Reader. If fromByte > 0, the reader starts from that byte
48-
func (s *ContentStore) Get(meta *models.LFSMetaObject, fromByte int64) (io.ReadCloser, error) {
47+
// it as an io.ReadSeekCloser.
48+
func (s *ContentStore) Get(meta *models.LFSMetaObject) (storage.Object, error) {
4949
f, err := s.Open(meta.RelativePath())
5050
if err != nil {
5151
log.Error("Whilst trying to read LFS OID[%s]: Unable to open Error: %v", meta.Oid, err)
5252
return nil, err
5353
}
54-
if fromByte > 0 {
55-
if fromByte >= meta.Size {
56-
return nil, ErrRangeNotSatisfiable{
57-
FromByte: fromByte,
58-
}
59-
}
60-
_, err = f.Seek(fromByte, io.SeekStart)
61-
if err != nil {
62-
log.Error("Whilst trying to read LFS OID[%s]: Unable to seek to %d Error: %v", meta.Oid, fromByte, err)
63-
}
64-
}
6554
return f, err
6655
}
6756

modules/lfs/pointers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ func IsPointerFile(buf *[]byte) *models.LFSMetaObject {
6767
// ReadMetaObject will read a models.LFSMetaObject and return a reader
6868
func ReadMetaObject(meta *models.LFSMetaObject) (io.ReadCloser, error) {
6969
contentStore := &ContentStore{ObjectStorage: storage.LFS}
70-
return contentStore.Get(meta, 0)
70+
return contentStore.Get(meta)
7171
}

modules/lfs/server.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ func getContentHandler(ctx *context.Context) {
175175
statusCode = 206
176176
fromByte, _ = strconv.ParseInt(match[1], 10, 32)
177177

178+
if fromByte >= meta.Size {
179+
writeStatus(ctx, http.StatusRequestedRangeNotSatisfiable)
180+
return
181+
}
182+
178183
if match[2] != "" {
179184
_toByte, _ := strconv.ParseInt(match[2], 10, 32)
180185
if _toByte >= fromByte && _toByte < toByte {
@@ -188,18 +193,24 @@ func getContentHandler(ctx *context.Context) {
188193
}
189194

190195
contentStore := &ContentStore{ObjectStorage: storage.LFS}
191-
content, err := contentStore.Get(meta, fromByte)
196+
content, err := contentStore.Get(meta)
192197
if err != nil {
193-
if IsErrRangeNotSatisfiable(err) {
194-
writeStatus(ctx, http.StatusRequestedRangeNotSatisfiable)
195-
} else {
196-
// Errors are logged in contentStore.Get
197-
writeStatus(ctx, 404)
198-
}
198+
// Errors are logged in contentStore.Get
199+
writeStatus(ctx, http.StatusNotFound)
199200
return
200201
}
201202
defer content.Close()
202203

204+
if fromByte > 0 {
205+
_, err = content.Seek(fromByte, io.SeekStart)
206+
if err != nil {
207+
log.Error("Whilst trying to read LFS OID[%s]: Unable to seek to %d Error: %v", meta.Oid, fromByte, err)
208+
209+
writeStatus(ctx, http.StatusInternalServerError)
210+
return
211+
}
212+
}
213+
203214
contentLength := toByte + 1 - fromByte
204215
ctx.Resp.Header().Set("Content-Length", strconv.FormatInt(contentLength, 10))
205216
ctx.Resp.Header().Set("Content-Type", "application/octet-stream")

0 commit comments

Comments
 (0)