Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions golibdave/golibdave.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func (s *session) prepareEpoch(epoch int, protocolVersion uint16) {
return
}

s.logger.Warn("prepareEpoch: resetting MLS session via Init",
slog.Int("epoch", epoch),
slog.Int("protocol_version", int(protocolVersion)),
)
s.session.Init(protocolVersion, uint64(s.channelID), string(s.selfUserID))
}

Expand Down Expand Up @@ -228,17 +232,33 @@ func (s *session) setupKeyRatchetForUser(userID godave.UserID, protocolVersion u
disabled := protocolVersion == disabledProtocolVersion

if userID == s.selfUserID {
kr := s.session.GetKeyRatchet(string(userID))
if !disabled && kr == nil {
s.logger.Warn("nil key ratchet for self after GetKeyRatchet",
slog.String("user_id", string(userID)),
slog.Int("protocol_version", int(protocolVersion)),
)
return
}
Comment on lines +235 to +242
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will never happen. If it did, we would be getting crashes everywhere due to newKeyRatchet.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's literally how I found the bug lol. my bot was crashing nonstop with ErrMissingKeyRatchet every time someone joined/left a voice channel until I traced it back to this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's literally how I found the bug lol. my bot was crashing nonstop with ErrMissingKeyRatchet every time someone joined/left a voice channel until I traced it back to this.

That isn't a crash, thats just a warning/error raised by the underlying library.

I will take a look when I find some time, but thank you for raising this issue. I am bit skeptical on changing the current behaviour because this is a very finiky system and I don't want to break it when DAVE fully rolls out and it breaks because we are always sending unencrypted packets / being unable to decrypt any packages correctly

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davfsa honestly I'm glad you're pushing back, I'm not a Go developer so having someone who actually knows the internals review this properly is exactly what I need. switching from discordgo to disgo was the right move.

no rush, appreciate it 👍

s.encryptor.SetPassthroughMode(disabled)
if !disabled {
s.encryptor.SetKeyRatchet(s.session.GetKeyRatchet(string(userID)))
s.encryptor.SetKeyRatchet(kr)
}
return
}

decryptor := s.decryptors[userID]
kr := s.session.GetKeyRatchet(string(userID))
if !disabled && kr == nil {
s.logger.Warn("nil key ratchet for user after GetKeyRatchet",
slog.String("user_id", string(userID)),
slog.Int("protocol_version", int(protocolVersion)),
)
return
}
Comment on lines +251 to +258
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto the above

decryptor.TransitionToPassthroughMode(disabled)
if !disabled {
decryptor.TransitionToKeyRatchet(s.session.GetKeyRatchet(string(userID)))
decryptor.TransitionToKeyRatchet(kr)
}
}

Expand Down
4 changes: 4 additions & 0 deletions libdave/key_ratchet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type KeyRatchet struct {
}

func newKeyRatchet(handle keyRatchetHandle) *KeyRatchet {
if handle == nil {
return nil
}

keyRatchet := &KeyRatchet{handle: handle}

runtime.SetFinalizer(keyRatchet, func(k *KeyRatchet) {
Expand Down