Files
fundamentum-client/crates/tessera/tests/surface.rs
T
2026-11-05 09:05:13 +01:00

151 lines
8.2 KiB
Rust

//! No key export: no public API of tessera returns private key bytes.
//!
//! - Compile time: the secret types implement none of the traits that would hand out their bytes.
//! - The API surface is locked: every `pub fn` in `src/` is listed here. A new public function fails this
//! test until someone has checked that it returns no secret and added it to the list.
use static_assertions::assert_not_impl_any;
use tessera::{RootKey, Subkey};
assert_not_impl_any!(RootKey: Clone, AsRef<[u8]>, std::ops::Deref, Into<[u8; 32]>, Into<Vec<u8>>, std::fmt::Display);
assert_not_impl_any!(Subkey: Clone, AsRef<[u8]>, std::ops::Deref, Into<[u8; 32]>, Into<Vec<u8>>, std::fmt::Display);
/// Every public function, as `file: signature` with whitespace collapsed. Each returns only public data.
const SURFACE: &[&str] = &[
"backup.rs: pub fn from_bytes(b: [u8; 32]) -> BackupSecret",
"backup.rs: pub fn mailbox_seed(&self) -> Zeroizing<[u8; 32]>",
"backup.rs: pub fn open(&self, aad: &[u8], sealed: &[u8]) -> Option<Vec<u8>>",
"backup.rs: pub fn seal(&self, nonce: &[u8; 24], aad: &[u8], plain: &[u8]) -> Vec<u8>",
"cert.rs: pub fn app_id(&self) -> &Id",
"cert.rs: pub fn certify(signer: &impl RootSigner, req: &CertRequest) -> Result<SubkeyCert, CertError>",
"cert.rs: pub fn from_bytes(b: &[u8]) -> Result<SubkeyCert, CertError>",
"cert.rs: pub fn from_text(s: &str) -> Result<SubkeyCert, CertError>",
"cert.rs: pub fn match_code(subkey: &PublicKey) -> String",
"cert.rs: pub fn not_after(&self) -> u64",
"cert.rs: pub fn not_before(&self) -> u64",
"cert.rs: pub fn purpose(&self) -> &Id",
"cert.rs: pub fn root(&self) -> PublicKey",
"cert.rs: pub fn standard(subkey: PublicKey, app_id: Id, purpose: Id, now: u64) -> CertRequest",
"cert.rs: pub fn subkey(&self) -> PublicKey",
"cert.rs: pub fn tbs(&self, root: &PublicKey) -> Vec<u8>",
"cert.rs: pub fn to_bytes(&self) -> Vec<u8>",
"cert.rs: pub fn to_text(&self) -> String",
"cert.rs: pub fn verify(&self, root: &PublicKey, now: u64) -> Result<(), CertError>",
"hlc.rs: pub fn from_bytes(b: &[u8; 12]) -> Hlc",
"hlc.rs: pub fn last(&self) -> Hlc",
"hlc.rs: pub fn observe(&mut self, remote: Hlc, now: u64) -> Result<Hlc, ClockError>",
"hlc.rs: pub fn tick(&mut self, now: u64) -> Result<Hlc, ClockError>",
"hlc.rs: pub fn to_bytes(&self) -> [u8; 12]",
"hlc.rs: pub fn with_max_drift(max_drift_ms: u64) -> Clock",
"id.rs: pub fn as_str(&self) -> &str",
"id.rs: pub fn new(s: &str) -> Result<Id, IdError>",
"keys.rs: pub fn as_bytes(&self) -> &[u8; 32]",
"keys.rs: pub fn backup_secret(&self, app_id: &crate::Id) -> crate::BackupSecret",
"keys.rs: pub fn as_bytes(&self) -> &[u8; 64]",
"keys.rs: pub fn from_bytes(b: [u8; 32]) -> PublicKey",
"keys.rs: pub fn from_bytes(b: [u8; 64]) -> Signature",
"keys.rs: pub fn from_mnemonic(phrase: &str) -> Result<RootKey, MnemonicError>",
"keys.rs: pub fn from_seed(seed: &[u8; 32]) -> Subkey",
"keys.rs: pub fn from_text(s: &str) -> Option<PublicKey>",
"keys.rs: pub fn public(&self) -> PublicKey",
"keys.rs: pub fn public(&self) -> PublicKey",
"keys.rs: pub fn sign_credential_tbs(&self, tbs: &[u8]) -> Signature",
"keys.rs: pub fn sign_event_tbs(&self, tbs: &[u8]) -> Signature",
"keys.rs: pub fn verify_credential_sig(subkey: &PublicKey, tbs: &[u8], sig: &Signature) -> bool",
"keys.rs: pub fn verify_event_sig(author: &PublicKey, tbs: &[u8], sig: &Signature) -> bool",
"lib.rs: pub fn hash(bytes: &[u8]) -> [u8; 32]",
"merge.rs: pub fn contains(&self, hash: &[u8; 32]) -> bool",
"merge.rs: pub fn events(&self) -> &[E]",
"merge.rs: pub fn from_stored(events: Vec<E>) -> Log<E>",
"merge.rs: pub fn is_empty(&self) -> bool",
"merge.rs: pub fn len(&self) -> usize",
"merge.rs: pub fn merge<E: LogEvent + Clone, R>( log: &Log<E>, batch: impl IntoIterator<Item = E>, mut verify: impl FnMut(&E) -> Result<(), R>, ) -> Merged<E, R>",
"merge.rs: pub fn new() -> Log<E>",
"merge.rs: pub fn to_bytes(&self) -> Vec<u8>",
"revoke.rs: pub fn accepted(&self) -> &[[u8; 32]]",
"revoke.rs: pub fn add(&mut self, r: Revocation, group_roots: &[PublicKey]) -> Result<(), RevocationError>",
"revoke.rs: pub fn check(&self, author: &PublicKey, hash: &[u8; 32]) -> Result<(), RevocationError>",
"revoke.rs: pub fn from_bytes(b: &[u8]) -> Result<Revocation, RevocationError>",
"revoke.rs: pub fn from_text(s: &str) -> Result<Revocation, RevocationError>",
"revoke.rs: pub fn is_revoked(&self, subkey: &PublicKey) -> bool",
"revoke.rs: pub fn revoke( signer: &impl RevokeSigner, subkey: PublicKey, revoked_at: u64, accepted: Vec<[u8; 32]>, ) -> Result<Vec<Revocation>, RevocationError>",
"revoke.rs: pub fn revoked_at(&self) -> u64",
"revoke.rs: pub fn root(&self) -> PublicKey",
"revoke.rs: pub fn subkey(&self) -> PublicKey",
"revoke.rs: pub fn tbs(&self) -> Vec<u8>",
"revoke.rs: pub fn to_bytes(&self) -> Vec<u8>",
"revoke.rs: pub fn to_text(&self) -> String",
"revoke.rs: pub fn verify(&self, root: &PublicKey) -> Result<(), RevocationError>",
];
/// `pub fn` signatures in `src/`, up to the opening brace, whitespace collapsed.
fn public_fns() -> Vec<String> {
let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
let mut out = Vec::new();
for entry in std::fs::read_dir(&src).unwrap() {
let path = entry.unwrap().path();
let name = path.file_name().unwrap().to_string_lossy().into_owned();
let text = std::fs::read_to_string(&path).unwrap();
let mut rest = text.as_str();
while let Some(at) = rest.find("pub fn ") {
let sig_and_more = &rest[at..];
// The first `{` or `;` outside brackets (`[u8; 32]` has a `;` inside).
let mut depth = 0i32;
let end = sig_and_more
.char_indices()
.find(|&(_, c)| {
match c {
'[' | '(' => depth += 1,
']' | ')' => depth -= 1,
_ => {}
}
depth == 0 && (c == '{' || c == ';')
})
.unwrap()
.0;
let sig: String = sig_and_more[..end].split_whitespace().collect::<Vec<_>>().join(" ");
out.push(format!("{name}: {sig}"));
rest = &sig_and_more[end..];
}
}
out.sort();
out
}
#[test]
fn the_public_surface_is_locked() {
let mut want: Vec<String> = SURFACE.iter().map(|s| s.to_string()).collect();
want.sort();
let got = public_fns();
let new: Vec<_> = got.iter().filter(|s| !want.contains(s)).collect();
let gone: Vec<_> = want.iter().filter(|s| !got.contains(s)).collect();
assert!(new.is_empty() && gone.is_empty(), "review the surface.\nnew: {new:#?}\ngone: {gone:#?}");
}
#[test]
fn only_public_data_comes_back_as_bytes() {
// The few functions that return raw bytes, and why each is public data.
let allowed = [
"pub fn as_bytes(&self) -> &[u8; 32]", // PublicKey
"pub fn as_bytes(&self) -> &[u8; 64]", // Signature
"pub fn hash(bytes: &[u8]) -> [u8; 32]", // SHA-256
"pub fn tbs(&self, root: &PublicKey) -> Vec<u8>", // the unsigned cert
"pub fn to_bytes(&self) -> Vec<u8>", // cert, log
"pub fn to_bytes(&self) -> [u8; 12]", // Hlc
"pub fn tbs(&self) -> Vec<u8>", // the unsigned revocation
"pub fn seal(&self, nonce: &[u8; 24], aad: &[u8], plain: &[u8]) -> Vec<u8>", // ciphertext
];
// The one secret that comes out, and why: the backup mailbox's access-key seed is a relay credential (it
// lets its holder fetch ciphertext, as an invite token's access key does), never an identity key.
let secret_ok = ["pub fn mailbox_seed(&self) -> Zeroizing<[u8; 32]>"];
for f in public_fns() {
let sig = f.split_once(": ").unwrap().1;
let returns_bytes = sig.contains("-> [u8") || sig.contains("-> &[u8") || sig.contains("-> Vec<u8>");
assert!(!returns_bytes || allowed.contains(&sig), "returns bytes: {f}");
assert!(
secret_ok.contains(&sig) || (!sig.contains("SigningKey") && !sig.contains("Zeroizing")),
"exposes a secret type: {f}"
);
}
}