fix: add missing format placeholders in log.error and log.debug calls#60
fix: add missing format placeholders in log.error and log.debug calls#60piyush140104 wants to merge 1 commit intoOWASP:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes incorrect Python logging calls in the ModSecurity log preprocessing script that previously caused TypeError formatting failures and suppressed useful log output.
Changes:
- Add missing
%splaceholders tolog.debug(...)calls inprocessLog. - Add missing
%splaceholder to alog.error(...)call in the file-write exception handler.
Comments suppressed due to low confidence (1)
honeytraps/waf_modsec/preprocess-modsec-log.py:106
linescan beNonewhen the original log file is missing (seegetLinesreturningNoneonFileNotFoundError), butprocessLogstill callslen(lines)in the loop. This will raise aTypeErrorand stop processing; consider treating missing files as an empty list or returning early with a log message whenlines is None.
log.debug("Write Mode: %s", writeMode)
# Log line prasing/modification
newLines = []
for i in range(index, len(lines)):
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| file.write(line + "\n") | ||
| #log.info(newLines) | ||
| file.close() | ||
| except Exception as e: | ||
| log.error("Error writing to file", e) | ||
| log.error("Error writing to file: %s", e) |
There was a problem hiding this comment.
The file handle may be left open if an exception occurs during file.write(...) (since file.close() is only reached on the success path). Using a context manager (with open(...) as f:) avoids leaks and also simplifies the code; also consider log.exception(...) or log.error(..., exc_info=True) here so the traceback is captured for operational debugging.
What
Added missing %s placeholders in 5 logging calls in honeytraps/waf_modsec/preprocess-modsec-log.py.
Fix
log.debug("Processed file lines: %s", procIndex) # line 95
log.debug("Original file lines: %s", origIndex) # line 97
log.debug("Head: %s", index) # line 101
log.debug("Write Mode: %s", writeMode) # line 102
log.error("Error writing to file: %s", e) # line 117
Reviewers
@adrianwinckles
Test
import logging
logging.basicConfig(level=logging.ERROR)
log = logging.getLogger('test')
log.error("Error writing to file", Exception("disk full"))
#Before: triggers --- Logging error --- TypeError, actual error never logged
log.error("Error writing to file: %s", Exception("disk full"))
#After: ERROR:test:Error writing to file: disk full
Closes #59