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

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