From 0e5e60d6543e0c9c18071a9d8f8837e0e2a66a3e Mon Sep 17 00:00:00 2001
From: Bob Mottram
Date: Sun, 20 Jun 2021 13:07:55 +0100
Subject: [PATCH 1/3] Underscore convention for test functions
---
tests.py | 244 +++++++++++++++++++++++++++----------------------------
1 file changed, 122 insertions(+), 122 deletions(-)
diff --git a/tests.py b/tests.py
index 02793351c..e546142ec 100644
--- a/tests.py
+++ b/tests.py
@@ -125,7 +125,7 @@ thrBob = None
thrEve = None
-def testHttpSigNew():
+def _testHttpSigNew():
print('testHttpSigNew')
messageBodyJson = {"hello": "world"}
messageBodyJsonStr = json.dumps(messageBodyJson)
@@ -398,12 +398,12 @@ def _testHttpsigBase(withDigest):
shutil.rmtree(path)
-def testHttpsig():
+def _testHttpsig():
_testHttpsigBase(True)
_testHttpsigBase(False)
-def testCache():
+def _testCache():
print('testCache')
personUrl = "cat@cardboard.box"
personJson = {
@@ -417,15 +417,15 @@ def testCache():
assert result['test'] == 'This is a test'
-def testThreadsFunction(param: str):
+def _testThreadsFunction(param: str):
for i in range(10000):
time.sleep(2)
-def testThreads():
+def _testThreads():
print('testThreads')
thr = \
- threadWithTrace(target=testThreadsFunction,
+ threadWithTrace(target=_testThreadsFunction,
args=('test',),
daemon=True)
thr.start()
@@ -1151,7 +1151,7 @@ def testFollowBetweenServers():
shutil.rmtree(baseDir + '/.tests')
-def testFollowersOfPerson():
+def _testFollowersOfPerson():
print('testFollowersOfPerson')
currDir = os.getcwd()
nickname = 'mxpop'
@@ -1200,7 +1200,7 @@ def testFollowersOfPerson():
shutil.rmtree(baseDir)
-def testNoOfFollowersOnDomain():
+def _testNoOfFollowersOnDomain():
print('testNoOfFollowersOnDomain')
currDir = os.getcwd()
nickname = 'mxpop'
@@ -1261,7 +1261,7 @@ def testNoOfFollowersOnDomain():
shutil.rmtree(baseDir)
-def testGroupFollowers():
+def _testGroupFollowers():
print('testGroupFollowers')
currDir = os.getcwd()
@@ -1306,7 +1306,7 @@ def testGroupFollowers():
shutil.rmtree(baseDir)
-def testFollows():
+def _testFollows():
print('testFollows')
currDir = os.getcwd()
nickname = 'test529'
@@ -1383,7 +1383,7 @@ def testFollows():
shutil.rmtree(baseDir)
-def testCreatePerson():
+def _testCreatePerson():
print('testCreatePerson')
currDir = os.getcwd()
nickname = 'test382'
@@ -1417,7 +1417,7 @@ def testCreatePerson():
shutil.rmtree(baseDir)
-def testAuthentication():
+def _testAuthentication():
print('testAuthentication')
currDir = os.getcwd()
nickname = 'test8743'
@@ -1824,7 +1824,7 @@ def testClientToServer():
# shutil.rmtree(bobDir)
-def testActorParsing():
+def _testActorParsing():
print('testActorParsing')
actor = 'https://mydomain:72/users/mynick'
domain, port = getDomainFromActor(actor)
@@ -1871,7 +1871,7 @@ def testActorParsing():
assert nickname == 'othernick'
-def testWebLinks():
+def _testWebLinks():
print('testWebLinks')
exampleText = \
@@ -1998,7 +1998,7 @@ def testWebLinks():
assert resultText == exampleText
-def testAddEmoji():
+def _testAddEmoji():
print('testAddEmoji')
content = "Emoji :lemon: :strawberry: :banana:"
httpPrefix = 'http'
@@ -2045,7 +2045,7 @@ def testAddEmoji():
shutil.rmtree(baseDirOriginal + '/.tests')
-def testGetStatusNumber():
+def _testGetStatusNumber():
print('testGetStatusNumber')
prevStatusNumber = None
for i in range(1, 20):
@@ -2056,7 +2056,7 @@ def testGetStatusNumber():
prevStatusNumber = int(statusNumber)
-def testJsonString() -> None:
+def _testJsonString() -> None:
print('testJsonString')
filename = '.epicyon_tests_testJsonString.json'
messageStr = "Crème brûlée यह एक परीक्षण ह"
@@ -2072,7 +2072,7 @@ def testJsonString() -> None:
os.remove(filename)
-def testSaveLoadJson():
+def _testSaveLoadJson():
print('testSaveLoadJson')
testJson = {
"param1": 3,
@@ -2092,7 +2092,7 @@ def testSaveLoadJson():
os.remove(testFilename)
-def testTheme():
+def _testTheme():
print('testTheme')
css = 'somestring --background-value: 24px; --foreground-value: 24px;'
result = setCSSparam(css, 'background-value', '32px')
@@ -2110,7 +2110,7 @@ def testTheme():
assert result == '--background-value: 32px; --foreground-value: 24px;'
-def testRecentPostsCache():
+def _testRecentPostsCache():
print('testRecentPostsCache')
recentPostsCache = {}
maxRecentPosts = 3
@@ -2126,7 +2126,7 @@ def testRecentPostsCache():
assert len(recentPostsCache['html'].items()) == maxRecentPosts
-def testRemoveTextFormatting():
+def _testRemoveTextFormatting():
print('testRemoveTextFormatting')
testStr = 'Text without formatting
'
resultStr = removeTextFormatting(testStr)
@@ -2136,7 +2136,7 @@ def testRemoveTextFormatting():
assert(resultStr == 'Text with formatting
')
-def testJsonld():
+def _testJsonld():
print("testJsonld")
jldDocument = {
@@ -2225,14 +2225,14 @@ def testJsonld():
signedDocument2['signature']['signatureValue'])
-def testSiteIsActive():
+def _testSiteIsActive():
print('testSiteIsActive')
assert(siteIsActive('https://archive.org'))
assert(siteIsActive('https://mastodon.social'))
assert(not siteIsActive('https://notarealwebsite.a.b.c'))
-def testRemoveHtml():
+def _testRemoveHtml():
print('testRemoveHtml')
testStr = 'This string has no html.'
assert(removeHtml(testStr) == testStr)
@@ -2249,7 +2249,7 @@ def testRemoveHtml():
'This string contains a url http://somesite.or.other')
-def testDangerousCSS():
+def _testDangerousCSS():
print('testDangerousCSS')
baseDir = os.getcwd()
for subdir, dirs, files in os.walk(baseDir):
@@ -2260,7 +2260,7 @@ def testDangerousCSS():
break
-def testDangerousMarkup():
+def _testDangerousMarkup():
print('testDangerousMarkup')
allowLocalNetworkAccess = False
content = 'This is a valid message
'
@@ -2322,7 +2322,7 @@ def testDangerousMarkup():
assert(not dangerousMarkup(content, allowLocalNetworkAccess))
-def runHtmlReplaceQuoteMarks():
+def _runHtmlReplaceQuoteMarks():
print('htmlReplaceQuoteMarks')
testStr = 'The "cat" "sat" on the mat'
result = htmlReplaceQuoteMarks(testStr)
@@ -2341,7 +2341,7 @@ def runHtmlReplaceQuoteMarks():
assert result == '“hello” “test” html'
-def testJsonPostAllowsComments():
+def _testJsonPostAllowsComments():
print('testJsonPostAllowsComments')
postJsonObject = {
"id": "123"
@@ -2373,7 +2373,7 @@ def testJsonPostAllowsComments():
assert not jsonPostAllowsComments(postJsonObject)
-def testRemoveIdEnding():
+def _testRemoveIdEnding():
print('testRemoveIdEnding')
testStr = 'https://activitypub.somedomain.net'
resultStr = removeIdEnding(testStr)
@@ -2399,7 +2399,7 @@ def testRemoveIdEnding():
'https://event.somedomain.net/users/foo/statuses/34544814814'
-def testValidContentWarning():
+def _testValidContentWarning():
print('testValidContentWarning')
resultStr = validContentWarning('Valid content warning')
assert resultStr == 'Valid content warning'
@@ -2412,7 +2412,7 @@ def testValidContentWarning():
assert resultStr == 'Invalid content warning'
-def testTranslations():
+def _testTranslations():
print('testTranslations')
languagesStr = ('ar', 'ca', 'cy', 'de', 'es', 'fr', 'ga',
'hi', 'it', 'ja', 'oc', 'pt', 'ru', 'zh')
@@ -2438,7 +2438,7 @@ def testTranslations():
assert langJson.get(englishStr)
-def testConstantTimeStringCheck():
+def _testConstantTimeStringCheck():
print('testConstantTimeStringCheck')
assert constantTimeStringCheck('testing', 'testing')
assert not constantTimeStringCheck('testing', '1234')
@@ -2476,7 +2476,7 @@ def testConstantTimeStringCheck():
assert int(timeDiffMicroseconds) < 10
-def testReplaceEmailQuote():
+def _testReplaceEmailQuote():
print('testReplaceEmailQuote')
testStr = 'This content has no quote.
'
assert htmlReplaceEmailQuote(testStr) == testStr
@@ -2534,7 +2534,7 @@ def testReplaceEmailQuote():
assert resultStr == expectedStr
-def testRemoveHtmlTag():
+def _testRemoveHtmlTag():
print('testRemoveHtmlTag')
testStr = "![]()
"
@@ -2543,7 +2543,7 @@ def testRemoveHtmlTag():
"src=\"https://somesiteorother.com/image.jpg\">
"
-def testHashtagRuleTree():
+def _testHashtagRuleTree():
print('testHashtagRuleTree')
operators = ('not', 'and', 'or', 'xor', 'from', 'contains')
@@ -2675,7 +2675,7 @@ def testHashtagRuleTree():
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
-def testGetNewswireTags():
+def _testGetNewswireTags():
print('testGetNewswireTags')
rssDescription = '
This is a test' + \
@@ -2704,7 +2704,7 @@ def testFirstParagraphFromString():
assert resultStr == testStr
-def testParseFeedDate():
+def _testParseFeedDate():
print('testParseFeedDate')
pubDate = "2020-12-14T00:08:06+00:00"
@@ -2724,7 +2724,7 @@ def testParseFeedDate():
assert publishedDate == "2020-11-22 18:51:33+00:00"
-def testValidNickname():
+def _testValidNickname():
print('testValidNickname')
domain = 'somedomain.net'
@@ -2741,7 +2741,7 @@ def testValidNickname():
assert not validNickname(domain, nickname)
-def testGuessHashtagCategory() -> None:
+def _testGuessHashtagCategory() -> None:
print('testGuessHashtagCategory')
hashtagCategories = {
"foo": ["swan", "goose"],
@@ -2754,7 +2754,7 @@ def testGuessHashtagCategory() -> None:
assert guess == "bar"
-def testGetMentionedPeople() -> None:
+def _testGetMentionedPeople() -> None:
print('testGetMentionedPeople')
baseDir = os.getcwd()
@@ -2768,7 +2768,7 @@ def testGetMentionedPeople() -> None:
assert actors[1] == "https://cave.site/users/bat"
-def testReplyToPublicPost() -> None:
+def _testReplyToPublicPost() -> None:
baseDir = os.getcwd()
nickname = 'test7492362'
domain = 'other.site'
@@ -2810,7 +2810,7 @@ def testReplyToPublicPost() -> None:
httpPrefix + '://rat.site/users/ninjarodent'
-def getFunctionCallArgs(name: str, lines: [], startLineCtr: int) -> []:
+def _getFunctionCallArgs(name: str, lines: [], startLineCtr: int) -> []:
"""Returns the arguments of a function call given lines
of source code and a starting line number
"""
@@ -2848,7 +2848,7 @@ def getFunctionCalls(name: str, lines: [], startLineCtr: int,
return callsFunctions
-def functionArgsMatch(callArgs: [], funcArgs: []):
+def _functionArgsMatch(callArgs: [], funcArgs: []):
"""Do the function artuments match the function call arguments
"""
if len(callArgs) == len(funcArgs):
@@ -2872,7 +2872,7 @@ def functionArgsMatch(callArgs: [], funcArgs: []):
return callArgsCtr >= funcArgsCtr
-def testFunctions():
+def _testFunctions():
print('testFunctions')
function = {}
functionProperties = {}
@@ -2959,11 +2959,11 @@ def testFunctions():
lineCtr += 1
continue
callArgs = \
- getFunctionCallArgs(name,
- modules[modName]['lines'],
- lineCtr)
- if not functionArgsMatch(callArgs,
- functionProperties[name]['args']):
+ _getFunctionCallArgs(name,
+ modules[modName]['lines'],
+ lineCtr)
+ funcArgs = functionProperties[name]['args']
+ if not _functionArgsMatch(callArgs, funcArgs):
print('Call to function ' + name +
' does not match its arguments')
print('def args: ' +
@@ -3012,7 +3012,7 @@ def testFunctions():
'runSharesExpireWatchdog',
'getThisWeeksEvents',
'getAvailability',
- 'testThreadsFunction',
+ '_testThreadsFunction',
'createServerAlice',
'createServerBob',
'createServerEve',
@@ -3224,7 +3224,7 @@ def testFunctions():
'-Gsep=+120 -Tx11 epicyon.dot')
-def testLinksWithinPost() -> None:
+def _testLinksWithinPost() -> None:
baseDir = os.getcwd()
nickname = 'test27636'
domain = 'rando.site'
@@ -3276,7 +3276,7 @@ def testLinksWithinPost() -> None:
assert postJsonObject['object']['content'] == content
-def testMastoApi():
+def _testMastoApi():
print('testMastoApi')
nickname = 'ThisIsATestNickname'
mastoId = getMastoApiV1IdFromNickname(nickname)
@@ -3287,7 +3287,7 @@ def testMastoApi():
assert nickname2 == nickname
-def testDomainHandling():
+def _testDomainHandling():
print('testDomainHandling')
testDomain = 'localhost'
assert decodedHost(testDomain) == testDomain
@@ -3299,7 +3299,7 @@ def testDomainHandling():
assert decodedHost(testDomain) == "españa.icom.museum"
-def testPrepareHtmlPostNickname():
+def _testPrepareHtmlPostNickname():
print('testPrepareHtmlPostNickname')
postHtml = '.'
-def testExtractTextFieldsInPOST():
+def _testExtractTextFieldsInPOST():
print('testExtractTextFieldsInPOST')
boundary = '-----------------------------116202748023898664511855843036'
formData = '-----------------------------116202748023898664511855' + \
@@ -3413,7 +3413,7 @@ def testExtractTextFieldsInPOST():
assert fields['message'] == 'This is a ; test'
-def testSpeakerReplaceLinks():
+def _testSpeakerReplaceLinks():
print('testSpeakerReplaceLinks')
text = 'The Tor Project: For Snowflake volunteers: If you use ' + \
'Firefox, Brave, or Chrome, our Snowflake extension turns ' + \
@@ -3431,7 +3431,7 @@ def testSpeakerReplaceLinks():
assert 'Web link support.torproject.org' in result
-def testCamelCaseSplit():
+def _testCamelCaseSplit():
print('testCamelCaseSplit')
testStr = 'ThisIsCamelCase'
assert camelCaseSplit(testStr) == 'This Is Camel Case'
@@ -3440,7 +3440,7 @@ def testCamelCaseSplit():
assert camelCaseSplit(testStr) == 'Notcamelcase test'
-def testEmojiImages():
+def _testEmojiImages():
print('testEmojiImages')
emojiFilename = 'emoji/default_emoji.json'
assert os.path.isfile(emojiFilename)
@@ -3454,7 +3454,7 @@ def testEmojiImages():
assert os.path.isfile(emojiImageFilename)
-def testExtractPGPPublicKey():
+def _testExtractPGPPublicKey():
print('testExtractPGPPublicKey')
pubKey = \
'-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n' + \
@@ -3601,7 +3601,7 @@ def testUpdateActor():
shutil.rmtree(baseDir + '/.tests')
-def testRemovePostInteractions() -> None:
+def _testRemovePostInteractions() -> None:
print('testRemovePostInteractions')
postJsonObject = {
"type": "Create",
@@ -3633,7 +3633,7 @@ def testRemovePostInteractions() -> None:
assert not removePostInteractions(postJsonObject, False)
-def testSpoofGeolocation() -> None:
+def _testSpoofGeolocation() -> None:
print('testSpoofGeolocation')
nogoLine = \
'NEW YORK, USA: 73.951W,40.879, 73.974W,40.83, ' + \
@@ -3882,7 +3882,7 @@ def testSpoofGeolocation() -> None:
kmlFile.close()
-def testSkills() -> None:
+def _testSkills() -> None:
print('testSkills')
actorJson = {
'hasOccupation': [
@@ -3908,7 +3908,7 @@ def testSkills() -> None:
assert actorSkillValue(actorJson, 'gardening') == 70
-def testRoles() -> None:
+def _testRoles() -> None:
print('testRoles')
actorJson = {
'hasOccupation': [
@@ -3935,60 +3935,60 @@ def testRoles() -> None:
def runAllTests():
print('Running tests...')
updateDefaultThemesList(os.getcwd())
- testFunctions()
- testRoles()
- testSkills()
- testSpoofGeolocation()
- testRemovePostInteractions()
- testExtractPGPPublicKey()
- testEmojiImages()
- testCamelCaseSplit()
- testSpeakerReplaceLinks()
- testExtractTextFieldsInPOST()
- testMarkdownToHtml()
- testValidHashTag()
- testPrepareHtmlPostNickname()
- testDomainHandling()
- testMastoApi()
- testLinksWithinPost()
- testReplyToPublicPost()
- testGetMentionedPeople()
- testGuessHashtagCategory()
- testValidNickname()
- testParseFeedDate()
- testFirstParagraphFromString()
- testGetNewswireTags()
- testHashtagRuleTree()
- testRemoveHtmlTag()
- testReplaceEmailQuote()
- testConstantTimeStringCheck()
- testTranslations()
- testValidContentWarning()
- testRemoveIdEnding()
- testJsonPostAllowsComments()
- runHtmlReplaceQuoteMarks()
- testDangerousCSS()
- testDangerousMarkup()
- testRemoveHtml()
- testSiteIsActive()
- testJsonld()
- testRemoveTextFormatting()
- testWebLinks()
- testRecentPostsCache()
- testTheme()
- testSaveLoadJson()
- testJsonString()
- testGetStatusNumber()
- testAddEmoji()
- testActorParsing()
- testHttpsig()
- testHttpSigNew()
- testCache()
- testThreads()
- testCreatePerson()
- testAuthentication()
- testFollowersOfPerson()
- testNoOfFollowersOnDomain()
- testFollows()
- testGroupFollowers()
+ _testFunctions()
+ _testRoles()
+ _testSkills()
+ _testSpoofGeolocation()
+ _testRemovePostInteractions()
+ _testExtractPGPPublicKey()
+ _testEmojiImages()
+ _testCamelCaseSplit()
+ _testSpeakerReplaceLinks()
+ _testExtractTextFieldsInPOST()
+ _testMarkdownToHtml()
+ _testValidHashTag()
+ _testPrepareHtmlPostNickname()
+ _testDomainHandling()
+ _testMastoApi()
+ _testLinksWithinPost()
+ _testReplyToPublicPost()
+ _testGetMentionedPeople()
+ _testGuessHashtagCategory()
+ _testValidNickname()
+ _testParseFeedDate()
+ _testFirstParagraphFromString()
+ _testGetNewswireTags()
+ _testHashtagRuleTree()
+ _testRemoveHtmlTag()
+ _testReplaceEmailQuote()
+ _testConstantTimeStringCheck()
+ _testTranslations()
+ _testValidContentWarning()
+ _testRemoveIdEnding()
+ _testJsonPostAllowsComments()
+ _runHtmlReplaceQuoteMarks()
+ _testDangerousCSS()
+ _testDangerousMarkup()
+ _testRemoveHtml()
+ _testSiteIsActive()
+ _testJsonld()
+ _testRemoveTextFormatting()
+ _testWebLinks()
+ _testRecentPostsCache()
+ _testTheme()
+ _testSaveLoadJson()
+ _testJsonString()
+ _testGetStatusNumber()
+ _testAddEmoji()
+ _testActorParsing()
+ _testHttpsig()
+ _testHttpSigNew()
+ _testCache()
+ _testThreads()
+ _testCreatePerson()
+ _testAuthentication()
+ _testFollowersOfPerson()
+ _testNoOfFollowersOnDomain()
+ _testFollows()
+ _testGroupFollowers()
print('Tests succeeded\n')
From c76a10480c4b88f271643d6138d2b61823362924 Mon Sep 17 00:00:00 2001
From: Bob Mottram
Date: Sun, 20 Jun 2021 14:25:18 +0100
Subject: [PATCH 2/3] Check user agent domain is not blocked
---
daemon.py | 45 ++++++++++++++++++++++++++++++++++++++++++++-
epicyon.py | 14 +++++++++++++-
tests.py | 12 +++++++++---
3 files changed, 66 insertions(+), 5 deletions(-)
diff --git a/daemon.py b/daemon.py
index c9bc684b5..3c76d90a3 100644
--- a/daemon.py
+++ b/daemon.py
@@ -452,6 +452,40 @@ class PubServer(BaseHTTPRequestHandler):
else:
print('ERROR: unable to create vote')
+ def _userAgentDomain(self) -> str:
+ """Returns the domain specified within User-Agent header
+ """
+ if not self.headers.get('User-Agent'):
+ return None
+ agentStr = self.headers.get('User-Agent')
+ if '+' not in agentStr:
+ return None
+ agentDomain = agentStr.split('+')[1].strip()
+ if '://' in agentDomain:
+ agentDomain = agentDomain.split('://')[1]
+ if '/' in agentDomain:
+ agentDomain = agentDomain.split('/')[0]
+ if ' ' in agentDomain:
+ agentDomain = agentDomain.replace(' ', '')
+ if ';' in agentDomain:
+ agentDomain = agentDomain.replace(';', '')
+ if '.' not in agentDomain:
+ return None
+ return agentDomain
+
+ def _blockedUserAgent(self) -> bool:
+ """Should a GET or POST be blocked based upon its user agent?
+ """
+ agentDomain = self._userAgentDomain()
+ if not agentDomain:
+ if self.server.userAgentDomainRequired:
+ return True
+ return False
+ blockedUA = isBlockedDomain(self.server.baseDir, agentDomain)
+ if blockedUA and self.server.debug:
+ print('Blocked User agent: ' + agentDomain)
+ return blockedUA
+
def _requestHTTP(self) -> bool:
"""Should a http response be given?
"""
@@ -10594,6 +10628,10 @@ class PubServer(BaseHTTPRequestHandler):
self._400()
return
+ if self._blockedUserAgent():
+ self._400()
+ return
+
GETstartTime = time.time()
GETtimings = {}
@@ -14843,7 +14881,8 @@ def loadTokens(baseDir: str, tokensDict: {}, tokensLookup: {}) -> None:
break
-def runDaemon(logLoginFailures: bool,
+def runDaemon(userAgentDomainRequired: bool,
+ logLoginFailures: bool,
city: str,
showNodeInfoAccounts: bool,
showNodeInfoVersion: bool,
@@ -14969,6 +15008,10 @@ def runDaemon(logLoginFailures: bool,
httpd.keyShortcuts = {}
loadAccessKeysForAccounts(baseDir, httpd.keyShortcuts, httpd.accessKeys)
+ # if set to True then the calling domain must be specified
+ # within the User-Agent header
+ httpd.userAgentDomainRequired = userAgentDomainRequired
+
httpd.unitTest = unitTest
httpd.allowLocalNetworkAccess = allowLocalNetworkAccess
if unitTest:
diff --git a/epicyon.py b/epicyon.py
index 1888d894c..406ba3856 100644
--- a/epicyon.py
+++ b/epicyon.py
@@ -274,6 +274,12 @@ parser.add_argument("--repliesEnabled", "--commentsEnabled",
type=str2bool, nargs='?',
const=True, default=True,
help="Enable replies to a post")
+parser.add_argument("--userAgentDomainRequired",
+ dest='userAgentDomainRequired',
+ type=str2bool, nargs='?',
+ const=True, default=False,
+ help="Whether User-Agent header must " +
+ "contain the calling domain")
parser.add_argument("--showPublishAsIcon",
dest='showPublishAsIcon',
type=str2bool, nargs='?',
@@ -2516,6 +2522,11 @@ showNodeInfoVersion = \
if showNodeInfoVersion is not None:
args.showNodeInfoVersion = bool(showNodeInfoVersion)
+userAgentDomainRequired = \
+ getConfigParam(baseDir, 'userAgentDomainRequired')
+if userAgentDomainRequired is not None:
+ args.userAgentDomainRequired = bool(userAgentDomainRequired)
+
city = \
getConfigParam(baseDir, 'city')
if city is not None:
@@ -2552,7 +2563,8 @@ if args.registration:
print('New registrations closed')
if __name__ == "__main__":
- runDaemon(args.logLoginFailures,
+ runDaemon(args.userAgentDomainRequired,
+ args.logLoginFailures,
args.city,
args.showNodeInfoAccounts,
args.showNodeInfoVersion,
diff --git a/tests.py b/tests.py
index e546142ec..54f17b7d3 100644
--- a/tests.py
+++ b/tests.py
@@ -519,8 +519,10 @@ def createServerAlice(path: str, domain: str, port: int,
showNodeInfoVersion = True
city = 'London, England'
logLoginFailures = False
+ userAgentDomainRequired = False
print('Server running: Alice')
- runDaemon(logLoginFailures, city,
+ runDaemon(userAgentDomainRequired,
+ logLoginFailures, city,
showNodeInfoAccounts,
showNodeInfoVersion,
brochMode,
@@ -622,8 +624,10 @@ def createServerBob(path: str, domain: str, port: int,
showNodeInfoVersion = True
city = 'London, England'
logLoginFailures = False
+ userAgentDomainRequired = False
print('Server running: Bob')
- runDaemon(logLoginFailures, city,
+ runDaemon(userAgentDomainRequired,
+ logLoginFailures, city,
showNodeInfoAccounts,
showNodeInfoVersion,
brochMode,
@@ -680,8 +684,10 @@ def createServerEve(path: str, domain: str, port: int, federationList: [],
showNodeInfoVersion = True
city = 'London, England'
logLoginFailures = False
+ userAgentDomainRequired = False
print('Server running: Eve')
- runDaemon(logLoginFailures, city,
+ runDaemon(userAgentDomainRequired,
+ logLoginFailures, city,
showNodeInfoAccounts,
showNodeInfoVersion,
brochMode,
From eab0a92af9f1a7c48f82bac995ddd22499da79da Mon Sep 17 00:00:00 2001
From: Bob Mottram
Date: Sun, 20 Jun 2021 14:39:53 +0100
Subject: [PATCH 3/3] Include User-Agent within POST
---
announce.py | 6 ++++--
availability.py | 3 ++-
bookmarks.py | 6 ++++--
delete.py | 3 ++-
follow.py | 6 ++++--
like.py | 6 ++++--
pgp.py | 3 ++-
posts.py | 12 ++++++++----
session.py | 8 +++++++-
shares.py | 6 ++++--
skills.py | 3 ++-
11 files changed, 43 insertions(+), 19 deletions(-)
diff --git a/announce.py b/announce.py
index 098cb8820..97c9d4fb1 100644
--- a/announce.py
+++ b/announce.py
@@ -253,7 +253,8 @@ def sendAnnounceViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newAnnounceJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, fromDomainFull,
+ session, newAnnounceJson, [], inboxUrl,
headers, 3, True)
if not postResult:
print('WARN: announce not posted')
@@ -332,7 +333,8 @@ def sendUndoAnnounceViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, unAnnounceJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, domainFull,
+ session, unAnnounceJson, [], inboxUrl,
headers, 3, True)
if not postResult:
print('WARN: undo announce not posted')
diff --git a/availability.py b/availability.py
index 441b01264..9df2173bd 100644
--- a/availability.py
+++ b/availability.py
@@ -143,7 +143,8 @@ def sendAvailabilityViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newAvailabilityJson, [],
+ postResult = postJson(httpPrefix, domainFull,
+ session, newAvailabilityJson, [],
inboxUrl, headers, 30, True)
if not postResult:
print('WARN: availability failed to post')
diff --git a/bookmarks.py b/bookmarks.py
index 04a68052b..58a772b3d 100644
--- a/bookmarks.py
+++ b/bookmarks.py
@@ -418,7 +418,8 @@ def sendBookmarkViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newBookmarkJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, domainFull,
+ session, newBookmarkJson, [], inboxUrl,
headers, 3, True)
if not postResult:
if debug:
@@ -502,7 +503,8 @@ def sendUndoBookmarkViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newBookmarkJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, domainFull,
+ session, newBookmarkJson, [], inboxUrl,
headers, 3, True)
if not postResult:
if debug:
diff --git a/delete.py b/delete.py
index 0566015d6..05ceb051d 100644
--- a/delete.py
+++ b/delete.py
@@ -93,7 +93,8 @@ def sendDeleteViaServer(baseDir: str, session,
'Authorization': authHeader
}
postResult = \
- postJson(session, newDeleteJson, [], inboxUrl, headers, 3, True)
+ postJson(httpPrefix, fromDomainFull,
+ session, newDeleteJson, [], inboxUrl, headers, 3, True)
if not postResult:
if debug:
print('DEBUG: POST delete failed for c2s to ' + inboxUrl)
diff --git a/follow.py b/follow.py
index 0b1ecbc4d..f7a48ed10 100644
--- a/follow.py
+++ b/follow.py
@@ -1029,7 +1029,8 @@ def sendFollowRequestViaServer(baseDir: str, session,
'Authorization': authHeader
}
postResult = \
- postJson(session, newFollowJson, [], inboxUrl, headers, 3, True)
+ postJson(httpPrefix, fromDomainFull,
+ session, newFollowJson, [], inboxUrl, headers, 3, True)
if not postResult:
if debug:
print('DEBUG: POST follow request failed for c2s to ' + inboxUrl)
@@ -1122,7 +1123,8 @@ def sendUnfollowRequestViaServer(baseDir: str, session,
'Authorization': authHeader
}
postResult = \
- postJson(session, unfollowJson, [], inboxUrl, headers, 3, True)
+ postJson(httpPrefix, fromDomainFull,
+ session, unfollowJson, [], inboxUrl, headers, 3, True)
if not postResult:
if debug:
print('DEBUG: POST unfollow failed for c2s to ' + inboxUrl)
diff --git a/like.py b/like.py
index 646913043..7e5d51374 100644
--- a/like.py
+++ b/like.py
@@ -205,7 +205,8 @@ def sendLikeViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newLikeJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, fromDomainFull,
+ session, newLikeJson, [], inboxUrl,
headers, 3, True)
if not postResult:
if debug:
@@ -287,7 +288,8 @@ def sendUndoLikeViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newUndoLikeJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, fromDomainFull,
+ session, newUndoLikeJson, [], inboxUrl,
headers, 3, True)
if not postResult:
if debug:
diff --git a/pgp.py b/pgp.py
index b32958603..619bad3d5 100644
--- a/pgp.py
+++ b/pgp.py
@@ -588,7 +588,8 @@ def pgpPublicKeyUpload(baseDir: str, session,
tries = 0
while tries < 4:
postResult = \
- postJson(session, actorUpdate, [], inboxUrl,
+ postJson(httpPrefix, domainFull,
+ session, actorUpdate, [], inboxUrl,
headers, 5, quiet)
if postResult:
break
diff --git a/posts.py b/posts.py
index 862705234..6e275d45f 100644
--- a/posts.py
+++ b/posts.py
@@ -4198,7 +4198,8 @@ def sendBlockViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newBlockJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, fromDomainFull,
+ session, newBlockJson, [], inboxUrl,
headers, 30, True)
if not postResult:
print('WARN: block unable to post')
@@ -4273,7 +4274,8 @@ def sendMuteViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newMuteJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, fromDomainFull,
+ session, newMuteJson, [], inboxUrl,
headers, 3, True)
if postResult is None:
print('WARN: mute unable to post')
@@ -4354,7 +4356,8 @@ def sendUndoMuteViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, undoMuteJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, fromDomainFull,
+ session, undoMuteJson, [], inboxUrl,
headers, 3, True)
if postResult is None:
print('WARN: undo mute unable to post')
@@ -4438,7 +4441,8 @@ def sendUndoBlockViaServer(baseDir: str, session,
'Content-type': 'application/json',
'Authorization': authHeader
}
- postResult = postJson(session, newBlockJson, [], inboxUrl,
+ postResult = postJson(httpPrefix, fromDomainFull,
+ session, newBlockJson, [], inboxUrl,
headers, 30, True)
if not postResult:
print('WARN: unblock unable to post')
diff --git a/session.py b/session.py
index 0c4de0b39..f6ccfbb3a 100644
--- a/session.py
+++ b/session.py
@@ -149,7 +149,8 @@ def getJson(session, url: str, headers: {}, params: {}, debug: bool,
return None
-def postJson(session, postJsonObject: {}, federationList: [],
+def postJson(httpPrefix: str, domainFull: str,
+ session, postJsonObject: {}, federationList: [],
inboxUrl: str, headers: {}, timeoutSec: int = 60,
quiet: bool = False) -> str:
"""Post a json message to the inbox of another person
@@ -160,6 +161,11 @@ def postJson(session, postJsonObject: {}, federationList: [],
print('postJson: ' + inboxUrl + ' not permitted')
return None
+ sessionHeaders = headers
+ sessionHeaders['User-Agent'] = 'Epicyon/' + __version__
+ sessionHeaders['User-Agent'] += \
+ '; +' + httpPrefix + '://' + domainFull + '/'
+
try:
postResult = \
session.post(url=inboxUrl,
diff --git a/shares.py b/shares.py
index 467209750..4fb62daad 100644
--- a/shares.py
+++ b/shares.py
@@ -410,7 +410,8 @@ def sendShareViaServer(baseDir, session,
'Authorization': authHeader
}
postResult = \
- postJson(session, newShareJson, [], inboxUrl, headers, 30, True)
+ postJson(httpPrefix, fromDomainFull,
+ session, newShareJson, [], inboxUrl, headers, 30, True)
if not postResult:
if debug:
print('DEBUG: POST share failed for c2s to ' + inboxUrl)
@@ -500,7 +501,8 @@ def sendUndoShareViaServer(baseDir: str, session,
'Authorization': authHeader
}
postResult = \
- postJson(session, undoShareJson, [], inboxUrl,
+ postJson(httpPrefix, fromDomainFull,
+ session, undoShareJson, [], inboxUrl,
headers, 30, True)
if not postResult:
if debug:
diff --git a/skills.py b/skills.py
index d2e8a8306..dae7d567b 100644
--- a/skills.py
+++ b/skills.py
@@ -246,7 +246,8 @@ def sendSkillViaServer(baseDir: str, session, nickname: str, password: str,
'Authorization': authHeader
}
postResult = \
- postJson(session, newSkillJson, [], inboxUrl,
+ postJson(httpPrefix, domainFull,
+ session, newSkillJson, [], inboxUrl,
headers, 30, True)
if not postResult:
if debug: