Fix: don't accept unencrypted messages

This commit is contained in:
robert
2026-02-26 17:09:01 +00:00
parent 85929ced9b
commit dc92793db9
2 changed files with 56 additions and 42 deletions

View File

@@ -185,53 +185,65 @@ func (c *Client) read_loop(ctx context.Context, pending []string) {
}
func (c *Client) handle_incoming_text(s string) {
if len(s) > 0 && s[0] == '/' {
// Ignore unknown commands after handshake for now.
return
}
if len(s) > 0 && s[0] == '/' {
return
}
var in incoming_frame
if err := json.Unmarshal([]byte(s), &in); err != nil {
return
}
var in incoming_frame
if err := json.Unmarshal([]byte(s), &in); err != nil {
return
}
c.mu.Lock()
if !c.ready {
c.mu.Unlock()
return
}
c.mu.Lock()
if !c.ready {
c.mu.Unlock()
return
}
shared_key := c.state.SharedKey
session_id_bytes := c.state.SessionIdBytes
c.mu.Unlock()
shared_key := c.state.SharedKey
session_id_bytes := c.state.SessionIdBytes
c.mu.Unlock()
content := in.Content
// Enforce encryption after handshake.
if !in.IsEncrypted {
c.emit(Event{
Kind: EventError,
Err: fmt.Errorf("received unencrypted message after handshake"),
})
return
}
if in.IsEncrypted {
plain, err := DecryptAESGCM(shared_key, session_id_bytes, in.Content, in.Salt, in.SeqNum, in.IsResponse)
if err != nil {
c.emit(Event{Kind: EventError, Err: err})
return
}
content = string(plain)
}
plain, err := DecryptAESGCM(
shared_key,
session_id_bytes,
in.Content,
in.Salt,
in.SeqNum,
in.IsResponse,
)
if err != nil {
c.emit(Event{Kind: EventError, Err: err})
return
}
c.mu.Lock()
if in.SeqNum > c.state.SeqInMax {
c.state.SeqInMax = in.SeqNum
}
c.mu.Unlock()
content := string(plain)
c.emit(Event{
Kind: EventMessage,
Message: Message{
Content: content,
Metadata: in.Metadata,
SeqNum: in.SeqNum,
IsResponse: in.IsResponse,
CreatedAt: in.CreatedAt,
},
})
c.mu.Lock()
if in.SeqNum > c.state.SeqInMax {
c.state.SeqInMax = in.SeqNum
}
c.mu.Unlock()
c.emit(Event{
Kind: EventMessage,
Message: Message{
Content: content,
Metadata: in.Metadata,
SeqNum: in.SeqNum,
IsResponse: in.IsResponse,
CreatedAt: in.CreatedAt,
},
})
}
func (c *Client) emit(ev Event) {