Skip to content

python -m gzip writes through a symlink at the output name and ignores the input mode #161088

Closed

Description

@gpshead

Bug report

Bug description:

python -m gzip derives the output name from the input name and opens it with "wb", so an existing symlink at that name is followed and its target is overwritten. It also creates the output with the default mode, so a private input file yields a world-readable output. gzip(1) (with -f) unlinks the old name, creates the output exclusively, and gives it the input's permission bits.

import gzip, os, subprocess, sys
open('target', 'wb').write(b'precious')
os.symlink('target', 'report')
with gzip.open('report.gz', 'wb') as f:
    f.write(b'payload')
subprocess.run([sys.executable, '-m', 'gzip', '-d', 'report.gz'])
print(open('target', 'rb').read())  # b'payload'

open('c.txt', 'wb').write(b'secret')
os.chmod('c.txt', 0o600)
subprocess.run([sys.executable, '-m', 'gzip', 'c.txt'])
print(oct(os.stat('c.txt.gz').st_mode & 0o777))  # 0o644 with umask 022

Expected report to be replaced by a regular file with target untouched, and c.txt.gz to be 0o600, as with gzip.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. self-assigned this
    on Nov 1, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 1, 2026
  3. added a commit that references this issue on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions