-
Notifications
You must be signed in to change notification settings - Fork 0
fix(cli): reject unknown flags before -- in exec/check subcommands
#202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
96aec0a
cli: reject unknown flags before `--` in exec and check subcommands
fohte ad84b3a
cli/validate: address review feedback for unknown flag validation
fohte 9c1c522
cli/validate: allow clap built-in flags (--help, --version) through v…
fohte df21773
cli/validate: use shlex::split instead of split_whitespace in test he…
fohte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /// Validate that no unknown flags appear before `--` in the raw CLI arguments | ||
| /// for `exec` and `check` subcommands. | ||
| /// | ||
| /// Because `trailing_var_arg = true` + `allow_hyphen_values = true` causes clap | ||
| /// to silently absorb unknown flags into the `command` Vec, we must perform this | ||
| /// check ourselves using the raw process arguments. | ||
| pub fn validate_no_unknown_flags(raw_args: &[String], subcommand: &str) -> Result<(), String> { | ||
| let known_flags = match subcommand { | ||
| "exec" => &["--sandbox", "--dry-run", "--verbose"] as &[&str], | ||
| "check" => &["--input-format", "--output-format", "--verbose"] as &[&str], | ||
| _ => return Ok(()), | ||
| }; | ||
|
|
||
| // Flags that take a value argument (the next token is consumed as a value) | ||
| let flags_with_value = match subcommand { | ||
| "exec" => &["--sandbox"] as &[&str], | ||
| "check" => &["--input-format", "--output-format"] as &[&str], | ||
| _ => return Ok(()), | ||
| }; | ||
|
|
||
| // Find the subcommand position in raw args | ||
| let sub_pos = match raw_args.iter().position(|a| a == subcommand) { | ||
| Some(pos) => pos, | ||
| None => return Ok(()), | ||
| }; | ||
|
|
||
| // Get args after the subcommand | ||
| let after_sub = &raw_args[sub_pos + 1..]; | ||
|
|
||
| // Find `--` position (relative to after_sub) | ||
| let double_dash_pos = after_sub.iter().position(|a| a == "--"); | ||
|
|
||
| // The region to check is everything before `--` (or everything if no `--`) | ||
| let region = match double_dash_pos { | ||
| Some(pos) => &after_sub[..pos], | ||
| None => after_sub, | ||
| }; | ||
|
|
||
| // Walk the region, skipping values of known flags | ||
| let mut i = 0; | ||
| // Track whether we've seen a non-flag token (the command name). | ||
| // Once we see a command name, subsequent tokens are command arguments, not runok flags. | ||
| let mut seen_command_name = false; | ||
|
|
||
| while i < region.len() { | ||
| let token = ®ion[i]; | ||
|
|
||
| if seen_command_name { | ||
| // After the command name, everything is a command argument | ||
| break; | ||
| } | ||
|
|
||
| if token.starts_with('-') { | ||
| // Check if it's a known flag (exact match or `--flag=value` form) | ||
| let is_known = known_flags | ||
| .iter() | ||
| .any(|f| token == *f || token.starts_with(&format!("{f}="))); | ||
|
|
||
| if !is_known { | ||
| return Err(format!( | ||
| "unknown flag '{token}' for 'runok {subcommand}'. \ | ||
| Use '--' to separate runok flags from the command: \ | ||
| runok {subcommand} [OPTIONS] -- <COMMAND>" | ||
| )); | ||
| } | ||
|
|
||
| // If this known flag takes a value and isn't `--flag=value` form, skip the next token | ||
| let takes_value = flags_with_value.iter().any(|f| token == *f); | ||
| if takes_value && !token.contains('=') { | ||
| i += 1; // skip value | ||
| } | ||
| } else { | ||
| // Non-flag token: this is the start of the command | ||
| seen_command_name = true; | ||
| } | ||
|
|
||
| i += 1; | ||
| } | ||
|
fohte marked this conversation as resolved.
Outdated
|
||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use rstest::rstest; | ||
|
|
||
| fn args(s: &str) -> Vec<String> { | ||
| s.split_whitespace().map(String::from).collect() | ||
| } | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||
|
|
||
| // === Valid cases (should pass) === | ||
|
|
||
| #[rstest] | ||
| #[case::exec_with_double_dash("runok exec -- ls -la")] | ||
| #[case::exec_known_flag_before_double_dash("runok exec --verbose -- ls -la")] | ||
| #[case::exec_sandbox_with_value("runok exec --sandbox strict -- ls")] | ||
| #[case::exec_sandbox_eq_form("runok exec --sandbox=strict -- ls")] | ||
| #[case::exec_dry_run("runok exec --dry-run -- git status")] | ||
| #[case::exec_all_flags("runok exec --sandbox strict --dry-run --verbose -- ls")] | ||
| #[case::exec_command_without_double_dash("runok exec ls -la")] | ||
| #[case::exec_command_with_flag_args("runok exec git log --oneline")] | ||
| #[case::check_with_double_dash("runok check -- ls -la")] | ||
| #[case::check_known_flags("runok check --input-format claude-code-hook --verbose -- ls")] | ||
| #[case::check_output_format("runok check --output-format json -- ls")] | ||
| #[case::check_command_without_double_dash("runok check ls -la")] | ||
| #[case::check_no_args("runok check")] | ||
| #[case::unknown_after_double_dash("runok exec -- --unknown-flag ls")] | ||
| #[case::check_unknown_after_double_dash("runok check -- --config foo break")] | ||
| fn valid_args(#[case] input: &str) { | ||
| let raw = args(input); | ||
| let subcommand = if input.contains("exec") { | ||
| "exec" | ||
| } else { | ||
| "check" | ||
| }; | ||
| assert!( | ||
| validate_no_unknown_flags(&raw, subcommand).is_ok(), | ||
| "expected Ok for: {input}" | ||
| ); | ||
| } | ||
|
|
||
| // === Invalid cases (should fail) === | ||
|
|
||
| #[rstest] | ||
| #[case::exec_unknown_flag("runok exec --unknown -- ls", "exec", "--unknown")] | ||
| #[case::exec_unknown_before_known( | ||
| "runok exec --config foo --verbose -- ls", | ||
| "exec", | ||
| "--config" | ||
| )] | ||
| #[case::check_unknown_flag("runok check --config foo -- break", "check", "--config")] | ||
| #[case::check_unknown_short_flag("runok check -x -- ls", "check", "-x")] | ||
| #[case::exec_unknown_no_double_dash("runok exec --unknown ls", "exec", "--unknown")] | ||
| fn invalid_args(#[case] input: &str, #[case] subcommand: &str, #[case] expected_flag: &str) { | ||
| let raw = args(input); | ||
| let result = validate_no_unknown_flags(&raw, subcommand); | ||
| let err = result.expect_err(&format!("expected Err for: {input}")); | ||
| assert_eq!( | ||
| err, | ||
| format!( | ||
| "unknown flag '{expected_flag}' for 'runok {subcommand}'. \ | ||
| Use '--' to separate runok flags from the command: \ | ||
| runok {subcommand} [OPTIONS] -- <COMMAND>" | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.