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

fix(turbo): validate graph extension #6995

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub const INVOCATION_DIR_ENV_VAR: &str = "TURBO_INVOCATION_DIR";

// Default value for the --cache-workers argument
const DEFAULT_NUM_WORKERS: u32 = 10;
const SUPPORTED_GRAPH_FILE_EXTENSIONS: [&str; 7] =
["svg", "png", "jpg", "pdf", "json", "html", "mermaid"];

#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize, ValueEnum)]
pub enum OutputLogsMode {
Expand Down Expand Up @@ -580,6 +582,23 @@ pub enum GenerateCommand {
Run(GeneratorCustomArgs),
}

fn validate_graph_extension(s: &str) -> Result<String, String> {
match s.is_empty() {
true => Ok(s.to_string()),
_ => match std::path::Path::new(s).extension().and_then(|e| e.to_str()) {
tknickman marked this conversation as resolved.
Show resolved Hide resolved
Some(ext) if SUPPORTED_GRAPH_FILE_EXTENSIONS.contains(&ext) => Ok(s.to_string()),
Some(ext) => Err(format!(
"Invalid file extension: '{}'. Allowed extensions are: {:?}",
ext, SUPPORTED_GRAPH_FILE_EXTENSIONS
)),
None => Err(format!(
"The provided filename is missing a file extension. Allowed extensions are: {:?}",
SUPPORTED_GRAPH_FILE_EXTENSIONS
)),
},
}
}

#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
#[command(groups = [
ArgGroup::new("daemon-group").multiple(false).required(false),
Expand Down Expand Up @@ -622,7 +641,7 @@ pub struct RunArgs {
/// Generate a graph of the task execution and output to a file when a
/// filename is specified (.svg, .png, .jpg, .pdf, .json,
/// .html). Outputs dot graph to stdout when if no filename is provided
#[clap(long, num_args = 0..=1, default_missing_value = "")]
#[clap(long, num_args = 0..=1, default_missing_value = "", value_parser = validate_graph_extension)]
pub graph: Option<String>,
/// Environment variable mode.
/// Use "loose" to pass the entire existing environment.
Expand Down