clawshell 0.1.0-alpha.0

A security privileged process for the OpenClaw ecosystem.
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use crate::config::{Config, Provider};
use crate::dlp::DlpScanner;
use crate::email::{
    EmailAccountCredentials, EmailGetMessageRequest, EmailListMessagesRequest, EmailMessageContent,
    EmailMessageMetadata, EmailPolicy, EmailService, EmailServiceError, ImapEmailService,
    normalize_sender_rule,
};
use crate::keys::{KeyManager, ResolvedKey};
use crate::proxy::ProxyClient;

use axum::Router;
use axum::body::Body;
use axum::extract::{DefaultBodyLimit, Path, Query, Request, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{any, get};
use bytes::Bytes;
use http_body_util::BodyExt;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, error, info, trace, warn};

#[derive(Debug, Clone)]
pub struct AppState {
    pub key_manager: Arc<KeyManager>,
    pub dlp_scanner: Arc<DlpScanner>,
    pub proxy_client: Arc<ProxyClient>,
    pub email_enabled: bool,
    pub email_policy: Option<EmailPolicy>,
    pub email_accounts: Arc<BTreeMap<String, EmailAccountCredentials>>,
    pub email_service: Arc<EmailService>,
}

impl AppState {
    pub fn from_config(config: &Config) -> Result<Self, String> {
        let mut upstream_urls = BTreeMap::new();
        upstream_urls.insert(Provider::Openai, config.upstream_url(Provider::Openai));
        upstream_urls.insert(
            Provider::Openrouter,
            config.upstream_url(Provider::Openrouter),
        );
        upstream_urls.insert(
            Provider::Anthropic,
            config.upstream_url(Provider::Anthropic),
        );

        let key_mappings = config
            .key_map()
            .iter()
            .map(|(virtual_key, (real_key, provider))| {
                (
                    virtual_key.clone(),
                    ResolvedKey {
                        real_key: real_key.clone(),
                        provider: *provider,
                    },
                )
            })
            .collect();

        let email_policy = if config.email.enabled {
            config.email.mode.map(|mode| {
                let sender_rules = match mode {
                    crate::config::EmailMode::Allowlist => &config.email.allow_senders,
                    crate::config::EmailMode::Denylist => &config.email.deny_senders,
                }
                .iter()
                .map(|rule| normalize_sender_rule(rule))
                .collect();

                EmailPolicy {
                    mode,
                    sender_rules,
                    default_max_results: config.email.default_max_results,
                }
            })
        } else {
            None
        };

        let email_accounts: BTreeMap<String, EmailAccountCredentials> = if config.email.enabled {
            config
                .email
                .accounts
                .iter()
                .map(|account| {
                    Ok((
                        account.virtual_key.clone(),
                        EmailAccountCredentials {
                            email: account.email.clone(),
                            app_password: account.app_password.clone(),
                            imap_host: account.imap_host.clone(),
                            imap_port: account.imap_port,
                        },
                    ))
                })
                .collect::<Result<_, String>>()?
        } else {
            BTreeMap::new()
        };

        Ok(Self {
            key_manager: Arc::new(KeyManager::new(key_mappings)),
            dlp_scanner: Arc::new(
                DlpScanner::new(&config.dlp.patterns, config.dlp.scan_responses)
                    .expect("Failed to compile DLP patterns"),
            ),
            proxy_client: Arc::new(ProxyClient::with_upstream_urls(
                upstream_urls,
                config.upstream.anthropic_version.clone(),
            )),
            email_enabled: config.email.enabled,
            email_policy,
            email_accounts: Arc::new(email_accounts),
            email_service: Arc::new(EmailService::Imap(ImapEmailService::default())),
        })
    }
}

/// Maximum request body size (10 MiB).
const MAX_BODY_SIZE: usize = 10 * 1024 * 1024;

pub fn build_router(state: AppState) -> Router {
    Router::new()
        .route("/v1/email/messages", get(handle_email_secure_messages))
        .route("/v1/email/messages/{id}", get(handle_email_message_content))
        .route("/", any(handle_request))
        .route("/{*path}", any(handle_request))
        .layer(DefaultBodyLimit::max(MAX_BODY_SIZE))
        .with_state(state)
}

