Skip to content

Commit 76e51a2

Browse files
authored
fix: create download temporary files privately (#1952)
Prevent a brief wider permission window while replacing a private download destination. Fixes https://hackerone.com/reports/4251098.
1 parent 61135a6 commit 76e51a2

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎tests/test_download.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,60 @@ def test_failure_cleans_temporary_file(self) -> None:
397397

398398
self.assertEqual("Error: replace failed\n", output)
399399
self.assertEqual(list(output_directory.iterdir()), [])
400+
401+
def test_replacement_temporary_starts_private(self) -> None:
402+
"""Temporary download content must not start with wider permissions."""
403+
if os.name == "nt":
404+
self.skipTest("POSIX file modes required")
405+
with TemporaryDirectory() as tmpdirname:
406+
root = Path(tmpdirname)
407+
destination = root / "translation.po"
408+
destination.write_bytes(b"old content")
409+
destination.chmod(0o600)
410+
(root / "other-link").hardlink_to(destination)
411+
original_open = os.open
412+
temporary_modes: list[int] = []
413+
414+
def record_open(
415+
path: str | Path,
416+
flags: int,
417+
mode: int = 0o777,
418+
*,
419+
dir_fd: int | None = None,
420+
) -> int:
421+
if Path(path).name.startswith(".wlc-"):
422+
temporary_modes.append(mode)
423+
return original_open(path, flags, mode, dir_fd=dir_fd)
424+
425+
with patch("wlc.main.os.open", side_effect=record_open):
426+
self.execute(
427+
["download", "hello/weblate/cs", "--output", str(destination)]
428+
)
429+
430+
self.assertEqual(temporary_modes, [0o600])
431+
self.assertEqual(stat.S_IMODE(destination.stat().st_mode), 0o600)
432+
433+
def test_new_download_uses_normal_creation_mode(self) -> None:
434+
"""A new output file should retain umask-based permissions."""
435+
with TemporaryDirectory() as tmpdirname:
436+
destination = Path(tmpdirname) / "translation.po"
437+
original_open = os.open
438+
temporary_modes: list[int] = []
439+
440+
def record_open(
441+
path: str | Path,
442+
flags: int,
443+
mode: int = 0o777,
444+
*,
445+
dir_fd: int | None = None,
446+
) -> int:
447+
if Path(path).name.startswith(".wlc-"):
448+
temporary_modes.append(mode)
449+
return original_open(path, flags, mode, dir_fd=dir_fd)
450+
451+
with patch("wlc.main.os.open", side_effect=record_open):
452+
self.execute(
453+
["download", "hello/weblate/cs", "--output", str(destination)]
454+
)
455+
456+
self.assertEqual(temporary_modes, [0o666])

‎wlc/main.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ def _download_destination_stat(path: Path) -> os.stat_result | None:
181181
return None
182182

183183

184-
def _open_download_temporary(path: Path) -> tuple[int, str]:
184+
def _open_download_temporary(path: Path, mode: int = 0o666) -> tuple[int, str]:
185185
"""Create a short, exclusive temporary download file."""
186186
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0)
187187
for _attempt in range(10):
188188
temporary_name = f".wlc-{secrets.token_hex(4)}"
189189
try:
190-
descriptor = os.open(path.parent / temporary_name, flags, 0o666)
190+
descriptor = os.open(path.parent / temporary_name, flags, mode)
191191
except FileExistsError:
192192
continue
193193
return descriptor, temporary_name
@@ -271,7 +271,9 @@ def _write_download_file(
271271
_write_existing_download_file(path, content, destination_stat)
272272
return
273273

274-
opened_descriptor, temporary_name = _open_download_temporary(path)
274+
opened_descriptor, temporary_name = _open_download_temporary(
275+
path, 0o600 if destination_stat is not None else 0o666
276+
)
275277
descriptor = opened_descriptor
276278
descriptor_open = True
277279
temporary_exists = True

0 commit comments

Comments
 (0)