bashkit 0.5.0

Awesomely fast virtual sandbox with bash and file system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Decision: Two modes — injection (script unaware) and placeholder (opaque env var replaced on the wire).
// Decision: Header-only for v1 — no URL query param or body mutation.
// Decision: Overwrite semantics — injected headers replace existing headers with same name.
// Decision: Non-blocking — injection failures don't block the request.
// Decision: Built on before_http hooks — no new interception points.
// See specs/credential-injection.md

//! Generic credential injection for outbound HTTP requests.
//!
//! Provides transparent per-host credential injection so sandboxed scripts
//! can make authenticated API calls without ever seeing the real secrets.
//!
//! Two modes are supported:
//!
//! - **Injection**: Script has no knowledge of credentials. Headers are added
//!   automatically based on the request URL.
//! - **Placeholder**: Script sees an opaque placeholder in an env var. The
//!   placeholder is replaced with the real credential in outbound headers.
//!
//! See [`crate::credential_injection_guide`] for the full guide.

use crate::hooks::{HookAction, HttpRequestEvent, Interceptor};
use crate::network::NetworkAllowlist;

/// A credential to inject into outbound HTTP requests.
///
/// # Examples
///
/// ```rust
/// use bashkit::Credential;
///
/// // Bearer token
/// let cred = Credential::bearer("ghp_xxxx");
///
/// // Custom header
/// let cred = Credential::header("X-Api-Key", "secret123");
///
/// // Multiple headers
/// let cred = Credential::headers(vec![
///     ("X-Api-Key".into(), "key123".into()),
///     ("X-Api-Secret".into(), "secret456".into()),
/// ]);
/// ```
#[derive(Clone)]
pub enum Credential {
    /// Inject `Authorization: Bearer <token>`.
    Bearer(String),
    /// Inject a single custom header.
    Header {
        /// Header name.
        name: String,
        /// Header value (the secret).
        value: String,
    },
    /// Inject multiple headers.
    Headers(Vec<(String, String)>),
}

impl Credential {
    /// Create a Bearer token credential.
    pub fn bearer(token: impl Into<String>) -> Self {
        Self::Bearer(token.into())
    }

    /// Create a single custom header credential.
    pub fn header(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self::Header {
            name: name.into(),
            value: value.into(),
        }
    }

    /// Create a multi-header credential.
    pub fn headers(headers: Vec<(String, String)>) -> Self {
        Self::Headers(headers)
    }

    /// Return the headers this credential would inject.
    fn to_headers(&self) -> Vec<(String, String)> {
        match self {
            Self::Bearer(token) => {
                vec![("Authorization".to_string(), format!("Bearer {token}"))]
            }
            Self::Header { name, value } => vec![(name.clone(), value.clone())],
            Self::Headers(headers) => headers.clone(),
        }
    }

    /// Return the header names this credential injects (for overwrite).
    fn header_names(&self) -> Vec<String> {
        match self {
            Self::Bearer(_) => vec!["authorization".to_string()],
            Self::Header { name, .. } => vec![name.to_lowercase()],
            Self::Headers(headers) => headers.iter().map(|(n, _)| n.to_lowercase()).collect(),
        }
    }

    /// Return the raw secret values (for placeholder replacement matching).
    fn secret_values(&self) -> Vec<String> {
        match self {
            Self::Bearer(token) => vec![token.clone()],
            Self::Header { value, .. } => vec![value.clone()],
            Self::Headers(headers) => headers.iter().map(|(_, v)| v.clone()).collect(),
        }
    }
}

impl std::fmt::Debug for Credential {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Bearer(_) => f.debug_tuple("Bearer").field(&"[REDACTED]").finish(),
            Self::Header { name, .. } => f
                .debug_struct("Header")
                .field("name", name)
                .field("value", &"[REDACTED]")
                .finish(),
            Self::Headers(headers) => {
                let redacted: Vec<_> = headers.iter().map(|(n, _)| (n, "[REDACTED]")).collect();
                f.debug_tuple("Headers").field(&redacted).finish()
            }
        }
    }
}

/// Placeholder prefix for generated placeholder tokens.
const PLACEHOLDER_PREFIX: &str = "bk_placeholder_";

