Skip to content

Commit

Permalink
detect capital R in rm command (#132)
Browse files Browse the repository at this point in the history
* detect capital R in rm command

* clippy fixes
  • Loading branch information
kaplanelad committed Dec 7, 2023
1 parent da4c5b2 commit 7e1f031
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions shellfirm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "shellfirm"
description = "`shellfirm` will intercept any risky patterns (default or defined by you) and prompt you a small challenge for double verification, kinda like a captcha for your terminal."
version = "0.2.8"
version = "0.2.9"
edition = "2021"
authors = ["Elad-Kaplan <kaplan.elad@gmail.com>"]
license = "MIT"
Expand All @@ -18,13 +18,13 @@ dirs = "4.0"
anyhow = "1.0.51"
serde = "1.0"
serde_yaml = "0.8.21"
serde_derive="1.0.131"
serde_derive = "1.0.131"
serde_regex = "1.1.0"
regex = "1.5"
rayon = "1.5.1"
rand = "0.8.4"
log="0.4.14"
env_logger="0.9.0"
log = "0.4.14"
env_logger = "0.9.0"
console = "^0.15.0"
exitcode = "^1.1.2"
lazy_static = "1.4.0"
Expand All @@ -46,4 +46,4 @@ cli = ["clap"]

[[bin]]
name = "shellfirm"
required-features = ["cli"]
required-features = ["cli"]
4 changes: 2 additions & 2 deletions shellfirm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() -> Result<()> {

let dest_checks_path = Path::new(&out_dir).join("all-checks.yaml");
let dest_groups = Path::new(&out_dir).join("all_the_files.rs");
let mut groups_names = File::create(&dest_groups)?;
let mut groups_names = File::create(dest_groups)?;

writeln!(&mut groups_names, r##"["##,)?;

Expand All @@ -26,7 +26,7 @@ fn main() -> Result<()> {
.unwrap()
.to_str()
.expect("could not get file name");
writeln!(&mut groups_names, r##""{name}","##, name = file_name)?;
writeln!(&mut groups_names, r##""{file_name}","##)?;
}

writeln!(&mut groups_names, r##"]"##,)?;
Expand Down
2 changes: 1 addition & 1 deletion shellfirm/checks/fs.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- from: fs
test: 'rm\s{1,}(-r|-fr|-rf)\s*(\*|\.{1,}|/)\s*$'
test: 'rm\s{1,}(-R|-r|-fR|-fr|-Rf|-rf)\s*(\*|\.{1,}|/)\s*$'
description: "You are going to delete everything in the path."
id: fs:recursively_delete
filters:
Expand Down
10 changes: 5 additions & 5 deletions shellfirm/src/bin/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn run_update_groups(
}),
Err(e) => Ok(shellfirm::CmdExit {
code: exitcode::CONFIG,
message: Some(format!("Could not update checks group. error: {}", e)),
message: Some(format!("Could not update checks group. error: {e}")),
}),
}
}
Expand All @@ -77,7 +77,7 @@ pub fn run_reset(config: &Config, force_selection: Option<usize>) -> shellfirm::
},
Err(e) => shellfirm::CmdExit {
code: exitcode::CONFIG,
message: Some(format!("reset settings error: {:?}", e)),
message: Some(format!("reset settings error: {e:?}")),
},
}
}
Expand All @@ -97,7 +97,7 @@ pub fn run_challenge(config: &Config, challenge: Option<Challenge>) -> Result<sh
}),
Err(e) => Ok(shellfirm::CmdExit {
code: exitcode::CONFIG,
message: Some(format!("change challenge error: {:?}", e)),
message: Some(format!("change challenge error: {e:?}")),
}),
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ pub fn run_ignore(
}),
Err(e) => Ok(shellfirm::CmdExit {
code: exitcode::CONFIG,
message: Some(format!("update pattern ignore errors: {:?}", e)),
message: Some(format!("update pattern ignore errors: {e:?}")),
}),
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ pub fn run_deny(
}),
Err(e) => Ok(shellfirm::CmdExit {
code: exitcode::CONFIG,
message: Some(format!("update pattern ignore errors: {:?}", e)),
message: Some(format!("update pattern ignore errors: {e:?}")),
}),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
source: shellfirm/src/bin/cmd/command.rs
expression: "run_pre_command(\"rm -rf /\", &settings, &settings.get_active_checks().unwrap(),\n true)"
expression: "execute(\"rm -rf /\", &settings, &settings.get_active_checks().unwrap(), true)"
---
Ok(
CmdExit {
code: 0,
message: Some(
"---\n- id: \"fs:recursively_delete\"\n test: \"rm\\\\s{1,}(-r|-fr|-rf)\\\\s*(\\\\*|\\\\.{1,}|/)\\\\s*$\"\n description: You are going to delete everything in the path.\n from: fs\n challenge: Math\n filters:\n IsExists: \"3\"\n",
"---\n- id: \"fs:recursively_delete\"\n test: \"rm\\\\s{1,}(-R|-r|-fR|-fr|-Rf|-rf)\\\\s*(\\\\*|\\\\.{1,}|/)\\\\s*$\"\n description: You are going to delete everything in the path.\n from: fs\n challenge: Math\n filters:\n IsExists: \"3\"\n",
),
},
)
4 changes: 2 additions & 2 deletions shellfirm/src/bin/shellfirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
let config = match Config::new(None) {
Ok(config) => config,
Err(err) => {
eprintln!("Loading config error: {}", err);
eprintln!("Loading config error: {err}");
exit(1)
}
};
Expand All @@ -51,7 +51,7 @@ fn main() {
let checks = match settings.get_active_checks() {
Ok(c) => c,
Err(e) => {
eprintln!("Could not load checks. err: Error: {}", e);
eprintln!("Could not load checks. err: Error: {e}");
exit(1)
}
};
Expand Down
2 changes: 1 addition & 1 deletion shellfirm/src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn challenge(
}

for description in descriptions {
eprintln!("* {}", description);
eprintln!("* {description}");
}
eprintln!();

Expand Down
4 changes: 2 additions & 2 deletions shellfirm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct Settings {
}

impl fmt::Display for Challenge {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Math => write!(f, "Math"),
Self::Enter => write!(f, "Enter"),
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Config {
// compatibility if the folder $HOME/.shellfirm exists, shillfirm
// continue work with that folder. If the folder does not exists, the default
// use config dir
let homedir = p.join(format!(".{}", package_name));
let homedir = p.join(format!(".{package_name}"));
let conf_dir = dirs::config_dir().unwrap_or_else(|| homedir.clone());
if homedir.is_dir() {
homedir
Expand Down
6 changes: 3 additions & 3 deletions shellfirm/src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn math_challenge() -> bool {
if answer == expected_answer {
break;
}
eprintln!("{}", WRONG_ANSWER);
eprintln!("{WRONG_ANSWER}");
}
true
}
Expand All @@ -53,7 +53,7 @@ pub fn enter_challenge() -> bool {
if answer == "\n" {
break;
}
eprintln!("{}", WRONG_ANSWER);
eprintln!("{WRONG_ANSWER}");
}
true
}
Expand All @@ -65,7 +65,7 @@ pub fn yes_challenge() -> bool {
if show_stdin_prompt().trim() == "yes" {
break;
}
eprintln!("{}", WRONG_ANSWER);
eprintln!("{WRONG_ANSWER}");
}
true
}
Expand Down
4 changes: 4 additions & 0 deletions shellfirm/tests/checks/fs-recursively_delete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
description: folder not found
- test: rmm -rf /
description: folder not found
- test: rm -Rf /
description: Handle capital R
- test: rm -FR /
description: invalid command
14 changes: 14 additions & 0 deletions shellfirm/tests/snapshots/checks__fs-recursively_delete.yaml.snap
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@ expression: test_file_results
check_detection_ids: [],
test_description: "folder not found",
},
TestSensitivePatternsResult {
file_path: "fs-recursively_delete.yaml",
test: "rm -Rf /",
check_detection_ids: [
"fs:recursively_delete",
],
test_description: "Handle capital R",
},
TestSensitivePatternsResult {
file_path: "fs-recursively_delete.yaml",
test: "rm -FR /",
check_detection_ids: [],
test_description: "invalid command",
},
]

0 comments on commit 7e1f031

Please sign in to comment.