Check that posts entering the inbox are valid

master
Bob Mottram 2019-07-18 10:26:47 +01:00
parent b614493d92
commit 985e66f0df
3 changed files with 58 additions and 10 deletions

View File

@ -232,8 +232,8 @@ if args.tests:
if args.testsnetwork: if args.testsnetwork:
print('Network Tests') print('Network Tests')
testPostMessageBetweenServers() #testPostMessageBetweenServers()
testFollowBetweenServers() #testFollowBetweenServers()
testClientToServer() testClientToServer()
sys.exit() sys.exit()

View File

@ -40,6 +40,23 @@ from like import undoLikesCollectionEntry
from blocking import isBlocked from blocking import isBlocked
from filters import isFiltered from filters import isFiltered
def validInbox(baseDir: str,nickname: str,domain: str) -> bool:
if ':' in domain:
domain=domain.split(':')[0]
inboxDir=baseDir+'/accounts/'+nickname+'@'+domain+'/inbox'
if not os.path.isdir(inboxDir):
return True
for subdir, dirs, files in os.walk(inboxDir):
for f in files:
filename = os.path.join(subdir, f)
if not os.path.isfile(filename):
print('filename: '+filename)
return False
if 'postNickname' in open(filename).read():
print(filename)
return False
return True
def getPersonPubKey(session,personUrl: str,personCache: {},debug: bool) -> str: def getPersonPubKey(session,personUrl: str,personCache: {},debug: bool) -> str:
if not personUrl: if not personUrl:
return None return None
@ -896,7 +913,13 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
if debug: if debug:
print('DEBUG: object capabilities passed') print('DEBUG: object capabilities passed')
print('copy from '+queueFilename+' to '+destinationFilename) print('copy from '+queueFilename+' to '+destinationFilename)
copyfile(queueFilename,destinationFilename)
if messageJson.get('postNickname'):
with open(destinationFilename, 'w') as fp:
commentjson.dump(messageJson['post'], fp, indent=4, sort_keys=False)
else:
with open(destinationFilename, 'w') as fp:
commentjson.dump(messageJson, fp, indent=4, sort_keys=False)
return True return True
def restoreQueueItems(baseDir: str,queue: []) -> None: def restoreQueueItems(baseDir: str,queue: []) -> None:
@ -1155,8 +1178,8 @@ def runInboxQueue(baseDir: str,httpPrefix: str,sendThreads: [],postLog: [], \
# GET happens on individual accounts. # GET happens on individual accounts.
# See posts.py/createBoxBase # See posts.py/createBoxBase
if len(recipientsDictFollowers)>0: if len(recipientsDictFollowers)>0:
copyfile(queueFilename, \ with open(queueJson['destination'].replace(inboxHandle,inboxHandle), 'w') as fp:
queueJson['destination'].replace(inboxHandle,inboxHandle)) commentjson.dump(queueJson['post'], fp, indent=4, sort_keys=False)
# for posts addressed to specific accounts # for posts addressed to specific accounts
for handle,capsId in recipientsDict.items(): for handle,capsId in recipientsDict.items():

View File

@ -49,10 +49,12 @@ from auth import createBasicAuthHeader
from auth import authorizeBasic from auth import authorizeBasic
from auth import storeBasicCredentials from auth import storeBasicCredentials
from like import likePost from like import likePost
from like import sendLikeViaServer
from announce import announcePublic from announce import announcePublic
from announce import sendAnnounceViaServer from announce import sendAnnounceViaServer
from media import getMediaPath from media import getMediaPath
from delete import sendDeleteViaServer from delete import sendDeleteViaServer
from inbox import validInbox
testServerAliceRunning = False testServerAliceRunning = False
testServerBobRunning = False testServerBobRunning = False
@ -1074,7 +1076,7 @@ def testClientToServer():
outboxPostId=postJsonObject['id'].replace('/activity','') outboxPostId=postJsonObject['id'].replace('/activity','')
assert outboxPostId assert outboxPostId
print('message id obtained: '+outboxPostId) print('message id obtained: '+outboxPostId)
assert validInbox(bobDir,'bob',bobDomain)
print('\n\nAlice follows Bob') print('\n\nAlice follows Bob')
sendFollowRequestViaServer(sessionAlice,'alice',password, \ sendFollowRequestViaServer(sessionAlice,'alice',password, \
@ -1095,6 +1097,7 @@ def testClientToServer():
assert os.path.isfile(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt') assert os.path.isfile(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt')
assert 'alice@'+aliceDomain+':'+str(alicePort) in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read() assert 'alice@'+aliceDomain+':'+str(alicePort) in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read()
assert 'bob@'+bobDomain+':'+str(bobPort) in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read() assert 'bob@'+bobDomain+':'+str(bobPort) in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read()
assert validInbox(bobDir,'bob',bobDomain)
print('\n\nBob follows Alice') print('\n\nBob follows Alice')
sendFollowRequestViaServer(sessionAlice,'bob','bobpass', \ sendFollowRequestViaServer(sessionAlice,'bob','bobpass', \
@ -1116,8 +1119,8 @@ def testClientToServer():
assert 'bob@'+bobDomain+':'+str(bobPort) in open(aliceDir+'/accounts/alice@'+aliceDomain+'/followers.txt').read() assert 'bob@'+bobDomain+':'+str(bobPort) in open(aliceDir+'/accounts/alice@'+aliceDomain+'/followers.txt').read()
assert 'alice@'+aliceDomain+':'+str(alicePort) in open(bobDir+'/accounts/bob@'+bobDomain+'/following.txt').read() assert 'alice@'+aliceDomain+':'+str(alicePort) in open(bobDir+'/accounts/bob@'+bobDomain+'/following.txt').read()
print('\n\nBob repeats the post') print('\n\nBob likes the post')
sessionBob = createSession(bobDomain,bobPort,useTor) sessionBob = createSession(bobDomain,bobPort,useTor)
password='bobpass' password='bobpass'
outboxPath=bobDir+'/accounts/bob@'+bobDomain+'/outbox' outboxPath=bobDir+'/accounts/bob@'+bobDomain+'/outbox'
@ -1126,6 +1129,26 @@ def testClientToServer():
assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==1 assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==1
print(str(len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))]))) print(str(len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])))
assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1 assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1
sendLikeViaServer(sessionBob,'bob','bobpass', \
bobDomain,bobPort, \
httpPrefix,outboxPostId, \
cachedWebfingers,personCache, \
True)
for i in range(20):
if os.path.isdir(outboxPath) and os.path.isdir(inboxPath):
if len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==2:
if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1:
break
time.sleep(1)
assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==2
assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1
print('Post liked')
print('\n\nBob repeats the post')
print(str(len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])))
assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==2
print(str(len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])))
assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1
sendAnnounceViaServer(sessionBob,'bob',password, \ sendAnnounceViaServer(sessionBob,'bob',password, \
bobDomain,bobPort, \ bobDomain,bobPort, \
httpPrefix,outboxPostId, \ httpPrefix,outboxPostId, \
@ -1133,12 +1156,12 @@ def testClientToServer():
personCache,True) personCache,True)
for i in range(20): for i in range(20):
if os.path.isdir(outboxPath) and os.path.isdir(inboxPath): if os.path.isdir(outboxPath) and os.path.isdir(inboxPath):
if len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==2: if len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==3:
if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==2: if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==2:
break break
time.sleep(1) time.sleep(1)
assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==2 assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==3
assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==2 assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==2
print('Post repeated') print('Post repeated')
@ -1161,6 +1184,7 @@ def testClientToServer():
assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==postsBefore-1 assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==postsBefore-1
print(">>> post deleted from Alice's outbox and Bob's inbox") print(">>> post deleted from Alice's outbox and Bob's inbox")
assert validInbox(bobDir,'bob',bobDomain)
print('\n\nAlice unfollows Bob') print('\n\nAlice unfollows Bob')
@ -1181,6 +1205,7 @@ def testClientToServer():
assert os.path.isfile(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt') assert os.path.isfile(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt')
assert 'alice@'+aliceDomain+':'+str(alicePort) not in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read() assert 'alice@'+aliceDomain+':'+str(alicePort) not in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read()
assert 'bob@'+bobDomain+':'+str(bobPort) not in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read() assert 'bob@'+bobDomain+':'+str(bobPort) not in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read()
assert validInbox(bobDir,'bob',bobDomain)
# stop the servers # stop the servers
thrAlice.kill() thrAlice.kill()