Bind plugin hook approval to plugin contents (#54619)

## Why

A plugin's scripts can change without changing its hook declaration. Declaration-only approval therefore lets changed plugin code run under an existing hook approval.

## What changed

- Fingerprint plugin bundle contents and include the content hash in the approval value returned by `hooks/list`. Persist it as `trusted_content_hash`, keeping `trusted_hash` compatible with older clients.
- Verify contents before each command or MCP hook execution, within the hook's timeout, and require renewed approval when approved contents change.
- Migrate legacy declaration-only approvals to a shared, immutable content baseline per plugin and config home.
- Exclude generated Python bytecode, `.venv`, and `.egg-info` entries from fingerprints. Run app-server hook listing on blocking tasks.

## Testing

Extend integration coverage for plugin upgrades and rollbacks requiring approval, same-turn script changes preventing further hook execution, shared legacy approval baselines, and generated Python artifacts preserving trust.

GitOrigin-RevId: faaf7658210d8adb2534ddd614c72ec993920840
This commit is contained in:
jif
2026-10-16 14:38:19 +00:00
committed by copyberry
parent 4eb4a65407
commit 2bf2bdf82c
31 changed files with 652 additions and 115 deletions
+28 -2
View File
@@ -216,6 +216,28 @@ pub(crate) async fn run_command(
) -> HandlerRunResult {
let started_at = chrono::Utc::now().timestamp();
let started = Instant::now();
let timeout_started = tokio::time::Instant::now();
let duration = Duration::from_secs(handler.timeout_sec);
if let Some(trust) = &handler.plugin_trust {
let error = match timeout(duration, trust.verify()).await {
Ok(Ok(())) => None,
Ok(Err(err)) => Some(err.to_string()),
Err(_) => Some(format!("hook timed out after {}s", handler.timeout_sec)),
};
if let Some(error) = error {
return finish_command_run(
started_at,
started,
CommandRunCompletion {
exit_code: None,
stdout: String::new(),
stderr: String::new(),
error: Some(error),
outcome: "trust_error",
},
);
}
}
let mut command = build_command(&runtime.shell, command_line, &runtime.environment, env);
command.current_dir(cwd);
@@ -268,10 +290,14 @@ pub(crate) async fn run_command(
.map_err(|err| ("wait_error", err.to_string()))
};
let timeout_duration = Duration::from_secs(handler.timeout_sec);
// Drain output while sending input so neither pipe can block the other, and
// include stdin writes in the deadline even when the hook never reads them.
match timeout(timeout_duration, try_join(write_stdin, wait_with_output)).await {
match timeout(
duration.saturating_sub(timeout_started.elapsed()),
try_join(write_stdin, wait_with_output),
)
.await
{
Ok(Ok(((), output))) => {
// Successfully completed hooks may intentionally leave detached helpers running.
#[cfg(windows)]