Blame SOURCES/mailman-CVE-2021-42096-2021-42097.patch

c633ef
diff --git a/Mailman/CSRFcheck.py b/Mailman/CSRFcheck.py
c633ef
index a1e78d9..24e3e11 100644
c633ef
--- a/Mailman/CSRFcheck.py
c633ef
+++ b/Mailman/CSRFcheck.py
c633ef
@@ -18,11 +18,13 @@
c633ef
 """ Cross-Site Request Forgery checker """
c633ef
 
c633ef
 import time
c633ef
+import urllib
c633ef
 import marshal
c633ef
 import binascii
c633ef
 
c633ef
 from Mailman import mm_cfg
c633ef
-from Mailman.Utils import sha_new
c633ef
+from Mailman.Logging.Syslog import syslog
c633ef
+from Mailman.Utils import UnobscureEmail, sha_new
c633ef
 
c633ef
 keydict = {
c633ef
     'user':      mm_cfg.AuthUser,
c633ef
@@ -37,6 +39,10 @@ keydict = {
c633ef
 def csrf_token(mlist, contexts, user=None):
c633ef
     """ create token by mailman cookie generation algorithm """
c633ef
 
c633ef
+    if user:
c633ef
+        # Unmunge a munged email address.
c633ef
+        user = UnobscureEmail(urllib.unquote(user))
c633ef
+        
c633ef
     for context in contexts:
c633ef
         key, secret = mlist.AuthContextInfo(context, user)
c633ef
         if key:
c633ef
@@ -49,9 +55,8 @@ def csrf_token(mlist, contexts, user=None):
c633ef
     token = binascii.hexlify(marshal.dumps((issued, keymac)))
c633ef
     return token
c633ef
 
c633ef
-def csrf_check(mlist, token):
c633ef
+def csrf_check(mlist, token, options_user=None):
c633ef
     """ check token by mailman cookie validation algorithm """
c633ef
-
c633ef
     try:
c633ef
         issued, keymac = marshal.loads(binascii.unhexlify(token))
c633ef
         key, received_mac = keymac.split(':', 1)
c633ef
@@ -62,6 +67,17 @@ def csrf_check(mlist, token):
c633ef
             key, user = key.split('+', 1)
c633ef
         else:
c633ef
             user = None
c633ef
+        if user:
c633ef
+            # This is for CVE-2021-42097.  The token is a user token because
c633ef
+            # of the fix for CVE-2021-42096 but it must match the user for
c633ef
+            # whom the options page is requested.
c633ef
+            raw_user = UnobscureEmail(urllib.unquote(user))
c633ef
+            if options_user and options_user != raw_user:
c633ef
+                syslog('mischief',
c633ef
+                       'Form for user %s submitted with CSRF token '
c633ef
+                       'issued for %s.',
c633ef
+                       options_user, raw_user)
c633ef
+                return False
c633ef
         context = keydict.get(key)
c633ef
         key, secret = mlist.AuthContextInfo(context, user)
c633ef
         assert key
c633ef
diff --git a/Mailman/Cgi/options.py b/Mailman/Cgi/options.py
c633ef
index 386b308..980fc09 100644
c633ef
--- a/Mailman/Cgi/options.py
c633ef
+++ b/Mailman/Cgi/options.py
c633ef
@@ -54,9 +54,6 @@ except NameError:
c633ef
     True = 1
c633ef
     False = 0
c633ef
 
c633ef
-AUTH_CONTEXTS = (mm_cfg.AuthListAdmin, mm_cfg.AuthSiteAdmin,
c633ef
-                 mm_cfg.AuthListModerator, mm_cfg.AuthUser)
c633ef
-
c633ef
 
c633ef
 def main():
c633ef
     global _
c633ef
@@ -124,15 +121,6 @@ def main():
c633ef
         print doc.Format()
c633ef
         return
c633ef
 
c633ef
-    if set(params) - set(safe_params):
c633ef
-        csrf_checked = csrf_check(mlist, cgidata.getfirst('csrf_token'))
c633ef
-    else:
c633ef
-        csrf_checked = True
c633ef
-    # if password is present, void cookie to force password authentication.
c633ef
-    if cgidata.getfirst('password'):
c633ef
-        os.environ['HTTP_COOKIE'] = ''
c633ef
-        csrf_checked = True
c633ef
-
c633ef
     # Set the language for the page.  If we're coming from the listinfo cgi,
c633ef
     # we might have a 'language' key in the cgi data.  That was an explicit
c633ef
     # preference to view the page in, so we should honor that here.  If that's
c633ef
@@ -168,6 +156,16 @@ def main():
c633ef
             user = user[-1]
c633ef
 
c633ef
     # Avoid cross-site scripting attacks
c633ef
+    if set(params) - set(safe_params):
c633ef
+        csrf_checked = csrf_check(mlist, cgidata.getfirst('csrf_token'),
c633ef
+                                  Utils.UnobscureEmail(urllib.unquote(user)))
c633ef
+    else:
c633ef
+        csrf_checked = True
c633ef
+    # if password is present, void cookie to force password authentication.
c633ef
+    if cgidata.getfirst('password'):
c633ef
+        os.environ['HTTP_COOKIE'] = ''
c633ef
+        csrf_checked = True
c633ef
+
c633ef
     safeuser = Utils.websafe(user)
c633ef
     try:
c633ef
         Utils.ValidateEmail(user)
c633ef
@@ -867,8 +865,9 @@ def options_page(mlist, doc, user, cpuser, userlang, message=''):
c633ef
         mlist.FormatButton('othersubs',
c633ef
                            _('List my other subscriptions')))
c633ef
     replacements['<mm-form-start>'] = (
c633ef
+        # Always make the CSRF token for the user. CVE-2021-42096
c633ef
         mlist.FormatFormStart('options', user, mlist=mlist, 
c633ef
-            contexts=AUTH_CONTEXTS, user=user))
c633ef
+            contexts=[mm_cfg.AuthUser], user=user))
c633ef
     replacements['<mm-user>'] = user
c633ef
     replacements['<mm-presentable-user>'] = presentable_user
c633ef
     replacements['<mm-email-my-pw>'] = mlist.FormatButton(
c633ef
diff --git a/Mailman/SecurityManager.py b/Mailman/SecurityManager.py
c633ef
index 9b7f03f..e9e5ce5 100644
c633ef
--- a/Mailman/SecurityManager.py
c633ef
+++ b/Mailman/SecurityManager.py
c633ef
@@ -104,6 +104,7 @@ class SecurityManager:
c633ef
             if user is None:
c633ef
                 # A bad system error
c633ef
                 raise TypeError, 'No user supplied for AuthUser context'
c633ef
+            user = Utils.UnobscureEmail(urllib.unquote(user))
c633ef
             secret = self.getMemberPassword(user)
c633ef
             userdata = urllib.quote(Utils.ObscureEmail(user), safe='')
c633ef
             key += 'user+%s' % userdata