From aec71d8d3241ce9663dda138c8f438f2730b44d9 Mon Sep 17 00:00:00 2001 From: lda Date: Wed, 15 May 2024 09:02:09 +0200 Subject: [PATCH] [ADD/UNTESTED] Finalise step 5.2 of auth rules. Next up is the invite membership(5.3). --- src/Room.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Room.c b/src/Room.c index 9ad15e0..34f1161 100644 --- a/src/Room.c +++ b/src/Room.c @@ -220,6 +220,26 @@ RoomStateGetID(Room * room, char *event_id) JsonFree(name); \ } \ return ret +static bool +RoomIsJoinRule(Room * room, char *e_id, char *jr) +{ + HashMap *state_point; + HashMap *joinrule = NULL; + char *id_joinrule = NULL; + bool ret = false; + + if (!room || !e_id || !jr) + { + return false; + } + + PrepareState(room, e_id, "m.room.join_rules", "", joinrule); + ret = StrEquals( + JsonValueAsString(JsonGet(joinrule, 1, "join_rule")), + jr); +finish: + FinishState(joinrule); +} /* Verifies if user has a specific membership before [e_id] in the room. */ static bool RoomUserHasMembership(Room * room, char *e_id, char *user, char *mbr) @@ -577,6 +597,14 @@ AuthoriseAliasV1(PduV1 pdu) return true; } static bool +AuthorizeInviteMembershipV1(Room * room, PduV1 pdu) +{ + /* TODO */ + (void) room; + (void) pdu; + return false; +} +static bool AuthorizeJoinMembershipV1(Room * room, PduV1 pdu) { /* Step 5.2.1: If the only previous event is an m.room.create and the @@ -609,10 +637,24 @@ AuthorizeJoinMembershipV1(Room * room, PduV1 pdu) return false; } /* Step 5.2.3: If the sender is banned, reject. */ - /* TODO */ + if (RoomUserHasMembership(room, pdu.event_id, pdu.sender, "ban")) + { + return false; + } /* Step 5.2.4: If the join_rule is invite then allow if membership * state is invite or join. */ + if (RoomIsJoinRule(room, pdu.event_id, "invite") && + (RoomUserHasMembership(room, pdu.event_id, pdu.sender, "invite") || + RoomUserHasMembership(room, pdu.event_id, pdu.sender, "join"))) + { + return true; + } + /* Step 5.2.5: If the join_rule is public, allow. */ + if (RoomIsJoinRule(room, pdu.event_id, "public")) + { + return true; + } /* Step 5.2.6: Otherwise, reject. */ return false; } @@ -644,9 +686,12 @@ AuthoriseMemberV1(Room * room, PduV1 pdu) /* Step 5.2: If membership is join. */ JumpIfMembership("join", AuthorizeJoinMembershipV1); + + /* Step 5.3: If membership is invite. */ + JumpIfMembership("invite", AuthorizeInviteMembershipV1); - /* Step 4.3: Otherwise, allow. */ - return true; + /* Step 5.6: Otherwise, the membership is unknown. Reject. */ + return false; #undef JumpIfMembership } static bool