Skip to content

Commit ed753d9

Browse files
bpo-42988: Remove the pydoc getfile feature (GH-25015)
CVE-2021-3426: Remove the "getfile" feature of the pydoc module which could be abused to read arbitrary files on the disk (directory traversal vulnerability). Moreover, even source code of Python modules can contain sensitive data like passwords. Vulnerability reported by David Schwörer. (cherry picked from commit 9b99947) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 9a8e078 commit ed753d9

File tree

3 files changed

+4
-24
lines changed

3 files changed

+4
-24
lines changed

Lib/pydoc.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,9 +2457,6 @@ def page(self, title, contents):
24572457
%s</head><body bgcolor="#f0f0f8">%s<div style="clear:both;padding-top:.5em;">%s</div>
24582458
</body></html>''' % (title, css_link, html_navbar(), contents)
24592459

2460-
def filelink(self, url, path):
2461-
return '<a href="getfile?key=%s">%s</a>' % (url, path)
2462-
24632460

24642461
html = _HTMLDoc()
24652462

@@ -2545,19 +2542,6 @@ def bltinlink(name):
25452542
'key = %s' % key, '#ffffff', '#ee77aa', '<br>'.join(results))
25462543
return 'Search Results', contents
25472544

2548-
def html_getfile(path):
2549-
"""Get and display a source file listing safely."""
2550-
path = urllib.parse.unquote(path)
2551-
with tokenize.open(path) as fp:
2552-
lines = html.escape(fp.read())
2553-
body = '<pre>%s</pre>' % lines
2554-
heading = html.heading(
2555-
'<big><big><strong>File Listing</strong></big></big>',
2556-
'#ffffff', '#7799ee')
2557-
contents = heading + html.bigsection(
2558-
'File: %s' % path, '#ffffff', '#ee77aa', body)
2559-
return 'getfile %s' % path, contents
2560-
25612545
def html_topics():
25622546
"""Index of topic texts available."""
25632547

@@ -2649,8 +2633,6 @@ def get_html_page(url):
26492633
op, _, url = url.partition('=')
26502634
if op == "search?key":
26512635
title, content = html_search(url)
2652-
elif op == "getfile?key":
2653-
title, content = html_getfile(url)
26542636
elif op == "topic?key":
26552637
# try topics first, then objects.
26562638
try:

Lib/test/test_pydoc.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,18 +1374,12 @@ def test_url_requests(self):
13741374
("topic?key=def", "Pydoc: KEYWORD def"),
13751375
("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"),
13761376
("foobar", "Pydoc: Error - foobar"),
1377-
("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"),
13781377
]
13791378

13801379
with self.restrict_walk_packages():
13811380
for url, title in requests:
13821381
self.call_url_handler(url, title)
13831382

1384-
path = string.__file__
1385-
title = "Pydoc: getfile " + path
1386-
url = "getfile?key=" + path
1387-
self.call_url_handler(url, title)
1388-
13891383

13901384
class TestHelper(unittest.TestCase):
13911385
def test_keywords(self):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CVE-2021-3426: Remove the ``getfile`` feature of the :mod:`pydoc` module which
2+
could be abused to read arbitrary files on the disk (directory traversal
3+
vulnerability). Moreover, even source code of Python modules can contain
4+
sensitive data like passwords. Vulnerability reported by David Schwörer.

0 commit comments

Comments
 (0)