fix(security): show authoritative tool approval details (#11743)
This commit is contained in:
@@ -48,7 +48,9 @@ use strum::VariantNames;
|
||||
|
||||
use goose::config::paths::Paths;
|
||||
use goose::config::providers;
|
||||
use goose::conversation::message::{ActionRequiredData, Message, MessageContent};
|
||||
use goose::conversation::message::{
|
||||
ActionRequiredData, Message, MessageContent, ToolConfirmationRequest,
|
||||
};
|
||||
use goose::providers::inventory::ProviderInventoryService;
|
||||
use goose::session::SessionManager;
|
||||
use rustyline::EditMode;
|
||||
@@ -1586,9 +1588,9 @@ impl CliSession {
|
||||
if first_token_at.is_none() && message_has_text(&message) {
|
||||
first_token_at = Some(Instant::now());
|
||||
}
|
||||
if let Some((id, security_prompt)) = find_tool_confirmation(&message) {
|
||||
if let Some(confirmation_request) = find_tool_confirmation(&message) {
|
||||
let selected_permission = if interactive {
|
||||
prompt_tool_confirmation(&security_prompt)?
|
||||
prompt_tool_confirmation(&confirmation_request)?
|
||||
} else {
|
||||
// Non-interactive/headless mode: refuse to run in
|
||||
// Approve/SmartApprove modes since auto-allowing would
|
||||
@@ -1617,14 +1619,14 @@ impl CliSession {
|
||||
self.agent
|
||||
.submit_tool_confirmation(
|
||||
&self.session_id,
|
||||
&id,
|
||||
&confirmation_request.id,
|
||||
selected_permission,
|
||||
)
|
||||
.await?;
|
||||
if cancelled_by_user {
|
||||
let mut response_message = Message::user();
|
||||
response_message.content.push(MessageContent::tool_response(
|
||||
id,
|
||||
confirmation_request.id,
|
||||
Err(ErrorData {
|
||||
code: ErrorCode::INVALID_REQUEST,
|
||||
message: std::borrow::Cow::from(
|
||||
@@ -2408,17 +2410,21 @@ fn emit_stream_event(event: &StreamEvent) {
|
||||
}
|
||||
|
||||
/// Prompt user for tool call confirmation, returns the Permission selected
|
||||
fn prompt_tool_confirmation(security_prompt: &Option<String>) -> Result<Permission> {
|
||||
fn prompt_tool_confirmation(request: &ToolConfirmationRequest) -> Result<Permission> {
|
||||
output::hide_thinking();
|
||||
|
||||
let prompt = if let Some(security_message) = security_prompt {
|
||||
println!("\n{}", security_message);
|
||||
output::render_tool_confirmation(
|
||||
&request.tool_name,
|
||||
&request.arguments,
|
||||
request.prompt.as_deref(),
|
||||
);
|
||||
let prompt = if request.prompt.is_some() {
|
||||
"Do you allow this tool call?".to_string()
|
||||
} else {
|
||||
"Goose would like to call the above tool, do you allow?".to_string()
|
||||
};
|
||||
|
||||
let permission_result = if security_prompt.is_none() {
|
||||
let permission_result = if request.prompt.is_none() {
|
||||
cliclack::select(prompt)
|
||||
.item(Permission::AllowOnce, "Allow", "Allow the tool call once")
|
||||
.item(
|
||||
@@ -2458,11 +2464,22 @@ fn prompt_tool_confirmation(security_prompt: &Option<String>) -> Result<Permissi
|
||||
}
|
||||
|
||||
/// Extract tool confirmation request from a message
|
||||
fn find_tool_confirmation(message: &Message) -> Option<(String, Option<String>)> {
|
||||
fn find_tool_confirmation(message: &Message) -> Option<ToolConfirmationRequest> {
|
||||
message.content.iter().find_map(|content| {
|
||||
if let MessageContent::ActionRequired(action) = content {
|
||||
if let ActionRequiredData::ToolConfirmation { id, prompt, .. } = &action.data {
|
||||
return Some((id.clone(), prompt.clone()));
|
||||
if let ActionRequiredData::ToolConfirmation {
|
||||
id,
|
||||
tool_name,
|
||||
arguments,
|
||||
prompt,
|
||||
} = &action.data
|
||||
{
|
||||
return Some(ToolConfirmationRequest {
|
||||
id: id.clone(),
|
||||
tool_name: tool_name.clone(),
|
||||
arguments: arguments.clone(),
|
||||
prompt: prompt.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
@@ -2905,10 +2922,32 @@ mod tests {
|
||||
use super::*;
|
||||
use goose::agents::extension::Envs;
|
||||
use goose::config::ExtensionConfig;
|
||||
use serde_json::json;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
use test_case::test_case;
|
||||
|
||||
#[test]
|
||||
fn provider_only_confirmation_preserves_authoritative_request() {
|
||||
let arguments = json!({"command": "cat ~/.ssh/id_rsa"})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone();
|
||||
let message = Message::assistant().with_action_required(
|
||||
"provider-request",
|
||||
"Bash".to_string(),
|
||||
arguments.clone(),
|
||||
Some("Review this request".to_string()),
|
||||
);
|
||||
|
||||
let request = find_tool_confirmation(&message).unwrap();
|
||||
|
||||
assert_eq!(request.id, "provider-request");
|
||||
assert_eq!(request.tool_name, "Bash");
|
||||
assert_eq!(request.arguments, arguments);
|
||||
assert_eq!(request.prompt.as_deref(), Some("Review this request"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn planner_classification_excludes_user_only_content() {
|
||||
use rmcp::model::{Annotations, Role, TextContent};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::session::builder::ExtensionFailure;
|
||||
use anstream::{adapter::strip_str, println};
|
||||
use anstream::{adapter::strip_str, eprintln, println};
|
||||
use bat::WrappingMode;
|
||||
use console::{measure_text_width, style, Color, StyledObject, Term};
|
||||
use goose::config::Config;
|
||||
@@ -652,6 +652,68 @@ pub(super) fn sanitize_terminal_line(line: &str) -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn sanitize_tool_confirmation_line(line: &str) -> String {
|
||||
let mut sanitized = String::with_capacity(line.len());
|
||||
for character in sanitize_terminal_line(line).chars() {
|
||||
if matches!(
|
||||
character,
|
||||
'\u{061c}'
|
||||
| '\u{200e}'
|
||||
| '\u{200f}'
|
||||
| '\u{202a}'..='\u{202e}'
|
||||
| '\u{2066}'..='\u{2069}'
|
||||
) {
|
||||
sanitized.push_str(&format!("\\u{:04x}", character as u32));
|
||||
} else {
|
||||
sanitized.push(character);
|
||||
}
|
||||
}
|
||||
sanitized
|
||||
}
|
||||
|
||||
fn sanitize_tool_confirmation_text(text: &str) -> String {
|
||||
text.split('\n')
|
||||
.map(sanitize_tool_confirmation_line)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
pub(super) fn format_tool_confirmation(tool_name: &str, arguments: &JsonObject) -> String {
|
||||
let mut request = JsonObject::new();
|
||||
request.insert(
|
||||
"tool_name".to_string(),
|
||||
Value::String(tool_name.to_string()),
|
||||
);
|
||||
request.insert("arguments".to_string(), Value::Object(arguments.clone()));
|
||||
|
||||
serde_json::to_string_pretty(&Value::Object(request))
|
||||
.expect("tool confirmation request must serialize")
|
||||
.lines()
|
||||
.map(sanitize_tool_confirmation_line)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
pub(super) fn render_tool_confirmation(
|
||||
tool_name: &str,
|
||||
arguments: &JsonObject,
|
||||
security_prompt: Option<&str>,
|
||||
) {
|
||||
eprintln!();
|
||||
if let Some(security_prompt) = security_prompt {
|
||||
eprintln!(" {}", style("Provider-provided approval notice").bold());
|
||||
for line in sanitize_tool_confirmation_text(security_prompt).split('\n') {
|
||||
eprintln!(" {}", style(line).dim());
|
||||
}
|
||||
eprintln!();
|
||||
}
|
||||
eprintln!(" {}", style("Tool approval request").bold());
|
||||
for line in format_tool_confirmation(tool_name, arguments).lines() {
|
||||
eprintln!(" {}", style(line).dim());
|
||||
}
|
||||
eprintln!();
|
||||
}
|
||||
|
||||
fn print_tool_output_line(line: &str) {
|
||||
println!(" {}", style(sanitize_terminal_line(line)).dim());
|
||||
}
|
||||
@@ -1709,6 +1771,171 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_confirmation_shows_execute_code_and_graph() {
|
||||
let arguments = json!({
|
||||
"code": "await developer.shell({ command: \"cat ~/.ssh/id_rsa\" });",
|
||||
"tool_graph": [{
|
||||
"tool": "developer/read",
|
||||
"description": "read package manifest",
|
||||
"depends_on": []
|
||||
}]
|
||||
})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let rendered = format_tool_confirmation("execute_typescript", &arguments);
|
||||
|
||||
assert!(rendered.contains("execute_typescript"));
|
||||
assert!(rendered.contains("read package manifest"));
|
||||
assert!(rendered.contains("cat ~/.ssh/id_rsa"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_confirmation_shows_load_arguments() {
|
||||
let arguments = json!({"source": "private-recipe", "cancel": true})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let rendered = format_tool_confirmation("load", &arguments);
|
||||
|
||||
assert!(rendered.contains("\"tool_name\": \"load\""));
|
||||
assert!(rendered.contains("\"source\": \"private-recipe\""));
|
||||
assert!(rendered.contains("\"cancel\": true"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_confirmation_does_not_truncate_delegate_instructions() {
|
||||
let instructions = format!("{} then run the final delegated command", "A".repeat(120));
|
||||
let arguments = json!({"instructions": instructions.clone()})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let rendered = format_tool_confirmation("delegate", &arguments);
|
||||
|
||||
assert!(rendered.contains(&instructions));
|
||||
assert!(!rendered.contains('…'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_confirmation_escapes_terminal_controls() {
|
||||
let arguments = json!({
|
||||
"command": "echo safe\u{1b}[2J\u{1b}]0;spoofed title\u{7}"
|
||||
})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let rendered = format_tool_confirmation("Bash\u{1b}[31m", &arguments);
|
||||
|
||||
assert!(rendered.contains("Bash\\u001b[31m"));
|
||||
assert!(rendered.contains("safe\\u001b[2J"));
|
||||
assert!(rendered.contains("title\\u0007"));
|
||||
assert!(!rendered.contains('\u{1b}'));
|
||||
assert!(!rendered.contains('\u{7}'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_confirmation_escapes_bidi_controls() {
|
||||
let controls = [
|
||||
'\u{061c}', '\u{200e}', '\u{200f}', '\u{202a}', '\u{202b}', '\u{202c}', '\u{202d}',
|
||||
'\u{202e}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{2069}',
|
||||
];
|
||||
let untrusted = controls.iter().collect::<String>();
|
||||
let arguments = json!({"command": format!("before{untrusted}after")})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let rendered = format_tool_confirmation(&format!("tool{untrusted}"), &arguments);
|
||||
|
||||
for control in controls {
|
||||
assert!(!rendered.contains(control));
|
||||
assert!(rendered.contains(&format!("\\u{:04x}", control as u32)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_confirmation_preserves_plain_unicode_text() {
|
||||
let arguments = json!({"query": "שלום مرحبا 日本語 🪿"})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let rendered = format_tool_confirmation("検索", &arguments);
|
||||
|
||||
assert!(rendered.contains("שלום مرحبا 日本語 🪿"));
|
||||
assert!(rendered.contains("検索"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_confirmation_sanitizes_unterminated_control_strings() {
|
||||
let rendered = sanitize_tool_confirmation_text(
|
||||
"visible bidi\u{202e}\nvisible OSC\u{1b}]unterminated\nvisible DCS\u{1b}Punterminated",
|
||||
);
|
||||
|
||||
assert!(rendered.contains("visible OSC"));
|
||||
assert!(rendered.contains("visible DCS"));
|
||||
assert!(!rendered.contains('\u{1b}'));
|
||||
assert!(!rendered.contains('\u{202e}'));
|
||||
assert!(rendered.contains("\\u202e"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_confirmation_renders_authoritative_details_on_stderr() {
|
||||
const CHILD_ENV: &str = "GOOSE_TEST_TOOL_CONFIRMATION_STDERR_CHILD";
|
||||
const AUTHORITATIVE_TOKEN: &str = "authoritative-redirect-token";
|
||||
const PROVIDER_TOKEN: &str = "provider-prompt-token";
|
||||
|
||||
if env::var_os(CHILD_ENV).is_some() {
|
||||
let forged_request = (0..80)
|
||||
.map(|line| format!("forged safe request line {line}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
let provider_prompt = format!(
|
||||
"{forged_request}\n{PROVIDER_TOKEN} OSC\u{1b}]unterminated\nDCS\u{1b}Punterminated"
|
||||
);
|
||||
let arguments = json!({"command": AUTHORITATIVE_TOKEN})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone();
|
||||
render_tool_confirmation("shell", &arguments, Some(&provider_prompt));
|
||||
return;
|
||||
}
|
||||
|
||||
let output = std::process::Command::new(env::current_exe().unwrap())
|
||||
.args([
|
||||
"--exact",
|
||||
"session::output::tests::tool_confirmation_renders_authoritative_details_on_stderr",
|
||||
"--nocapture",
|
||||
])
|
||||
.env(CHILD_ENV, "1")
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(output.status.success());
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(!stdout.contains(AUTHORITATIVE_TOKEN));
|
||||
assert!(!stdout.contains(PROVIDER_TOKEN));
|
||||
assert!(stderr.contains(AUTHORITATIVE_TOKEN));
|
||||
assert!(stderr.contains(PROVIDER_TOKEN));
|
||||
assert!(stderr.contains("forged safe request line 79"));
|
||||
let provider_notice = stderr.find("Provider-provided approval notice").unwrap();
|
||||
let provider_content = stderr.find(PROVIDER_TOKEN).unwrap();
|
||||
let authoritative_block = stderr.find("Tool approval request").unwrap();
|
||||
assert!(provider_notice < provider_content);
|
||||
assert!(provider_content < authoritative_block);
|
||||
let authoritative_tail = stderr.get(authoritative_block..).unwrap();
|
||||
assert!(authoritative_tail.contains(AUTHORITATIVE_TOKEN));
|
||||
assert!(!authoritative_tail.contains(PROVIDER_TOKEN));
|
||||
assert!(!stderr.contains("\u{1b}]"));
|
||||
assert!(!stderr.contains("\u{1b}P"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formats_subagent_tool_call_names() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user