Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix username spoofing #1875

Merged
merged 2 commits into from
Sep 5, 2022
Merged
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
2 changes: 1 addition & 1 deletion bridge/matrix/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func interface2Struct(in interface{}, out interface{}) error {
return json.Unmarshal(jsonObj, out)
}

// getDisplayName retrieves the displayName for mxid, querying the homserver if the mxid is not in the cache.
// getDisplayName retrieves the displayName for mxid, querying the homeserver if the mxid is not in the cache.
func (b *Bmatrix) getDisplayName(mxid string) string {
if b.GetBool("UseUserName") {
return mxid[1:]
Expand Down
46 changes: 35 additions & 11 deletions bridge/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,37 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {

username := newMatrixUsername(msg.Username)

body := username.plain + msg.Text
formattedBody := username.formatted + helper.ParseMarkdown(msg.Text)

if b.GetBool("SpoofUsername") {
// https://spec.matrix.org/v1.3/client-server-api/#mroommember
type stateMember struct {
AvatarURL string `json:"avatar_url,omitempty"`
DisplayName string `json:"displayname"`
Membership string `json:"membership"`
}

// TODO: reset username afterwards with DisplayName: null ?
m := stateMember{
AvatarURL: "",
DisplayName: username.plain,
Membership: "join",
}

_, err := b.mc.SendStateEvent(channel, "m.room.member", b.UserID, m)
if err == nil {
body = msg.Text
formattedBody = helper.ParseMarkdown(msg.Text)
}
}

// Make a action /me of the message
if msg.Event == config.EventUserAction {
m := matrix.TextMessage{
MsgType: "m.emote",
Body: username.plain + msg.Text,
FormattedBody: username.formatted + helper.ParseMarkdown(msg.Text),
Body: body,
FormattedBody: formattedBody,
Format: "org.matrix.custom.html",
}

Expand Down Expand Up @@ -224,10 +249,10 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
if msg.ID != "" {
rmsg := EditedMessage{
TextMessage: matrix.TextMessage{
Body: username.plain + msg.Text,
Body: body,
MsgType: "m.text",
Format: "org.matrix.custom.html",
FormattedBody: username.formatted + helper.ParseMarkdown(msg.Text),
FormattedBody: formattedBody,
},
}

Expand Down Expand Up @@ -266,8 +291,8 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
if msg.Event == config.EventJoinLeave {
m := matrix.TextMessage{
MsgType: "m.notice",
Body: username.plain + msg.Text,
FormattedBody: username.formatted + msg.Text,
Body: body,
FormattedBody: formattedBody,
Format: "org.matrix.custom.html",
}

Expand Down Expand Up @@ -297,8 +322,8 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
m := ReplyMessage{
TextMessage: matrix.TextMessage{
MsgType: "m.text",
Body: username.plain + msg.Text,
FormattedBody: username.formatted + helper.ParseMarkdown(msg.Text),
Body: body,
FormattedBody: formattedBody,
Format: "org.matrix.custom.html",
},
}
Expand Down Expand Up @@ -338,7 +363,7 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
)

err = b.retry(func() error {
resp, err = b.mc.SendText(channel, username.plain+msg.Text)
resp, err = b.mc.SendText(channel, body)

return err
})
Expand All @@ -356,8 +381,7 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
)

err = b.retry(func() error {
resp, err = b.mc.SendFormattedText(channel, username.plain+msg.Text,
username.formatted+helper.ParseMarkdown(msg.Text))
resp, err = b.mc.SendFormattedText(channel, body, formattedBody)

return err
})
Expand Down
5 changes: 5 additions & 0 deletions matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,11 @@ RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
#OPTIONAL (default false)
ShowJoinPart=false

#Rename the bot in the current room to the username of the message
#This will make an additional API request per message and will probably count towards rate limits
#OPTIONAL (default false)
SpoofUsername=false

#StripNick only allows alphanumerical nicks. See https://github.com/42wim/matterbridge/issues/285
#It will strip other characters from the nick
#OPTIONAL (default false)
Expand Down