Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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)
----------------
Expand Down
6 changes: 4 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 15 additions & 6 deletions src/pyramid_exclog/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,7 +51,9 @@ def _get_url(request):

%(params)s

"""

_USER_TEMPLATE = """
UNAUTHENTICATED USER

%(usr)s
Expand Down Expand Up @@ -88,7 +91,6 @@ def _get_message(request):

"""
url = _get_url(request)
unauth = request.unauthenticated_userid

try:
params = request.params
Expand All @@ -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=()):
Expand Down
45 changes: 37 additions & 8 deletions tests/test_it.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pyramid import testing
from pyramid.interfaces import IAuthenticationPolicy
import sys
import unittest

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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'
Expand All @@ -215,15 +230,15 @@ 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)

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)
Expand Down Expand Up @@ -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
)
Loading