Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e29398

Browse files
authored
gh-100001: Also escape \s in http.server log messages. (#100038)
Also \ escape \s in the http.server BaseHTTPRequestHandler.log_message so that it is technically possible to parse the line and reconstruct what the original data was. Without this a \xHH is ambiguious as to if it is a hex replacement we put in or the characters r"\x" came through in the original request line.
1 parent e9e63ad commit 7e29398

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

Lib/http/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ def log_error(self, format, *args):
566566
# https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes
567567
_control_char_table = str.maketrans(
568568
{c: fr'\x{c:02x}' for c in itertools.chain(range(0x20), range(0x7f,0xa0))})
569+
_control_char_table[ord('\\')] = r'\\'
569570

570571
def log_message(self, format, *args):
571572
"""Log an arbitrary message.

Lib/test/test_httpservers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ def test_unprintable_not_logged(self):
10001000
log_message(self.handler, '/\033bar\000\033')
10011001
log_message(self.handler, '/spam %s.', 'a')
10021002
log_message(self.handler, '/spam %s.', '\033\x7f\x9f\xa0beans')
1003+
log_message(self.handler, '"GET /foo\\b"ar\007 HTTP/1.0"')
10031004
stderr = fake_stderr.getvalue()
10041005
self.assertNotIn('\033', stderr) # non-printable chars are caught.
10051006
self.assertNotIn('\000', stderr) # non-printable chars are caught.
@@ -1008,6 +1009,7 @@ def test_unprintable_not_logged(self):
10081009
self.assertIn(r'/\x1bbar\x00\x1b', lines[1])
10091010
self.assertIn('/spam a.', lines[2])
10101011
self.assertIn('/spam \\x1b\\x7f\\x9f\xa0beans.', lines[3])
1012+
self.assertIn(r'"GET /foo\\b"ar\x07 HTTP/1.0"', lines[4])
10111013

10121014
def test_http_1_1(self):
10131015
result = self.send_typical_request(b'GET / HTTP/1.1\r\n\r\n')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Also \ escape \s in the http.server BaseHTTPRequestHandler.log_message so
2+
that it is technically possible to parse the line and reconstruct what the
3+
original data was. Without this a \xHH is ambiguious as to if it is a hex
4+
replacement we put in or the characters r"\x" came through in the original
5+
request line.

0 commit comments

Comments
 (0)