From 96009644e1a6ef75b97d15560419d9456a005d46 Mon Sep 17 00:00:00 2001 From: Abhijay Jain Date: Tue, 22 Sep 2026 04:17:16 +0000 Subject: [PATCH] feat(recipe): enforce parameter limits (max 32 params, 200 select options, 128 KiB) (#12259) --- crates/goose/src/recipe/validate_recipe.rs | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/crates/goose/src/recipe/validate_recipe.rs b/crates/goose/src/recipe/validate_recipe.rs index ecfc22802..e9f035cf1 100644 --- a/crates/goose/src/recipe/validate_recipe.rs +++ b/crates/goose/src/recipe/validate_recipe.rs @@ -12,6 +12,10 @@ use serde_path_to_error::Segment; use std::collections::{HashMap, HashSet}; use std::path::Path; +const MAX_PARAMETERS: usize = 32; +const MAX_TOTAL_SELECT_OPTIONS: usize = 200; +const MAX_PARAMETERS_SIZE_BYTES: usize = 128 * 1024; + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum RecipeFileFormat { Json, @@ -289,6 +293,7 @@ fn validate_scheduling_parameters( template_variables: &HashSet, ) -> Result<(), String> { let parameters = parameters.as_deref().unwrap_or_default(); + validate_parameter_limits(parameters).map_err(|e| e.to_string())?; let file_defaults = parameters .iter() @@ -585,9 +590,43 @@ fn validate_parameters_in_template( Err(anyhow::anyhow!("{}", message.trim_end())) } +fn validate_parameter_limits(parameters: &[RecipeParameter]) -> Result<()> { + if parameters.len() > MAX_PARAMETERS { + return Err(anyhow::anyhow!( + "Recipe has {} parameters but the maximum is {}.", + parameters.len(), + MAX_PARAMETERS + )); + } + + let total_options: usize = parameters + .iter() + .map(|p| p.options.as_deref().unwrap_or_default().len()) + .sum(); + if total_options > MAX_TOTAL_SELECT_OPTIONS { + return Err(anyhow::anyhow!( + "Recipe has {} total select options but the maximum is {}.", + total_options, + MAX_TOTAL_SELECT_OPTIONS + )); + } + + let serialized = serde_yaml::to_string(¶meters) + .map_err(|e| anyhow::anyhow!("Failed to serialize parameters: {}", e))?; + if serialized.len() > MAX_PARAMETERS_SIZE_BYTES { + return Err(anyhow::anyhow!( + "Serialized parameters exceed the {} KiB limit.", + MAX_PARAMETERS_SIZE_BYTES / 1024 + )); + } + + Ok(()) +} + fn validate_optional_parameters(parameters: &Option>) -> Result<()> { let empty_params = vec![]; let params = parameters.as_ref().unwrap_or(&empty_params); + validate_parameter_limits(params)?; let file_params_with_defaults: Vec = params .iter() @@ -631,6 +670,87 @@ parameters: ) } + fn make_string_param(key: &str) -> String { + format!( + " - key: {key}\n input_type: string\n requirement: required\n description: p\n" + ) + } + + fn recipe_with_parameters(parameters: &str) -> String { + let vars: String = parameters + .lines() + .filter_map(|l| { + let trimmed = l.trim_start(); + trimmed + .strip_prefix("- key: ") + .map(|k| format!("{{{{ {k} }}}} ")) + }) + .collect(); + format!( + "version: 1.0.0\ntitle: T\ndescription: D\ninstructions: use {vars}\nparameters:\n{parameters}\n" + ) + } + + #[test] + fn rejects_too_many_parameters() { + let params: String = (0..=MAX_PARAMETERS) + .map(|i| make_string_param(&format!("p{i}"))) + .collect(); + let recipe_content = recipe_with_parameters(¶ms); + let error = validate_recipe_template_from_content(&recipe_content, None).unwrap_err(); + assert!(error.to_string().contains("maximum is 32"), "{}", error); + } + + #[test] + fn accepts_exactly_max_parameters() { + let params: String = (0..MAX_PARAMETERS) + .map(|i| make_string_param(&format!("p{i}"))) + .collect(); + let recipe_content = recipe_with_parameters(¶ms); + assert!(validate_recipe_template_from_content(&recipe_content, None).is_ok()); + } + + #[test] + fn rejects_too_many_total_select_options() { + let options: String = (0..=MAX_TOTAL_SELECT_OPTIONS) + .map(|i| format!(" - opt{i}\n")) + .collect(); + let param = format!(" - key: choice\n input_type: select\n requirement: required\n description: p\n options:\n{options}"); + let recipe_content = recipe_with_parameters(¶m); + let error = validate_recipe_template_from_content(&recipe_content, None).unwrap_err(); + assert!(error.to_string().contains("maximum is 200"), "{}", error); + } + + #[test] + fn rejects_oversized_serialized_parameters() { + let long_desc = "x".repeat(MAX_PARAMETERS_SIZE_BYTES); + let param = format!(" - key: p\n input_type: string\n requirement: required\n description: \"{long_desc}\"\n"); + let recipe_content = recipe_with_parameters(¶m); + let error = validate_recipe_template_from_content(&recipe_content, None).unwrap_err(); + assert!(error.to_string().contains("128 KiB"), "{}", error); + } + + #[test] + fn scheduling_rejects_too_many_parameters() { + let params: String = (0..=MAX_PARAMETERS) + .map(|i| make_string_param(&format!("p{i}"))) + .collect(); + let recipe_content = recipe_with_parameters(¶ms); + let error = scheduling_error(&recipe_content); + assert!(error.to_string().contains("maximum is 32"), "{}", error); + } + + #[test] + fn scheduling_rejects_too_many_total_select_options() { + let options: String = (0..=MAX_TOTAL_SELECT_OPTIONS) + .map(|i| format!(" - opt{i}\n")) + .collect(); + let param = format!(" - key: choice\n input_type: select\n requirement: required\n description: p\n options:\n{options}"); + let recipe_content = recipe_with_parameters(¶m); + let error = scheduling_error(&recipe_content); + assert!(error.to_string().contains("maximum is 200"), "{}", error); + } + #[test] fn test_rejects_string_then_file_parameter_with_same_key() { let recipe_content = recipe_with_duplicate_parameter_keys(