Skip to content

Commit 88c5b8c

Browse files
authored
fix(p2p): preemptive panic recovery (#174)
This protects the library from potential dos vectors introduced by go-header users in the header deserialization and verification code paths.
1 parent de4400b commit 88c5b8c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

p2p/session.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,14 @@ func (s *session[H]) doRequest(
259259
}
260260

261261
// processResponses converts HeaderResponse to Header.
262-
func (s *session[H]) processResponses(responses []*p2p_pb.HeaderResponse) ([]H, error) {
262+
func (s *session[H]) processResponses(responses []*p2p_pb.HeaderResponse) (h []H, err error) {
263+
defer func() {
264+
r := recover()
265+
if r != nil {
266+
err = fmt.Errorf("PANIC processing responses: %s", r)
267+
}
268+
}()
269+
263270
hdrs, err := processResponses[H](responses)
264271
if err != nil {
265272
return nil, err

p2p/subscriber.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ func (s *Subscriber[H]) Stop(context.Context) error {
9999
// SetVerifier set given verification func as Header PubSub topic validator
100100
// Does not punish peers if *header.VerifyError is given with Uncertain set to true.
101101
func (s *Subscriber[H]) SetVerifier(val func(context.Context, H) error) error {
102-
pval := func(ctx context.Context, p peer.ID, msg *pubsub.Message) pubsub.ValidationResult {
102+
pval := func(ctx context.Context, p peer.ID, msg *pubsub.Message) (res pubsub.ValidationResult) {
103+
defer func() {
104+
err := recover()
105+
if err != nil {
106+
log.Errorf("PANIC while unmarshalling or verifying header: %s", err)
107+
res = pubsub.ValidationReject
108+
}
109+
}()
110+
103111
hdr := header.New[H]()
104112
err := hdr.UnmarshalBinary(msg.Data)
105113
if err != nil {

0 commit comments

Comments
 (0)