ac942c
From 7bd6835819d7d6b158f307939da1e618f860c42a Mon Sep 17 00:00:00 2001
ac942c
From: Florence Blanc-Renaud <flo@redhat.com>
ac942c
Date: Tue, 21 Nov 2023 09:49:14 +0100
ac942c
Subject: [PATCH] Integration tests for verifying Referer header in the UI
ac942c
ac942c
Validate that the change_password and login_password endpoints
ac942c
verify the HTTP Referer header. There is some overlap in the
ac942c
tests: belt and suspenders.
ac942c
ac942c
All endpoints except session/login_x509 are covered, sometimes
ac942c
having to rely on expected bad results (see the i18n endpoint).
ac942c
ac942c
session/login_x509 is not tested yet as it requires significant
ac942c
additional setup in order to associate a user certificate with
ac942c
a user entry, etc.
ac942c
ac942c
This can be manually verified by modifying /etc/httpd/conf.d/ipa.conf
ac942c
and adding:
ac942c
ac942c
Satisfy Any
ac942c
Require all granted
ac942c
ac942c
Then comment out Auth and SSLVerify, etc. and restart httpd.
ac942c
ac942c
With a valid Referer will fail with a 401 and log that there is no
ac942c
KRB5CCNAME. This comes after the referer check.
ac942c
ac942c
With an invalid Referer it will fail with a 400 Bad Request as
ac942c
expected.
ac942c
ac942c
CVE-2023-5455
ac942c
ac942c
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
ac942c
---
ac942c
 ipatests/test_ipaserver/httptest.py           |   7 +-
ac942c
 ipatests/test_ipaserver/test_changepw.py      |  12 +-
ac942c
 .../test_ipaserver/test_login_password.py     |  88 ++++++++++++
ac942c
 ipatests/test_ipaserver/test_referer.py       | 136 ++++++++++++++++++
ac942c
 ipatests/util.py                              |   4 +-
ac942c
 5 files changed, 242 insertions(+), 5 deletions(-)
ac942c
 create mode 100644 ipatests/test_ipaserver/test_login_password.py
ac942c
 create mode 100644 ipatests/test_ipaserver/test_referer.py
ac942c
ac942c
diff --git a/ipatests/test_ipaserver/httptest.py b/ipatests/test_ipaserver/httptest.py
ac942c
index 13003bc85c2e1fee5cb78e1c9fc50d39e6e88eb1..01c2abae890af6cc3e36fbfec25f6b4665865c87 100644
ac942c
--- a/ipatests/test_ipaserver/httptest.py
ac942c
+++ b/ipatests/test_ipaserver/httptest.py
ac942c
@@ -34,7 +34,7 @@ class Unauthorized_HTTP_test(object):
ac942c
     cacert = api.env.tls_ca_cert
ac942c
     content_type = 'application/x-www-form-urlencoded'
ac942c
 
