annotate MoinMoin/security/antispam.py @ 6133:a6283e189869 tip

fixup: remove nonexisting passlib.utils._blowfish this was removed by the passlib 1.7.1 upgrade.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 01 Jun 2017 18:10:19 +0200
parents 500f68d3e2fd
children
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 This implements a global (and a local) blacklist against wiki spammers.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
5 @copyright: 2005-2008 MoinMoin:ThomasWaldmann
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
7 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
2992
954c617d9f76 antispam: check edit comments also against BadContent, use builtin frozenset, if possible (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2988
diff changeset
9 import re, time, datetime
954c617d9f76 antispam: check edit comments also against BadContent, use builtin frozenset, if possible (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2988
diff changeset
10
3110
a48929a5036c logging: make it work correctly by doing logging configuration very early
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3107
diff changeset
11 from MoinMoin import log
a48929a5036c logging: make it work correctly by doing logging configuration very early
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3107
diff changeset
12 logging = log.getLogger(__name__)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
13
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
14 from MoinMoin.security import Permissions
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
15 from MoinMoin import caching, wikiutil
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
16
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
17 # Errors ---------------------------------------------------------------
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
18
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
19 class Error(Exception):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
20 """Base class for antispam errors."""
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
21
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
22 def __str__(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
23 return repr(self)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
24
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
25 class WikirpcError(Error):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
26 """ Raised when we get xmlrpclib.Fault """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
27
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
28 def __init__(self, msg, fault):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
29 """ Init with msg and xmlrpclib.Fault dict """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
30 self.msg = msg
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
31 self.fault = fault
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
32
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
33 def __str__(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
34 """ Format the using description and data from the fault """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
35 return self.msg + ": [%(faultCode)s] %(faultString)s" % self.fault
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
36
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
37
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
38 # Functions ------------------------------------------------------------
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
39
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
40 def makelist(text):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
41 """ Split text into lines, strip them, skip # comments """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
42 lines = text.splitlines()
1955
c872387196da refactor antispam: make master url configurable, remove now useless merging code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
43 result = []
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
44 for line in lines:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
45 line = line.split(' # ', 1)[0] # rest of line comment
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46 line = line.strip()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
47 if line and not line.startswith('#'):
1955
c872387196da refactor antispam: make master url configurable, remove now useless merging code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
48 result.append(line)
c872387196da refactor antispam: make master url configurable, remove now useless merging code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
49 return result
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
50
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
51
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
52 def getblacklist(request, pagename, do_update):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
53 """ Get blacklist, possibly downloading new copy
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
54
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
55 @param request: current request (request instance)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
56 @param pagename: bad content page name (unicode)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
57 @rtype: list
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
58 @return: list of blacklisted regular expressions
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
59 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
60 from MoinMoin.PageEditor import PageEditor
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
61 p = PageEditor(request, pagename, uid_override="Antispam subsystem")
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
62 mymtime = wikiutil.version2timestamp(p.mtime_usecs())
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
63 if do_update:
1648
d9077dcd38e1 fix antispam polling moinmaster on every save
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1550
diff changeset
64 tooold = time.time() - 1800
682
1314fdb74689 refactor CacheEntry usage to use scope param
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 477
diff changeset
65 failure = caching.CacheEntry(request, "antispam", "failure", scope='wiki')
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
66 fail_time = failure.mtime() # only update if no failure in last hour
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
67 if (mymtime < tooold) and (fail_time < tooold):
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
68 logging.info("%d *BadContent too old, have to check for an update..." % tooold)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
69 import xmlrpclib
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
70 import socket
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
71
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
72 timeout = 15 # time out for reaching the master server via xmlrpc
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
73 old_timeout = socket.getdefaulttimeout()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
74 socket.setdefaulttimeout(timeout)
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
75
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
76 master_url = request.cfg.antispam_master_url
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
77 master = xmlrpclib.ServerProxy(master_url)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
78 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
79 # Get BadContent info
4333
b52a579db72d fix crash in antispam code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3159
diff changeset
80 master.putClientInfo('ANTISPAM-CHECK', request.url)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
81 response = master.getPageInfo(pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
82
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
83 # It seems that response is always a dict
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
84 if isinstance(response, dict) and 'faultCode' in response:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
85 raise WikirpcError("failed to get BadContent information",
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
86 response)
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
87
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
88 # Compare date against local BadContent copy
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
89 masterdate = response['lastModified']
477
4e85cddd2db4 fixed antispam for python 2.5a xmlrpclib
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
90
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
91 if isinstance(masterdate, datetime.datetime):
1955
c872387196da refactor antispam: make master url configurable, remove now useless merging code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
92 # for python 2.5
477
4e85cddd2db4 fixed antispam for python 2.5a xmlrpclib
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
93 mydate = datetime.datetime(*tuple(time.gmtime(mymtime))[0:6])
4e85cddd2db4 fixed antispam for python 2.5a xmlrpclib
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
94 else:
4e85cddd2db4 fixed antispam for python 2.5a xmlrpclib
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
95 # for python <= 2.4.x
4e85cddd2db4 fixed antispam for python 2.5a xmlrpclib
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
96 mydate = xmlrpclib.DateTime(tuple(time.gmtime(mymtime)))
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
97
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
98 logging.debug("master: %s mine: %s" % (masterdate, mydate))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
99 if mydate < masterdate:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
100 # Get new copy and save
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
101 logging.info("Fetching page from %s..." % master_url)
4333
b52a579db72d fix crash in antispam code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3159
diff changeset
102 master.putClientInfo('ANTISPAM-FETCH', request.url)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
103 response = master.getPage(pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
104 if isinstance(response, dict) and 'faultCode' in response:
1955
c872387196da refactor antispam: make master url configurable, remove now useless merging code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
105 raise WikirpcError("failed to get BadContent data", response)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
106 p._write_file(response)
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
107 mymtime = wikiutil.version2timestamp(p.mtime_usecs())
1648
d9077dcd38e1 fix antispam polling moinmaster on every save
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1550
diff changeset
108 else:
d9077dcd38e1 fix antispam polling moinmaster on every save
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1550
diff changeset
109 failure.update("") # we didn't get a modified version, this avoids
d9077dcd38e1 fix antispam polling moinmaster on every save
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1550
diff changeset
110 # permanent polling for every save when there
d9077dcd38e1 fix antispam polling moinmaster on every save
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1550
diff changeset
111 # is no updated master page
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
112
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
113 except (socket.error, xmlrpclib.ProtocolError), err:
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
114 logging.error('Timeout / socket / protocol error when accessing %s: %s' % (master_url, str(err)))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
115 # update cache to wait before the next try
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
116 failure.update("")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
117
1762
ca08a865c597 antispam: catch xmlrpclib.Fault to avoid trouble with saving pages when MoinMaster wiki fails internally (ported from 1.5-798)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1648
diff changeset
118 except (xmlrpclib.Fault, ), err:
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
119 logging.error('Fault on %s: %s' % (master_url, str(err)))
1762
ca08a865c597 antispam: catch xmlrpclib.Fault to avoid trouble with saving pages when MoinMaster wiki fails internally (ported from 1.5-798)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1648
diff changeset
120 # update cache to wait before the next try
ca08a865c597 antispam: catch xmlrpclib.Fault to avoid trouble with saving pages when MoinMaster wiki fails internally (ported from 1.5-798)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1648
diff changeset
121 failure.update("")
ca08a865c597 antispam: catch xmlrpclib.Fault to avoid trouble with saving pages when MoinMaster wiki fails internally (ported from 1.5-798)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1648
diff changeset
122
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
123 except Error, err:
1955
c872387196da refactor antispam: make master url configurable, remove now useless merging code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
124 # In case of Error, we log the error and use the local BadContent copy.
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
125 logging.error(str(err))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
126
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
127 # set back socket timeout
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
128 socket.setdefaulttimeout(old_timeout)
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
129
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
130 blacklist = p.get_raw_body()
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
131 return mymtime, makelist(blacklist)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
132
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
133
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
134 class SecurityPolicy(Permissions):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
135 """ Extend the default security policy with antispam feature """
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
136
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
137 def save(self, editor, newtext, rev, **kw):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
138 BLACKLISTPAGES = ["BadContent", "LocalBadContent"]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
139 if not editor.page_name in BLACKLISTPAGES:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
140 request = editor.request
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
141
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
142 # Start timing of antispam operation
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
143 request.clock.start('antispam')
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
144
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
145 blacklist = []
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
146 latest_mtime = 0
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
147 for pn in BLACKLISTPAGES:
2979
b95840d1e675 antispam: MoinMaster wiki shall not fetch updates from itself (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2926
diff changeset
148 do_update = (pn != "LocalBadContent" and
b95840d1e675 antispam: MoinMaster wiki shall not fetch updates from itself (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2926
diff changeset
149 request.cfg.interwikiname != 'MoinMaster') # MoinMaster wiki shall not fetch updates from itself
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
150 blacklist_mtime, blacklist_entries = getblacklist(request, pn, do_update)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
151 blacklist += blacklist_entries
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
152 latest_mtime = max(latest_mtime, blacklist_mtime)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
153
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
154 if blacklist:
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
155 invalid_cache = not getattr(request.cfg.cache, "antispam_blacklist", None)
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
156 if invalid_cache or request.cfg.cache.antispam_blacklist[0] < latest_mtime:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
157 mmblcache = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
158 for blacklist_re in blacklist:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
159 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
160 mmblcache.append(re.compile(blacklist_re, re.I))
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
161 except re.error, err:
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
162 logging.error("Error in regex '%s': %s. Please check the pages %s." % (
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
163 blacklist_re,
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
164 str(err),
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
165 ', '.join(BLACKLISTPAGES)))
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
166 request.cfg.cache.antispam_blacklist = (latest_mtime, mmblcache)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
167
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
168 from MoinMoin.Page import Page
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
169
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
170 oldtext = ""
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
171 if rev > 0: # rev is the revision of the old page
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
172 page = Page(request, editor.page_name, rev=rev)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
173 oldtext = page.get_raw_body()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
174
2992
954c617d9f76 antispam: check edit comments also against BadContent, use builtin frozenset, if possible (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2988
diff changeset
175 newset = frozenset(newtext.splitlines(1))
954c617d9f76 antispam: check edit comments also against BadContent, use builtin frozenset, if possible (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2988
diff changeset
176 oldset = frozenset(oldtext.splitlines(1))
954c617d9f76 antispam: check edit comments also against BadContent, use builtin frozenset, if possible (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2988
diff changeset
177 difference = newset - oldset
954c617d9f76 antispam: check edit comments also against BadContent, use builtin frozenset, if possible (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2988
diff changeset
178 addedtext = kw.get('comment', u'') + u''.join(difference)
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
179
2209
ebdcd00ce19d fixed updating of in-process cache of antispam patterns (port from 1.5 repo)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1955
diff changeset
180 for blacklist_re in request.cfg.cache.antispam_blacklist[1]:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
181 match = blacklist_re.search(addedtext)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
182 if match:
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
183 # Log error and raise SaveError, PageEditor should handle this.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
184 _ = editor.request.getText
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
185 msg = _('Sorry, can not save page because "%(content)s" is not allowed in this wiki.') % {
4512
89b91bf87dad Fixed XSS issue in antispam
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3159
diff changeset
186 'content': wikiutil.escape(match.group())
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
187 }
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
188 logging.info(msg)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
189 raise editor.SaveError(msg)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
190 request.clock.stop('antispam')
994
862edd1566bb security package whitespace-only cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 746
diff changeset
191
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
192 # No problem to save if my base class agree
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
193 return Permissions.save(self, editor, newtext, rev, **kw)
3159
915a431b663c logging: security package refactored, moved frozenset to python_compatibility
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3110
diff changeset
194