mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-02 13:04:08 +01:00
Encrypt Yaak Proxy's databases at rest with SQLCipher (#823)
This commit is contained in:
1 parent
cc382b9226
commit
91696c2d1f
42 files changed
+3366
-286
No files matched your search
@@ -33,7 +33,6 @@ use std::sync::mpsc as std_mpsc;
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpListener;
|
||||
use ts_rs::TS;
|
||||
use yaak_proxy::CaStore;
|
||||
|
||||
pub const CONTROL_FILE: &str = "control.json";
|
||||
const LOCK_FILE: &str = "control.lock";
|
||||
@@ -212,10 +211,14 @@ pub struct ControlStatus {
|
||||
pub configured_port: u16,
|
||||
/// Flows are stored. Paused, traffic still passes through, unrecorded.
|
||||
pub recording: bool,
|
||||
/// Flows stored; zero while they are locked.
|
||||
pub flow_count: usize,
|
||||
pub ca: CaStatus,
|
||||
/// `None` when it could not be read.
|
||||
pub system_proxy: Option<SystemProxyStatus>,
|
||||
/// Why captured traffic cannot be read, while it cannot.
|
||||
#[serde(default)]
|
||||
pub locked: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
@@ -458,10 +461,21 @@ impl ApiError {
|
||||
|
||||
impl From<String> for ApiError {
|
||||
fn from(message: String) -> Self {
|
||||
// Locked, said through a call that returns its reason as text.
|
||||
if message.starts_with(crate::LOCKED) {
|
||||
return Self::new(StatusCode::SERVICE_UNAVAILABLE, message);
|
||||
}
|
||||
Self::new(StatusCode::INTERNAL_SERVER_ERROR, message)
|
||||
}
|
||||
}
|
||||
|
||||
/// Captured traffic is locked for now: unavailable, not missing or broken.
|
||||
impl From<crate::Locked> for ApiError {
|
||||
fn from(locked: crate::Locked) -> Self {
|
||||
Self::new(StatusCode::SERVICE_UNAVAILABLE, locked.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle(state: &ServerState, req: Request<Incoming>) -> ControlResponse {
|
||||
if !authorized(&req, &state.token) {
|
||||
return error_response(ApiError::new(
|
||||
@@ -515,7 +529,7 @@ async fn route(state: &ServerState, req: Request<Incoming>) -> Result<ControlRes
|
||||
}
|
||||
(&Method::GET, ["flows"]) => {
|
||||
let filter = FlowFilter::from_query(&query).map_err(bad_request)?;
|
||||
json(&flows::list(&ctx.db, &filter)?)
|
||||
json(&flows::list(&ctx.db()?, &filter)?)
|
||||
}
|
||||
(&Method::POST, ["flows", "clear"]) => {
|
||||
let ctx = ctx.clone();
|
||||
@@ -524,7 +538,7 @@ async fn route(state: &ServerState, req: Request<Incoming>) -> Result<ControlRes
|
||||
(&Method::GET, ["flows", id]) => {
|
||||
found(ctx, id)?;
|
||||
let limit = body_limit(&query)?;
|
||||
json(&flows::detail(&ctx.db, &ctx.blobs, id, limit).await?)
|
||||
json(&flows::detail(&ctx.db()?, &ctx.blobs()?, id, limit).await?)
|
||||
}
|
||||
(&Method::GET, ["flows", id, "request-body"]) => raw_body(ctx, id, FlowBody::Request).await,
|
||||
(&Method::GET, ["flows", id, "response-body"]) => {
|
||||
@@ -533,20 +547,20 @@ async fn route(state: &ServerState, req: Request<Incoming>) -> Result<ControlRes
|
||||
(&Method::GET, ["flows", id, "messages"]) => {
|
||||
found(ctx, id)?;
|
||||
let (after_seq, limit) = message_page(&query)?;
|
||||
let messages = messages::list(&ctx.db, id, after_seq, limit)?;
|
||||
let total = messages::count(&ctx.db, id)?;
|
||||
let messages = messages::list(&ctx.db()?, id, after_seq, limit)?;
|
||||
let total = messages::count(&ctx.db()?, id)?;
|
||||
json(&ListMessagesResponse { messages, total })
|
||||
}
|
||||
(&Method::GET, ["flows", id, "messages", seq]) => {
|
||||
let flow = flows::find(&ctx.db, id)
|
||||
let flow = flows::find(&ctx.db()?, id)
|
||||
.ok_or_else(|| ApiError::new(StatusCode::NOT_FOUND, format!("No flow {id}")))?;
|
||||
let seq: i64 = seq.parse().map_err(|_| bad_request(format!("Invalid seq {seq:?}")))?;
|
||||
let message = messages::find(&ctx.db, id, seq)?.ok_or_else(|| {
|
||||
let message = messages::find(&ctx.db()?, id, seq)?.ok_or_else(|| {
|
||||
ApiError::new(StatusCode::NOT_FOUND, format!("No message {seq} in flow {id}"))
|
||||
})?;
|
||||
let payload = messages::payload(&ctx.db, &ctx.blobs, &message)?;
|
||||
let payload = messages::payload(&ctx.db()?, &ctx.blobs()?, &message)?;
|
||||
let (decoded, decode_error) = if message.kind == MessageKind::GrpcMessage {
|
||||
match grpc::decode(&ctx.db, &ctx.blobs, &ctx.grpc, &flow, seq, false).await {
|
||||
match grpc::decode(&ctx.db()?, &ctx.blobs()?, &ctx.grpc, &flow, seq, false).await {
|
||||
Ok(decoded) => (Some(decoded), None),
|
||||
Err(e) => (None, Some(e)),
|
||||
}
|
||||
@@ -556,20 +570,22 @@ async fn route(state: &ServerState, req: Request<Incoming>) -> Result<ControlRes
|
||||
let payload = BodyPreview::new(&payload, message.truncated);
|
||||
json(&MessageDetail { message, payload, decoded, decode_error })
|
||||
}
|
||||
(&Method::GET, ["grpc-schemas"]) => json(&grpc::list(&ctx.db)?),
|
||||
(&Method::GET, ["grpc-schemas"]) => json(&grpc::list(&ctx.db()?)?),
|
||||
(&Method::POST, ["grpc-schemas"]) => {
|
||||
let body: AddGrpcSchemaRequest = read_json(req).await?;
|
||||
let added = grpc::add(&ctx.db, &ctx.grpc, &body.host_pattern, &body.proto_paths);
|
||||
let db = ctx.db()?;
|
||||
let added = grpc::add(&db, &ctx.grpc, &body.host_pattern, &body.proto_paths);
|
||||
json(&added.await.map_err(bad_request)?)
|
||||
}
|
||||
(&Method::POST, ["grpc-schemas", "reflect"]) => {
|
||||
let body: ReflectGrpcHostRequest = read_json(req).await?;
|
||||
let (host, plaintext) = grpc::reflection_host(&body.host);
|
||||
let reflected = grpc::reflect(&ctx.db, &ctx.grpc, &host, plaintext).await;
|
||||
let db = ctx.db()?;
|
||||
let reflected = grpc::reflect(&db, &ctx.grpc, &host, plaintext).await;
|
||||
json(&reflected.map_err(bad_request)?)
|
||||
}
|
||||
(&Method::DELETE, ["grpc-schemas", id]) => {
|
||||
json(&RemovedResponse { removed: grpc::remove(&ctx.db, &ctx.grpc, id)? })
|
||||
json(&RemovedResponse { removed: grpc::remove(&ctx.db()?, &ctx.grpc, id)? })
|
||||
}
|
||||
(&Method::POST, ["flows", id, "save"]) => {
|
||||
found(ctx, id)?;
|
||||
@@ -579,17 +595,23 @@ async fn route(state: &ServerState, req: Request<Incoming>) -> Result<ControlRes
|
||||
}
|
||||
(&Method::GET, ["har"]) => {
|
||||
let filter = FlowFilter::from_query(&query).map_err(bad_request)?;
|
||||
let flows = flows::list(&ctx.db, &filter)?;
|
||||
json(&har::export(&ctx.db, &ctx.blobs, &flows, ctx.capture_body_limit()?).await?)
|
||||
let flows = flows::list(&ctx.db()?, &filter)?;
|
||||
json(&har::export(&ctx.db()?, &ctx.blobs()?, &flows, ctx.capture_body_limit()?).await?)
|
||||
}
|
||||
(&Method::GET, ["ca"]) => {
|
||||
let ca = CaStore::load_or_create(&ctx.ca_dir())
|
||||
.map_err(|e| format!("Failed to load proxy CA: {e}"))?;
|
||||
// Off the runtime's few workers: it may wait for the keychain or a conversion.
|
||||
let ca = {
|
||||
let ctx = ctx.clone();
|
||||
blocking(move || ctx.load_ca()).await?
|
||||
};
|
||||
Ok(response(StatusCode::OK, "application/x-pem-file", ca.pem().to_string()))
|
||||
}
|
||||
(&Method::POST, ["ca", "trust"]) => {
|
||||
let ca = CaStore::load_or_create(&ctx.ca_dir())
|
||||
.map_err(|e| format!("Failed to load proxy CA: {e}"))?;
|
||||
// Off the runtime's few workers: it may wait for the keychain or a conversion.
|
||||
let ca = {
|
||||
let ctx = ctx.clone();
|
||||
blocking(move || ctx.load_ca()).await?
|
||||
};
|
||||
ca::trust(ca.cert_path()).await.map_err(bad_request)?;
|
||||
ctx.forget_passthrough();
|
||||
json(&ca::status(ca.cert_path()).await?)
|
||||
@@ -625,24 +647,30 @@ async fn status(ctx: &ProxyCtx) -> Result<ControlStatus, ApiError> {
|
||||
proxy_port,
|
||||
configured_port: ctx.settings()?.port,
|
||||
recording: ctx.recording(),
|
||||
flow_count: flows::count(&ctx.db)?,
|
||||
// Answered while locked too, saying so.
|
||||
flow_count: match ctx.db() {
|
||||
Ok(db) => flows::count(&db)?,
|
||||
Err(_) => 0,
|
||||
},
|
||||
ca,
|
||||
system_proxy,
|
||||
locked: ctx.locked().map(|lock| lock.message),
|
||||
})
|
||||
}
|
||||
|
||||
fn found(ctx: &ProxyCtx, id: &str) -> Result<(), ApiError> {
|
||||
match flows::find(&ctx.db, id) {
|
||||
match flows::find(&ctx.db()?, id) {
|
||||
Some(_) => Ok(()),
|
||||
None => Err(ApiError::new(StatusCode::NOT_FOUND, format!("No flow {id}"))),
|
||||
}
|
||||
}
|
||||
|
||||
async fn raw_body(ctx: &ProxyCtx, id: &str, which: FlowBody) -> Result<ControlResponse, ApiError> {
|
||||
let flow = flows::find(&ctx.db, id)
|
||||
let flow = flows::find(&ctx.db()?, id)
|
||||
.ok_or_else(|| ApiError::new(StatusCode::NOT_FOUND, format!("No flow {id}")))?;
|
||||
let body =
|
||||
bodies::read_body(&ctx.db, &ctx.blobs, &flow, which, ctx.capture_body_limit()?).await?;
|
||||
bodies::read_body(&ctx.db()?, &ctx.blobs()?, &flow, which, ctx.capture_body_limit()?)
|
||||
.await?;
|
||||
let content_type =
|
||||
find_header(flow.headers(which), "content-type").unwrap_or("application/octet-stream");
|
||||
let mut resp = response(StatusCode::OK, content_type, body.data);
|
||||
|
||||
Reference in new issue
Block a user