ac942c
-    def send_request(self, method='POST', params=None):
ac942c
+    def send_request(self, method='POST', params=None, host=None):
ac942c
         """
ac942c
         Send a request to HTTP server
ac942c
 
ac942c
@@ -44,7 +44,10 @@ class Unauthorized_HTTP_test(object):
ac942c
             # urlencode *can* take two arguments
ac942c
             # pylint: disable=too-many-function-args
ac942c
             params = urllib.parse.urlencode(params, True)
ac942c
-        url = 'https://' + self.host + self.app_uri
ac942c
+        if host:
ac942c
+            url = 'https://' + host + self.app_uri
ac942c
+        else:
ac942c
+            url = 'https://' + self.host + self.app_uri
ac942c
 
ac942c
         headers = {'Content-Type' : self.content_type,
ac942c
                    'Referer' : url}
ac942c
diff --git a/ipatests/test_ipaserver/test_changepw.py b/ipatests/test_ipaserver/test_changepw.py
ac942c
index 3f8331266889a209b62f97a0eed40c51f7aa3475..64a2aa533a31b0d655a0028a2e008e8559f9b4cd 100644
ac942c
--- a/ipatests/test_ipaserver/test_changepw.py
ac942c
+++ b/ipatests/test_ipaserver/test_changepw.py
ac942c
@@ -52,10 +52,11 @@ class test_changepw(XMLRPC_test, Unauthorized_HTTP_test):
ac942c
         except errors.NotFound:
ac942c
             pass
ac942c
 
ac942c
-    def _changepw(self, user, old_password, new_password):
ac942c
+    def _changepw(self, user, old_password, new_password, host=None):
ac942c
         return self.send_request(params={'user': str(user),
ac942c
                                   'old_password' : str(old_password),
ac942c
                                   'new_password' : str(new_password)},
ac942c
+                                 host=host
ac942c
                                  )
ac942c
 
ac942c
     def _checkpw(self, user, password):
ac942c
@@ -88,6 +89,15 @@ class test_changepw(XMLRPC_test, Unauthorized_HTTP_test):
ac942c
         # make sure that password is NOT changed
ac942c
         self._checkpw(testuser, old_password)
ac942c
 
ac942c
+    def test_invalid_referer(self):
ac942c
+        response = self._changepw(testuser, old_password, new_password,
ac942c
+                                  'attacker.test')
ac942c
+
ac942c
+        assert_equal(response.status, 400)
ac942c
+
ac942c
+        # make sure that password is NOT changed
ac942c
+        self._checkpw(testuser, old_password)
ac942c
+
ac942c
     def test_pwpolicy_error(self):
ac942c
         response = self._changepw(testuser, old_password, '1')
ac942c
 
ac942c
diff --git a/ipatests/test_ipaserver/test_login_password.py b/ipatests/test_ipaserver/test_login_password.py
ac942c
new file mode 100644
ac942c
index 0000000000000000000000000000000000000000..9425cb7977fbc87210bf91464e0257830a938baf
ac942c
--- /dev/null
ac942c
+++ b/ipatests/test_ipaserver/test_login_password.py
ac942c
@@ -0,0 +1,88 @@
ac942c
+# Copyright (C) 2023  Red Hat
ac942c
+# see file 'COPYING' for use and warranty information
ac942c
+#
ac942c
+# This program is free software; you can redistribute it and/or modify
ac942c
+# it under the terms of the GNU General Public License as published by
ac942c
+# the Free Software Foundation, either version 3 of the License, or
ac942c
+# (at your option) any later version.
ac942c
+#
ac942c
+# This program is distributed in the hope that it will be useful,
ac942c
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ac942c
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ac942c
+# GNU General Public License for more details.
ac942c
+#
ac942c
+# You should have received a copy of the GNU General Public License
ac942c
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ac942c
+
ac942c
+import os
ac942c
+import pytest
ac942c
+import uuid
ac942c
+
ac942c
+from ipatests.test_ipaserver.httptest import Unauthorized_HTTP_test
ac942c
+from ipatests.test_xmlrpc.xmlrpc_test import XMLRPC_test
ac942c
+from ipatests.util import assert_equal
ac942c
+from ipalib import api, errors
ac942c
+from ipapython.ipautil import run
ac942c
+
ac942c
+testuser = u'tuser'
ac942c
+password = u'password'
ac942c
+
ac942c
+
ac942c
+@pytest.mark.tier1
ac942c
+class test_login_password(XMLRPC_test, Unauthorized_HTTP_test):
ac942c
+    app_uri = '/ipa/session/login_password'
ac942c
+
ac942c
+    @pytest.fixture(autouse=True)
ac942c
+    def login_setup(self, request):
ac942c
+        ccache = os.path.join('/tmp', str(uuid.uuid4()))
ac942c
+        try:
ac942c
+            api.Command['user_add'](uid=testuser, givenname=u'Test', sn=u'User')
ac942c
+            api.Command['passwd'](testuser, password=password)
ac942c
+            run(['kinit', testuser], stdin='{0}\n{0}\n{0}\n'.format(password),
ac942c
+                env={"KRB5CCNAME": ccache})
ac942c
+        except errors.ExecutionError as e:
ac942c
+            pytest.skip(
ac942c
+                'Cannot set up test user: %s' % e
ac942c
+            )
ac942c
+
ac942c
+        def fin():
ac942c
+            try:
ac942c
+                api.Command['user_del']([testuser])
ac942c
+            except errors.NotFound:
ac942c
+                pass
ac942c
+            os.unlink(ccache)
ac942c
+
ac942c
+        request.addfinalizer(fin)
ac942c
+
ac942c
+    def _login(self, user, password, host=None):
ac942c
+        return self.send_request(params={'user': str(user),
ac942c
+                                 'password' : str(password)},
ac942c
+                                 host=host)
ac942c
+
ac942c
+    def test_bad_options(self):
ac942c
+        for params in (
ac942c
+            None,                             # no params
ac942c
+            {"user": "foo"},                  # missing options
ac942c
+            {"user": "foo", "password": ""},  # empty option
ac942c
+        ):
ac942c
+            response = self.send_request(params=params)
ac942c
+            assert_equal(response.status, 400)
ac942c
+            assert_equal(response.reason, 'Bad Request')
ac942c
+
ac942c
+    def test_invalid_auth(self):
ac942c
+        response = self._login(testuser, 'wrongpassword')
ac942c
+
ac942c
+        assert_equal(response.status, 401)
ac942c
+        assert_equal(response.getheader('X-IPA-Rejection-Reason'),
ac942c
+                     'invalid-password')
ac942c
+
ac942c
+    def test_invalid_referer(self):
ac942c
+        response = self._login(testuser, password, 'attacker.test')
ac942c
+
ac942c
+        assert_equal(response.status, 400)
ac942c
+
ac942c
+    def test_success(self):
ac942c
+        response = self._login(testuser, password)
ac942c
+
ac942c
+        assert_equal(response.status, 200)
ac942c
+        assert response.getheader('X-IPA-Rejection-Reason') is None
ac942c
diff --git a/ipatests/test_ipaserver/test_referer.py b/ipatests/test_ipaserver/test_referer.py
ac942c
new file mode 100644
ac942c
index 0000000000000000000000000000000000000000..4eade8bbaf304c48bf71c16892858d899b43cf88
ac942c
--- /dev/null
ac942c
+++ b/ipatests/test_ipaserver/test_referer.py
ac942c
@@ -0,0 +1,128 @@
ac942c
+# Copyright (C) 2023  Red Hat
ac942c
+# see file 'COPYING' for use and warranty information
ac942c
+#
ac942c
+# This program is free software; you can redistribute it and/or modify
ac942c
+# it under the terms of the GNU General Public License as published by
ac942c
+# the Free Software Foundation, either version 3 of the License, or
ac942c
+# (at your option) any later version.
ac942c
+#
ac942c
+# This program is distributed in the hope that it will be useful,
ac942c
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ac942c
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ac942c
+# GNU General Public License for more details.
ac942c
+#
ac942c
+# You should have received a copy of the GNU General Public License
ac942c
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ac942c
+
ac942c
+import os
ac942c
+import pytest
ac942c
+import uuid
ac942c
+
ac942c
+from ipatests.test_ipaserver.httptest import Unauthorized_HTTP_test
ac942c
+from ipatests.test_xmlrpc.xmlrpc_test import XMLRPC_test
ac942c
+from ipatests.util import assert_equal
ac942c
+from ipalib import api, errors
ac942c
+from ipapython.ipautil import run
ac942c
+
ac942c
+testuser = u'tuser'
ac942c
+password = u'password'
ac942c
+
ac942c
+
ac942c
+@pytest.mark.tier1
ac942c
+class test_referer(XMLRPC_test, Unauthorized_HTTP_test):
ac942c
+
ac942c
+    @pytest.fixture(autouse=True)
ac942c
+    def login_setup(self, request):
ac942c
+        ccache = os.path.join('/tmp', str(uuid.uuid4()))
ac942c
+        tokenid = None
ac942c
+        try:
ac942c
+            api.Command['user_add'](uid=testuser, givenname=u'Test', sn=u'User')
ac942c
+            api.Command['passwd'](testuser, password=password)
ac942c
+            run(['kinit', testuser], stdin='{0}\n{0}\n{0}\n'.format(password),
ac942c
+                env={"KRB5CCNAME": ccache})
ac942c
+            result = api.Command["otptoken_add"](
ac942c
+                type=u'HOTP', description=u'testotp',
ac942c
+                ipatokenotpalgorithm=u'sha512', ipatokenowner=testuser,
ac942c
+                ipatokenotpdigits=u'6')
ac942c
+            tokenid = result['result']['ipatokenuniqueid'][0]
ac942c
+        except errors.ExecutionError as e:
ac942c
+            pytest.skip(
ac942c
+                'Cannot set up test user: %s' % e
ac942c
+            )
ac942c
+
ac942c
+        def fin():
ac942c
+            try:
ac942c
+                api.Command['user_del']([testuser])
ac942c
+                api.Command['otptoken_del']([tokenid])
ac942c
+            except errors.NotFound:
ac942c
+                pass
ac942c
+            os.unlink(ccache)
ac942c
+
ac942c
+        request.addfinalizer(fin)
ac942c
+
ac942c
+    def _request(self, params={}, host=None):
ac942c
+        # implicit is that self.app_uri is set to the appropriate value
ac942c
+        return self.send_request(params=params, host=host)
ac942c
+
ac942c
+    def test_login_password_valid(self):
ac942c
+        """Valid authentication of a user"""
ac942c
+        self.app_uri = "/ipa/session/login_password"
ac942c
+        response = self._request(
ac942c
+            params={'user': 'tuser', 'password': password})
ac942c
+        assert_equal(response.status, 200, self.app_uri)
ac942c
+
ac942c
+    def test_change_password_valid(self):
ac942c
+        """This actually changes the user password"""
ac942c
+        self.app_uri = "/ipa/session/change_password"
ac942c
+        response = self._request(
ac942c
+            params={'user': 'tuser',
ac942c
+                    'old_password': password,
ac942c
+                    'new_password': 'new_password'}
ac942c
+        )
ac942c
+        assert_equal(response.status, 200, self.app_uri)
ac942c
+
ac942c
+    def test_sync_token_valid(self):
ac942c
+        """We aren't testing that sync works, just that we can get there"""
ac942c
+        self.app_uri = "/ipa/session/sync_token"
ac942c
+        response = self._request(
ac942c
+            params={'user': 'tuser',
ac942c
+                    'first_code': '1234',
ac942c
+                    'second_code': '5678',
ac942c
+                    'password': 'password'})
ac942c
+        assert_equal(response.status, 200, self.app_uri)
ac942c
+
ac942c
+    # /ipa/session/login_x509 is not tested yet as it requires
ac942c
+    # significant additional setup.
ac942c
+    # This can be manually verified by adding
ac942c
+    # Satisfy Any and Require all granted to the configuration
ac942c
+    # section and comment out all Auth directives. The request
ac942c
+    # will fail and log that there is no KRB5CCNAME which comes
ac942c
+    # after the referer check.
ac942c
+
ac942c
+    def test_endpoints_auth_required(self):
ac942c
+        """Test endpoints that require pre-authorization which will
ac942c
+           fail before we even get to the Referer check
ac942c
+        """
ac942c
+        self.endpoints = {
ac942c
+            "/ipa/xml",
ac942c
+            "/ipa/session/login_kerberos",
ac942c
+            "/ipa/session/json",
ac942c
+            "/ipa/session/xml"
ac942c
+        }
ac942c
+        for self.app_uri in self.endpoints:
ac942c
+            response = self._request(host="attacker.test")
ac942c
+
ac942c
+            # referer is checked after auth
ac942c
+            assert_equal(response.status, 401, self.app_uri)
ac942c
+
ac942c
+    def notest_endpoints_invalid(self):
ac942c
+        """Pass in a bad Referer, expect a 400 Bad Request"""
ac942c
+        self.endpoints = {
ac942c
+            "/ipa/session/login_password",
ac942c
+            "/ipa/session/change_password",
ac942c
+            "/ipa/session/sync_token",
ac942c
+        }
ac942c
+        for self.app_uri in self.endpoints:
ac942c
+            response = self._request(host="attacker.test")
ac942c
+
ac942c
+            assert_equal(response.status, 400, self.app_uri)
ac942c
diff --git a/ipatests/util.py b/ipatests/util.py
ac942c
index 6d1e78260089e65d5d2dd44a5a312021c2fdd781..bde93b6c5c96c2f996462683036dbbb549f914f2 100644
ac942c
--- a/ipatests/util.py
ac942c
+++ b/ipatests/util.py
ac942c
@@ -169,12 +169,12 @@ class ExceptionNotRaised(Exception):
ac942c
         return self.msg % self.expected.__name__
ac942c
 
ac942c
 
ac942c
-def assert_equal(val1, val2):
ac942c
+def assert_equal(val1, val2, msg=''):
ac942c
     """
ac942c
     Assert ``val1`` and ``val2`` are the same type and of equal value.
ac942c
     """
ac942c
     assert type(val1) is type(val2), '%r != %r' % (val1, val2)
ac942c
-    assert val1 == val2, '%r != %r' % (val1, val2)
ac942c
+    assert val1 == val2, '%r != %r %r' % (val1, val2, msg)
ac942c
 
ac942c
 
ac942c
 def assert_not_equal(val1, val2):
ac942c
-- 
ac942c
2.26.3
ac942c