@@ -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 ])
0 commit comments