Skip to content

Commit

Permalink
fix: validate lightpush requests
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Apr 9, 2024
1 parent 826c7fb commit e6e4c83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
18 changes: 17 additions & 1 deletion cmd/waku/server/rest/lightpush_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (msg lightpushRequest) Check() error {
if msg.Message == nil {
return errors.New("waku message is required")
}

return nil
}

Expand Down Expand Up @@ -66,14 +67,29 @@ func (serv *LightpushService) postMessagev1(w http.ResponseWriter, req *http.Req
message, err := request.Message.ToProto()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, err = w.Write([]byte(err.Error()))
if err != nil {
serv.log.Error("writing response", zap.Error(err))
}
return
}

if err = message.Validate(); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
_, err = w.Write([]byte(err.Error()))
if err != nil {
serv.log.Error("writing response", zap.Error(err))
}
return
}

_, err = serv.node.Lightpush().Publish(req.Context(), message, lightpush.WithPubSubTopic(request.PubSubTopic))
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
_, err = w.Write([]byte(err.Error()))
serv.log.Error("writing response", zap.Error(err))
if err != nil {
serv.log.Error("writing response", zap.Error(err))
}
} else {
writeErrOrResponse(w, err, true)
}
Expand Down
7 changes: 5 additions & 2 deletions waku/v2/protocol/lightpush/waku_lightpush.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (wakuLP *WakuLightPush) onRequest(ctx context.Context) func(network.Stream)

responsePushRPC.RequestId = requestPushRPC.RequestId
if err := requestPushRPC.ValidateRequest(); err != nil {
responseMsg := err.Error()
responseMsg := "invalid request: " + err.Error()
responsePushRPC.Response.Info = &responseMsg
wakuLP.metrics.RecordError(requestBodyFailure)
wakuLP.reply(stream, responsePushRPC, logger)
Expand Down Expand Up @@ -205,6 +205,9 @@ func (wakuLP *WakuLightPush) request(ctx context.Context, req *pb.PushRequest, p
return nil, err
}
pushRequestRPC := &pb.PushRpc{RequestId: hex.EncodeToString(params.requestID), Request: req}
if err = pushRequestRPC.ValidateRequest(); err != nil {
return nil, err
}

writer := pbio.NewDelimitedWriter(stream)
reader := pbio.NewDelimitedReader(stream, math.MaxInt32)
Expand Down Expand Up @@ -234,7 +237,7 @@ func (wakuLP *WakuLightPush) request(ctx context.Context, req *pb.PushRequest, p

if err = pushResponseRPC.ValidateResponse(pushRequestRPC.RequestId); err != nil {
wakuLP.metrics.RecordError(responseBodyFailure)
return nil, err
return nil, fmt.Errorf("invalid response: %w", err)
}

return pushResponseRPC.Response, nil
Expand Down

0 comments on commit e6e4c83

Please sign in to comment.