Skip to content

Commit f3b8cdd

Browse files
committed
prevent log forging, fix #1
1 parent 1e9a6c3 commit f3b8cdd

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55
The format is based on [Keep a Changelog](http://keepachangelog.com/).
66

7+
## 1.0.1 - 2019-07-20
8+
- prevent log forging, fix #1
9+
710
## 1.0.0 - 2019-07-20
811
Breaking change:
912
- add more specific init method for each framework

json_logging/__init__.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ def format(self, record):
233233
return JSON_SERIALIZER(json_log_object)
234234

235235

236+
def _sanitize_log_msg(record):
237+
return record.getMessage().replace('\n', '_').replace('\r', '_').replace('\t', '_')
238+
239+
236240
class JSONLogFormatter(logging.Formatter):
237241
"""
238242
Formatter for non-web application log
@@ -266,9 +270,8 @@ def format(self, record):
266270
"level": record.levelname,
267271
"line_no": record.lineno,
268272
"module": record.module,
269-
"msg": record.getMessage(),
273+
"msg": _sanitize_log_msg(record),
270274
}
271-
272275
if hasattr(record, 'props'):
273276
json_log_object.update(record.props)
274277

@@ -312,7 +315,7 @@ def format(self, record):
312315
"module": record.module,
313316
"line_no": record.lineno,
314317
"correlation_id": _request_util.get_correlation_id(),
315-
"msg": record.getMessage()
318+
"msg": _sanitize_log_msg(record)
316319
}
317320

318321
if hasattr(record, 'props'):

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name="json-logging",
15-
version='1.0.0',
15+
version='1.0.1',
1616
packages=find_packages(exclude=['contrib', 'docs', 'tests*', 'example', 'dist', 'build']),
1717
license='Apache License 2.0',
1818
description="JSON Python Logging",

0 commit comments

Comments
 (0)