From 402711d33afefd7690f6c260a0962387e1d0f48b Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Thu, 5 Nov 2020 15:47:43 +0100 Subject: refactored trillian response checking Added a few checks that might be redundant, but better safe than sorry. --- handler.go | 35 ++++++++++------------------------- trillian.go | 49 +++++++++++++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 47 deletions(-) diff --git a/handler.go b/handler.go index 1099527..7ec69bc 100644 --- a/handler.go +++ b/handler.go @@ -65,11 +65,8 @@ func addEntry(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.R }, } trsp, err := i.Client.QueueLeaf(ctx, &treq) - if err != nil { - return http.StatusInternalServerError, fmt.Errorf("backend QueueLeaf request failed: %v", err) - } - if status, err := checkQueueLeaf(trsp); err != nil { - return status, err + if status, errInner := checkQueueLeaf(trsp, err); errInner != nil { + return status, fmt.Errorf("bad QueueLeafResponse: %v", errInner) } sdi, err := i.LogParameters.genV1Sdi(trsp.QueuedLeaf.Leaf.LeafValue) @@ -100,11 +97,8 @@ func getEntries(ctx context.Context, i *Instance, w http.ResponseWriter, r *http Count: req.End - req.Start + 1, } trsp, err := i.Client.GetLeavesByRange(ctx, &treq) - if err != nil { - return http.StatusInternalServerError, fmt.Errorf("backend GetLeavesByRange request failed: %v", err) - } - if status, err := checkGetLeavesByRange(trsp, req); err != nil { - return status, err + if status, errInner := checkGetLeavesByRange(req, trsp, err); errInner != nil { + return status, fmt.Errorf("bad GetLeavesByRangeResponse: %v", errInner) } rsp, err := i.LogParameters.newGetEntriesResponse(trsp.Leaves) @@ -142,11 +136,8 @@ func getProofByHash(ctx context.Context, i *Instance, w http.ResponseWriter, r * OrderBySequence: true, } trsp, err := i.Client.GetInclusionProofByHash(ctx, &treq) - if err != nil { - return http.StatusInternalServerError, fmt.Errorf("failed fetching inclusion proof from Trillian backend: %v", err) - } - if status, err := checkGetInclusionProofByHash(trsp, i.LogParameters); err != nil { - return status, err + if status, errInner := checkGetInclusionProofByHash(i.LogParameters, trsp, err); errInner != nil { + return status, fmt.Errorf("bad GetInclusionProofByHashResponse: %v", errInner) } rsp, err := NewInclusionProofV1(i.LogParameters.LogId, uint64(req.TreeSize), trsp.Proof[0]).MarshalB64() @@ -173,11 +164,8 @@ func getConsistencyProof(ctx context.Context, i *Instance, w http.ResponseWriter SecondTreeSize: int64(req.Second), } trsp, err := i.Client.GetConsistencyProof(ctx, &treq) - if err != nil { - return http.StatusInternalServerError, fmt.Errorf("failed fetching consistency proof from Trillian backend: %v", err) - } - if status, err := checkGetConsistencyProofResponse(trsp, i.LogParameters); err != nil { - return status, err + if status, errInner := checkGetConsistencyProof(i.LogParameters, trsp, err); errInner != nil { + return status, fmt.Errorf("bad GetConsistencyProofResponse: %v", errInner) } rsp, err := NewConsistencyProofV1(i.LogParameters.LogId, req.First, req.Second, trsp.Proof).MarshalB64() @@ -197,11 +185,8 @@ func getSth(ctx context.Context, i *Instance, w http.ResponseWriter, _ *http.Req LogId: i.LogParameters.TreeId, } trsp, err := i.Client.GetLatestSignedLogRoot(ctx, &treq) - if err != nil { - return http.StatusInternalServerError, fmt.Errorf("failed fetching signed tree head from Trillian backend: %v", err) - } - if status, err := checkTrillianGetLatestSignedLogRoot(trsp, i.LogParameters); err != nil { - return status, err + if status, errInner := checkGetLatestSignedLogRoot(i.LogParameters, trsp, err); errInner != nil { + return status, fmt.Errorf("bad GetLatestSignedLogRootResponse: %v", errInner) } th, err := NewTreeHeadV1(i.LogParameters, trsp.SignedLogRoot) diff --git a/trillian.go b/trillian.go index 57494b1..02b220c 100644 --- a/trillian.go +++ b/trillian.go @@ -11,7 +11,10 @@ import ( "google.golang.org/grpc/codes" ) -func checkQueueLeaf(rsp *trillian.QueueLeafResponse) (int, error) { +func checkQueueLeaf(rsp *trillian.QueueLeafResponse, err error) (int, error) { + if err != nil || rsp == nil || rsp.QueuedLeaf == nil { + return http.StatusInternalServerError, fmt.Errorf("%v", err) + } if codes.Code(rsp.QueuedLeaf.GetStatus().GetCode()) == codes.AlreadyExists { // no need to report this as an invalid request, just (re)issue sdi glog.V(3).Infof("queued leaf is a duplicate => %X", rsp.QueuedLeaf.Leaf.LeafValue) @@ -19,15 +22,18 @@ func checkQueueLeaf(rsp *trillian.QueueLeafResponse) (int, error) { return 0, nil } -func checkGetLeavesByRange(rsp *trillian.GetLeavesByRangeResponse, req *GetEntriesRequest) (int, error) { +func checkGetLeavesByRange(req *GetEntriesRequest, rsp *trillian.GetLeavesByRangeResponse, err error) (int, error) { + if err != nil || rsp == nil || len(rsp.Leaves) == 0 || rsp.SignedLogRoot == nil || rsp.SignedLogRoot.LogRoot == nil { + return http.StatusInternalServerError, fmt.Errorf("%v", err) + } 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) + return http.StatusInternalServerError, fmt.Errorf("too many leaves: %d for [%d,%d]", len(rsp.Leaves), req.Start, req.End) } // Ensure that a bad start parameter results in an error 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 err := lr.UnmarshalBinary(rsp.SignedLogRoot.LogRoot); err != nil { + return http.StatusInternalServerError, fmt.Errorf("cannot unmarshal log root: %v", err) } if uint64(req.Start) >= lr.TreeSize { return http.StatusNotFound, fmt.Errorf("invalid start(%d): tree size is %d", req.Start, lr.TreeSize) @@ -42,32 +48,31 @@ func checkGetLeavesByRange(rsp *trillian.GetLeavesByRangeResponse, req *GetEntri return 0, nil } -func checkGetInclusionProofByHash(rsp *trillian.GetInclusionProofByHashResponse, lp *LogParameters) (int, error) { - if rsp.Proof == nil || len(rsp.Proof) == 0 { - return http.StatusNotFound, fmt.Errorf("incomplete backend response") - } // maybe redundant, but better safe than sorry +func checkGetInclusionProofByHash(lp *LogParameters, rsp *trillian.GetInclusionProofByHashResponse, err error) (int, error) { + if err != nil || rsp == nil || len(rsp.Proof) == 0 || rsp.Proof[0] == nil { + return http.StatusInternalServerError, fmt.Errorf("%v", err) + } return checkHashPath(lp.HashType.Size(), rsp.Proof[0].Hashes) } -func checkGetConsistencyProofResponse(rsp *trillian.GetConsistencyProofResponse, lp *LogParameters) (int, error) { - if rsp.Proof == nil { - return http.StatusNotFound, fmt.Errorf("incomplete backend response") - } // not redundant, out-of-range parameters yield an empty proof w/o error +func checkGetConsistencyProof(lp *LogParameters, rsp *trillian.GetConsistencyProofResponse, err error) (int, error) { + if err != nil || rsp == nil || rsp.Proof == nil { + return http.StatusInternalServerError, fmt.Errorf("%v", err) + } return checkHashPath(lp.HashType.Size(), rsp.Proof.Hashes) } -func checkTrillianGetLatestSignedLogRoot(rsp *trillian.GetLatestSignedLogRootResponse, lp *LogParameters) (int, error) { - if rsp.SignedLogRoot == nil { - return http.StatusInternalServerError, fmt.Errorf("incomplete backend response") - } // maybe redundant - +func checkGetLatestSignedLogRoot(lp *LogParameters, rsp *trillian.GetLatestSignedLogRootResponse, err error) (int, error) { + if err != nil || rsp == nil || rsp.SignedLogRoot == nil || rsp.SignedLogRoot.LogRoot == nil { + return http.StatusInternalServerError, fmt.Errorf("%v", err) + } 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 err := lr.UnmarshalBinary(rsp.SignedLogRoot.LogRoot); err != nil { + return http.StatusInternalServerError, fmt.Errorf("cannot unmarshal log root: %v", err) } if len(lr.RootHash) != lp.HashType.Size() { return http.StatusInternalServerError, fmt.Errorf("invalid root hash: %v", lr.RootHash) - } // maybe redundant, but would not necessarily be caught by marshal error + } return 0, nil } @@ -76,6 +81,6 @@ func checkHashPath(hashSize int, path [][]byte) (int, error) { if len(hash) != hashSize { return http.StatusInternalServerError, fmt.Errorf("invalid proof: %v", path) } - } // maybe redundant, but would not necessarily be caught by marshal error + } return 0, nil } -- cgit v1.2.3