0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 18:22:50 +01:00

ircd:Ⓜ️ Add more event::conforms checks; add skip-checks constructor.

This commit is contained in:
Jason Volk 2018-02-21 14:24:34 -08:00
parent 268861013e
commit 4f4b53416c
2 changed files with 88 additions and 20 deletions

View file

@ -148,6 +148,7 @@ struct ircd::m::event::conforms
conforms() = default;
conforms(const event &);
conforms(const event &, const uint64_t &skip);
friend string_view reflect(const code &);
friend std::ostream &operator<<(std::ostream &, const conforms &);
@ -156,13 +157,23 @@ struct ircd::m::event::conforms
enum ircd::m::event::conforms::code
:uint
{
INVALID_EVENT_ID,
INVALID_ROOM_ID,
INVALID_SENDER_ID,
INVALID_REDACTS_ID,
INVALID_TYPE,
DEPTH_NEGATIVE,
DEPTH_ZERO_NON_CREATE,
INVALID_OR_MISSING_EVENT_ID, ///< event_id empty or failed mxid grammar check
INVALID_OR_MISSING_ROOM_ID, ///< room_id empty or failed mxid grammar check
INVALID_OR_MISSING_SENDER_ID, ///< sender empty or failed mxid grammar check
MISSING_TYPE, ///< type empty
MISSING_ORIGIN, ///< origin empty
INVALID_ORIGIN, ///< origin not a proper domain
INVALID_OR_MISSING_REDACTS_ID, ///< for m.room.redaction
USELESS_REDACTS_ID, ///< redacts present in non-m.room.redaction
MISSING_MEMBERSHIP, ///< for m.room.member, membership empty
INVALID_MEMBERSHIP, ///< for m.room.member (does not check actual states)
USELESS_MEMBERSHIP, ///< membership present in non-m.room.member
MISSING_CONTENT_MEMBERSHIP, ///< for m.room.member, content.membership
INVALID_CONTENT_MEMBERSHIP, ///< for m.room.member, content.membership
MISSING_PREV_EVENTS, ///< for non-m.room.create, empty prev_events
MISSING_PREV_STATE, ///< for state_key'ed, empty prev_state
DEPTH_NEGATIVE, ///< depth < 0
DEPTH_ZERO, ///< for non-m.room.create, depth=0
_NUM_
};

View file

@ -515,12 +515,23 @@ namespace ircd::m
decltype(ircd::m::event_conforms_reflects)
ircd::m::event_conforms_reflects
{
"INVALID_EVENT_ID",
"INVALID_ROOM_ID",
"INVALID_SENDER_ID",
"INVALID_REDACTS_ID",
"INVALID_OR_MISSING_EVENT_ID",
"INVALID_OR_MISSING_ROOM_ID",
"INVALID_OR_MISSING_SENDER_ID",
"MISSING_TYPE",
"MISSING_ORIGIN",
"INVALID_ORIGIN",
"INVALID_OR_MISSING_REDACTS_ID",
"USELESS_REDACTS_ID",
"MISSING_MEMBERSHIP",
"INVALID_MEMBERSHIP",
"USELESS_MEMBERSHIP",
"MISSING_CONTENT_MEMBERSHIP",
"INVALID_CONTENT_MEMBERSHIP",
"MISSING_PREV_EVENTS",
"MISSING_PREV_STATE",
"DEPTH_NEGATIVE",
"DEPTH_ZERO_NON_CREATE",
"DEPTH_ZERO",
};
std::ostream &
@ -542,32 +553,78 @@ catch(const std::out_of_range &e)
return "??????"_sv;
}
ircd::m::event::conforms::conforms(const event &e,
const uint64_t &skip)
:conforms{e}
{
report &= ~skip;
}
ircd::m::event::conforms::conforms(const event &e)
:report{0}
{
if(!valid(m::id::EVENT, json::get<"event_id"_>(e)))
set(INVALID_EVENT_ID);
set(INVALID_OR_MISSING_EVENT_ID);
if(!valid(m::id::ROOM, json::get<"room_id"_>(e)))
set(INVALID_ROOM_ID);
set(INVALID_OR_MISSING_ROOM_ID);
if(!valid(m::id::USER, json::get<"sender"_>(e)))
set(INVALID_SENDER_ID);
set(INVALID_OR_MISSING_SENDER_ID);
if(empty(json::get<"type"_>(e)))
set(MISSING_TYPE);
if(empty(json::get<"origin"_>(e)))
set(MISSING_ORIGIN);
//TODO: XXX
if(false)
set(INVALID_ORIGIN);
if(json::get<"type"_>(e) == "m.room.redaction")
if(!valid(m::id::EVENT, json::get<"redacts"_>(e)))
set(INVALID_REDACTS_ID);
set(INVALID_OR_MISSING_REDACTS_ID);
//TODO: XXX
if(empty(json::get<"type"_>(e)))
set(INVALID_TYPE);
if(json::get<"type"_>(e) != "m.room.redaction")
if(!empty(json::get<"redacts"_>(e)))
set(USELESS_REDACTS_ID);
if(json::get<"type"_>(e) == "m.room.member")
if(empty(json::get<"membership"_>(e)))
set(MISSING_MEMBERSHIP);
if(json::get<"type"_>(e) == "m.room.member")
if(!all_of<std::islower>(json::get<"membership"_>(e)))
set(INVALID_MEMBERSHIP);
if(json::get<"type"_>(e) != "m.room.member")
if(!empty(json::get<"membership"_>(e)))
set(USELESS_MEMBERSHIP);
if(json::get<"type"_>(e) == "m.room.member")
if(empty(unquote(json::get<"content"_>(e).get("membership"))))
set(MISSING_CONTENT_MEMBERSHIP);
if(json::get<"type"_>(e) == "m.room.member")
if(!all_of<std::islower>(unquote(json::get<"content"_>(e).get("membership"))))
set(INVALID_CONTENT_MEMBERSHIP);
if(json::get<"type"_>(e) != "m.room.create")
if(empty(json::get<"prev_events"_>(e)))
set(MISSING_PREV_EVENTS);
if(json::get<"type"_>(e) != "m.room.create")
if(!empty(json::get<"state_key"_>(e)))
if(empty(json::get<"prev_state"_>(e)))
set(MISSING_PREV_STATE);
if(json::get<"depth"_>(e) < 0)
set(DEPTH_NEGATIVE);
if(json::get<"type"_>(e) != "m.room.create")
if(json::get<"depth"_>(e) == 0)
set(DEPTH_ZERO_NON_CREATE);
set(DEPTH_ZERO);
}
void