From 56a7df84e3864733620a051fb1cdb7540dba9d54 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Tue, 24 Nov 2026 17:20:14 -0700 Subject: [PATCH] stop accessing request.unauthenticated_userid if a legacy policy is not registered --- CHANGES.rst | 5 ++++ docs/index.rst | 6 +++-- src/pyramid_exclog/__init__.py | 21 +++++++++++----- tests/test_it.py | 45 ++++++++++++++++++++++++++++------ 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index c91b76d..562a8c5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,11 @@ unreleased ---------- +- Stop using the deprecated ``request.unauthenticated_userid``. The + unauthenticated userid is now read from the authentication policy directly. + When no authentication policy is configured, including when a Pyramid 2.0 + security policy is used, the ``UNAUTHENTICATED USER`` section is omitted from + the ``exclog.extra_info`` output instead of logging ``None``. 1.2 (2026-11-21) ---------------- diff --git a/docs/index.rst b/docs/index.rst index be247f5..e9642a8 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -321,8 +321,10 @@ section of your Pyramid's ``.ini`` file. These are: If ``exclog.extra_info`` is true the error message will also include the environ and params attributes of ``pyramid.request.Request`` formatted - using ``pprint.pformat()``. The output from - ``pyramid.security.unauthenticated_id()`` is also included. + using ``pprint.pformat()``. If an :term:`authentication policy` is + configured, its unauthenticated userid is also included. Pyramid 2.0 + security policies do not have an unauthenticated userid, so it is omitted + when one is used. This setting defaults to false diff --git a/src/pyramid_exclog/__init__.py b/src/pyramid_exclog/__init__.py index af58a3a..ca94727 100644 --- a/src/pyramid_exclog/__init__.py +++ b/src/pyramid_exclog/__init__.py @@ -1,6 +1,7 @@ import builtins from pprint import pformat from pyramid.httpexceptions import WSGIHTTPException +from pyramid.interfaces import IAuthenticationPolicy from pyramid.path import DottedNameResolver from pyramid.settings import asbool, aslist import pyramid.tweens @@ -50,7 +51,9 @@ def _get_url(request): %(params)s +""" +_USER_TEMPLATE = """ UNAUTHENTICATED USER %(usr)s @@ -88,7 +91,6 @@ def _get_message(request): """ url = _get_url(request) - unauth = request.unauthenticated_userid try: params = request.params @@ -97,16 +99,23 @@ def _get_message(request): except IOError as ex: params = 'IOError while decoding params: %s' % ex - if not isinstance(unauth, str): - unauth = repr(unauth) - - return _MESSAGE_TEMPLATE % dict( + message = _MESSAGE_TEMPLATE % dict( url=url, env=pformat(request.environ), params=pformat(params), - usr=unauth, ) + # request.unauthenticated_userid is deprecated in pyramid 2.0, so ask the + # legacy authentication policy directly and omit the user if there is none + authn_policy = request.registry.queryUtility(IAuthenticationPolicy) + if authn_policy is not None: + unauth = authn_policy.unauthenticated_userid(request) + if not isinstance(unauth, str): + unauth = repr(unauth) + message += _USER_TEMPLATE % dict(usr=unauth) + + return message + class ErrorHandler(object): def __init__(self, ignored, getLogger, get_message, hidden_cookies=()): diff --git a/tests/test_it.py b/tests/test_it.py index c429ae0..62563ac 100644 --- a/tests/test_it.py +++ b/tests/test_it.py @@ -1,4 +1,5 @@ from pyramid import testing +from pyramid.interfaces import IAuthenticationPolicy import sys import unittest @@ -109,20 +110,36 @@ def test_hidden_cookies(self): self.assertFalse('test_cookie_value' in msg) def test_user_info_user(self): - self.config.testing_securitypolicy(userid='hank', permissive=True) + _register_authn_policy(self.config, 'hank') self.registry.settings['exclog.extra_info'] = True self.assertRaises(NotImplementedError, self._callFUT) self.assertEqual(len(self.logger.exceptions), 1) msg = self.logger.exceptions[0] self.assertTrue('UNAUTHENTICATED USER\n\nhank' in msg, msg) - def test_user_info_no_user(self): + def test_user_info_security_policy(self): + # pyramid 2.0 security policies have no unauthenticated userid + self.config.testing_securitypolicy(userid='hank', permissive=True) + self.config.registry.unregisterUtility(provided=IAuthenticationPolicy) + self.registry.settings['exclog.extra_info'] = True + self.assertRaises(NotImplementedError, self._callFUT) + msg = self.logger.exceptions[0] + self.assertFalse('UNAUTHENTICATED USER' in msg, msg) + + def test_user_info_anonymous(self): + _register_authn_policy(self.config, None) self.registry.settings['exclog.extra_info'] = True self.assertRaises(NotImplementedError, self._callFUT) - self.assertEqual(len(self.logger.exceptions), 1) msg = self.logger.exceptions[0] self.assertTrue('UNAUTHENTICATED USER\n\nNone\n' in msg, msg) + def test_user_info_no_authn_policy(self): + self.registry.settings['exclog.extra_info'] = True + self.assertRaises(NotImplementedError, self._callFUT) + self.assertEqual(len(self.logger.exceptions), 1) + msg = self.logger.exceptions[0] + self.assertFalse('UNAUTHENTICATED USER' in msg, msg) + def test_exception_while_logging(self): from pyramid.request import Request @@ -203,9 +220,7 @@ def test_unicode_user_id_with_non_utf_8_url(self): # On Python 2 we may get a unicode userid while QUERY_STRING is a "str" # object containing non-ascii bytes. with testing.testConfig() as config: - config.testing_securitypolicy( - userid=b'\xe6\xbc\xa2'.decode('utf-8') - ) + _register_authn_policy(config, b'\xe6\xbc\xa2'.decode('utf-8')) request = _request_factory('/') request.environ['PATH_INFO'] = '/url' request.environ['QUERY_STRING'] = '\xfa=\xfa' @@ -215,7 +230,7 @@ def test_unicode_user_id_with_non_utf_8_url(self): def test_non_ascii_bytes_in_userid(self): byte_str = b'\xe6\xbc\xa2' with testing.testConfig() as config: - config.testing_securitypolicy(userid=byte_str) + _register_authn_policy(config, byte_str) request = _request_factory('/') msg = self._callFUT(request) self.assertTrue(repr(byte_str) in msg, msg) @@ -223,7 +238,7 @@ def test_non_ascii_bytes_in_userid(self): def test_integer_user_id(self): # userids can apparently be integers as well with testing.testConfig() as config: - config.testing_securitypolicy(userid=42) + _register_authn_policy(config, 42) request = _request_factory('/') msg = self._callFUT(request) self.assertTrue('42' in msg) @@ -364,3 +379,17 @@ def _request_factory(*args, **kwargs): request.registry = get_current_registry() return request + + +class DummyAuthenticationPolicy: + def __init__(self, userid): + self.userid = userid + + def unauthenticated_userid(self, request): + return self.userid + + +def _register_authn_policy(config, userid): + config.registry.registerUtility( + DummyAuthenticationPolicy(userid), IAuthenticationPolicy + )