Skip to content

Docs for pkgutil.get_data inconsistent with semantics #69517

Closed

Description

@anntzer
mannequin
BPO 25330
Nosy @Yhg1s, @anntzer, @pganssle

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 = None
created_at = <Date 2015-10-07.03:06:14.291>
labels = ['3.7', 'library']
title = 'Docs for pkgutil.get_data inconsistent with semantics'
updated_at = <Date 2020-09-11.22:09:18.404>
user = 'https://github.com/anntzer'

bugs.python.org fields:

activity = <Date 2020-09-11.22:09:18.404>
actor = 'brett.cannon'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2015-10-07.03:06:14.291>
creator = 'Antony.Lee'
dependencies = []
files = []
hgrepos = []
issue_num = 25330
keywords = []
message_count = 6.0
messages = ['252450', '252480', '263243', '263268', '310777', '310879']
nosy_count = 4.0
nosy_names = ['twouters', 'Antony.Lee', 'WGH', 'p-ganssle']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue25330'
versions = ['Python 3.6', 'Python 3.7']

Activity

  1. anntzer commented on Oct 7, 2015

    anntzermannequin
    MannequinAuthor

    The docs of pkgutil.get_data say "The resource argument should be in the form of a relative filename, using / as the path separator. The parent directory name .. is not allowed, and nor is a rooted name (starting with a /)."

    In fact (on Python 3.5 at least):

    • pkgutil.get_data("logging", "/init.py") works, but simply chops off the first slash, returning the contents of the stdlib's logging/init.py.
    • pkgutil.get_data("logging", "../re.py") works, returning the contents of the stdlib's re.py.

    People who actually thought about the implications of get_data/zipimport/etc. can decide whether to remove this functionality or to update the docs, I'm just reporting it.

    Also, it would be nice if get_data gained a "text mode" (i.e. returning str instead of bytes and with support for universal newlines).

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 7, 2015
  3. brettcannon commented on Oct 7, 2015

    @brettcannon
    Member

    Changing this to be a single issue about the difference between the docs and the semantics of pkgutil.

    The feature request can be made into a separate issue, but there are plans to replace the API with a more stringently defined one in importlib.

  4. changed the title [-]Two issues with pkgutil.get_data[/-] [+]Docs for pkgutil.get_data inconsistent with semantics[/+] on Oct 7, 2015
  5. WGH commented on Apr 12, 2016

    WGHmannequin
    Mannequin

    I think it can even be considered a security bug. A classic path traversal. The fact that documentation falsely suggests that there's no such vulnerability is clearly not helping.

    Python 2.7 is affected as well, by the way.

  6. brettcannon commented on Apr 12, 2016

    @brettcannon
    Member

    This can't change in Python 2.7 because of backwards-compatibility. And I would argue this isn't a serious security risk as pkgutil.get_data() typically works with string constants and values provided by the library and not user-provided values. This is basically the same as taking a value for open() and has the same risks.

  7. pganssle commented on Jan 26, 2018

    @pganssle
    Member

    I'm not sure if this warrants a separate issue, but I also notice this in the documentation:

    If the package cannot be located or loaded, or it uses a loader which does not support get_data, then None is returned. In particular, the loader for namespace packages does not support get_data.

    But in reality this seems to raise a FileNotFoundError:

        >>> import pkgutil
        >>> data = pkgutil.get_data('dateutil.zoneinfo', 'dateutil-zoneinfo.tar.gz')
        >>> len(data)
        139671
        >>> data = pkgutil.get_data('dateutil.zoneinfo', 'foo-bar.tar.gz')
        FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/python3.6/site-packages/dateutil/zoneinfo/foo-bar.tar.gz'

    Am I misunderstanding the documentation, or should the failure mode be corrected to specify that it raises an error?

  8. brettcannon commented on Jan 27, 2018

    @brettcannon
    Member

    Notice that the returning of None only has to do with searching for the *package*, not the *data file*. So I think the docs are still correct according to your example, Paul.

  9. transferred this issue fromon Apr 10, 2022
  10. methane commented on Sep 29, 2026

    @methane
    Member

    fixed by #148197

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.7 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directory

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions