python / python/cpython

tarfile: Extracting symlink using extractfile results in Keyerror: linkname not found

オープン
#122,736 コメント 2 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

I have the following tar archive containing some files and a symlink to a file that is not part of the tar archive.

drwxr-xr-x guif/users        0 2024-08-06 15:51 test/
-rw-r--r-- guif/users        0 2024-08-06 15:50 test/file3
lrwxrwxrwx guif/users        0 2024-08-06 15:51 test/mylink -> /usr/bin/env
-rw-r--r-- guif/users        0 2024-08-06 15:50 test/file1
-rw-r--r-- guif/users        0 2024-08-06 15:50 test/file2

I want to extract the symlink. With the getmember method I receive a valid member of the tar archive.
Using this member in the extractfile however results in an error.

#!/usr/bin/env python

import tarfile


def main():
    with tarfile.open('./test.tar') as tar_file:
        member = tar_file.getmember('test/mylink')
        file_content = tar_file.extractfile(member)


if __name__ == '__main__':
    main()

Traceback (most recent call last):
  File "/home/guif/repos/private/tmp/./main.py", line 13, in <module>
    main()
  File "/home/guif/repos/private/tmp/./main.py", line 9, in main
    file_content = tar_file.extractfile(member)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/l014xp1qxdl6gim3zc0jv3mpxhbp346s-python3-3.12.4/lib/python3.12/tarfile.py", line 2385, in extractfile
    return self.extractfile(self._find_link_target(tarinfo))
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/l014xp1qxdl6gim3zc0jv3mpxhbp346s-python3-3.12.4/lib/python3.12/tarfile.py", line 2725, in _find_link_target
    raise KeyError("linkname %r not found" % linkname)
KeyError: "linkname 'test//usr/bin/env' not found"

As you can see 'test//usr/bin/env can not be found.
The path does not look like the symlink I try to extract but more like <symlink name>/<link target>

From the doc:

Extract a member from the archive as a file object. `member' may be
a filename or a TarInfo object. If `member' is a regular file or
a link, an io.BufferedReader object is returned. For all other
existing members, None is returned. If `member' does not appear
in the archive, KeyError is raised.
CPython versions tested on:

3.10, 3.12

Operating systems tested on:

Linux

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

tarfile.py の extractfile() と _find_link_target() 付近を読み、次に提供されたアーカイブとスクリプトを実行して KeyError を再現します。リンクについて文書化されている動作と、この外部ターゲットのケースを比較し、意図した結果を明確にするリグレッションテストを追加します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。