Skip to content

Commit 7c2284f

Browse files
bpo-42988: Remove the pydoc getfile feature (GH-25015) (#25066)
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 <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 7937395 commit 7c2284f

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
@@ -2348,9 +2348,6 @@ def page(self, title, contents):
23482348
%s</head><body bgcolor="#f0f0f8">%s<div style="clear:both;padding-top:.5em;">%s</div>
23492349
</body></html>''' % (title, css_link, html_navbar(), contents)
23502350

2351-
def filelink(self, url, path):
2352-
return '<a href="getfile?key=%s">%s</a>' % (url, path)
2353-
23542351

23552352
html = _HTMLDoc()
23562353

@@ -2436,19 +2433,6 @@ def bltinlink(name):
24362433
'key = %s' % key, '#ffffff', '#ee77aa', '<br>'.join(results))
24372434
return 'Search Results', contents
24382435

2439-
def html_getfile(path):
2440-
"""Get and display a source file listing safely."""
2441-
path = urllib.parse.unquote(path)
2442-
with tokenize.open(path) as fp:
2443-
lines = html.escape(fp.read())
2444-
body = '<pre>%s</pre>' % lines
2445-
heading = html.heading(
2446-
'<big><big><strong>File Listing</strong></big></big>',
2447-
'#ffffff', '#7799ee')
2448-
contents = heading + html.bigsection(
2449-
'File: %s' % path, '#ffffff', '#ee77aa', body)
2450-
return 'getfile %s' % path, contents
2451-
24522436
def html_topics():
24532437
"""Index of topic texts available."""
24542438

@@ -2540,8 +2524,6 @@ def get_html_page(url):
25402524
op, _, url = url.partition('=')
25412525
if op == "search?key":
25422526
title, content = html_search(url)
2543-
elif op == "getfile?key":
2544-
title, content = html_getfile(url)
25452527
elif op == "topic?key":
25462528
# try topics first, then objects.
25472529
try:

Lib/test/test_pydoc.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,18 +1049,12 @@ def test_url_requests(self):
10491049
("topic?key=def", "Pydoc: KEYWORD def"),
10501050
("topic?key=STRINGS", "Pydoc: TOPIC STRINGS"),
10511051
("foobar", "Pydoc: Error - foobar"),
1052-
("getfile?key=foobar", "Pydoc: Error - getfile?key=foobar"),
10531052
]
10541053

10551054
with self.restrict_walk_packages():
10561055
for url, title in requests:
10571056
self.call_url_handler(url, title)
10581057

1059-
path = string.__file__
1060-
title = "Pydoc: getfile " + path
1061-
url = "getfile?key=" + path
1062-
self.call_url_handler(url, title)
1063-
10641058

10651059
class TestHelper(unittest.TestCase):
10661060
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)