diff --git a/acceptreject.py b/acceptreject.py index bd54600c3..f3fb95619 100644 --- a/acceptreject.py +++ b/acceptreject.py @@ -19,6 +19,7 @@ from utils import hasObjectDict from utils import acctDir from utils import hasGroupType from utils import localActorUrl +from utils import hasActor def _createAcceptReject(baseDir: str, federationList: [], @@ -191,9 +192,7 @@ def receiveAcceptReject(session, baseDir: str, """ if messageJson['type'] != 'Accept' and messageJson['type'] != 'Reject': return False - if not messageJson.get('actor'): - if debug: - print('DEBUG: ' + messageJson['type'] + ' has no actor') + if not hasActor(messageJson, debug): return False if not hasUsersPath(messageJson['actor']): if debug: diff --git a/announce.py b/announce.py index 603fd8af1..8363f2e9e 100644 --- a/announce.py +++ b/announce.py @@ -24,6 +24,7 @@ from utils import undoAnnounceCollectionEntry from utils import updateAnnounceCollection from utils import localActorUrl from utils import replaceUsersWithAt +from utils import hasActor from posts import sendSignedJson from posts import getPersonBox from session import postJson @@ -54,7 +55,7 @@ def outboxAnnounce(recentPostsCache: {}, """ Adds or removes announce entries from the shares collection within a given post """ - if not messageJson.get('actor'): + if not hasActor(messageJson, debug): return False if not isinstance(messageJson['actor'], str): return False diff --git a/availability.py b/availability.py index ee6c2d5d6..be0635477 100644 --- a/availability.py +++ b/availability.py @@ -19,6 +19,7 @@ from utils import loadJson from utils import saveJson from utils import acctDir from utils import localActorUrl +from utils import hasActor def setAvailability(baseDir: str, nickname: str, domain: str, @@ -60,7 +61,7 @@ def outboxAvailability(baseDir: str, nickname: str, messageJson: {}, return False if not messageJson['type'] == 'Availability': return False - if not messageJson.get('actor'): + if not hasActor(messageJson, debug): return False if not messageJson.get('object'): return False diff --git a/blocking.py b/blocking.py index ddfb688c6..ad739fe18 100644 --- a/blocking.py +++ b/blocking.py @@ -29,6 +29,7 @@ from utils import getDomainFromActor from utils import getNicknameFromActor from utils import acctDir from utils import localActorUrl +from utils import hasActor from conversation import muteConversation from conversation import unmuteConversation @@ -699,7 +700,7 @@ def outboxMute(baseDir: str, httpPrefix: str, """ if not messageJson.get('type'): return - if not messageJson.get('actor'): + if not hasActor(messageJson, debug): return domainFull = getFullDomain(domain, port) if not messageJson['actor'].endswith(domainFull + '/users/' + nickname): @@ -754,7 +755,7 @@ def outboxUndoMute(baseDir: str, httpPrefix: str, """ if not messageJson.get('type'): return - if not messageJson.get('actor'): + if not hasActor(messageJson, debug): return domainFull = getFullDomain(domain, port) if not messageJson['actor'].endswith(domainFull + '/users/' + nickname): diff --git a/bookmarks.py b/bookmarks.py index fb0c3f769..f1a04f45c 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -26,6 +26,7 @@ from utils import saveJson from utils import hasObjectDict from utils import acctDir from utils import localActorUrl +from utils import hasActor from posts import getPersonBox from session import postJson @@ -539,9 +540,7 @@ def outboxBookmark(recentPostsCache: {}, return if messageJson['type'] != 'Add': return - if not messageJson.get('actor'): - if debug: - print('DEBUG: no actor in bookmark Add') + if not hasActor(messageJson, debug): return if not hasObjectDict(messageJson): if debug: @@ -603,9 +602,7 @@ def outboxUndoBookmark(recentPostsCache: {}, return if messageJson['type'] != 'Remove': return - if not messageJson.get('actor'): - if debug: - print('DEBUG: no actor in unbookmark Remove') + if not hasActor(messageJson, debug): return if not hasObjectDict(messageJson): if debug: diff --git a/daemon.py b/daemon.py index 22829b5b0..25f6756e8 100644 --- a/daemon.py +++ b/daemon.py @@ -234,6 +234,7 @@ from categories import updateHashtagCategories from languages import getActorLanguages from languages import setActorLanguages from like import updateLikesCollection +from utils import hasActor from utils import setReplyIntervalHours from utils import canReplyTo from utils import isDM @@ -725,24 +726,15 @@ class PubServer(BaseHTTPRequestHandler): else: self.send_header('Cache-Control', 'public') self.send_header('Origin', self.server.domainFull) - self.send_header('X-AP-Instance-ID', self.server.instanceId) - self.send_header('X-Clacks-Overhead', 'GNU Natalie Nguyen') if length > -1: self.send_header('Content-Length', str(length)) if callingDomain: self.send_header('Host', callingDomain) if permissive: self.send_header('Access-Control-Allow-Origin', '*') - if 'image/' in fileFormat or \ - 'audio/' in fileFormat or \ - 'video/' in fileFormat: - acStr = \ - 'Host, Server, x-goog-meta-frames, Content-Length, ' + \ - 'Content-Type, Range, X-Requested-With, ' + \ - 'If-Modified-Since, If-None-Match' - self.send_header('Access-Control-Allow-Headers', acStr) - self.send_header('Access-Control-Expose-Headers', acStr) return + self.send_header('X-AP-Instance-ID', self.server.instanceId) + self.send_header('X-Clacks-Overhead', 'GNU Natalie Nguyen') if cookie: cookieStr = cookie if 'HttpOnly;' not in cookieStr: @@ -1278,7 +1270,7 @@ class PubServer(BaseHTTPRequestHandler): # check for blocked domains so that they can be rejected early messageDomain = None - if not messageJson.get('actor'): + if not hasActor(messageJson, self.server.debug): print('Message arriving at inbox queue has no actor') self._400() self.server.POSTbusy = False diff --git a/follow.py b/follow.py index b0aa8f3c5..b8f84729b 100644 --- a/follow.py +++ b/follow.py @@ -9,6 +9,7 @@ __module_group__ = "ActivityPub" from pprint import pprint import os +from utils import hasActor from utils import removeDomainPort from utils import hasObjectDict from utils import hasUsersPath @@ -648,9 +649,7 @@ def receiveFollowRequest(session, baseDir: str, httpPrefix: str, if not messageJson['type'].startswith('Join'): return False print('Receiving follow request') - if not messageJson.get('actor'): - if debug: - print('DEBUG: follow request has no actor') + if not hasActor(messageJson, debug): return False if not hasUsersPath(messageJson['actor']): if debug: diff --git a/inbox.py b/inbox.py index 5b16fe16b..ab7542d9e 100644 --- a/inbox.py +++ b/inbox.py @@ -73,6 +73,7 @@ from utils import undoAnnounceCollectionEntry from utils import dangerousMarkup from utils import isDM from utils import isReply +from utils import hasActor from httpsig import messageContentDigest from posts import savePostToBox from posts import isCreateInsideAnnounce @@ -307,7 +308,7 @@ def inboxPermittedMessage(domain: str, messageJson: {}, federationList: []) -> bool: """ check that we are receiving from a permitted domain """ - if not messageJson.get('actor'): + if not hasActor(messageJson, False): return False actor = messageJson['actor'] @@ -685,9 +686,7 @@ def _receiveUndo(session, baseDir: str, httpPrefix: str, return False if debug: print('DEBUG: Undo activity received') - if not messageJson.get('actor'): - if debug: - print('DEBUG: follow request has no actor') + if not hasActor(messageJson, debug): return False if not hasUsersPath(messageJson['actor']): if debug: @@ -807,7 +806,7 @@ def _receiveUpdateToQuestion(recentPostsCache: {}, messageJson: {}, # message url of the question if not messageJson.get('id'): return - if not messageJson.get('actor'): + if not hasActor(messageJson, False): return messageId = removeIdEnding(messageJson['id']) if '#' in messageId: @@ -849,9 +848,7 @@ def _receiveUpdate(recentPostsCache: {}, session, baseDir: str, """ if messageJson['type'] != 'Update': return False - if not messageJson.get('actor'): - if debug: - print('DEBUG: ' + messageJson['type'] + ' has no actor') + if not hasActor(messageJson, debug): return False if not hasObjectDict(messageJson): if debug: @@ -925,9 +922,7 @@ def _receiveLike(recentPostsCache: {}, """ if messageJson['type'] != 'Like': return False - if not messageJson.get('actor'): - if debug: - print('DEBUG: ' + messageJson['type'] + ' has no actor') + if not hasActor(messageJson, debug): return False if not messageJson.get('object'): if debug: @@ -1034,7 +1029,7 @@ def _receiveUndoLike(recentPostsCache: {}, """ if messageJson['type'] != 'Undo': return False - if not messageJson.get('actor'): + if not hasActor(messageJson, debug): return False if not hasObjectDict(messageJson): return False @@ -1136,9 +1131,7 @@ def _receiveBookmark(recentPostsCache: {}, return False if messageJson['type'] != 'Add': return False - if not messageJson.get('actor'): - if debug: - print('DEBUG: no actor in inbox bookmark Add') + if not hasActor(messageJson, debug): return False if not hasObjectDict(messageJson): if debug: @@ -1252,9 +1245,7 @@ def _receiveUndoBookmark(recentPostsCache: {}, return False if messageJson['type'] != 'Remove': return False - if not messageJson.get('actor'): - if debug: - print('DEBUG: no actor in inbox undo bookmark Remove') + if not hasActor(messageJson, debug): return False if not hasObjectDict(messageJson): if debug: @@ -1359,9 +1350,7 @@ def _receiveDelete(session, handle: str, isGroup: bool, baseDir: str, """ if messageJson['type'] != 'Delete': return False - if not messageJson.get('actor'): - if debug: - print('DEBUG: ' + messageJson['type'] + ' has no actor') + if not hasActor(messageJson, debug): return False if debug: print('DEBUG: Delete activity arrived') @@ -1456,9 +1445,7 @@ def _receiveAnnounce(recentPostsCache: {}, if debug: print('DEBUG: bad handle ' + handle) return False - if not messageJson.get('actor'): - if debug: - print('DEBUG: ' + messageJson['type'] + ' has no actor') + if not hasActor(messageJson, debug): return False if debug: print('DEBUG: receiving announce on ' + handle) @@ -1669,7 +1656,7 @@ def _receiveUndoAnnounce(recentPostsCache: {}, """ if messageJson['type'] != 'Undo': return False - if not messageJson.get('actor'): + if not hasActor(messageJson, debug): return False if not hasObjectDict(messageJson): return False diff --git a/outbox.py b/outbox.py index 2a596ecee..61b825529 100644 --- a/outbox.py +++ b/outbox.py @@ -28,6 +28,7 @@ from utils import loadJson from utils import saveJson from utils import acctDir from utils import localActorUrl +from utils import hasActor from blocking import isBlockedDomain from blocking import outboxBlock from blocking import outboxUndoBlock @@ -92,9 +93,7 @@ def _outboxPersonReceiveUpdate(recentPostsCache: {}, if debug: print('DEBUG: c2s actor update has no "to" field') return - if not messageJson.get('actor'): - if debug: - print('DEBUG: c2s actor update has no actor field') + if not hasActor(messageJson, debug): return if not messageJson.get('id'): if debug: diff --git a/skills.py b/skills.py index fd2061807..55935f560 100644 --- a/skills.py +++ b/skills.py @@ -20,6 +20,7 @@ from utils import getOccupationSkills from utils import setOccupationSkillsList from utils import acctDir from utils import localActorUrl +from utils import hasActor def setSkillsFromDict(actorJson: {}, skillsDict: {}) -> []: @@ -150,7 +151,7 @@ def outboxSkills(baseDir: str, nickname: str, messageJson: {}, return False if not messageJson['type'] == 'Skill': return False - if not messageJson.get('actor'): + if not hasActor(messageJson, debug): return False if not messageJson.get('object'): return False diff --git a/tests.py b/tests.py index 0afae44c2..47a133ebf 100644 --- a/tests.py +++ b/tests.py @@ -1299,7 +1299,7 @@ def testPostMessageBetweenServers(baseDir: str) -> None: assert attached.get('type') assert attached.get('url') assert attached['mediaType'] == 'image/png' - if not '/system/media_attachments/files/' in attached['url']: + if '/system/media_attachments/files/' not in attached['url']: print(attached['url']) assert '/system/media_attachments/files/' in attached['url'] assert attached['url'].endswith('.png') diff --git a/utils.py b/utils.py index 6411f78e8..48387f510 100644 --- a/utils.py +++ b/utils.py @@ -3020,3 +3020,17 @@ def replaceUsersWithAt(actor: str) -> str: actor = actor.replace(path, '/@') break return actor + + +def hasActor(postJsonObject: {}, debug: bool) -> bool: + """Does the given post have an actor? + """ + if postJsonObject.get('actor'): + return True + if debug: + if postJsonObject.get('type'): + msg = postJsonObject['type'] + ' has missing actor' + if postJsonObject.get('id'): + msg += ' ' + postJsonObject['id'] + print(msg) + return False