From be6289be515ee130acfa4cfb5953c417db73127c Mon Sep 17 00:00:00 2001 From: Derek Collison Date: Sun, 13 Dec 2020 11:49:48 -0800 Subject: [PATCH] Fix for https://github.com/nats-io/jetstream/issues/406 Signed-off-by: Derek Collison --- server/filestore.go | 6 ++++++ server/filestore_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/server/filestore.go b/server/filestore.go index da13e9ee..1aaf65a8 100644 --- a/server/filestore.go +++ b/server/filestore.go @@ -2289,6 +2289,12 @@ func (mb *msgBlock) readIndexInfo() error { mb.last.ts = readTimeStamp() dmapLen := readCount() + // Check if this is a short write index file. + if bi < 0 || bi+checksumSize > len(buf) { + defer os.Remove(mb.ifn) + return fmt.Errorf("short index file") + } + // Checksum copy(mb.lchk[0:], buf[bi:bi+checksumSize]) bi += checksumSize diff --git a/server/filestore_test.go b/server/filestore_test.go index 5e431718..956fe18d 100644 --- a/server/filestore_test.go +++ b/server/filestore_test.go @@ -17,6 +17,7 @@ import ( "archive/tar" "bytes" "compress/gzip" + "encoding/base64" "encoding/hex" "encoding/json" "fmt" @@ -28,6 +29,7 @@ import ( "path" "path/filepath" "reflect" + "strings" "testing" "time" ) @@ -2410,3 +2412,16 @@ func TestFileStoreConsumerPerf(t *testing.T) { oc.syncStateFile() fmt.Printf("time to sync is %v\n", time.Since(start)) } + +func TestFileStoreStreamIndexBug(t *testing.T) { + // https://github.com/nats-io/jetstream/issues/406 + badIdxBytes, _ := base64.StdEncoding.DecodeString("FgGBkw7D/f8/772iDPDIgbU=") + dir, _ := ioutil.TempDir("", "js-bad-idx-") + defer os.RemoveAll(dir) + fn := path.Join(dir, "1.idx") + ioutil.WriteFile(fn, badIdxBytes, 0644) + mb := &msgBlock{index: 1, ifn: fn} + if err := mb.readIndexInfo(); err == nil || !strings.Contains(err.Error(), "short index") { + t.Fatalf("Expected error during readIndexInfo(): %v", err) + } +}