claude_wrapper/dangerous.rs
1//! Opt-in dangerous operations. Currently: bypass permissions.
2//!
3//! Running the `claude` CLI with `--permission-mode bypassPermissions`
4//! turns off all confirmation prompts for tool use. It's legitimate
5//! for some automation -- but it's also the fastest way to turn a bug
6//! into a destructive action.
7//!
8//! This module isolates that capability behind a type you have to
9//! explicitly reach for ([`DangerousClient`]), and a runtime env-var
10//! gate ([`ALLOW_ENV`]) you have to explicitly set.
11//!
12//! # Example
13//!
14//! ```no_run
15//! # async fn example() -> claude_wrapper::Result<()> {
16//! use claude_wrapper::{Claude, QueryCommand};
17//! use claude_wrapper::dangerous::DangerousClient;
18//!
19//! // At process start:
20//! // export CLAUDE_WRAPPER_ALLOW_DANGEROUS=1
21//!
22//! let claude = Claude::builder().build()?;
23//! let dangerous = DangerousClient::new(claude)?;
24//!
25//! let output = dangerous
26//! .query_bypass(QueryCommand::new("clean up the build artifacts"))
27//! .await?;
28//! println!("{}", output.stdout);
29//! # Ok(())
30//! # }
31//! ```
32//!
33//! # Why this shape
34//!
35//! - **Separate type.** `DangerousClient::new` is the only public path
36//! to building a bypassed query. If a reader of calling code sees
37//! `DangerousClient`, the danger is obvious at the call site.
38//! - **Runtime env-var gate.** The check happens at construction, so
39//! a caller who forgot to set the env-var gets a typed error rather
40//! than silently running with bypass off (which might surprise them)
41//! or silently running with bypass on (which might destroy things).
42//! - **Not a cargo feature.** Feature-gating adds a second layer of
43//! friction (recompile) without making the runtime behaviour any
44//! safer. The env-var matches how Go's `claude-code-go/dangerous`
45//! gates the same operation.
46//!
47//! # Migrating from [`crate::PermissionMode::BypassPermissions`]
48//!
49//! The enum variant is kept (marked `#[deprecated]`) so existing
50//! callers continue to compile with a warning. New code should go
51//! through `DangerousClient`.
52
53use crate::Claude;
54#[cfg(feature = "async")]
55use crate::command::ClaudeCommand;
56use crate::command::query::QueryCommand;
57use crate::error::{Error, Result};
58use crate::exec::CommandOutput;
59#[allow(deprecated)]
60use crate::types::PermissionMode;
61
62/// The env-var that must equal `"1"` at process start for
63/// [`DangerousClient::new`] to succeed. Set deliberately -- this is
64/// the explicit acknowledgement that bypass mode is OK in this process.
65pub const ALLOW_ENV: &str = "CLAUDE_WRAPPER_ALLOW_DANGEROUS";
66
67/// Wrapper that lets callers run bypass-permissions queries against an
68/// underlying [`Claude`] client. Construction is gated by the
69/// [`ALLOW_ENV`] env-var.
70#[derive(Debug, Clone)]
71pub struct DangerousClient {
72 inner: Claude,
73}
74
75impl DangerousClient {
76 /// Wrap `claude`, refusing to construct unless the [`ALLOW_ENV`]
77 /// env-var equals `"1"`.
78 ///
79 /// The check is made at each construction rather than memoized so
80 /// that a test which flips the env-var mid-process sees the change.
81 pub fn new(claude: Claude) -> Result<Self> {
82 if !is_allowed() {
83 return Err(Error::DangerousNotAllowed { env_var: ALLOW_ENV });
84 }
85 Ok(Self { inner: claude })
86 }
87
88 /// Borrow the underlying [`Claude`] for composition with other
89 /// wrapper APIs.
90 pub fn claude(&self) -> &Claude {
91 &self.inner
92 }
93
94 /// Run `cmd` with `--permission-mode bypassPermissions`
95 /// unconditionally overridden. Any permission mode the caller
96 /// already set on `cmd` is replaced. Requires the `async` feature.
97 #[cfg(feature = "async")]
98 pub async fn query_bypass(&self, cmd: QueryCommand) -> Result<CommandOutput> {
99 #[allow(deprecated)]
100 let cmd = cmd.permission_mode(PermissionMode::BypassPermissions);
101 cmd.execute(&self.inner).await
102 }
103
104 /// Blocking mirror of [`DangerousClient::query_bypass`]. Requires
105 /// the `sync` feature.
106 #[cfg(feature = "sync")]
107 pub fn query_bypass_sync(&self, cmd: QueryCommand) -> Result<CommandOutput> {
108 #[allow(deprecated)]
109 let cmd = cmd.permission_mode(PermissionMode::BypassPermissions);
110 cmd.execute_sync(&self.inner)
111 }
112}
113
114fn is_allowed() -> bool {
115 std::env::var(ALLOW_ENV).as_deref() == Ok("1")
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121 use std::sync::Mutex;
122
123 // The env-var is process-global; serialize the tests that touch
124 // it so they don't interleave and flip the answer out from under
125 // each other.
126 static ENV_LOCK: Mutex<()> = Mutex::new(());
127
128 fn with_allow_env<T>(value: Option<&str>, f: impl FnOnce() -> T) -> T {
129 let _g = ENV_LOCK.lock().unwrap();
130 let prev = std::env::var(ALLOW_ENV).ok();
131 // SAFETY: set_var and remove_var are marked unsafe in recent
132 // std because multi-threaded processes can race. We serialize
133 // with ENV_LOCK above; cargo test runs tests concurrently,
134 // but the lock pins this env-var one caller at a time.
135 unsafe {
136 match value {
137 Some(v) => std::env::set_var(ALLOW_ENV, v),
138 None => std::env::remove_var(ALLOW_ENV),
139 }
140 }
141 let out = f();
142 unsafe {
143 match prev {
144 Some(v) => std::env::set_var(ALLOW_ENV, v),
145 None => std::env::remove_var(ALLOW_ENV),
146 }
147 }
148 out
149 }
150
151 #[test]
152 fn new_refuses_without_env() {
153 with_allow_env(None, || {
154 let claude = Claude::builder().binary("/usr/bin/true").build().unwrap();
155 let err = DangerousClient::new(claude).unwrap_err();
156 assert!(matches!(
157 err,
158 Error::DangerousNotAllowed { env_var } if env_var == ALLOW_ENV
159 ));
160 });
161 }
162
163 #[test]
164 fn new_refuses_with_wrong_value() {
165 with_allow_env(Some("true"), || {
166 let claude = Claude::builder().binary("/usr/bin/true").build().unwrap();
167 assert!(matches!(
168 DangerousClient::new(claude).unwrap_err(),
169 Error::DangerousNotAllowed { .. }
170 ));
171 });
172 with_allow_env(Some("yes"), || {
173 let claude = Claude::builder().binary("/usr/bin/true").build().unwrap();
174 assert!(matches!(
175 DangerousClient::new(claude).unwrap_err(),
176 Error::DangerousNotAllowed { .. }
177 ));
178 });
179 }
180
181 #[test]
182 fn new_accepts_with_allow_env_set_to_one() {
183 with_allow_env(Some("1"), || {
184 let claude = Claude::builder().binary("/usr/bin/true").build().unwrap();
185 let d = DangerousClient::new(claude).unwrap();
186 // The wrapper exposes the underlying client for
187 // composition. Verify the binary threaded through.
188 assert_eq!(d.claude().binary(), std::path::Path::new("/usr/bin/true"));
189 });
190 }
191
192 #[test]
193 fn error_message_names_the_env_var() {
194 // User-facing error has to be clear enough that a reader
195 // knows which env-var to set without digging.
196 let e = Error::DangerousNotAllowed { env_var: ALLOW_ENV };
197 let s = e.to_string();
198 assert!(s.contains(ALLOW_ENV));
199 }
200}