From 8e6742de1c63ecaf33d44340dfa72524dcf5eddc Mon Sep 17 00:00:00 2001
From: Jake Taylor <jake@elixxir.io>
Date: Thu, 4 Aug 2022 12:02:31 -0500
Subject: [PATCH] fix segfaults surrounding connect.Callbacks implementation of
 auth.Callbacks

---
 connect/authCallbacks.go | 53 ++++++++++++++--------------------------
 connect/connect.go       |  6 ++---
 2 files changed, 21 insertions(+), 38 deletions(-)

diff --git a/connect/authCallbacks.go b/connect/authCallbacks.go
index cf1740905..d8bda8d1d 100644
--- a/connect/authCallbacks.go
+++ b/connect/authCallbacks.go
@@ -17,13 +17,11 @@ import (
 )
 
 // clientAuthCallback provides callback functionality for interfacing between
-// auth.State and Connection. This is used both for blocking creation of a
-// Connection object until the auth Request is confirmed and for dynamically
-// building new Connection objects when an auth Request is received.
+// auth.State and Connection. This is used for building new Connection
+// objects when an auth Confirm is received.
 type clientAuthCallback struct {
 	// Used for signaling confirmation of E2E partnership
 	confirmCallback Callback
-	requestCallback Callback
 
 	// Used for building new Connection objects
 	connectionE2e    clientE2e.Handler
@@ -31,14 +29,12 @@ type clientAuthCallback struct {
 	authState        auth.State
 }
 
-// getClientAuthCallback returns a callback interface to be passed into the creation
-// of an auth.State object.
-// it will accept requests only if a request callback is passed in
-func getClientAuthCallback(confirm, request Callback, e2e clientE2e.Handler,
+// getClientAuthCallback returns an auth.Callbacks interface to be passed into the creation
+// of an auth.State object for connect clients.
+func getClientAuthCallback(confirm Callback, e2e clientE2e.Handler,
 	auth auth.State, params xxdk.E2EParams) *clientAuthCallback {
 	return &clientAuthCallback{
 		confirmCallback:  confirm,
-		requestCallback:  request,
 		connectionE2e:    e2e,
 		connectionParams: params,
 		authState:        auth,
@@ -58,17 +54,13 @@ func (a clientAuthCallback) Confirm(requestor contact.Contact,
 		jww.ERROR.Printf("Unable to build connection with "+
 			"partner %s: %+v", requestor.ID, err)
 		// Send a nil connection to avoid hold-ups down the line
-		if a.confirmCallback != nil {
-			a.confirmCallback(nil)
-		}
+		a.confirmCallback(nil)
 		return
 	}
 
 	// Return the new Connection object
-	if a.confirmCallback != nil {
-		a.confirmCallback(BuildConnection(newPartner, a.connectionE2e,
-			a.authState, a.connectionParams))
-	}
+	a.confirmCallback(BuildConnection(newPartner, a.connectionE2e,
+		a.authState, a.connectionParams))
 }
 
 // Request will be called when an auth Request message is processed.
@@ -82,12 +74,10 @@ func (a clientAuthCallback) Reset(contact.Contact,
 }
 
 // serverAuthCallback provides callback functionality for interfacing between
-// auth.State and Connection. This is used both for blocking creation of a
-// Connection object until the auth Request is confirmed and for dynamically
-// building new Connection objects when an auth Request is received.
+// auth.State and Connection. This is used for building new Connection
+//objects when an auth Request is received.
 type serverAuthCallback struct {
 	// Used for signaling confirmation of E2E partnership
-	confirmCallback Callback
 	requestCallback Callback
 
 	// Used to track stale connections
@@ -97,13 +87,11 @@ type serverAuthCallback struct {
 	connectionParams xxdk.E2EParams
 }
 
-// getServerAuthCallback returns a callback interface to be passed into the creation
-// of a xxdk.E2e object.
-// it will accept requests only if a request callback is passed in
-func getServerAuthCallback(confirm, request Callback, cl *ConnectionList,
+// getServerAuthCallback returns an auth.Callbacks interface to be passed into the creation
+// of a xxdk.E2e object for connect servers.
+func getServerAuthCallback(request Callback, cl *ConnectionList,
 	params xxdk.E2EParams) *serverAuthCallback {
 	return &serverAuthCallback{
-		confirmCallback:  confirm,
 		requestCallback:  request,
 		cl:               cl,
 		connectionParams: params,
@@ -118,25 +106,22 @@ func (a serverAuthCallback) Confirm(contact.Contact,
 // Request will be called when an auth Request message is processed.
 func (a serverAuthCallback) Request(requestor contact.Contact,
 	_ receptionID.EphemeralIdentity, _ rounds.Round, user *xxdk.E2e) {
-	if a.requestCallback == nil {
-		jww.ERROR.Printf("Received a request when requests are" +
-			"not enable, will not accept")
-	}
+	jww.DEBUG.Printf("Connection auth request for %s received",
+		requestor.ID.String())
+
+	// Auto-confirm the auth request
 	_, err := user.GetAuth().Confirm(requestor)
 	if err != nil {
 		jww.ERROR.Printf("Unable to build connection with "+
 			"partner %s: %+v", requestor.ID, err)
-		// Send a nil connection to avoid hold-ups down the line
-		a.requestCallback(nil)
+		return
 	}
+
 	// After confirmation, get the new partner
 	newPartner, err := user.GetE2E().GetPartner(requestor.ID)
 	if err != nil {
 		jww.ERROR.Printf("Unable to build connection with "+
 			"partner %s: %+v", requestor.ID, err)
-		// Send a nil connection to avoid hold-ups down the line
-		a.requestCallback(nil)
-
 		return
 	}
 
diff --git a/connect/connect.go b/connect/connect.go
index f998281a2..5794cbbe6 100644
--- a/connect/connect.go
+++ b/connect/connect.go
@@ -88,7 +88,7 @@ func Connect(recipient contact.Contact, user *xxdk.E2e,
 	cb := func(connection Connection) {
 		signalChannel <- connection
 	}
-	callback := getClientAuthCallback(cb, nil, user.GetE2E(),
+	callback := getClientAuthCallback(cb, user.GetE2E(),
 		user.GetAuth(), p)
 	user.GetAuth().AddPartnerCallback(recipient.ID, callback)
 
@@ -128,8 +128,6 @@ func Connect(recipient contact.Contact, user *xxdk.E2e,
 //
 // This calls xxdk.LoginEphemeral under the hood and the connection
 // server must be the only listener on auth.
-//
-// The given Callback needs to handle receiving a nil Connection.
 func StartServer(identity xxdk.ReceptionIdentity, connectionCallback Callback,
 	net *xxdk.Cmix, params xxdk.E2EParams, clParams ConnectionListParams) (*ConnectionServer, error) {
 
@@ -141,7 +139,7 @@ func StartServer(identity xxdk.ReceptionIdentity, connectionCallback Callback,
 	}
 
 	// Build callback for E2E negotiation
-	callback := getServerAuthCallback(nil, connectionCallback, cl, params)
+	callback := getServerAuthCallback(connectionCallback, cl, params)
 
 	e2eClient, err := xxdk.LoginEphemeral(net, callback, identity, params)
 	if err != nil {
-- 
GitLab