|
| 1 | +use sudo_test::{Command, Directory, Env, TextFile, ROOT_GROUP}; |
| 2 | + |
| 3 | +use crate::{DEFAULT_EDITOR, OTHER_USERNAME, SUDOERS_ALL_ALL_NOPASSWD, USERNAME}; |
| 4 | + |
| 5 | +const CHMOD_EXEC: &str = "555"; |
| 6 | +const EDITOR_DUMMY: &str = "#!/bin/sh |
| 7 | +echo \"#\" >> \"$1\""; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn cannot_edit_writable_paths() { |
| 11 | + let env = Env(SUDOERS_ALL_ALL_NOPASSWD) |
| 12 | + .user(USERNAME) |
| 13 | + .directory(Directory("/tmp/bar").chmod("755")) |
| 14 | + .file(DEFAULT_EDITOR, TextFile(EDITOR_DUMMY).chmod(CHMOD_EXEC)) |
| 15 | + .build(); |
| 16 | + |
| 17 | + for file in ["/tmp/foo.sh", "/tmp/bar/foo.sh", "/var/tmp/foo.sh"] { |
| 18 | + let output = Command::new("sudoedit") |
| 19 | + .as_user(USERNAME) |
| 20 | + .arg(file) |
| 21 | + .output(&env); |
| 22 | + |
| 23 | + output.assert_exit_code(1); |
| 24 | + |
| 25 | + if sudo_test::is_original_sudo() { |
| 26 | + if file != "/tmp/bar/foo.sh" { |
| 27 | + assert_contains!( |
| 28 | + output.stderr(), |
| 29 | + "editing files in a writable directory is not permitted" |
| 30 | + ); |
| 31 | + } else { |
| 32 | + // I don't know why ogsudo gives this error -- probably because opening failed |
| 33 | + assert_contains!(output.stderr(), "No such file or directory"); |
| 34 | + } |
| 35 | + } else { |
| 36 | + assert_contains!( |
| 37 | + output.stderr(), |
| 38 | + "cannot open a file in a path writable by the user" |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn can_edit_writable_paths_as_root() { |
| 46 | + // note: we already have tests that sudoedit "works" so we are skipping |
| 47 | + // the content check here: the point here is that sudoedit does not stop |
| 48 | + // the user. |
| 49 | + |
| 50 | + let env = Env(SUDOERS_ALL_ALL_NOPASSWD) |
| 51 | + .user(USERNAME) |
| 52 | + .directory(Directory("/tmp/bar").chmod("755")) |
| 53 | + .file(DEFAULT_EDITOR, TextFile(EDITOR_DUMMY).chmod(CHMOD_EXEC)) |
| 54 | + .build(); |
| 55 | + |
| 56 | + let file = "/tmp/foo.sh"; |
| 57 | + Command::new("sudoedit") |
| 58 | + .arg(file) |
| 59 | + .output(&env) |
| 60 | + .assert_success(); |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn cannot_edit_symlinks() { |
| 65 | + let env = Env(SUDOERS_ALL_ALL_NOPASSWD) |
| 66 | + .user(USERNAME) |
| 67 | + .file(DEFAULT_EDITOR, TextFile(EDITOR_DUMMY).chmod(CHMOD_EXEC)) |
| 68 | + .build(); |
| 69 | + |
| 70 | + let file = "/usr/bin/sudoedit"; |
| 71 | + |
| 72 | + let output = Command::new("sudoedit") |
| 73 | + .as_user(USERNAME) |
| 74 | + .arg(file) |
| 75 | + .output(&env); |
| 76 | + |
| 77 | + assert_contains!(output.stderr(), "editing symbolic links is not permitted"); |
| 78 | + |
| 79 | + output.assert_exit_code(1); |
| 80 | +} |
| 81 | + |
| 82 | +#[test] |
| 83 | +fn cannot_edit_files_target_user_cannot_access() { |
| 84 | + let file = "/test.txt"; |
| 85 | + |
| 86 | + let env = Env(SUDOERS_ALL_ALL_NOPASSWD) |
| 87 | + .user(USERNAME) |
| 88 | + .user(OTHER_USERNAME) |
| 89 | + .group(USERNAME) |
| 90 | + .group(OTHER_USERNAME) |
| 91 | + .file(DEFAULT_EDITOR, TextFile(EDITOR_DUMMY).chmod(CHMOD_EXEC)) |
| 92 | + .file( |
| 93 | + file, |
| 94 | + TextFile("") |
| 95 | + .chown(format!("{USERNAME}:{ROOT_GROUP}")) |
| 96 | + .chmod("460"), |
| 97 | + ) |
| 98 | + .build(); |
| 99 | + |
| 100 | + let test_cases = [ |
| 101 | + // incorrect user |
| 102 | + &["-u", OTHER_USERNAME][..], |
| 103 | + // correct user, but does not have write permits |
| 104 | + &["-u", USERNAME][..], |
| 105 | + // incorrect group |
| 106 | + &["-u", OTHER_USERNAME, "-g", USERNAME][..], |
| 107 | + // group permission doesn't override matching user permissions |
| 108 | + &["-u", USERNAME, "-g", ROOT_GROUP][..], |
| 109 | + &["-g", ROOT_GROUP][..], |
| 110 | + ]; |
| 111 | + |
| 112 | + for args in test_cases { |
| 113 | + let output = Command::new("sudoedit") |
| 114 | + .args(args) |
| 115 | + .arg(file) |
| 116 | + .as_user(USERNAME) |
| 117 | + .output(&env); |
| 118 | + |
| 119 | + assert_contains!(output.stderr(), "Permission denied"); |
| 120 | + output.assert_exit_code(1); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[test] |
| 125 | +fn can_edit_files_target_user_or_group_can_access() { |
| 126 | + // note: we already have tests that sudoedit "works" so we are skipping |
| 127 | + // the content check here: the point here is that sudoedit does not stop |
| 128 | + // the user. |
| 129 | + |
| 130 | + let file = "/test.txt"; |
| 131 | + let env = Env(SUDOERS_ALL_ALL_NOPASSWD) |
| 132 | + .user(USERNAME) |
| 133 | + .user(OTHER_USERNAME) |
| 134 | + .group(OTHER_USERNAME) |
| 135 | + .file(DEFAULT_EDITOR, TextFile(EDITOR_DUMMY).chmod(CHMOD_EXEC)) |
| 136 | + .file( |
| 137 | + file, |
| 138 | + TextFile("") |
| 139 | + .chown(format!("{OTHER_USERNAME}:{OTHER_USERNAME}")) |
| 140 | + .chmod("660"), |
| 141 | + ) |
| 142 | + .build(); |
| 143 | + |
| 144 | + for user in ["root", OTHER_USERNAME] { |
| 145 | + Command::new("sudoedit") |
| 146 | + .args(["-u", user, file]) |
| 147 | + .as_user(USERNAME) |
| 148 | + .output(&env) |
| 149 | + .assert_success(); |
| 150 | + } |
| 151 | + |
| 152 | + Command::new("sudoedit") |
| 153 | + .args(["-g", OTHER_USERNAME, file]) |
| 154 | + .as_user(USERNAME) |
| 155 | + .output(&env) |
| 156 | + .assert_success(); |
| 157 | + |
| 158 | + Command::new("sudoedit") |
| 159 | + .args(["-u", USERNAME, "-g", OTHER_USERNAME, file]) |
| 160 | + .as_user(USERNAME) |
| 161 | + .output(&env) |
| 162 | + .assert_success(); |
| 163 | +} |
0 commit comments