def check_redaction(self, event, auth_events): """Check whether the event sender is allowed to redact the target event. Returns: True if the the sender is allowed to redact the target event if the target event was created by them. False if the sender is allowed to redact the target event with no further checks. Raises: AuthError if the event sender is definitely not allowed to redact the target event. """ user_level = self._get_user_power_level(event.user_id, auth_events) redact_level = self._get_named_level(auth_events, "redact", 50) if user_level >= redact_level: return False redacter_domain = EventID.from_string(event.event_id).domain redactee_domain = EventID.from_string(event.redacts).domain if redacter_domain == redactee_domain: return True raise AuthError( 403, "You don't have permission to redact events" )
def validate_new(self, event): """Validates the event has roughly the right format Args: event (FrozenEvent) """ self.validate_builder(event) if event.format_version == EventFormatVersions.V1: EventID.from_string(event.event_id) required = [ "auth_events", "content", "hashes", "origin", "prev_events", "sender", "type", ] for k in required: if not hasattr(event, k): raise SynapseError(400, "Event does not have key %s" % (k,)) # Check that the following keys have string values event_strings = [ "origin", ] for s in event_strings: if not isinstance(getattr(event, s), string_types): raise SynapseError(400, "'%s' not a string type" % (s,)) if event.type == EventTypes.Aliases: if "aliases" in event.content: for alias in event.content["aliases"]: if len(alias) > MAX_ALIAS_LENGTH: raise SynapseError( 400, ("Can't create aliases longer than" " %d characters" % (MAX_ALIAS_LENGTH,)), Codes.INVALID_PARAM, )
def validate(self, event): EventID.from_string(event.event_id) RoomID.from_string(event.room_id) required = [ # "auth_events", "content", # "hashes", "origin", # "prev_events", "sender", "type", ] for k in required: if not hasattr(event, k): raise SynapseError(400, "Event does not have key %s" % (k,)) # Check that the following keys have string values strings = [ "origin", "sender", "type", ] if hasattr(event, "state_key"): strings.append("state_key") for s in strings: if not isinstance(getattr(event, s), basestring): raise SynapseError(400, "Not '%s' a string type" % (s,)) if event.type == EventTypes.Member: if "membership" not in event.content: raise SynapseError(400, "Content has not membership key") if event.content["membership"] not in Membership.LIST: raise SynapseError(400, "Invalid membership key")
def validate(self, event): EventID.from_string(event.event_id) RoomID.from_string(event.room_id) required = [ # "auth_events", "content", # "hashes", "origin", # "prev_events", "sender", "type", ] for k in required: if not hasattr(event, k): raise SynapseError(400, "Event does not have key %s" % (k, )) # Check that the following keys have string values strings = [ "origin", "sender", "type", ] if hasattr(event, "state_key"): strings.append("state_key") for s in strings: if not isinstance(getattr(event, s), string_types): raise SynapseError(400, "Not '%s' a string type" % (s, )) if event.type == EventTypes.Member: if "membership" not in event.content: raise SynapseError(400, "Content has not membership key") if event.content["membership"] not in Membership.LIST: raise SynapseError(400, "Invalid membership key")
def validate_new(self, event: EventBase, config: HomeServerConfig) -> None: """Validates the event has roughly the right format Suitable for checking a locally-created event. It has stricter checks than is appropriate for an event received over federation (for which, see event_auth.validate_event_for_room_version) Args: event: The event to validate. config: The homeserver's configuration. """ self.validate_builder(event) if event.format_version == EventFormatVersions.V1: EventID.from_string(event.event_id) required = [ "auth_events", "content", "hashes", "origin", "prev_events", "sender", "type", ] for k in required: if k not in event: raise SynapseError(400, "Event does not have key %s" % (k, )) # Check that the following keys have string values event_strings = ["origin"] for s in event_strings: if not isinstance(getattr(event, s), str): raise SynapseError(400, "'%s' not a string type" % (s, )) # Depending on the room version, ensure the data is spec compliant JSON. if event.room_version.strict_canonicaljson: # Note that only the client controlled portion of the event is # checked, since we trust the portions of the event we created. validate_canonicaljson(event.content) if event.type == EventTypes.Aliases: if "aliases" in event.content: for alias in event.content["aliases"]: if len(alias) > MAX_ALIAS_LENGTH: raise SynapseError( 400, ("Can't create aliases longer than" " %d characters" % (MAX_ALIAS_LENGTH, )), Codes.INVALID_PARAM, ) if event.type == EventTypes.Retention: self._validate_retention(event) if event.type == EventTypes.ServerACL: if not server_matches_acl_event(config.server.server_name, event): raise SynapseError( 400, "Can't create an ACL event that denies the local server") if event.type == EventTypes.PowerLevels: try: jsonschema.validate( instance=event.content, schema=POWER_LEVELS_SCHEMA, cls=plValidator, ) except jsonschema.ValidationError as e: if e.path: # example: "users_default": '0' is not of type 'integer' # cast safety: path entries can be integers, if we fail to validate # items in an array. However the POWER_LEVELS_SCHEMA doesn't expect # to see any arrays. message = ( '"' + cast(str, e.path[-1]) + '": ' + e.message # noqa: B306 ) # jsonschema.ValidationError.message is a valid attribute else: # example: '0' is not of type 'integer' message = e.message # noqa: B306 # jsonschema.ValidationError.message is a valid attribute raise SynapseError( code=400, msg=message, errcode=Codes.BAD_JSON, )
def parse_eventid(self, s): """Parse the string given by 's' as a Event ID and return a EventID object.""" return EventID.from_string(s, hs=self)
def parse_eventid(self, s): """Parse the string given by 's' as a Event ID and return a EventID object.""" return EventID.from_string(s)