Skip to content

[security] CVE-2021-3426: Information disclosure via pydoc -p: /getfile?key=path allows to read arbitrary file on the filesystem #87154

Closed

Description

@hroncok
mannequin
BPO 42988
Nosy @malemburg, @gpshead, @vstinner, @ned-deily, @ambv, @serhiy-storchaka, @JulienPalard, @hroncok, @frenzymadness, @miss-islington, @Fidget-Spinner
PRs
  • bpo-42988: Improve pydoc web server security #24285
  • bpo-42988: Fix security issue in the pydoc server #24337
  • bpo-42988: Remove the pydoc getfile feature #25015
  • [3.9] bpo-42988: Remove the pydoc getfile feature (GH-25015) #25064
  • [3.8] bpo-42988: Remove the pydoc getfile feature (GH-25015) #25065
  • [3.7] bpo-42988: Remove the pydoc getfile feature (GH-25015) #25066
  • [3.6] bpo-42988: Remove the pydoc getfile feature (GH-25015) #25067
  • 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 = None
    closed_at = <Date 2021-03-29.19:39:54.149>
    created_at = <Date 2021-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 = <Date 2021-03-29.19:39:54.148>
    user = 'https://github.com/hroncok'

    bugs.python.org fields:

    activity = <Date 2021-03-29.19:39:54.148>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-03-29.19:39:54.149>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2021-01-21.12:18:37.837>
    creator = 'hroncok'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42988
    keywords = ['patch']
    message_count = 30.0
    messages = ['385412', '385413', '385415', '385418', '385420', '385421', '385422', '385435', '385460', '385485', '385488', '385489', '385492', '385503', '385710', '385721', '385866', '386221', '386222', '388399', '388451', '388452', '388455', '388645', '389452', '389695', '389699', '389700', '389710', '389711']
    nosy_count = 11.0
    nosy_names = ['lemburg', 'gregory.p.smith', 'vstinner', 'ned.deily', 'lukasz.langa', 'serhiy.storchaka', 'mdk', 'hroncok', 'frenzy', 'miss-islington', 'kj']
    pr_nums = ['24285', '24337', '25015', '25064', '25065', '25066', '25067']
    priority = 'critical'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'security'
    url = 'https://bugs.python.org/issue42988'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    Activity

    1. hroncok commented on Jan 21, 2021

      hroncokmannequin
      MannequinAuthor

      Hello Python security,
      a Fedora user has reported the following security vulnerability to us (I was able to verify it):

      Running pydoc -p allows other local users to extract arbitrary files.

      Steps to Reproduce:

      1. start pydoc on a port
      2. as a different user guess or extract the port
      3. call getfile on the server to extract arbitrary files, e.g. http://localhost:8888/getfile?key=/home/dave/.ssh/id_rsa

      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.

    2. added
      3.7 (EOL)
      3.8 (EOL)
      3.9 (EOL)
      3.10 (EOL)
      stdlib
      type-security
      on Jan 21, 2021
    3. JulienPalard commented on Jan 21, 2021

      @JulienPalard
      Member

      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.

    4. vstinner commented on Jan 21, 2021

      @vstinner
      Member
    5. 28 remaining items

    6. vstinner commented on Mar 10, 2021

      @vstinner
      Member

      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*.

    7. vstinner commented on Mar 10, 2021

      @vstinner
      Member
    8. gpshead commented on Mar 14, 2021

      @gpshead
      Member

      FWIW, I don't think we should even have a server feature in pydoc...

    9. vstinner commented on Mar 24, 2021

      @vstinner
      Member

      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.

    10. vstinner commented on Mar 29, 2021

      @vstinner
      Member

      New changeset 9b99947 by Victor Stinner in branch 'master':
      bpo-42988: Remove the pydoc getfile feature (GH-25015)
      9b99947

    11. miss-islington commented on Mar 29, 2021

      @miss-islington
      Contributor

      New changeset 7e38d33 by Miss Islington (bot) in branch '3.8':
      bpo-42988: Remove the pydoc getfile feature (GH-25015)
      7e38d33

    12. miss-islington commented on Mar 29, 2021

      @miss-islington
      Contributor

      New changeset ed753d9 by Miss Islington (bot) in branch '3.9':
      bpo-42988: Remove the pydoc getfile feature (GH-25015)
      ed753d9

    13. ned-deily commented on Mar 29, 2021

      @ned-deily
      Member

      New changeset 7c2284f by Miss Islington (bot) in branch '3.7':
      bpo-42988: Remove the pydoc getfile feature (GH-25015) (bpo-25066)
      7c2284f

    14. ned-deily commented on Mar 29, 2021

      @ned-deily
      Member

      New changeset 5b1e502 by Miss Islington (bot) in branch '3.6':
      bpo-42988: Remove the pydoc getfile feature (GH-25015) (GH-25067)
      5b1e502

    15. transferred this issue fromon Apr 10, 2022
    16. apetro76 commented on Aug 28, 2023

      @apetro76

      Hello Python security, a Fedora user has reported the following security vulnerability to us (I was able to verify it):

      Running pydoc -p allows other local users to extract arbitrary files.

      Steps to Reproduce:

      1. start pydoc on a port
      2. as a different user guess or extract the port
      3. call getfile on the server to extract arbitrary files, e.g. http://localhost:8888/getfile?key=/home/dave/.ssh/id_rsa

      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?

    17. gpshead commented on Aug 28, 2023

      @gpshead
      Member

      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.server module 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.

    18. apetro76 commented on Aug 28, 2023

      @apetro76

      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.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)3.7 (EOL)3.8 (EOL)3.9 (EOL)stdlibtype-security

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions