Skip to content

Commit

Permalink
WSGIPublisher: set REMOTE_USER even in case of error
Browse files Browse the repository at this point in the history
This fixes a problem that user name was empty in access log for error
pages.

Fixes #1155
  • Loading branch information
perrinjerome committed Sep 19, 2023
1 parent 024e33c commit 4833c07
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
- Added image dimensions to SVG file properties
`#1146 <https://github.com/zopefoundation/Zope/pull/1146>`_.

- Fix username not in access log for error requests, see issue
`#1155 <https://github.com/zopefoundation/Zope/issues/1155>`_.

5.8.4 (2023-09-06)
------------------

Expand Down
13 changes: 7 additions & 6 deletions src/ZPublisher/WSGIPublisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,13 @@ def publish_module(environ, start_response,
try:
with load_app(module_info) as new_mod_info:
with transaction_pubevents(request, response):
response = _publish(request, new_mod_info)

user = getSecurityManager().getUser()
if user is not None and \
user.getUserName() != 'Anonymous User':
environ['REMOTE_USER'] = user.getUserName()
try:
response = _publish(request, new_mod_info)
finally:
user = getSecurityManager().getUser()
if user is not None and \
user.getUserName() != 'Anonymous User':
environ['REMOTE_USER'] = user.getUserName()
break
except TransientError:
if request.supports_retry():
Expand Down
9 changes: 9 additions & 0 deletions src/ZPublisher/tests/test_WSGIPublisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,15 @@ def test_set_REMOTE_USER_environ(self):
self._callFUT(environ, start_response, _publish)
self.assertFalse('REMOTE_USER' in environ)

def test_set_REMOTE_USER_environ_error(self):
environ = self._makeEnviron()
start_response = DummyCallable()
_publish = DummyCallable()
_publish._raise = ValueError()
with self.assertRaises(ValueError):
self._callFUT(environ, start_response, _publish)
self.assertEqual(environ['REMOTE_USER'], user_name)

def test_webdav_source_port(self):
from ZPublisher import WSGIPublisher
old_webdav_source_port = WSGIPublisher._WEBDAV_SOURCE_PORT
Expand Down

0 comments on commit 4833c07

Please sign in to comment.