/// Generate a random placeholder token.
///
/// Format: `bk_placeholder_<32 hex chars>` (128 bits of randomness).
fn generate_placeholder() -> String {
    use std::collections::hash_map::RandomState;
    use std::hash::{BuildHasher, Hasher};

    // Use two RandomState hashers for 128 bits of randomness.
    // This avoids pulling `rand` as a non-optional dependency.
    let s1 = RandomState::new();
    let s2 = RandomState::new();
    let mut h1 = s1.build_hasher();
    h1.write_u64(0);
    let mut h2 = s2.build_hasher();
    h2.write_u64(1);
    format!(
        "{}{:016x}{:016x}",
        PLACEHOLDER_PREFIX,
        h1.finish(),
        h2.finish()
    )
}

/// A rule mapping a URL pattern to a credential.
struct CredentialRule {
    /// URL pattern (same format as NetworkAllowlist patterns).
    pattern: String,
    /// The credential to inject.
    credential: Credential,
    /// For placeholder mode: the placeholder string visible to scripts.
    placeholder: Option<String>,
}

/// A compiled credential rule with a pre-built allowlist for URL matching.
struct CompiledRule {
    allowlist: NetworkAllowlist,
    credential: Credential,
    placeholder: Option<String>,
}

/// Collects credential rules and builds a `before_http` hook.
///
/// This is an internal type used by [`crate::BashBuilder`]. Users interact
/// with it via [`crate::BashBuilder::credential`] and
/// [`crate::BashBuilder::credential_placeholder`].
pub(crate) struct CredentialPolicy {
    rules: Vec<CredentialRule>,
}

impl CredentialPolicy {
    pub(crate) fn new() -> Self {
        Self { rules: Vec::new() }
    }

    /// Add an injection-mode rule.
    pub(crate) fn add_injection(&mut self, pattern: impl Into<String>, credential: Credential) {
        self.rules.push(CredentialRule {
            pattern: pattern.into(),
            credential,
            placeholder: None,
        });
    }

    /// Add a placeholder-mode rule. Returns `(env_name, placeholder_value)`
    /// so the caller can set the env var.
    pub(crate) fn add_placeholder(
        &mut self,
        pattern: impl Into<String>,
        credential: Credential,
    ) -> String {
        let placeholder = generate_placeholder();
        self.rules.push(CredentialRule {
            pattern: pattern.into(),
            credential,
            placeholder: Some(placeholder.clone()),
        });
        placeholder
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.rules.is_empty()
    }

