You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The example for the InterceptLogger has a small issue when a library formats a string into something containing curly braces and you add a keyword argument to the logging call.
To demonstrate, I made these this change to test_interception.py:
to demonstrate the issue it caused. The test fails with a KeyError. To be clear, this test will pass without the change to the InterceptLogger above, because formatting is only triggered when args or kwargs are passed, see here:
I originally wanted to submit a PR for this, but I'm hesitant because the actual current code (as shown in the example) works, so it doesn't really need to be fixed. The bug is only triggered when one tries to extend it, and even in that case, it happens only when a library originally used curly braces in its formatted messages. In my case, this got triggered when running parse with the DEBUG logging level.
So, what am I to do? Should I submit a PR that changes the example in the README and the test? Or is this basically a non-issue? I did consider potential security implications of this, because user-generated input can make it into the message, which can cause information leaks when the result is returned to the user, but I haven't looked into actually creating a PoC, and for that to actually be harmful you'd need to return your logs to the user, which is probably a bad idea anyway.
The text was updated successfully, but these errors were encountered:
Hey @iFreilicht. Thank you for sharing these details.
Indeed, the problem is that when there are additional arguments such as your logger_name, the log() function will try to format them within the message. But since you do not control the base message, this can cause unexpected errors. This is a problem quite similar to : Why logging a message with f-string sometimes raises an exception?
We could escape the braces present in the message as you suggested, but that would only be relevant when additional arguments (such as logger_name) are provided to the log() method. For the default case, since not everyone needs logger_name to appear in the "extra" dict, it would cause undesirable brace duplication.
I'm wondering, though: do you actually need the logger_name to be passed as a formatting argument? Could you maybe use bind() instead, which would allow you to save the logger_name as an attribute of the extra dict without it interfering with formatting?
For example:
classInterceptHandler(logging.Handler):
defemit(self, record):
# Get corresponding Loguru level if it exists.try:
level=logger.level(record.levelname).nameexceptValueError:
level=record.levelno# Find caller from where originated the logged message.frame, depth=inspect.currentframe(), 0whileframe:
filename=frame.f_code.co_filenameis_logging=filename==logging.__file__is_frozen="importlib"infilenameand"_bootstrap"infilenameifdepth>0andnot (is_loggingoris_frozen):
breakframe=frame.f_backdepth+=1logger.bind(logger_name=record.name).opt(depth=depth, exception=record.exc_info).log(
level, record.getMessage()
)
The example for the
InterceptLogger
has a small issue when a library formats a string into something containing curly braces and you add a keyword argument to the logging call.To demonstrate, I made these this change to
test_interception.py
:Which is something we did in our codebase.
And added this test
to demonstrate the issue it caused. The test fails with a
KeyError
. To be clear, this test will pass without the change to theInterceptLogger
above, because formatting is only triggered when args or kwargs are passed, see here:loguru/loguru/_logger.py
Lines 2139 to 2141 in 0a0985b
Alternatively, to repro the issue, you can use this patch, which also contains the fix:
I originally wanted to submit a PR for this, but I'm hesitant because the actual current code (as shown in the example) works, so it doesn't really need to be fixed. The bug is only triggered when one tries to extend it, and even in that case, it happens only when a library originally used curly braces in its formatted messages. In my case, this got triggered when running parse with the DEBUG logging level.
So, what am I to do? Should I submit a PR that changes the example in the README and the test? Or is this basically a non-issue? I did consider potential security implications of this, because user-generated input can make it into the
message
, which can cause information leaks when the result is returned to the user, but I haven't looked into actually creating a PoC, and for that to actually be harmful you'd need to return your logs to the user, which is probably a bad idea anyway.The text was updated successfully, but these errors were encountered: