~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/vuln-notif

0
0
main
387 lines · 13.8 KB Raw History
//! Contract test for the PDS surface `vuln-notif` publishes through.
//!
//! Env-gated against a real running `atproto-pds` — the local PLC/PDS pair the
//! e2e scripts start — because the in-process `atproto-pds` dev-dependency
//! cannot be built on this toolchain. Needs `ATP_PDS_URL`, `ATP_HANDLE` and
//! `ATP_APP_PASSWORD`; without them it prints a skip line and returns green.
//!
//! Every XRPC operation is driven through the public `Publisher` surface the
//! service itself uses, and asserted the way the publish path consumes it:
//!
//! - `Publisher::login` is `createSession` with the app password; the raw
//!   response shape (DID, handle, both JWTs) is also checked directly.
//! - `create_row` is `createRecord` for the custom NSID
//!   `com.nahdig.cve.nvdV2` with `validate` omitted — the transport never
//!   sends the flag. The local PDS has our lexicons, so the record carries the
//!   one required field, `createdAt`.
//! - `check_row` is `getRecord`: present → `found`, absent → the
//!   `RecordNotFound` classifier path, reported as missing and not an error.
//! - A duplicate `create_row` exercises the `RecordAlreadyExists` classifier
//!   path: it reports `found`, not a failure.
//! - `create_batch` is `applyWrites`, asserted index-aligned.
//! - `create_large_row` is `uploadBlob` + a pointer `createRecord`; the record
//!   and blob are read back over `listRecords`/`com.atproto.sync.getBlob` and
//!   the envelope, gzip payload, SHA-256 and CBOR sizes are checked.

use std::io::{Read, Write};
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use flate2::Compression;
use flate2::write::GzEncoder;
use serde_json::{Value, json};
use vuln_notif::constants::{
    BLOB_MIME, COLLECTION, PDS_REQUESTS_PER_5MIN, PDS_WRITE_DAILY, PDS_WRITE_HOURLY, RECORD_TARGET,
};
use vuln_notif::db::DbHandle;
use vuln_notif::db::model::CveRow;
use vuln_notif::pds::{Publisher, PublisherConfig, RateGovernor};
use vuln_notif::records::{self, PreparedRow};
use vuln_notif::time::RealTime;

const SKIP: &str = "SKIP pds_contract: ATP_PDS_URL, ATP_HANDLE and ATP_APP_PASSWORD must be set \
(start scripts/local_plc.sh, scripts/local_atproto_pds.sh, \
scripts/create_local_account.sh, then export the printed ATP_* values)";

fn env(name: &str) -> Option<String> {
    std::env::var(name)
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
}

fn normal_prepared(cve_id: &str, record: Value) -> PreparedRow {
    PreparedRow {
        cve_id: cve_id.to_owned(),
        record,
        mode: "normal".to_owned(),
        blob: None,
        sha256: None,
        blob_bytes: 0,
        record_bytes: 0,
        reason: None,
    }
}

fn gzip(bytes: &[u8]) -> Vec<u8> {
    let mut encoder = GzEncoder::new(Vec::new(), Compression::best());
    encoder.write_all(bytes).expect("gzip payload");
    encoder.finish().expect("gzip trailer")
}

/// A live PDS record must satisfy the custom lexicon. Only `createdAt` is
/// required; `$type` has to equal the collection.
fn record(id: &str) -> Value {
    json!({
        "$type": COLLECTION,
        "createdAt": "2025-01-01T00:00:00.000Z",
        "id": id,
        "sourceIdentifier": "security@example.invalid",
        "published": "2024-01-01T00:00:00.000Z",
        "lastModified": "2024-01-02T00:00:00.000Z",
        "vulnStatus": "Analyzed",
    })
}