#[derive(Debug, Deserialize)]
struct EmailSecureMessagesQuery {
    folder: Option<String>,
    limit: Option<u32>,
    unread_only: Option<bool>,
    from: Option<String>,
    subject: Option<String>,
}

#[derive(Debug, Serialize)]
struct EmailSecureMessage {
    id: String,
    thread_id: Option<String>,
    from: String,
    subject: Option<String>,
    date: Option<String>,
    snippet: Option<String>,
    internal_date_ms: Option<i64>,
    labels: Vec<String>,
}

#[derive(Debug, Serialize)]
struct EmailSecureMessagesResponse {
    messages: Vec<EmailSecureMessage>,
    next_page_token: Option<String>,
}

#[derive(Debug, Deserialize)]
struct EmailMessageContentPath {
    id: String,
}

#[derive(Debug, Serialize)]
struct EmailMessageContentResponse {
    metadata: EmailMessageMetadata,
    headers: BTreeMap<String, String>,
    text_body: Option<String>,
    html_body: Option<String>,
}

async fn handle_email_secure_messages(
    State(state): State<AppState>,
    Query(query): Query<EmailSecureMessagesQuery>,
    headers: axum::http::HeaderMap,
) -> Result<Response, Response> {
    let method = axum::http::Method::GET;
    let path = "/v1/email/messages";

    if !state.email_enabled {
        warn!(method = %method, path = %path, "Email endpoint is disabled");
        return Err(error_response(
            StatusCode::NOT_FOUND,
            "Email secure endpoint is disabled",
        ));
    }

    let auth_header = headers
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .map(|s| s.to_string())
        .ok_or_else(|| {
            warn!(method = %method, path = %path, "Missing Authorization header");
            error_response(StatusCode::UNAUTHORIZED, "Missing Authorization header")
        })?;

    let virtual_key = KeyManager::extract_virtual_key(&auth_header)
        .map(|s| s.to_string())
        .ok_or_else(|| {
            warn!(method = %method, path = %path, "Invalid Authorization header format");
            error_response(
                StatusCode::UNAUTHORIZED,
                "Invalid Authorization header format. Expected: Bearer <key>",
            )
        })?;

    let account = state.email_accounts.get(&virtual_key).ok_or_else(|| {
        warn!(
            method = %method,
            path = %path,
            virtual_key = %virtual_key,
            "Virtual key is not authorized for Email"
        );
        error_response(StatusCode::UNAUTHORIZED, "Unknown API key")
    })?;

    let policy = state.email_policy.as_ref().ok_or_else(|| {
        error!(
            method = %method,
            path = %path,
            "Email endpoint enabled without an active sender policy"
        );
        error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            "Email policy configuration error",
        )
    })?;

    let max_results = query.limit.unwrap_or(policy.default_max_results);
    if max_results == 0 || max_results > 100 {
        return Err(error_response(
            StatusCode::BAD_REQUEST,
            "limit must be between 1 and 100",
        ));
    }

    let folder = query
        .folder
        .as_deref()
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .unwrap_or("INBOX")
        .to_string();

    let service_request = EmailListMessagesRequest {
        folder,
        max_results,
        unread_only: query.unread_only.unwrap_or(false),
        from_contains: query.from.clone(),
        subject_contains: query.subject.clone(),
    };

    let email_response = state
        .email_service
        .list_message_metadata(&virtual_key, account, &service_request)
        .await
        .map_err(|e| {
            error!(
                method = %method,
                path = %path,
                virtual_key = %virtual_key,
                error = %e,
                "Failed to fetch Email messages"
            );
            error_response(StatusCode::BAD_GATEWAY, "Failed to fetch Email messages")
        })?;

    let mut visible_messages = Vec::new();
    for message in email_response.messages {
        let Some(from_header) = message.from.as_deref() else {
            continue;
        };
        if !policy.sender_visible(from_header) {
            continue;
        }
        visible_messages.push(EmailSecureMessage {
            id: message.id,
            thread_id: message.thread_id,
            from: from_header.to_string(),
            subject: message.subject,
            date: message.date,
            snippet: message.snippet,
            internal_date_ms: message.internal_date_ms,
            labels: message.label_ids,
        });
    }

    let response = EmailSecureMessagesResponse {
        messages: visible_messages,
        next_page_token: email_response.next_page_token,
    };

    Ok(axum::Json(response).into_response())
}

