fix: properly check responses for registration and cancellation requests

This commit is contained in:
Felipe M 2025-08-12 18:56:06 +02:00
parent 9bd0071b4a
commit 96d8b84dcb
No known key found for this signature in database
GPG key ID: 52E5D65FCF99808A

View file

@ -235,18 +235,20 @@ func (r *InBandRegistration) RegisterAccount(serverJID jid.JID, request *Registr
Error: fmt.Sprintf("failed to send registration request: %v", err), Error: fmt.Sprintf("failed to send registration request: %v", err),
}, nil }, nil
} }
defer response.Close()
// Try to unmarshal the response as an error IQ first // Try to unmarshal the response as an error IQ first
responseIQ, err := stanza.UnmarshalIQError(response, xml.StartElement{}) responseIQ, err := stanza.UnmarshalIQError(response, xml.StartElement{})
registrationResponse := &InBandRegistrationResponse{} registrationResponse := &InBandRegistrationResponse{}
response.Close()
if err != nil { if err != nil {
// If we can't unmarshal as error IQ, check if it's a success response // If we can't parse the response, treat it as a failure and log the parse error
// For now, assume success if no error occurred during sending registrationResponse.Success = false
registrationResponse.Success = true registrationResponse.Error = "Failed to parse server response for registration request"
registrationResponse.Message = "Account registration completed successfully" r.logger.LogWarn("Registration response could not be parsed, treating as failure",
r.logger.LogDebug("Registration response could not be parsed as error IQ, assuming success", "server", serverJID.String(), "username", request.Username, "error", err) "server", serverJID.String(),
"username", request.Username,
"parse_error", err.Error())
} else { } else {
// Successfully unmarshaled - check IQ type // Successfully unmarshaled - check IQ type
if responseIQ.Type == stanza.ErrorIQ { if responseIQ.Type == stanza.ErrorIQ {
@ -370,18 +372,20 @@ func (r *InBandRegistration) CancelRegistration(serverJID jid.JID, request *Canc
Error: fmt.Sprintf("failed to send registration cancellation request: %v", err), Error: fmt.Sprintf("failed to send registration cancellation request: %v", err),
}, nil }, nil
} }
defer response.Close()
// Try to unmarshal the response as an error IQ first // Try to unmarshal the response as an error IQ first
responseIQ, err := stanza.UnmarshalIQError(response, xml.StartElement{}) responseIQ, err := stanza.UnmarshalIQError(response, xml.StartElement{})
cancellationResponse := &InBandRegistrationResponse{} cancellationResponse := &InBandRegistrationResponse{}
response.Close()
if err != nil { if err != nil {
// If we can't unmarshal as error IQ, check if it's a success response // If we can't parse the response, treat it as a failure and log the parse error
// For now, assume success if no error occurred during sending cancellationResponse.Success = false
cancellationResponse.Success = true cancellationResponse.Error = "Failed to parse server response for cancellation request"
cancellationResponse.Message = "Registration cancelled successfully" r.logger.LogWarn("Cancellation response could not be parsed, treating as failure",
r.logger.LogDebug("Cancellation response could not be parsed as error IQ, assuming success", "server", serverJID.String(), "username", request.Username, "error", err) "server", serverJID.String(),
"username", request.Username,
"parse_error", err.Error())
} else { } else {
// Successfully unmarshaled - check IQ type // Successfully unmarshaled - check IQ type
if responseIQ.Type == stanza.ErrorIQ { if responseIQ.Type == stanza.ErrorIQ {