mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-01 18:00:46 +01:00
* fix(volume): reject a negative suffix length in Range headers
parseRange accepted "bytes=--5" as a suffix range of -5 bytes and
returned {start: size+5, length: -5}. The volume server then answered
206 with an empty body and Content-Range "bytes 15-9/10", and the
filer's inline-content reader sliced entry.Content[15:10] and panicked.
Restore the check Go's net/http parseRange has: a suffix-length must be
a non-negative integer (RFC 7233 2.1), so a leading '-' or a negative
value is an invalid range. Apply the same check to the Rust volume
server's parse_range_header.
* test: negative-suffix manifest read is now rejected with 416
The parser change makes a negative suffix an invalid-range error, so
the duplicate-range case in this test can no longer reach the "empty
200" path. Assert the new rejection and that nothing is read.
* volume: treat a zero-length suffix range as unsatisfiable
"bytes=-0" resolved to {start: size, length: 0}, and the range handler
then answered 206 with "Content-Range: bytes 10-9/10", a value clients
must not recombine (and a first-byte-pos RFC 9110 cannot express).
Classify the zero-length suffix with the existing no-overlap case —
its resolved first-byte-pos sits at the content size — so the request
gets a 416 "bytes */size" unless another range in the set overlaps.
Applies to the Go and Rust volume servers alike.
---------
Co-authored-by: chrislusf <chrislusf@gmail.com>
Co-authored-by: user.email <chrislusf@users.noreply.github.com>
67 lines
2.7 KiB
Go
67 lines
2.7 KiB
Go
package weed_server
|
|
|
|
import "testing"
|
|
|
|
func TestParseRange(t *testing.T) {
|
|
const size = int64(10)
|
|
|
|
tests := []struct {
|
|
name string
|
|
rangeSpec string
|
|
wantErr bool
|
|
wantStart int64
|
|
wantLen int64
|
|
}{
|
|
{name: "start within bounds", rangeSpec: "bytes=0-", wantStart: 0, wantLen: 10},
|
|
{name: "start at last valid byte", rangeSpec: "bytes=9-", wantStart: 9, wantLen: 1},
|
|
{name: "start equal to size is not satisfiable", rangeSpec: "bytes=10-", wantErr: true},
|
|
{name: "start past size is not satisfiable", rangeSpec: "bytes=11-", wantErr: true},
|
|
{name: "start with explicit end within bounds", rangeSpec: "bytes=2-5", wantStart: 2, wantLen: 4},
|
|
{name: "end clamped to the last byte", rangeSpec: "bytes=2-100", wantStart: 2, wantLen: 8},
|
|
{name: "suffix range within bounds", rangeSpec: "bytes=-3", wantStart: 7, wantLen: 3},
|
|
{name: "zero-length suffix range is not satisfiable", rangeSpec: "bytes=-0", wantErr: true},
|
|
{name: "zero-length suffix beside a satisfiable range is skipped", rangeSpec: "bytes=-0,0-1", wantStart: 0, wantLen: 2},
|
|
{name: "suffix range larger than size is clamped, not rejected", rangeSpec: "bytes=-100", wantStart: 0, wantLen: 10},
|
|
{name: "negative suffix length is invalid", rangeSpec: "bytes=--5", wantErr: true},
|
|
{name: "negative zero suffix length is invalid", rangeSpec: "bytes=--0", wantErr: true},
|
|
{name: "empty suffix length is invalid", rangeSpec: "bytes=-", wantErr: true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ranges, err := parseRange(tt.rangeSpec, size)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("parseRange(%q, %d) = %+v, want an error", tt.rangeSpec, size, ranges)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("parseRange(%q, %d) returned unexpected error: %v", tt.rangeSpec, size, err)
|
|
}
|
|
if len(ranges) != 1 {
|
|
t.Fatalf("parseRange(%q, %d) = %d ranges, want 1", tt.rangeSpec, size, len(ranges))
|
|
}
|
|
if ranges[0].start != tt.wantStart || ranges[0].length != tt.wantLen {
|
|
t.Fatalf("parseRange(%q, %d) = {start:%d length:%d}, want {start:%d length:%d}",
|
|
tt.rangeSpec, size, ranges[0].start, ranges[0].length, tt.wantStart, tt.wantLen)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseRangeOnEmptyFile(t *testing.T) {
|
|
// A zero-length file has no valid byte offsets at all, not even 0, so any
|
|
// range request against it must be rejected rather than satisfied with an
|
|
// empty range.
|
|
if _, err := parseRange("bytes=0-", 0); err == nil {
|
|
t.Fatal("parseRange(\"bytes=0-\", 0) = nil error, want an error")
|
|
}
|
|
if _, err := parseRange("bytes=-0", 0); err == nil {
|
|
t.Fatal("parseRange(\"bytes=-0\", 0) = nil error, want an error")
|
|
}
|
|
if _, err := parseRange("bytes=-5", 0); err == nil {
|
|
t.Fatal("parseRange(\"bytes=-5\", 0) = nil error, want an error")
|
|
}
|
|
}
|