changeset 471:ed9e12701e0f

backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`: No error is logged either, this case can happen often is roster permission is not granted. fix 390
author Goffi <goffi@goffi.org>
date Thu, 21 Oct 2021 17:31:01 +0200
parents a549c8e17827
children d993e8b0fd60
files sat_pubsub/backend.py sat_pubsub/error.py sat_pubsub/privilege.py
diffstat 3 files changed, 18 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/sat_pubsub/backend.py	Thu Oct 21 17:24:25 2021 +0200
+++ b/sat_pubsub/backend.py	Thu Oct 21 17:31:01 2021 +0200
@@ -1090,6 +1090,8 @@
 
         try:
             roster = await self.privilege.getRoster(owner_jid)
+        except error.NotAllowedError:
+            return
         except Exception as e:
             log.msg("Error while getting roster of {owner_jid}: {msg}".format(
                 owner_jid = owner_jid.full(),
--- a/sat_pubsub/error.py	Thu Oct 21 17:24:25 2021 +0200
+++ b/sat_pubsub/error.py	Thu Oct 21 17:31:01 2021 +0200
@@ -148,7 +148,6 @@
     This node does not support publishing.
     """
 
-
 class BadAccessTypeError(Error):
     pass
 
@@ -157,3 +156,6 @@
     """
     A requirement is not fulfilled
     """
+
+class NotAllowedError(Error):
+    """A permission necessary to do a privileged action is not given"""
--- a/sat_pubsub/privilege.py	Thu Oct 21 17:24:25 2021 +0200
+++ b/sat_pubsub/privilege.py	Thu Oct 21 17:31:01 2021 +0200
@@ -19,19 +19,21 @@
 "This module implements XEP-0356 (Privileged Entity) to manage rosters, messages and "
 "presences"
 
+from typing import Dict, List, Optional, Set
 import time
-from typing import Optional, Dict, List, Set
-from datetime import datetime, timezone
+
+from twisted.internet import defer
+from twisted.python import log
+from twisted.python import failure
+from twisted.words.protocols.jabber import error, jid
+from twisted.words.xish import domish
 from wokkel import xmppim
-from wokkel.compat import IQ
 from wokkel import pubsub
 from wokkel import disco
+from wokkel.compat import IQ
 from wokkel.iwokkel import IPubSubService
-from twisted.python import log
-from twisted.python import failure
-from twisted.internet import defer
-from twisted.words.xish import domish
-from twisted.words.protocols.jabber import jid, error
+
+from .error import NotAllowedError
 
 FORWARDED_NS = 'urn:xmpp:forward:0'
 PRIV_ENT_NS = 'urn:xmpp:privilege:1'
@@ -61,9 +63,6 @@
 class InvalidStanza(Exception):
     pass
 
-class NotAllowedError(Exception):
-    pass
-
 class PrivilegesHandler(disco.DiscoClientProtocol):
     # FIXME: need to manage updates, XEP-0356 must be updated to get roster pushes
     # TODO: cache
@@ -236,7 +235,7 @@
             roster[item.entity] = item
         return roster
 
-    async def getRoster(self, to_jid: jid.JID) -> Roster:
+    async def getRoster(self, to_jid: jid.JID) -> Optional[Roster]:
         """Retrieve contact list.
 
         @param to_jid: jid of the entity owning the roster
@@ -246,8 +245,7 @@
             # no need to try to get the roster if it's not a user of our own server
             return None
         if self._permissions[PERM_ROSTER] not in ('get', 'both'):
-            log.msg("WARNING: permission not allowed to get roster")
-            raise failure.Failure(NotAllowedError('roster get is not allowed'))
+            raise NotAllowedError('roster get is not allowed')
 
         iq = IQ(self.xmlstream, 'get')
         iq.addElement((ROSTER_NS, 'query'))
@@ -304,7 +302,7 @@
         """
         if self._permissions[PERM_MESSAGE] not in ('outgoing',):
             log.msg("WARNING: permission not allowed to send privileged messages")
-            raise failure.Failure(NotAllowedError('privileged messages are not allowed'))
+            raise NotAllowedError('privileged messages are not allowed')
 
         main_message = domish.Element((None, "message"))
         if to_jid is None: