Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions pkg/connector/handle_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/highesttt/matrix-line-messenger/pkg/line"
)

const lineDecryptFallbackText = "[Unable to decrypt message. Open an issue on GitHub.]"

func (lc *LineClient) newMessageHandler() *handlers.Handler {
return &handlers.Handler{
Log: lc.UserLogin.Bridge.Log,
Expand Down Expand Up @@ -208,8 +210,8 @@ func (lc *LineClient) parseMessageTimestamp(msg *line.Message) time.Time {
func (lc *LineClient) decryptMessageBody(msg *line.Message, portalIDStr string, opType int) (bodyText, unwrappedText string) {
// Handle Content
bodyText = msg.Text
if bodyText == "" && len(msg.Chunks) > 0 {
bodyText = "[Unable to decrypt message. Open an issue on GitHub.]"
if len(msg.Chunks) > 0 && (bodyText == "" || isLineDecryptFallbackText(bodyText)) {
Comment thread
indent-zero[bot] marked this conversation as resolved.
bodyText = ""
Comment thread
indent-zero[bot] marked this conversation as resolved.
if lc.E2EE != nil {
// Ensure peer keys are available before attempting decryption
lc.ensurePeerKeyForMessage(context.Background(), msg)
Expand Down Expand Up @@ -304,6 +306,10 @@ func (lc *LineClient) decryptMessageBody(msg *line.Message, portalIDStr string,
return bodyText, unwrappedText
}

func isLineDecryptFallbackText(text string) bool {
return strings.TrimSpace(text) == lineDecryptFallbackText
}

// convertLineMessage converts an inbound LINE message into a Matrix
// ConvertedMessage. bodyText/unwrappedText are the (decrypted) message text as
// returned by decryptMessageBody. Shared by the live message path and backfill.
Expand Down
42 changes: 42 additions & 0 deletions pkg/connector/handle_message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package connector

import (
"testing"

"github.com/highesttt/matrix-line-messenger/pkg/line"
)

func TestDecryptMessageBodySkipsGeneratedFallbackWhenDecryptUnavailable(t *testing.T) {
msg := &line.Message{
Text: "",
Chunks: []string{"encrypted"},
}

bodyText, unwrappedText := (&LineClient{}).decryptMessageBody(msg, "chat-mid", int(OpReceiveMessage))
Comment thread
indent-zero[bot] marked this conversation as resolved.
Outdated
if bodyText != "" || unwrappedText != "" {
t.Fatalf("decryptMessageBody returned body=%q unwrapped=%q, want empty strings", bodyText, unwrappedText)
}
}

func TestDecryptMessageBodyTreatsLineFallbackAsEncryptedFailure(t *testing.T) {
msg := &line.Message{
Text: lineDecryptFallbackText,
Chunks: []string{"encrypted"},
}

bodyText, unwrappedText := (&LineClient{}).decryptMessageBody(msg, "chat-mid", int(OpReceiveMessage))
if bodyText != "" || unwrappedText != "" {
t.Fatalf("decryptMessageBody returned body=%q unwrapped=%q, want empty strings", bodyText, unwrappedText)
}
}

func TestDecryptMessageBodyKeepsFallbackTextWithoutEncryptedChunks(t *testing.T) {
msg := &line.Message{
Text: lineDecryptFallbackText,
}

bodyText, unwrappedText := (&LineClient{}).decryptMessageBody(msg, "chat-mid", int(OpReceiveMessage))
if bodyText != lineDecryptFallbackText || unwrappedText != lineDecryptFallbackText {
t.Fatalf("decryptMessageBody returned body=%q unwrapped=%q, want fallback text", bodyText, unwrappedText)
}
}
Loading