Skip to content

Commit

Permalink
activitypub.inbox: only mark id seen in memcache after we've enqueued…
Browse files Browse the repository at this point in the history
… the receive task

for #1047 (comment)
  • Loading branch information
snarfed committed Jun 27, 2024
1 parent 866f659 commit 25199f8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
12 changes: 8 additions & 4 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,16 +1010,16 @@ def inbox(protocol=None, id=None):

# are we already processing or done with this activity?
id = activity.get('id')
memcache_key = None
if id:
if util.domain_or_parent_in(util.domain_from_link(id), web_opt_out_domains()):
logger.info(f'Discarding, {id} is on an opted out domain')
return '', 204

key = f'AP-id-{id}'
if memcache.get(key):
memcache_key = f'AP-id-{id}'
if memcache.get(memcache_key):
logger.info(f'Already seen this activity {id}')
return '', 204
memcache.set(key, 'seen', expire=60 * 60) # 1 hour in seconds

# check actor, signature, auth
type = activity.get('type')
Expand Down Expand Up @@ -1073,13 +1073,17 @@ def inbox(protocol=None, id=None):

if not id:
id = f'{actor_id}#{type}-{object.get("id", "")}-{util.now().isoformat()}'

try:
obj = Object.get_or_create(id=id, as2=unwrap(activity), authed_as=authed_as,
source_protocol=ActivityPub.LABEL)
except AssertionError as e:
error(f'Invalid activity, probably due to id: {e}', status=400)

return create_task(queue='receive', obj=obj.key.urlsafe(), authed_as=authed_as)
ret = create_task(queue='receive', obj=obj.key.urlsafe(), authed_as=authed_as)
if memcache_key:
memcache.set(memcache_key, 'seen', expire=60 * 60) # 1 hour in seconds
return ret


# protocol in subdomain
Expand Down
1 change: 1 addition & 0 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,7 @@ def test_inbox_ignore_forward_with_ld_sig(self, _, __, ___):
self.assertEqual(202, got.status_code)
self.assertIn('Ignoring LD Signature', got.text)
self.assertIsNone(Object.get_by_id('http://inst/post'))
self.assertIsNone(common.memcache.get('AP-id-http://inst/post'))

def test_inbox_http_sig_is_not_actor_author(self, mock_head, mock_get, mock_post):
mock_get.side_effect = [
Expand Down

0 comments on commit 25199f8

Please sign in to comment.