async fn handle_email_message_content(
    State(state): State<AppState>,
    Path(path_params): Path<EmailMessageContentPath>,
    headers: axum::http::HeaderMap,
) -> Result<Response, Response> {
    let method = axum::http::Method::GET;
    let path = "/v1/email/messages/{id}";

    if !state.email_enabled {
        warn!(method = %method, path = %path, "Email endpoint is disabled");
        return Err(error_response(
            StatusCode::NOT_FOUND,
            "Email secure endpoint is disabled",
        ));
    }

    let auth_header = headers
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .map(|s| s.to_string())
        .ok_or_else(|| {
            warn!(method = %method, path = %path, "Missing Authorization header");
            error_response(StatusCode::UNAUTHORIZED, "Missing Authorization header")
        })?;

    let virtual_key = KeyManager::extract_virtual_key(&auth_header)
        .map(|s| s.to_string())
        .ok_or_else(|| {
            warn!(method = %method, path = %path, "Invalid Authorization header format");
            error_response(
                StatusCode::UNAUTHORIZED,
                "Invalid Authorization header format. Expected: Bearer <key>",
            )
        })?;

    let account = state.email_accounts.get(&virtual_key).ok_or_else(|| {
        warn!(
            method = %method,
            path = %path,
            virtual_key = %virtual_key,
            "Virtual key is not authorized for Email"
        );
        error_response(StatusCode::UNAUTHORIZED, "Unknown API key")
    })?;

    let policy = state.email_policy.as_ref().ok_or_else(|| {
        error!(
            method = %method,
            path = %path,
            "Email endpoint enabled without an active sender policy"
        );
        error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            "Email policy configuration error",
        )
    })?;

    let message_id = path_params.id.trim();
    if message_id.is_empty() || message_id.parse::<u64>().is_err() {
        return Err(error_response(
            StatusCode::BAD_REQUEST,
            "invalid message id",
        ));
    }

    let service_request = EmailGetMessageRequest {
        folder: "INBOX".to_string(),
        message_id: message_id.to_string(),
    };

    let content: EmailMessageContent = state
        .email_service
        .get_message_content(&virtual_key, account, &service_request)
        .await
        .map_err(|error| match error {
            EmailServiceError::NotFound(_) => {
                error_response(StatusCode::NOT_FOUND, "Email message not found")
            }
            other => {
                error!(
                    method = %method,
                    path = %path,
                    virtual_key = %virtual_key,
                    message_id = %message_id,
                    error = %other,
                    "Failed to fetch Email message content"
                );
                error_response(
                    StatusCode::BAD_GATEWAY,
                    "Failed to fetch Email message content",
                )
            }
        })?;

    let from_header = content
        .metadata
        .from
        .as_deref()
        .or_else(|| content.headers.get("from").map(String::as_str));
    if from_header.is_none_or(|from| !policy.sender_visible(from)) {
        return Err(error_response(
            StatusCode::NOT_FOUND,
            "Email message not found",
        ));
    }

    let response = EmailMessageContentResponse {
        metadata: content.metadata,
        headers: content.headers,
        text_body: content.text_body,
        html_body: content.html_body,
    };

    Ok(axum::Json(response).into_response())
}

