Skip to content

Commit d3f6741

Browse files
committed
temp fix - exclude chat message tlvs that break macos client
1 parent 07ed269 commit d3f6741

2 files changed

Lines changed: 86 additions & 9 deletions

File tree

foodgroup/chat.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (s ChatService) ChannelMsgToHost(ctx context.Context, sess *state.Session,
8585
Body: bodyOut,
8686
})
8787
} else {
88-
// forward message all participants, except sender
88+
// forward message all participants, except sender
8989
s.chatMessageRelayer.RelayToAllExcept(ctx, sess.ChatRoomCookie(), sess.IdentScreenName(), wire.SNACMessage{
9090
Frame: frameOut,
9191
Body: bodyOut,
@@ -116,7 +116,14 @@ func (s ChatService) transformChatMessage(inBody wire.SNAC_0x0E_0x05_ChatChannel
116116
if !hasMessage {
117117
return wire.TLVRestBlock{}, errors.New("SNAC(0x0E,0x05) does not contain a message TLV")
118118
}
119-
messageText, err := textFromChatMsgBlob(messageBlob)
119+
msgBlock := wire.TLVRestBlock{}
120+
if err := wire.UnmarshalBE(&msgBlock, bytes.NewBuffer(messageBlob)); err != nil {
121+
return wire.TLVRestBlock{}, err
122+
}
123+
124+
msgBlock = removeUnsupportedTLVs(msgBlock)
125+
126+
messageText, err := textFromChatMsgBlob(msgBlock)
120127
if err != nil {
121128
return wire.TLVRestBlock{}, err
122129
}
@@ -141,7 +148,18 @@ func (s ChatService) transformChatMessage(inBody wire.SNAC_0x0E_0x05_ChatChannel
141148
}
142149

143150
// return the incoming payload without modification
144-
return newMessageBlock(sender, messageBlob), nil
151+
return newMessageBlock(sender, msgBlock), nil
152+
}
153+
154+
// remove TLVs that break the macos 2.x chat
155+
func removeUnsupportedTLVs(block wire.TLVRestBlock) wire.TLVRestBlock {
156+
newBlock := wire.TLVRestBlock{}
157+
for _, tlv := range block.TLVList {
158+
if tlv.Tag < 4 {
159+
newBlock.TLVList = append(newBlock.TLVList, tlv)
160+
}
161+
}
162+
return newBlock
145163
}
146164

147165
// rollDice generates a chat response for the results of a die roll.
@@ -163,13 +181,9 @@ func (s ChatService) rollDice(sess *state.Session, dice int, sides int) wire.TLV
163181

164182
// textFromChatMsgBlob extracts plaintext message text from HTML located in
165183
// chat message info TLV(0x05).
166-
func textFromChatMsgBlob(msg []byte) ([]byte, error) {
167-
block := wire.TLVRestBlock{}
168-
if err := wire.UnmarshalBE(&block, bytes.NewBuffer(msg)); err != nil {
169-
return nil, err
170-
}
184+
func textFromChatMsgBlob(msg wire.TLVRestBlock) ([]byte, error) {
171185

172-
b, hasMsg := block.Bytes(wire.ChatTLVMessageInfoText)
186+
b, hasMsg := msg.Bytes(wire.ChatTLVMessageInfoText)
173187
if !hasMsg {
174188
return nil, errors.New("SNAC(0x0E,0x05) has no chat msg text TLV")
175189
}

foodgroup/chat_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,69 @@ func TestChatService_ChannelMsgToHost(t *testing.T) {
402402
},
403403
},
404404
},
405+
{
406+
name: "send chat room message, expect that TLVs that crash macos client",
407+
userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime,
408+
sessOptChatRoomCookie("the-chat-cookie")),
409+
inputSNAC: wire.SNACMessage{
410+
Frame: wire.SNACFrame{
411+
RequestID: 1234,
412+
},
413+
Body: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
414+
Cookie: 1234,
415+
Channel: 14,
416+
TLVRestBlock: wire.TLVRestBlock{
417+
TLVList: wire.TLVList{
418+
{
419+
Tag: wire.ChatTLVPublicWhisperFlag,
420+
Value: []byte{},
421+
},
422+
wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
423+
TLVList: wire.TLVList{
424+
wire.NewTLVBE(wire.ChatTLVMessageInfoText,
425+
"<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
426+
wire.NewTLVBE(0x04, "remove me"),
427+
wire.NewTLVBE(0x05, "remove me"),
428+
},
429+
}),
430+
},
431+
},
432+
},
433+
},
434+
mockParams: mockParams{
435+
chatMessageRelayerParams: chatMessageRelayerParams{
436+
chatRelayToAllExceptParams: chatRelayToAllExceptParams{
437+
{
438+
screenName: state.NewIdentScreenName("user_sending_chat_msg"),
439+
cookie: "the-chat-cookie",
440+
message: wire.SNACMessage{
441+
Frame: wire.SNACFrame{
442+
FoodGroup: wire.Chat,
443+
SubGroup: wire.ChatChannelMsgToClient,
444+
},
445+
Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
446+
Cookie: 1234,
447+
Channel: 14,
448+
TLVRestBlock: wire.TLVRestBlock{
449+
TLVList: wire.TLVList{
450+
wire.NewTLVBE(wire.ChatTLVSenderInformation,
451+
newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
452+
wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
453+
wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
454+
TLVList: wire.TLVList{
455+
wire.NewTLVBE(wire.ChatTLVMessageInfoText,
456+
"<HTML><BODY BGCOLOR=\"#ffffff\"><FONT LANG=\"0\">Hello</FONT></BODY></HTML>"),
457+
},
458+
}),
459+
},
460+
},
461+
},
462+
},
463+
},
464+
},
465+
},
466+
},
467+
},
405468
}
406469

407470
for _, tc := range cases {

0 commit comments

Comments
 (0)