From 723fd495dde835d078b9f2074b6b62c06dea4575 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 15 Aug 2022 10:19:47 +0100 Subject: [PATCH] Fix parsing of `"events_default"` level in `m.room.power_levels` events --- eventauth_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++ eventcontent.go | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/eventauth_test.go b/eventauth_test.go index baf8d772..63532d3f 100644 --- a/eventauth_test.go +++ b/eventauth_test.go @@ -1137,3 +1137,62 @@ func newMemberContent( ThirdPartyInvite: thirdPartyInvite, } } + +var negativePowerLevelTestRoom = &testAuthEvents{ + CreateJSON: json.RawMessage(`{ + "type": "m.room.create", + "state_key": "", + "sender": "@u1:a", + "room_id": "!r1:a", + "event_id": "$e1:a", + "content": { + "room_version": "10" + } + }`), + PowerLevelsJSON: json.RawMessage(`{ + "type": "m.room.power_levels", + "state_key": "", + "sender": "@u1:a", + "room_id": "!r1:a", + "event_id": "$e3:a", + "content": { + "events_default": -9007199254740990, + "state_default": -9007199254740990, + "users_default": -9007199254740990, + "users": { + } + } + }`), + MemberJSON: map[string]json.RawMessage{ + "@u1:a": json.RawMessage(`{ + "type": "m.room.member", + "state_key": "@u1:a", + "sender": "@u1:a", + "room_id": "!r1:a", + "event_id": "$e2:a", + "content": { + "membership": "join" + } + }`), + }, +} + +func TestNegativePowerLevels(t *testing.T) { + // User should be able to demote the user default level + // below their own effective level. + eventShouldSucceed, err := NewEventFromTrustedJSON(RawJSON(`{ + "type": "m.room.message", + "sender": "@u1:a", + "room_id": "!r1:a", + "event_id": "$e5:a", + "content": { + "stuff": "i am a message event" + } + }`), false, RoomVersionV1) + if err != nil { + t.Fatal(err) + } + if err = Allowed(eventShouldSucceed, negativePowerLevelTestRoom); err != nil { + t.Error("TestNegativePowerLevels should have succeeded but it didn't:", err) + } +} diff --git a/eventcontent.go b/eventcontent.go index 5f9ba635..2fba9994 100644 --- a/eventcontent.go +++ b/eventcontent.go @@ -431,7 +431,7 @@ func NewPowerLevelContentFromEvent(event *Event) (c PowerLevelContent, err error UsersDefaultLevel levelJSONValue `json:"users_default"` EventLevels map[string]levelJSONValue `json:"events"` StateDefaultLevel levelJSONValue `json:"state_default"` - EventDefaultLevel levelJSONValue `json:"event_default"` + EventDefaultLevel levelJSONValue `json:"events_default"` NotificationLevels map[string]levelJSONValue `json:"notifications"` } if err = json.Unmarshal(event.Content(), &content); err != nil {