async fn handle_request(
    State(state): State<AppState>,
    request: Request,
) -> Result<Response, Response> {
    let start = Instant::now();
    let (parts, body) = request.into_parts();
    let method = parts.method;
    let uri = parts.uri;
    let path = uri.path();
    let headers = parts.headers;

    trace!(
        method = %method,
        path = %path,
        header_count = headers.len(),
        "Incoming request"
    );

    // 1. Extract and validate the virtual key
    let auth_header = headers
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .map(|s| s.to_string())
        .ok_or_else(|| {
            warn!(method = %method, path = %path, "Missing Authorization header");
            error_response(StatusCode::UNAUTHORIZED, "Missing Authorization header")
        })?;

    let virtual_key = KeyManager::extract_virtual_key(&auth_header)
        .map(|s| s.to_string())
        .ok_or_else(|| {
            warn!(method = %method, path = %path, "Invalid Authorization header format");
            error_response(
                StatusCode::UNAUTHORIZED,
                "Invalid Authorization header format. Expected: Bearer <key>",
            )
        })?;

    let resolved = state.key_manager.resolve(&virtual_key).ok_or_else(|| {
        warn!(
            method = %method,
            path = %path,
            virtual_key = %virtual_key,
            "Unknown virtual key"
        );
        error_response(StatusCode::UNAUTHORIZED, "Unknown API key")
    })?;
    let real_key = resolved.real_key.clone();
    let provider = resolved.provider;

    debug!(
        method = %method,
        path = %path,
        virtual_key = %virtual_key,
        provider = ?provider,
        "Key resolved successfully"
    );

    // 2. Read the body
    let body_bytes: Bytes = body
        .collect()
        .await
        .map_err(|e| {
            error!(error = %e, "Failed to read request body");
            error_response(StatusCode::BAD_REQUEST, "Failed to read request body")
        })?
        .to_bytes();

    trace!(
        method = %method,
        path = %path,
        body_size = body_bytes.len(),
        "Request body read"
    );

    // 3. DLP scan on request body (block patterns reject, redact patterns mask)
    let body_bytes = if !body_bytes.is_empty() {
        let result = state.dlp_scanner.scan_and_redact(&body_bytes);
        if !result.blocked.is_empty() {
            warn!(
                method = %method,
                path = %path,
                virtual_key = %virtual_key,
                detections = ?result.blocked,
                "Sensitive data detected in request"
            );
            return Err(error_response(
                StatusCode::BAD_REQUEST,
                &format!(
                    "Request blocked: sensitive data detected ({})",
                    result.blocked.join(", ")
                ),
            ));
        }
        if result.was_redacted {
            info!(
                method = %method,
                path = %path,
                virtual_key = %virtual_key,
                "PII redacted from request body before forwarding"
            );
            Bytes::from(result.redacted)
        } else {
            body_bytes
        }
    } else {
        body_bytes
    };

    // 4. Forward to upstream
    info!(
        method = %method,
        path = %path,
        virtual_key = %virtual_key,
        "Forwarding request to upstream"
    );

    let response = state
        .proxy_client
        .forward(
            method.clone(),
            &uri,
            headers,
            &real_key,
            body_bytes,
            provider,
        )
        .await
        .map_err(|e| {
            error!(
                method = %method,
                path = %path,
                virtual_key = %virtual_key,
                error = %e,
                "Proxy error"
            );
            e.into_response()
        })?;

    // 5. DLP scan on response body (redact all PII before returning to client)
    let response = if state.dlp_scanner.scan_responses() {
        trace!("Response DLP scanning enabled, checking response body");
        let is_streaming = response
            .headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .is_some_and(|ct| ct.contains("text/event-stream"));

        if !is_streaming {
            trace!("Non-streaming response, scanning body for PII");
            let (parts, body) = response.into_parts();
            let body = body
                .collect()
                .await
                .map_err(|e| {
                    error!(error = %e, "Failed to read response body for DLP scan");
                    error_response(
                        StatusCode::INTERNAL_SERVER_ERROR,
                        "Failed to process response",
                    )
                })?
                .to_bytes();

            let (redacted, redacted_names) = state.dlp_scanner.redact_all(&body);
            if !redacted_names.is_empty() {
                warn!(
                    method = %method,
                    path = %path,
                    virtual_key = %virtual_key,
                    redacted_patterns = ?redacted_names,
                    "PII redacted from upstream response"
                );
                let redacted_bytes = Bytes::from(redacted);
                let mut parts = parts;
                // Remove stale content-length; axum/hyper will recalculate it
                parts.headers.remove("content-length");
                Response::from_parts(parts, Body::from(redacted_bytes))
            } else {
                Response::from_parts(parts, Body::from(body))
            }
        } else {
            warn!(
                method = %method,
                path = %path,
                virtual_key = %virtual_key,
                "Streaming response (SSE) — DLP scanning is not supported for streaming responses; \
                 PII in streamed content will not be redacted"
            );
            response
        }
    } else {
        trace!("Response DLP scanning disabled");
        response
    };

    let latency = start.elapsed();
    info!(
        method = %method,
        path = %path,
        virtual_key = %virtual_key,
        status = %response.status(),
        latency_ms = latency.as_millis(),
        "Request completed"
    );

    Ok(response)
}

fn error_response(status: StatusCode, message: &str) -> Response {
    let body = serde_json::json!({ "error": message });
    (status, axum::Json(body)).into_response()
}

#[cfg(test)]
mod tests;