From 14f2ed32f13b55dbce0f417f21ccf7b68056ae05 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Fri, 30 Oct 2020 16:09:11 +0100 Subject: added max range and get-entries sanity checking --- trillian.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 trillian.go (limited to 'trillian.go') diff --git a/trillian.go b/trillian.go new file mode 100644 index 0000000..e5117da --- /dev/null +++ b/trillian.go @@ -0,0 +1,32 @@ +package stfe + +import ( + "fmt" + + "net/http" + + "github.com/google/trillian" + "github.com/google/trillian/types" +) + +// checkGetLeavesByRange does santity-checking on a Trillian response +func checkGetLeavesByRange(rsp *trillian.GetLeavesByRangeResponse, req GetEntriesRequest) (int, error) { + if len(rsp.Leaves) > int(req.End-req.Start+1) { + return http.StatusInternalServerError, fmt.Errorf("backend GetLeavesByRange returned too many leaves: %d for [%d,%d]", len(rsp.Leaves), req.Start, req.End) + } + + var lr types.LogRootV1 + if err := lr.UnmarshalBinary(rsp.GetSignedLogRoot().GetLogRoot()); err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed unmarshaling log root: %v", err) + } + if uint64(req.Start) >= lr.TreeSize { + return http.StatusBadRequest, fmt.Errorf("invalid start(%d): tree size is %d", req.Start, lr.TreeSize) + } + + for i, leaf := range rsp.Leaves { + if leaf.LeafIndex != req.Start+int64(i) { + return http.StatusInternalServerError, fmt.Errorf("backend GetLeavesByRange returned unexpected leaf index: wanted %d, got %d", req.Start+int64(i), leaf.LeafIndex) + } + } + return 0, nil +} -- cgit v1.2.3