    /// Convert this policy into a `before_http` interceptor hook.
    ///
    /// The hook:
    /// 1. Matches request URL against rule patterns
    /// 2. For injection rules: overwrites headers with credential headers
    /// 3. For placeholder rules: finds placeholder strings in header values
    ///    and replaces them with real credential values
    pub(crate) fn into_hook(self) -> Interceptor<HttpRequestEvent> {
        // Pre-build allowlists for each rule so we can use URL matching.
        let compiled: Vec<CompiledRule> = self
            .rules
            .into_iter()
            .map(|rule| {
                let allowlist = NetworkAllowlist::new().allow(&rule.pattern);
                CompiledRule {
                    allowlist,
                    credential: rule.credential,
                    placeholder: rule.placeholder,
                }
            })
            .collect();

        Box::new(move |mut event: HttpRequestEvent| {
            for rule in &compiled {
                if !rule.allowlist.is_allowed(&event.url) {
                    continue;
                }

                match &rule.placeholder {
                    None => {
                        // Injection mode: overwrite existing headers, then add credential headers.
                        let names_to_remove = rule.credential.header_names();
                        event
                            .headers
                            .retain(|(name, _)| !names_to_remove.contains(&name.to_lowercase()));
                        event.headers.extend(rule.credential.to_headers());
                    }
                    Some(placeholder) => {
                        // Placeholder mode: replace only in credential-owned header values.
                        let credential_header_secrets = rule
                            .credential
                            .header_names()
                            .into_iter()
                            .zip(rule.credential.secret_values())
                            .collect::<Vec<_>>();
                        let placeholder_str: &str = placeholder;
                        for (header_name, header_value) in &mut event.headers {
                            if let Some((_, secret_value)) = credential_header_secrets
                                .iter()
                                .find(|(name, _)| name.eq_ignore_ascii_case(header_name))
                            {
                                *header_value = header_value.replace(placeholder_str, secret_value);
                            }
                        }
                    }
                }
            }

            HookAction::Continue(event)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_placeholder_generation() {
        let p1 = generate_placeholder();
        let p2 = generate_placeholder();
        assert!(p1.starts_with(PLACEHOLDER_PREFIX));
        assert!(p2.starts_with(PLACEHOLDER_PREFIX));
        // Should be different (128 bits of randomness)
        assert_ne!(p1, p2);
        // Fixed length: prefix (15) + 32 hex chars = 47
        assert_eq!(p1.len(), 47);
    }

    #[test]
    fn test_credential_debug_redacts() {
        let cred = Credential::bearer("super_secret");
        let debug = format!("{:?}", cred);
        assert!(!debug.contains("super_secret"));
        assert!(debug.contains("[REDACTED]"));
    }

    #[test]
    fn test_credential_to_headers_bearer() {
        let cred = Credential::bearer("tok123");
        let headers = cred.to_headers();
        assert_eq!(
            headers,
            vec![("Authorization".to_string(), "Bearer tok123".to_string())]
        );
    }

    #[test]
    fn test_credential_to_headers_custom() {
        let cred = Credential::header("X-Api-Key", "key123");
        let headers = cred.to_headers();
        assert_eq!(
            headers,
            vec![("X-Api-Key".to_string(), "key123".to_string())]
        );
    }

    #[test]
    fn test_credential_to_headers_multi() {
        let cred = Credential::headers(vec![
            ("X-Key".into(), "k".into()),
            ("X-Secret".into(), "s".into()),
        ]);
        let headers = cred.to_headers();
        assert_eq!(headers.len(), 2);
    }

    #[test]
    fn test_injection_hook_adds_headers() {
        let mut policy = CredentialPolicy::new();
        policy.add_injection("https://api.example.com", Credential::bearer("tok"));

        let hook = policy.into_hook();
        let event = HttpRequestEvent {
            method: "GET".into(),
            url: "https://api.example.com/data".into(),
            headers: vec![],
        };

        match hook(event) {
            HookAction::Continue(e) => {
                assert_eq!(e.headers.len(), 1);
                assert_eq!(e.headers[0].0, "Authorization");
                assert_eq!(e.headers[0].1, "Bearer tok");
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }

    #[test]
    fn test_injection_hook_overwrites_existing_header() {
        let mut policy = CredentialPolicy::new();
        policy.add_injection("https://api.example.com", Credential::bearer("real_tok"));

        let hook = policy.into_hook();
        let event = HttpRequestEvent {
            method: "GET".into(),
            url: "https://api.example.com/data".into(),
            headers: vec![("Authorization".into(), "Bearer fake_tok".into())],
        };

        match hook(event) {
            HookAction::Continue(e) => {
                assert_eq!(e.headers.len(), 1);
                assert_eq!(e.headers[0].1, "Bearer real_tok");
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }

    #[test]
    fn test_injection_hook_skips_non_matching_url() {
        let mut policy = CredentialPolicy::new();
        policy.add_injection("https://api.example.com", Credential::bearer("tok"));

        let hook = policy.into_hook();
        let event = HttpRequestEvent {
            method: "GET".into(),
            url: "https://other.example.com/data".into(),
            headers: vec![],
        };

        match hook(event) {
            HookAction::Continue(e) => {
                assert!(
                    e.headers.is_empty(),
                    "should not inject for non-matching URL"
                );
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }

    #[test]
    fn test_placeholder_hook_replaces_in_header() {
        let mut policy = CredentialPolicy::new();
        let placeholder =
            policy.add_placeholder("https://api.openai.com", Credential::bearer("sk-real-key"));

        let hook = policy.into_hook();
        let event = HttpRequestEvent {
            method: "POST".into(),
            url: "https://api.openai.com/v1/chat/completions".into(),
            headers: vec![("Authorization".into(), format!("Bearer {}", placeholder))],
        };

        match hook(event) {
            HookAction::Continue(e) => {
                assert_eq!(e.headers.len(), 1);
                assert_eq!(e.headers[0].1, "Bearer sk-real-key");
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }

    #[test]
    fn test_placeholder_not_replaced_for_wrong_host() {
        let mut policy = CredentialPolicy::new();
        let placeholder =
            policy.add_placeholder("https://api.openai.com", Credential::bearer("sk-real-key"));

        let hook = policy.into_hook();
        let event = HttpRequestEvent {
            method: "POST".into(),
            url: "https://evil.com/exfiltrate".into(),
            headers: vec![("Authorization".into(), format!("Bearer {}", placeholder))],
        };

        match hook(event) {
            HookAction::Continue(e) => {
                // Placeholder should NOT be replaced — wrong host
                assert!(e.headers[0].1.contains("bk_placeholder_"));
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }

    #[test]
    fn test_placeholder_only_replaced_in_credential_headers() {
        let mut policy = CredentialPolicy::new();
        let placeholder =
            policy.add_placeholder("https://api.openai.com", Credential::bearer("sk-real-key"));

        let hook = policy.into_hook();
        let event = HttpRequestEvent {
            method: "POST".into(),
            url: "https://api.openai.com/v1/chat/completions".into(),
            headers: vec![
                ("Authorization".into(), format!("Bearer {}", placeholder)),
                ("X-Debug".into(), format!("leak={}", placeholder)),
            ],
        };

        match hook(event) {
            HookAction::Continue(e) => {
                assert_eq!(e.headers[0].1, "Bearer sk-real-key");
                assert!(e.headers[1].1.contains("bk_placeholder_"));
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }

    #[test]
    fn test_path_scoped_credential() {
        let mut policy = CredentialPolicy::new();
        policy.add_injection("https://api.example.com/v1/", Credential::bearer("v1_tok"));

        let hook = policy.into_hook();

        // Should match /v1/ prefix
        let event = HttpRequestEvent {
            method: "GET".into(),
            url: "https://api.example.com/v1/users".into(),
            headers: vec![],
        };
        match hook(event) {
            HookAction::Continue(e) => assert_eq!(e.headers.len(), 1),
            HookAction::Cancel(_) => panic!("should not cancel"),
        }

        // Should NOT match /v2/
        let event = HttpRequestEvent {
            method: "GET".into(),
            url: "https://api.example.com/v2/users".into(),
            headers: vec![],
        };
        match hook(event) {
            HookAction::Continue(e) => assert!(e.headers.is_empty()),
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }

    #[test]
    fn test_multiple_rules() {
        let mut policy = CredentialPolicy::new();
        policy.add_injection("https://github.com", Credential::bearer("gh_tok"));
        policy.add_injection(
            "https://api.openai.com",
            Credential::header("X-Api-Key", "openai_key"),
        );

        let hook = policy.into_hook();

        // GitHub request
        let event = HttpRequestEvent {
            method: "GET".into(),
            url: "https://github.com/api/repos".into(),
            headers: vec![],
        };
        match hook(event) {
            HookAction::Continue(e) => {
                assert_eq!(e.headers.len(), 1);
                assert_eq!(e.headers[0].1, "Bearer gh_tok");
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }

        // OpenAI request
        let event = HttpRequestEvent {
            method: "POST".into(),
            url: "https://api.openai.com/v1/chat".into(),
            headers: vec![],
        };
        match hook(event) {
            HookAction::Continue(e) => {
                assert_eq!(e.headers.len(), 1);
                assert_eq!(e.headers[0].0, "X-Api-Key");
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }

    #[test]
    fn test_header_name_case_insensitive_overwrite() {
        let mut policy = CredentialPolicy::new();
        policy.add_injection("https://api.example.com", Credential::bearer("real"));

        let hook = policy.into_hook();
        let event = HttpRequestEvent {
            method: "GET".into(),
            url: "https://api.example.com/data".into(),
            headers: vec![("authorization".into(), "Bearer fake".into())],
        };

        match hook(event) {
            HookAction::Continue(e) => {
                assert_eq!(e.headers.len(), 1);
                assert_eq!(e.headers[0].1, "Bearer real");
            }
            HookAction::Cancel(_) => panic!("should not cancel"),
        }
    }
}