Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

4.2 KB
Raw Blame
//! App text output masks credentials in workload logs and errors. Resource downloads preserve bytes.
use axum::{Router, extract::Request, http::StatusCode, response::IntoResponse};
use serde_json::json;
use tokio::net::TcpListener;

const LOG: &str = "acme_live_1234567890 bxl_app_ABCDEFGH bxt_QRSTUV23 postgres://reporter:[email protected]/unit";
const MASKED: &str =
    "acme_live_…7890 bxl_app_…EFGH bxt_…UV23 postgres://reporter:****@db.example/unit";

async fn platform() -> String {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let url = format!("http://{}", listener.local_addr().unwrap());
    let app = Router::new().fallback(|req: Request| async move {
        let path = req.uri().path();
        if path.ends_with("/logs") && req.uri().query().is_some_and(|q| q.contains("follow=1")) {
            return (
                [("content-type", "text/event-stream")],
                format!("event: log\ndata: {}\n\n", json!({"line":LOG})),
            )
                .into_response();
        }
        if path.ends_with("/logs") {
            return axum::Json(json!([{"line":LOG}])).into_response();
        }
        if path.ends_with("/blob") {
            return (
                [("content-type", "application/octet-stream")],
                [b"\xff\0".as_slice(), LOG.as_bytes()].concat(),
            )
                .into_response();
        }
        (
            StatusCode::FORBIDDEN,
            axum::Json(json!({"error":{"code":"forbidden","message":LOG,"next":LOG}})),
        )
            .into_response()
    });
    tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    url
}
async fn command(url: &str, args: &[&str]) -> std::process::Output {
    let dir = tempfile::tempdir().unwrap();
    tokio::process::Command::new(assert_cmd::cargo::cargo_bin!("boxlite"))
        .env_clear()
        .env("HOME", dir.path())
        .env("BOXLITE_API_KEY", "test-key")
        .current_dir(dir.path())
        .args(["--url", url, "--path-prefix", "org"])
        .args(args)
        .output()
        .await
        .unwrap()
}

#[tokio::test]
async fn app_redaction_masks_logs_across_json_human_and_stream_outputs() {
    let url = platform().await;
    for flags in [vec![], vec!["--json"], vec!["--follow", "--json"]] {
        let mut args = vec!["app", "--app", "shop", "logs"];
        args.extend(flags);
        let out = command(&url, &args).await;
        assert!(
            out.status.success(),
            "{}",
            String::from_utf8_lossy(&out.stderr)
        );
        let text = String::from_utf8(out.stdout).unwrap();
        assert!(
            text.contains(MASKED),
            "App log output did not mask credentials"
        );
    }
}

#[tokio::test]
async fn app_redaction_masks_structured_errors_and_preserves_raw_api_downloads() {
    let url = platform().await;
    let raw = command(&url, &["api", "GET", "/blob"]).await;
    assert!(raw.status.success());
    assert_eq!(raw.stdout, [b"\xff\0".as_slice(), LOG.as_bytes()].concat());
    let out = command(&url, &["app", "--json", "list"]).await;
    assert_eq!(out.status.code(), Some(5));
    let body: serde_json::Value = serde_json::from_slice(&out.stderr).unwrap();
    assert!(
        body["error"]["message"] == MASKED && body["error"]["next"] == MASKED,
        "App stderr did not mask credentials"
    );
}

#[tokio::test]
async fn app_redaction_masks_clap_argument_errors_before_the_command_runs() {
    let url = platform().await;
    let out = command(
        &url,
        &[
            "app",
            "--json",
            "request",
            "/",
            "--as",
            "[email protected]",
            "--role",
            "bxl_app_ABCDEFGH",
        ],
    )
    .await;
    assert!(!out.status.success());
    let text = String::from_utf8(out.stderr).unwrap();
    assert!(
        !text.contains("bxl_app_ABCDEFGH"),
        "App parser stderr disclosed its credential-shaped input"
    );
    let body: serde_json::Value = serde_json::from_str(&text).unwrap();
    assert_eq!(body["error"]["code"], "usage");
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("bxl_app_…EFGH")
    );
}