Skip to content
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

feat(cli): disallow path pointing to parent directories #2125

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion crates/oxc_cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,20 @@ pub struct LintOptions {
pub config: Option<PathBuf>,

/// Single file, single path or list of paths
#[bpaf(positional("PATH"), many)]
#[bpaf(positional("PATH"), many, guard(validate_paths, PATHS_ERROR_MESSAGE))]
pub paths: Vec<PathBuf>,
}

fn validate_paths(paths: &Vec<PathBuf>) -> bool {
if paths.is_empty() {
true
} else {
paths.iter().all(|p| p.components().next() != Some(std::path::Component::ParentDir))
}
}

const PATHS_ERROR_MESSAGE: &str = "PATH cannot start with \"..\"";

// This is formatted according to
// <https://docs.rs/bpaf/latest/bpaf/params/struct.NamedArg.html#method.help>
/// Allowing / Denying Multiple Lints
Expand Down Expand Up @@ -335,6 +345,19 @@ mod lint_options {
);
}

#[test]
fn no_parent_path() {
match lint_command().run_inner(&["../parent_dir"]) {
Ok(_) => panic!("Should not allow parent dir"),
Err(err) => match err {
bpaf::ParseFailure::Stderr(doc) => {
assert_eq!("`../parent_dir`: PATH cannot start with \"..\"", format!("{doc}"));
}
_ => unreachable!(),
},
}
}

#[test]
fn fix() {
let options = get_lint_options("--fix test.js");
Expand Down