python / python/cpython

Improper sanitization and documentation for `ZipFile.mkdir(mode)`

Open
#154,490 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

When calling ZipFile.mkdir(), the user provided mode does not have the file format bits cleared, which may cause the created entry be recognized as an incorrect or unknown file format.

import io
import stat
import zipfile

with zipfile.ZipFile(io.BytesIO(), 'w') as zh:
    zh.mkdir('foo/', 0o107777)  # regular file
    zinfo = zh.getinfo('foo/')
    print(oct(stat.S_IFMT(zinfo.external_attr >> 16)))  # 0o140000 (socket file)

with zipfile.ZipFile(io.BytesIO(), 'w') as zh:
    zh.mkdir('foo/', 0o127777)  # symbolic link
    zinfo = zh.getinfo('foo/')
    print(oct(stat.S_IFMT(zinfo.external_attr >> 16)))  # 0o160000 (unknown)

Additionally, the current source code and doc for ZipFile.mkdir()'s default mode value are 511, which is identical to 0o777 but less intuitive.

Suggestion

Instead of the current sanitization:

zinfo.external_attr = ((0o40000 | mode) & 0xFFFF) << 16

we should probably sanitize the provided mode as what stat.S_IMODE does, i.e.:

zinfo.external_attr = (0o40000 | (mode & 0o7777)) << 16

We should probably also revise the source code and doc for ZipFile.mkdir()'s default mode value to 0o777, to be consistent with os.mkdir() and more intuitive.

CPython versions tested on:

3.14

Operating systems tested on:

No response

Linked PRs
  • gh-154509

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the ZipFile.mkdir() entry point and inspect how mode is sanitized into external_attr, along with the documented default value. Compare the behavior with stat.S_IMODE and os.mkdir(); done means file-type bits are excluded and the default is shown as 0o777, while checking linked PR gh-154509 for work already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.