#[tokio::test(flavor = "multi_thread")]
async fn publisher_contract_against_live_pds() {
    let (Some(pds_url), Some(handle), Some(app_password)) = (
        env("ATP_PDS_URL"),
        env("ATP_HANDLE"),
        env("ATP_APP_PASSWORD"),
    ) else {
        eprintln!("{SKIP}");
        return;
    };
    let base = pds_url.trim_end_matches('/').to_string();
    let http = reqwest::Client::builder()
        .timeout(Duration::from_secs(60))
        .build()
        .expect("http client");

    // `createSession` with the app password, asserted on the wire before the
    // Publisher reuses it.
    let response = http
        .post(format!("{base}/xrpc/com.atproto.server.createSession"))
        .json(&json!({"identifier": handle, "password": app_password}))
        .send()
        .await
        .expect("createSession request");
    assert_eq!(
        response.status(),
        200,
        "createSession must accept the app password"
    );
    let content_type = response
        .headers()
        .get(reqwest::header::CONTENT_TYPE)
        .and_then(|value| value.to_str().ok())
        .unwrap_or_default()
        .to_string();
    assert!(
        content_type.starts_with("application/json"),
        "createSession answers JSON, got {content_type:?}"
    );
    let session: Value = response.json().await.expect("createSession JSON");
    assert_eq!(session["handle"].as_str(), Some(handle.as_str()));
    let did = session["did"]
        .as_str()
        .expect("createSession returns the account DID")
        .to_string();
    assert!(did.starts_with("did:"), "session DID: {did}");
    assert!(
        session["accessJwt"]
            .as_str()
            .is_some_and(|token| !token.is_empty())
    );
    assert!(
        session["refreshJwt"]
            .as_str()
            .is_some_and(|token| !token.is_empty())
    );
    assert_eq!(session["active"], true);

    let tmp = tempfile::TempDir::new().expect("tempdir");
    let db = DbHandle::open(tmp.path().join("cache.db"))
        .await
        .expect("temp cache DB");
    let governor = Arc::new(RateGovernor::new(
        db.clone(),
        PDS_WRITE_HOURLY,
        PDS_WRITE_DAILY,
        PDS_REQUESTS_PER_5MIN,
        Arc::new(RealTime),
    ));
    let publisher = Publisher::new(PublisherConfig {
        pds_url: base.clone(),
        handle: handle.clone(),
        app_password,
        collection: COLLECTION.to_string(),
        timeout_secs: 60.0,
        governor,
    })
    .expect("publisher");
    publisher.login().await.expect("Publisher::login");
    assert!(
        publisher.login_calls() >= 1,
        "login goes through createSession"
    );
    let repo = publisher.repo().to_string();
    assert_eq!(repo, did, "login learns the DID createSession named");
    assert_eq!(publisher.collection(), COLLECTION);

    let stamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("clock")
        .as_nanos();
    let cve_id = format!("CVE-2099-{stamp}");
    let prepared = normal_prepared(&cve_id, record(&cve_id));

    // createRecord for the custom NSID, with `validate` omitted.
    let created = publisher.create_row(&prepared).await.expect("create_row");
    assert!(
        created.created && !created.found,
        "create_row must create on a fresh key: {created:?}"
    );
    assert_eq!(created.error, None, "{created:?}");
    let rkey = records::sanitize_rkey(&cve_id);
    let uri = format!("at://{repo}/{COLLECTION}/{rkey}");
    assert_eq!(created.uri.as_deref(), Some(uri.as_str()));
    assert!(created.cid.as_deref().is_some_and(|cid| !cid.is_empty()));

    // getRecord: the record is present.
    let check = publisher
        .check_row(&CveRow::new(cve_id.clone(), record(&cve_id)))
        .await
        .expect("check_row present");
    assert!(check.found, "check_row must find the record: {check:?}");
    assert_eq!(check.error, None, "{check:?}");
    assert_eq!(check.uri.as_deref(), Some(uri.as_str()));

    // getRecord: RecordNotFound classifies as absence, not failure.
    let missing_row = CveRow::new(format!("CVE-2099-{stamp}404"), json!({}));
    let missing = publisher
        .check_row(&missing_row)
        .await
        .expect("check_row missing");
    assert!(
        !missing.found,
        "a missing record is not present: {missing:?}"
    );
    assert_eq!(
        missing.error, None,
        "RecordNotFound is the classifier's missing path, not an error: {missing:?}"
    );

    // createRecord again: RecordAlreadyExists classifies as present.
    let duplicate = publisher
        .create_row(&prepared)
        .await
        .expect("duplicate create_row");
    assert!(
        duplicate.found && !duplicate.created,
        "a duplicate must classify as present: {duplicate:?}"
    );
    assert_eq!(
        duplicate.error, None,
        "RecordAlreadyExists is the classifier's present path: {duplicate:?}"
    );

    // applyWrites: one result per write, index-aligned.
    let batch_ids = [
        format!("CVE-2099-{stamp}002"),
        format!("CVE-2099-{stamp}003"),
    ];
    let batch: Vec<PreparedRow> = batch_ids
        .iter()
        .map(|id| normal_prepared(id, record(id)))
        .collect();
    let (written, refs) = publisher.create_batch(&batch).await.expect("create_batch");
    assert_eq!(written.error, None, "{written:?}");
    let refs = refs.expect("applyWrites returns index-aligned results");
    assert_eq!(refs.len(), batch.len());
    for (reference, id) in refs.iter().zip(&batch_ids) {
        assert_eq!(
            reference.uri,
            format!("at://{repo}/{COLLECTION}/{}", records::sanitize_rkey(id))
        );
        assert!(!reference.cid.is_empty());
    }

    // uploadBlob + pointer createRecord.
    let payload = serde_json::to_vec(&json!({
        "id": format!("CVE-2099-{stamp}004"),
        "configurations": [{"nodes": [{"operator": "OR", "negate": false}]}],
        "descriptions": [{"lang": "en", "value": "contract blob"}],
    }))
    .expect("payload JSON");
    let payload_sha = records::sha256_hex(&payload);
    let gzipped = gzip(&payload);
    let blob_id = format!("CVE-2099-{stamp}004");
    let pointer = json!({
        "$type": COLLECTION,
        "createdAt": "2025-01-01T00:00:00.000Z",
        "id": blob_id.clone(),
        "vulnStatus": "Analyzed",
        "dataSha256": payload_sha.clone(),
        "dataBytes": payload.len(),
        "dataEncoding": "gzip",
    });
    let prepared_blob = PreparedRow {
        cve_id: blob_id.clone(),
        record: pointer,
        mode: "blob".to_owned(),
        blob: Some(gzipped.clone()),
        sha256: Some(payload_sha.clone()),
        blob_bytes: gzipped.len() as u64,
        record_bytes: 0,
        reason: None,
    };
    let large = publisher
        .create_large_row(&prepared_blob)
        .await
        .expect("create_large_row");
    assert!(large.created, "blob pointer record: {large:?}");
    assert_eq!(large.error, None, "{large:?}");
    assert_eq!(large.blob_bytes, gzipped.len() as u64);
    assert!(
        large.record_bytes > 0 && large.record_bytes <= RECORD_TARGET as u64,
        "compact record stays under {RECORD_TARGET} bytes: {large:?}"
    );
    let pointer_uri = format!(
        "at://{repo}/{COLLECTION}/{}",
        records::sanitize_rkey(&blob_id)
    );
    assert_eq!(large.uri.as_deref(), Some(pointer_uri.as_str()));
    let blob_cid = large.blob_cid.clone().expect("uploadBlob returns a CID");

    // Read the compact record back and check its shape.
    let listing: Value = http
        .get(format!("{base}/xrpc/com.atproto.repo.listRecords"))
        .query(&[
            ("repo", repo.as_str()),
            ("collection", COLLECTION),
            ("limit", "100"),
        ])
        .send()
        .await
        .expect("listRecords request")
        .error_for_status()
        .expect("listRecords status")
        .json()
        .await
        .expect("listRecords JSON");
    let value = listing["records"]
        .as_array()
        .expect("records array")
        .iter()
        .find(|entry| entry["value"]["id"].as_str() == Some(blob_id.as_str()))
        .map(|entry| &entry["value"])
        .unwrap_or_else(|| panic!("{blob_id} is not in listRecords: {listing}"));
    assert!(
        value.get("configurations").is_none(),
        "the pointer record must not carry configurations: {value}"
    );
    assert_eq!(value["dataEncoding"], "gzip");
    assert_eq!(value["dataSha256"].as_str(), Some(payload_sha.as_str()));
    assert_eq!(
        value["dataBytes"].as_u64(),
        Some(payload.len() as u64),
        "dataBytes is the uncompressed payload length"
    );
    let blob = &value["dataBlob"];
    assert_eq!(blob["$type"], "blob", "blob envelope: {blob}");
    assert_eq!(blob["mimeType"], BLOB_MIME);
    assert_eq!(blob["ref"]["$link"].as_str(), Some(blob_cid.as_str()));
    assert_eq!(blob["size"].as_u64(), Some(gzipped.len() as u64));
    assert_eq!(
        blob.as_object().expect("blob object").len(),
        4,
        "exactly $type/ref/mimeType/size: {blob}"
    );
    let cbor = records::cbor_bytes(value).expect("compact record CBOR");
    assert!(
        cbor.len() <= RECORD_TARGET,
        "compact record CBOR is {} bytes, over the {RECORD_TARGET} target",
        cbor.len()
    );

    // getBlob serves the exact bytes once a public record references them.
    let response = http
        .get(format!("{base}/xrpc/com.atproto.sync.getBlob"))
        .query(&[("did", repo.as_str()), ("cid", blob_cid.as_str())])
        .send()
        .await
        .expect("getBlob request");
    assert_eq!(response.status(), 200, "getBlob must serve the blob");
    assert_eq!(
        response
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|value| value.to_str().ok()),
        Some(BLOB_MIME)
    );
    let served = response.bytes().await.expect("getBlob bytes");
    assert_eq!(
        &served[..],
        &gzipped[..],
        "getBlob returns the uploaded bytes"
    );
    let mut decoder = flate2::read::GzDecoder::new(&served[..]);
    let mut decompressed = Vec::new();
    decoder
        .read_to_end(&mut decompressed)
        .expect("gzip-decode the served blob");
    assert_eq!(
        decompressed, payload,
        "gzip decodes to the canonical payload"
    );
    let decompressed_json: Value = serde_json::from_slice(&decompressed).expect("payload is JSON");
    assert!(
        decompressed_json.get("configurations").is_some(),
        "the blob carries what the pointer elides: {decompressed_json}"
    );

    publisher.close().await.expect("close publisher");
    db.close().await.expect("close cache DB");
}