You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[security] CVE-2021-3426: Information disclosure via pydoc -p: /getfile?key=path allows to read arbitrary file on the filesystem #87154
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-03-29.19:39:54.149>created_at=<Date2021-01-21.12:18:37.837>labels= ['type-security', '3.8', '3.9', '3.10', '3.7', 'library']
title='[security] CVE-2021-3426: Information disclosure via pydoc -p: /getfile?key=path allows to read arbitrary file on the filesystem'updated_at=<Date2021-03-29.19:39:54.148>user='https://github.com/hroncok'
Actual results:
any local user on the multi-user system can read all my keys and secrets
Expected results:
Access is prevented.
Additional info:
At least a warning should be printed, that this is insecure on multi-user systems.
Python notebook works around this by providing a token that is required to access the notepad. Depending on the system being able to read arbitrary files can allow to impersonate my, by e.g. stealing my ssh-key (if it is non-encrypted)
I've originally reported this to security@python.org but I was asked to open a public issue here.
Nice find! I am able to reproduce it too in many Python releases.
I see differnt ways we can fix it:
# Using a random secret generated at startup time
Used any way, like by providing an hmac on getfile urls to ensure they are signed with the server secret.
Con: getfile URLS won't work from a run to another run (the secret should be random and changed at every start), and can't be shared (do someone share them in the first place?)
# Allowlist according to sys.path
In getfile implementation, we can check if the asked path is under a path from sys.path.
Con: If someone have ~/ in sys.path, we still can access all its home, or if someone start it using python -m pydoc while being in its home directory as Python will prepend the cwd in sys.path.
# Allowlist populated while generating links
Idea is: each time the server generates a getfile link, the target is added to an allowlist.
Each time a getfile link is requested, if the file is not in the allowlist, request is denied.
Con: Refreshing a page won't work after a server restart (thus having an empty allowlist).
# fnmatch allowlist
We could allow only .py files.
Con: Secrets stored in .py files under user project could still be leaked.
-----------------
My personal preference goes for the allowlist populated while generating links.
The "pydoc -p port" command only listen on the local link ("localhost") by default, even if it's possible to listen on another IPv4 address using -n HOSTNAME command line option.
While the "getfile" feature is convenient when the pydoc server is accessed from a different machine, I don't think that it's worth it, compared to the security risks and the complexity of PR 24285 and PR 24337 fixes.
I propose to simply remove the "getfile" feature with PR 25015, but keep links using file:// scheme. So we delegate the security to the web browser. If the web browser is allowed to read sensitive data of a local Python file, it's not our problem: pydoc doesn't make things worse.
Actual results: any local user on the multi-user system can read all my keys and secrets
Expected results: Access is prevented.
Additional info: At least a warning should be printed, that this is insecure on multi-user systems.
Python notebook works around this by providing a token that is required to access the notepad. Depending on the system being able to read arbitrary files can allow to impersonate my, by e.g. stealing my ssh-key (if it is non-encrypted)
I've originally reported this to security@python.org but I was asked to open a public issue here.
Curious, what is actually being called out as a vulnerability here? The fact that pydoc doesn't enforce authentication? Seems this works the same as any other service/application works (when accessing the application/service, you are granted the permissions of the account running that application/service when it interacts with the operating system). I can think of dozens of applications and protocols that would be victim to this same finding. For example, running python -m http.server 80 . from a home directory does the same thing (still works even on version 3.11) does that mean that python3.11 is broken?
pydoc's former built in web server could reasonably have been expected not to serve other data, as that isn't what the user asked for or would ever assume it would allow. Thus it was reasonable to consider it doing so a vulnerability.
thanks for the explanation, that does make a bit more sense. Personally I am not familiar with pydoc, so when reading the vulnerability through the first time it sounded no different than if nginx, apache or iis was misconfigured, or running a tftp, ftp, or smb share that disclosed sensitive documents or for that matter any other production application that is used for file sharing. I appreciate the explanation and will chalk it up to my ignorance to pydoc and how its used.
Activity
hroncok commentedon Jan 21, 2021
Hello Python security,
a Fedora user has reported the following security vulnerability to us (I was able to verify it):
Running
pydoc -pallows other local users to extract arbitrary files.Steps to Reproduce:
Actual results:
any local user on the multi-user system can read all my keys and secrets
Expected results:
Access is prevented.
Additional info:
At least a warning should be printed, that this is insecure on multi-user systems.
Python notebook works around this by providing a token that is required to access the notepad. Depending on the system being able to read arbitrary files can allow to impersonate my, by e.g. stealing my ssh-key (if it is non-encrypted)
I've originally reported this to security@python.org but I was asked to open a public issue here.
JulienPalard commentedon Jan 21, 2021
Nice find! I am able to reproduce it too in many Python releases.
I see differnt ways we can fix it:
# Using a random secret generated at startup time
Used any way, like by providing an hmac on getfile urls to ensure they are signed with the server secret.
Con: getfile URLS won't work from a run to another run (the secret should be random and changed at every start), and can't be shared (do someone share them in the first place?)
# Allowlist according to sys.path
In getfile implementation, we can check if the asked path is under a path from sys.path.
Con: If someone have ~/ in sys.path, we still can access all its home, or if someone start it using
python -m pydocwhile being in its home directory as Python will prepend the cwd in sys.path.# Allowlist populated while generating links
Idea is: each time the server generates a getfile link, the target is added to an allowlist.
Each time a getfile link is requested, if the file is not in the allowlist, request is denied.
Con: Refreshing a page won't work after a server restart (thus having an empty allowlist).
# fnmatch allowlist
We could allow only
.pyfiles.Con: Secrets stored in
.pyfiles under user project could still be leaked.-----------------
My personal preference goes for the allowlist populated while generating links.
vstinner commentedon Jan 21, 2021
Downstream Fedora issue: https://bugzilla.redhat.com/show_bug.cgi?id=1917807
28 remaining items
vstinner commentedon Mar 10, 2021
I created https://python-security.readthedocs.io/vuln/pydoc-getfile.html to track this vulnerability. The is no CVE section yet since the CVE is currently only *RESERVED*.
vstinner commentedon Mar 10, 2021
Fedora downstream issue: https://bugzilla.redhat.com/show_bug.cgi?id=1937476
gpshead commentedon Mar 14, 2021
FWIW, I don't think we should even have a server feature in pydoc...
vstinner commentedon Mar 24, 2021
The "pydoc -p port" command only listen on the local link ("localhost") by default, even if it's possible to listen on another IPv4 address using -n HOSTNAME command line option.
While the "getfile" feature is convenient when the pydoc server is accessed from a different machine, I don't think that it's worth it, compared to the security risks and the complexity of PR 24285 and PR 24337 fixes.
I propose to simply remove the "getfile" feature with PR 25015, but keep links using file:// scheme. So we delegate the security to the web browser. If the web browser is allowed to read sensitive data of a local Python file, it's not our problem: pydoc doesn't make things worse.
vstinner commentedon Mar 29, 2021
New changeset 9b99947 by Victor Stinner in branch 'master':
bpo-42988: Remove the pydoc getfile feature (GH-25015)
9b99947
miss-islington commentedon Mar 29, 2021
New changeset 7e38d33 by Miss Islington (bot) in branch '3.8':
bpo-42988: Remove the pydoc getfile feature (GH-25015)
7e38d33
miss-islington commentedon Mar 29, 2021
New changeset ed753d9 by Miss Islington (bot) in branch '3.9':
bpo-42988: Remove the pydoc getfile feature (GH-25015)
ed753d9
ned-deily commentedon Mar 29, 2021
New changeset 7c2284f by Miss Islington (bot) in branch '3.7':
bpo-42988: Remove the pydoc getfile feature (GH-25015) (bpo-25066)
7c2284f
ned-deily commentedon Mar 29, 2021
New changeset 5b1e502 by Miss Islington (bot) in branch '3.6':
bpo-42988: Remove the pydoc getfile feature (GH-25015) (GH-25067)
5b1e502
apetro76 commentedon Aug 28, 2023
Curious, what is actually being called out as a vulnerability here? The fact that pydoc doesn't enforce authentication? Seems this works the same as any other service/application works (when accessing the application/service, you are granted the permissions of the account running that application/service when it interacts with the operating system). I can think of dozens of applications and protocols that would be victim to this same finding. For example, running python -m http.server 80 . from a home directory does the same thing (still works even on version 3.11) does that mean that python3.11 is broken?
gpshead commentedon Aug 28, 2023
It's a matter of meeting expectations for users of a tool so that they know when they're choosing to expose themselves to risks.
Python's standard library
http.servermodule is documented as not suitable for production use. ie: the user chooses to run that despite being warned, that isn't a flaw, that is their choice.pydoc's former built in web server could reasonably have been expected not to serve other data, as that isn't what the user asked for or would ever assume it would allow. Thus it was reasonable to consider it doing so a vulnerability.
apetro76 commentedon Aug 28, 2023
thanks for the explanation, that does make a bit more sense. Personally I am not familiar with pydoc, so when reading the vulnerability through the first time it sounded no different than if nginx, apache or iis was misconfigured, or running a tftp, ftp, or smb share that disclosed sensitive documents or for that matter any other production application that is used for file sharing. I appreciate the explanation and will chalk it up to my ignorance to pydoc and how its used.