@@ -505,3 +505,61 @@ fn kyaml_output_format_refuses_a_directory_at_an_output_path() {
505505 "the report was written before the copy failed"
506506 ) ;
507507}
508+
509+ /// Root ignores permission bits, so the permission tests below only mean
510+ /// something when run unprivileged. They return early otherwise.
511+ #[ cfg( unix) ]
512+ fn writes_ignore_permissions ( dir : & std:: path:: Path ) -> bool {
513+ use std:: os:: unix:: fs:: PermissionsExt ;
514+ let probe = dir. join ( "privilege-probe" ) ;
515+ std:: fs:: write ( & probe, "" ) . unwrap ( ) ;
516+ std:: fs:: set_permissions ( & probe, std:: fs:: Permissions :: from_mode ( 0o444 ) ) . unwrap ( ) ;
517+ let privileged = std:: fs:: OpenOptions :: new ( ) . write ( true ) . open ( & probe) . is_ok ( ) ;
518+ std:: fs:: remove_file ( & probe) . unwrap ( ) ;
519+ privileged
520+ }
521+
522+ #[ cfg( unix) ]
523+ #[ test]
524+ fn kyaml_output_format_refuses_a_read_only_copy_before_writing ( ) {
525+ use std:: os:: unix:: fs:: PermissionsExt ;
526+ let ( dir, project) = project_dir ( ) ;
527+ if writes_ignore_permissions ( dir. path ( ) ) {
528+ eprintln ! ( "skipped: running with privileges that ignore permission bits" ) ;
529+ return ;
530+ }
531+ let out = dir. path ( ) . join ( "report.json" ) ;
532+ std:: fs:: write ( & out, "old report\n " ) . unwrap ( ) ;
533+ let copy = dir. path ( ) . join ( "report.kyaml" ) ;
534+ std:: fs:: write ( & copy, "old copy\n " ) . unwrap ( ) ;
535+ std:: fs:: set_permissions ( & copy, std:: fs:: Permissions :: from_mode ( 0o444 ) ) . unwrap ( ) ;
536+ let output = assail_kyaml ( & project, & out) ;
537+ assert ! ( !output. status. success( ) ) ;
538+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
539+ assert ! ( stderr. contains( "nothing was written" ) , "{stderr}" ) ;
540+ assert_eq ! ( std:: fs:: read_to_string( & out) . unwrap( ) , "old report\n " ) ;
541+ assert_eq ! ( std:: fs:: read_to_string( & copy) . unwrap( ) , "old copy\n " ) ;
542+ }
543+
544+ #[ cfg( unix) ]
545+ #[ test]
546+ fn kyaml_output_format_refuses_a_directory_it_cannot_create_in ( ) {
547+ use std:: os:: unix:: fs:: PermissionsExt ;
548+ let ( dir, project) = project_dir ( ) ;
549+ if writes_ignore_permissions ( dir. path ( ) ) {
550+ eprintln ! ( "skipped: running with privileges that ignore permission bits" ) ;
551+ return ;
552+ }
553+ let reports = dir. path ( ) . join ( "reports" ) ;
554+ std:: fs:: create_dir ( & reports) . unwrap ( ) ;
555+ let out = reports. join ( "report.json" ) ;
556+ std:: fs:: write ( & out, "old report\n " ) . unwrap ( ) ;
557+ std:: fs:: set_permissions ( & reports, std:: fs:: Permissions :: from_mode ( 0o555 ) ) . unwrap ( ) ;
558+ let output = assail_kyaml_attested ( & project, & out) ;
559+ std:: fs:: set_permissions ( & reports, std:: fs:: Permissions :: from_mode ( 0o755 ) ) . unwrap ( ) ;
560+ assert ! ( !output. status. success( ) ) ;
561+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
562+ assert ! ( stderr. contains( "nothing was written" ) , "{stderr}" ) ;
563+ assert_eq ! ( std:: fs:: read_to_string( & out) . unwrap( ) , "old report\n " ) ;
564+ assert ! ( !reports. join( "report.kyaml" ) . exists( ) ) ;
565+ }
0 commit comments