use super::*;
pub(super) const BLOB_MANIFEST: &str = "blob.json";
pub(super) const FILE_MANIFEST: &str = "file.json";
pub(super) const CHUNKS_DIR: &str = "chunks";
pub(super) fn chunk_path(dir: &std::path::Path, index: usize) -> std::path::PathBuf {
dir.join(CHUNKS_DIR).join(format!("{index:06}.bin"))
}
#[allow(deprecated)] pub(super) fn file_key_bytes(
resp: &vti_specs::rooms::keys::file_key::v0_2::Response,
) -> Result<vti_rooms::files::FileKey, Box<dyn std::error::Error>> {
use base64::Engine as _;
let bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(resp.key.as_str())?;
bytes
.try_into()
.map_err(|_| "the VTA returned a file key that is not 32 bytes".into())
}
#[allow(deprecated)] pub async fn cmd_rooms_file_seal(
client: &VtaClient,
room_id: &str,
path: &std::path::Path,
out: &std::path::Path,
padme: bool,
) -> Result<(), Box<dyn std::error::Error>> {
use vti_rooms::files::{self, FileBinding, Padding};
if out.exists() {
return Err(format!("{} already exists; choose a new directory", out.display()).into());
}
let plaintext = std::fs::read(path)?;
let name = path
.file_name()
.and_then(|n| n.to_str())
.ok_or("the file has no usable name")?;
let file_id = files::new_file_id();
let resp = client
.room_file_key(room_id, &files::encode_file_id(&file_id), None)
.await?;
let key = file_key_bytes(&resp)?;
let sealed = files::seal_file(
&key,
FileBinding {
room_id: room_id.to_string(),
file_id,
epoch: resp.epoch.get(),
segment_size: files::DEFAULT_SEGMENT_SIZE,
},
name,
None,
&plaintext,
if padme { Padding::Padme } else { Padding::None },
)?;
std::fs::create_dir_all(out.join(CHUNKS_DIR))?;
for (i, chunk) in sealed.chunks.iter().enumerate() {
std::fs::write(chunk_path(out, i), chunk)?;
}
std::fs::write(
out.join(BLOB_MANIFEST),
serde_json::to_vec_pretty(&sealed.blob)?,
)?;
std::fs::write(
out.join(FILE_MANIFEST),
serde_json::to_vec_pretty(&sealed.file)?,
)?;
println!(
"Sealed {} ({} bytes) under epoch {} into {}\n blobRef {}\n chunks {} of {} bytes",
name,
sealed.file.size,
sealed.file.epoch,
out.display(),
sealed.file.blob_ref,
sealed.blob.chunks.chunk_count,
sealed.blob.chunks.chunk_size,
);
println!(
" {FILE_MANIFEST} goes inside the sealed record body; it names the file and is never \
sent to the host in the clear."
);
Ok(())
}
pub async fn cmd_rooms_file_open(
client: &VtaClient,
room_id: &str,
dir: &std::path::Path,
out: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
use vti_rooms::files::{self, BlobManifest, FileManifest};
if out.exists() {
return Err(format!("{} already exists; refusing to overwrite it", out.display()).into());
}
let file: FileManifest = serde_json::from_slice(&std::fs::read(dir.join(FILE_MANIFEST))?)?;
let blob: BlobManifest = serde_json::from_slice(&std::fs::read(dir.join(BLOB_MANIFEST))?)?;
file.check_blob(&blob)?;
let mut chunks = Vec::with_capacity(blob.chunks.chunk_count as usize);
for (i, expected) in blob.chunks.chunk_digests.iter().enumerate() {
let chunk = std::fs::read(chunk_path(dir, i))?;
if !files::same_digest(&files::digest_multibase(&chunk), expected) {
return Err(format!("chunk {i} does not match the manifest").into());
}
chunks.push(chunk);
}
let resp = client
.room_file_key(room_id, &file.file_id, Some(file.epoch))
.await?;
let key = file_key_bytes(&resp)?;
let plaintext = files::open_file(&key, room_id, &file, &blob, &chunks)?;
let tmp = out.with_extension("partial");
std::fs::write(&tmp, &plaintext)?;
std::fs::rename(&tmp, out)?;
println!(
"Opened {} ({} bytes) into {} — the digest its author signed matches.",
file.name.escape_debug(),
plaintext.len(),
out.display()
);
Ok(())
}
pub(super) const SEALED_WRITE_ATTEMPTS: usize = 4;
pub(super) async fn room_tier(
client: &VtaClient,
target: &RoomTarget<'_>,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
let (sealed, member, _) = room_tier_and_epoch(client, target).await?;
Ok((sealed, member))
}
#[allow(deprecated)] pub(super) async fn room_tier_and_epoch(
client: &VtaClient,
target: &RoomTarget<'_>,
) -> Result<(bool, String, u64), Box<dyn std::error::Error>> {
use trust_tasks_rs::Payload as _;
let (member, reply) = act_and_send(
client,
target,
vti_specs::rooms::info::v0_3::Payload::TYPE_URI,
serde_json::json!({ "roomId": target.room_id }),
)
.await
.map_err(|e| format!("could not read the room's tier (rooms/info): {e}"))?;
let visibility = reply
.get("visibility")
.and_then(Value::as_str)
.ok_or("the host's rooms/info response names no visibility")?;
let epoch = reply
.get("epoch")
.and_then(Value::as_u64)
.ok_or("the host's rooms/info response names no epoch")?;
Ok((visibility != "open", member, epoch))
}
pub(super) async fn write_sealed(
client: &VtaClient,
target: &RoomTarget<'_>,
content: SealedRecordBody,
expected_version: Option<u64>,
) -> Result<u64, Box<dyn std::error::Error>> {
write_sealed_naming(client, target, content, expected_version, None).await
}
#[allow(deprecated)] pub(super) async fn write_sealed_naming(
client: &VtaClient,
target: &RoomTarget<'_>,
content: SealedRecordBody,
expected_version: Option<u64>,
blobs: Option<Vec<String>>,
) -> Result<u64, Box<dyn std::error::Error>> {
let head = list_page(client, target, None, None, None)
.await?
.head_version
.unwrap_or(0);
let key = content.key.clone();
let mut intended = head + 1;
let mut expected = expected_version;
let mut landed = 0;
let mut content = content;
for _ in 0..SEALED_WRITE_ATTEMPTS {
let unsigned = serde_json::to_vec(&content.clone().at_version(intended))?;
let sealed = match client
.room_seal_as_principal_v0_3(target.room_id, &key, intended, &unsigned)
.await
{
Ok(r) => serde_json::to_value(r.sealed)?,
Err(VtaError::UnsupportedTaskType { .. })
if content.written_at.is_some()
&& content.kind.is_none()
&& content.in_reply_to.is_none()
&& content.edited_at.is_none() =>
{
content.written_at = None;
continue;
}
Err(e @ VtaError::UnsupportedTaskType { .. }) => {
return Err(format!(
"{e}\n\nYour VTA predates `rooms/keys/seal/0.3`, which seals a body \
carrying --kind or --reply-to: upgrade it, or write the record without \
them."
)
.into());
}
Err(e) => return Err(e.into()),
};
let mut payload = serde_json::json!({
"roomId": target.room_id,
"key": key,
"sealed": {
"ciphertext": sealed["ciphertext"],
"nonce": sealed["nonce"],
"epoch": sealed["epoch"],
},
});
if let Some(v) = expected {
payload["expectedVersion"] = serde_json::json!(v);
}
if let Some(b) = &blobs {
payload["blobs"] = serde_json::json!(b);
}
let (_, reply) = if blobs.is_some() {
act_and_send(
client,
target,
vti_rooms::wire::ROOMS_RECORDS_PUT_V3_TYPE,
payload,
)
.await
.map_err(|e| put_refused(e, target))?
} else {
let (principal, reply, _) = act_and_send_newest(
client,
target,
(vti_rooms::wire::ROOMS_RECORDS_PUT_V5_TYPE, payload.clone()),
Some((vti_rooms::wire::ROOMS_RECORDS_PUT_V3_TYPE, payload)),
)
.await
.map_err(|e| put_refused(e, target))?;
(principal, reply)
};
let put: vti_rooms::wire::PutRecordResponse = parse_reply("rooms/records/put", reply)?;
landed = put.version;
if landed == intended {
return Ok(landed);
}
expected = Some(landed);
intended = landed + 1;
}
Err(format!(
"{key} is stored at version {landed} but sealed for another version, so it does not \
open: the room kept moving under {SEALED_WRITE_ATTEMPTS} attempts. Write it again \
with --expected-version {landed}."
)
.into())
}
pub(super) fn keys_commit_command(target: &RoomTarget<'_>) -> String {
let mut cmd = format!(
"pnm rooms keys commit --room {} --host {}",
target.room_id, target.host_url
);
if let Some(did) = target.host_did {
cmd.push_str(&format!(" --host-did {did}"));
}
cmd
}
pub(super) fn put_refused(
e: Box<dyn std::error::Error>,
target: &RoomTarget<'_>,
) -> Box<dyn std::error::Error> {
let refusal = e
.downcast_ref::<vtc_client::VtcError>()
.and_then(vtc_client::git_ns::task_error);
match refusal {
Some((code, message)) => match put_refusal_hint(&code, &message, target) {
Some(hint) => hint.into(),
None => e,
},
None => e,
}
}
pub(super) fn put_refusal_hint(
code: &str,
message: &str,
target: &RoomTarget<'_>,
) -> Option<String> {
use vti_specs::rooms::records::put;
let epoch_mismatch = [
put::v0_1::error_codes::EPOCH_MISMATCH.code,
put::v0_2::error_codes::EPOCH_MISMATCH.code,
];
if !epoch_mismatch.contains(&code) {
return None;
}
Some(format!(
"the host refused the record: {message}\n\n\
The room has moved on to a newer epoch — it was renewed, or its membership changed — \
and this VTA has not applied the commit that moved it, so it sealed the record under \
the old epoch. Nothing was written. Catch up, then write again:\n\n {}",
keys_commit_command(target)
))
}
pub(super) fn open_refusal_hint(e: &VtaError, target: &RoomTarget<'_>) -> String {
use vti_specs::rooms::keys::open::v0_2::error_codes::UNKNOWN_EPOCH;
epoch_refusal_hint(e, target, UNKNOWN_EPOCH.code, Sealed::Record)
}
pub(super) fn file_key_refusal_hint(e: &VtaError, target: &RoomTarget<'_>) -> String {
use vti_specs::rooms::keys::file_key::v0_2::error_codes::UNKNOWN_EPOCH;
epoch_refusal_hint(e, target, UNKNOWN_EPOCH.code, Sealed::File)
}
#[derive(Clone, Copy)]
pub(super) enum Sealed {
Record,
File,
}
impl Sealed {
fn noun(self) -> &'static str {
match self {
Self::Record => "record",
Self::File => "file",
}
}
}
pub(super) fn epoch_refusal_hint(
e: &VtaError,
target: &RoomTarget<'_>,
code: &str,
sealed: Sealed,
) -> String {
let noun = sealed.noun();
let text = e.to_string();
let reason = (e.rejection_code() == Some(code))
.then(|| e.rejection_details())
.flatten()
.and_then(|d| d.get("reason"))
.and_then(Value::as_str);
let backfill = || {
format!(
"The {noun} is older than any key this VTA holds — it was written before you \
joined. Fetch the room's history: `pnm rooms keys backfill --room {} --host-did \
{}`.",
target.room_id,
target.host_did.unwrap_or("<host-did>")
)
};
let hint = match reason {
Some("removed") => {
let at = e
.rejection_details()
.and_then(|d| d.get("removedAtEpoch"))
.and_then(Value::as_u64)
.map(|n| format!(" at epoch {n}"))
.unwrap_or_default();
format!(
"You were removed from this room's group{at}. {}s sealed since then will \
never open for you, and no commit will change that; what was sealed before \
your removal still opens. Rejoining takes a fresh invitation from the room's \
owner.",
capitalised(noun)
)
}
Some("notDelivered") => format!(
"The {noun} is newer than this VTA's group: a commit has not been applied here. \
Catch up, then try again:\n\n {}",
keys_commit_command(target),
),
Some("beyondChain") => backfill(),
_ if text.contains("cannot reach") => backfill(),
_ if text.contains("has not been delivered") => format!(
"The {noun} is newer than this VTA's group. Catch up with `{}`; if that says you \
were removed, nothing sealed after your removal will open.",
keys_commit_command(target)
),
_ => format!("The {noun} did not open with any key this VTA holds."),
};
format!("{e}\n\n{hint}")
}
pub(super) async fn open_and_verify(
client: &VtaClient,
target: &RoomTarget<'_>,
key: &str,
version: u64,
sealed: &SealedContent,
host_author: Option<&str>,
) -> Result<VerifiedRecordBody, Box<dyn std::error::Error>> {
let sealed_at = sealed
.sealed_at(version)
.map_err(|why| format!("refusing record {key}: the host says {why}"))?;
let opened = client
.room_open(
target.room_id,
key,
sealed_at,
&sealed.ciphertext,
&sealed.nonce,
sealed.epoch,
)
.await
.map_err(|e| open_refusal_hint(&e, target))?;
let plaintext = opened
.get("plaintext")
.and_then(Value::as_str)
.ok_or("the VTA opened the record but returned no plaintext")?;
let bytes =
base64::Engine::decode(&base64::engine::general_purpose::URL_SAFE_NO_PAD, plaintext)?;
let resolver = vta_sdk::trust_task_proof::TrustTaskVmResolver::from_optional(
vta_sdk::resolver::shared_did_resolver_from_env().await.ok(),
);
let opened_at = opened
.pointer("/ext/org.openvtc.rooms/sealedAt")
.and_then(Value::as_u64)
.unwrap_or(sealed_at);
if opened_at > sealed_at {
return Err(format!(
"refusing record {key}: the VTA opened it at version {opened_at}, past the {sealed_at} it was asked for"
)
.into());
}
let location = RecordLocation {
room_id: target.room_id,
key,
version: opened_at,
};
Ok(verify_record_body(&bytes, location, host_author, &resolver)
.await
.map_err(|e| format!("refusing record {key}: {e}"))?)
}
pub(super) const DIRECT_PART: usize = 8 * 1024 * 1024;
pub(super) const SINGLE_PART_MAX: usize = 16 * 1024 * 1024;
pub(super) const DIRECT_BEGIN_ATTEMPTS: usize = 64;
pub(super) fn direct_plan(ciphertext: &[u8]) -> (usize, Value) {
use sha2::Digest as _;
let part = if ciphertext.len() <= SINGLE_PART_MAX {
ciphertext.len().max(1)
} else {
DIRECT_PART
};
let digests: Vec<String> = ciphertext
.chunks(part)
.map(|c| vti_common::blob_store::sha256_digest_multibase(&sha2::Sha256::digest(c).into()))
.collect();
(
part,
serde_json::json!({ "partSize": part, "partCount": digests.len(), "partDigests": digests }),
)
}
#[allow(deprecated)] pub async fn cmd_rooms_file_put(
client: &VtaClient,
target: RoomTarget<'_>,
path: &std::path::Path,
title: Option<String>,
description: Option<String>,
tags: Vec<String>,
padme: bool,
) -> Result<(), Box<dyn std::error::Error>> {
use trust_tasks_rs::Payload as _;
use vti_rooms::files::{self, FileBinding, Padding};
use vti_specs::rooms::blobs::upload;
let (sealed_tier, member, room_epoch) = room_tier_and_epoch(client, &target).await?;
let plaintext = std::fs::read(path)?;
let name = path
.file_name()
.and_then(|n| n.to_str())
.ok_or("the file has no usable name")?;
let file_id = files::new_file_id();
let (file_key, epoch) = if sealed_tier {
let resp = client
.room_file_key(target.room_id, &files::encode_file_id(&file_id), None)
.await?;
(file_key_bytes(&resp)?, resp.epoch.get())
} else {
(files::new_file_key(), room_epoch)
};
let mut sealed = files::seal_file(
&file_key,
FileBinding {
room_id: target.room_id.to_string(),
file_id,
epoch,
segment_size: files::DEFAULT_SEGMENT_SIZE,
},
name,
None,
&plaintext,
if padme { Padding::Padme } else { Padding::None },
)?;
if !sealed_tier {
sealed.file.file_key = Some(files::encode_file_key(&file_key));
}
let ciphertext: Vec<u8> = sealed.chunks.concat();
let (key_did, key_secret) = crate::local_keygen::generate_unbound_admin_did_key();
let key_secret = zeroize::Zeroizing::new(key_secret);
let (part_size, plan) = direct_plan(&ciphertext);
let begin_payload = serde_json::json!({
"roomId": target.room_id,
"manifest": sealed.blob,
"uploadKey": key_did,
"directParts": plan,
});
let begin = || async {
let (_, reply) = act_and_send(
client,
&target,
upload::begin::v0_2::Payload::TYPE_URI,
begin_payload.clone(),
)
.await?;
parse_reply::<upload::begin::v0_2::Response>("rooms/blobs/upload/begin", reply)
};
let mut begun = begin().await?;
let direct = wire_str(&begun.mode) == "direct";
let upload_id = begun.upload_id.to_string();
let host = target.client();
let sent: Result<(), Box<dyn std::error::Error>> = async {
if begun.already_committed == Some(true) {
return Ok(());
}
if direct {
for _ in 0..DIRECT_BEGIN_ATTEMPTS {
if begun.missing.is_empty() {
return Ok(());
}
if begun.targets.is_empty() {
return Err("the host answered direct mode with no targets".into());
}
for t in &begun.targets {
let i = usize::try_from(t.index)
.ok()
.filter(|i| i * part_size < ciphertext.len().max(1))
.ok_or("the host issued a target for a part that does not exist")?;
let part =
&ciphertext[i * part_size..((i + 1) * part_size).min(ciphertext.len())];
vtc_client::rooms::files::put_part(t, part).await?;
}
begun = begin().await?;
}
return Err("the upload did not finish: the host kept asking for parts".into());
}
for index in &begun.missing {
let i = usize::try_from(index.0)
.ok()
.filter(|i| *i < sealed.chunks.len())
.ok_or_else(|| {
format!("the host asked for chunk {}, which does not exist", index.0)
})?;
host.upload_chunk_v2(
&upload_id,
i as u64,
&sealed.chunks[i],
&key_did,
&key_secret,
)
.await?;
}
Ok(())
}
.await;
let finish = |type_uri: &'static str| {
let (upload_id, key_did, key_secret, member) = (
upload_id.clone(),
key_did.clone(),
key_secret.clone(),
member.clone(),
);
let target = ⌖
async move {
let payload = serde_json::json!({ "uploadId": upload_id });
if direct {
act_and_send_as(client, target, type_uri, payload, Some(&member))
.await
.map(|(_, reply)| reply)
} else {
let doc = vta_sdk::trust_task_sign::build_signed(
type_uri,
payload,
&key_did,
&key_secret,
target.host_did.unwrap_or("did:key:zHost"),
)
.await
.map_err(|e| format!("could not sign with the upload key: {e}"))?;
let doc: Value = serde_json::from_str(&doc)?;
Ok(target.client().send_signed_room_document(&doc).await?)
}
}
};
if let Err(e) = sent {
let _ = finish(upload::abort::v0_2::Payload::TYPE_URI).await;
return Err(e);
}
let committed: upload::commit::v0_2::Response = parse_reply(
"rooms/blobs/upload/commit",
finish(upload::commit::v0_2::Payload::TYPE_URI).await?,
)?;
let blob_ref = wire_str(&committed.blob_ref);
if !files::same_digest(&blob_ref, &sealed.file.blob_ref) {
return Err(format!(
"the host committed {blob_ref}, not the blob sealed here ({})",
sealed.file.blob_ref
)
.into());
}
let key = opaque_key();
let version = if sealed_tier {
let mut content = SealedRecordBody::new(
RecordLocation {
room_id: target.room_id,
key: &key,
version: 1,
},
&member,
);
content.title = title;
content.description = description;
content.tags = tags;
content.file = Some(sealed.file.clone());
write_sealed_naming(
client,
&target,
content,
Some(0),
Some(vec![blob_ref.clone()]),
)
.await?
} else {
let mut cleartext = serde_json::to_value(CleartextContent {
title,
description,
tags,
..Default::default()
})?;
cleartext["file"] = serde_json::to_value(&sealed.file)?;
let (_, reply) = act_and_send(
client,
&target,
vti_rooms::wire::ROOMS_RECORDS_PUT_V4_TYPE,
serde_json::json!({
"roomId": target.room_id,
"key": key,
"cleartext": cleartext,
"expectedVersion": 0,
"blobs": [blob_ref],
}),
)
.await
.map_err(|e| put_refused(e, &target))?;
parse_reply::<vti_rooms::wire::PutRecordResponse>("rooms/records/put", reply)?.version
};
keep_name_near_head(client, &target, version).await;
if crate::render::is_json_output() {
crate::render::print_json(&serde_json::json!({
"key": key,
"version": version,
"blobRef": blob_ref,
"size": sealed.file.size,
"mode": if direct { "direct" } else { "proxied" },
}))?;
return Ok(());
}
println!(
"Put {name} ({} bytes, {}) as record {key} at version {version}, as {member}\n blobRef {blob_ref}\n Members fetch it with `pnm rooms file get {key}`.",
sealed.file.size,
if direct {
"straight to the room's storage"
} else {
"through the host"
}
);
Ok(())
}
pub(super) fn local_name(untrusted: &str) -> String {
let base = untrusted
.rsplit(['/', '\\'])
.next()
.unwrap_or_default()
.trim_start_matches('.');
let clean = inert(base).replace(['\n', '\t'], " ");
if clean.trim().is_empty() {
"room-file".to_string()
} else {
clean
}
}
#[allow(deprecated)] pub async fn cmd_rooms_file_get(
client: &VtaClient,
target: RoomTarget<'_>,
key: &str,
out: Option<&std::path::Path>,
) -> Result<(), Box<dyn std::error::Error>> {
use trust_tasks_rs::Payload as _;
use vti_rooms::files::{self, BlobManifest};
use vti_specs::rooms::blobs::{chunk as blob_chunk, get as blob_get};
let (member, record) = act_and_send(
client,
&target,
vti_rooms::wire::ROOMS_RECORDS_GET_V2_TYPE,
serde_json::json!({ "roomId": target.room_id, "key": key }),
)
.await?;
let record: vtc_client::rooms::files::GetRecordV2Response =
parse_reply("rooms/records/get", record)?;
let (file, author, carried) = match record.cleartext.get("file") {
Some(file) if record.sealed.is_none() => {
let file: files::FileManifest = serde_json::from_value(file.clone())
.map_err(|e| format!("the record's file manifest does not read: {e}"))?;
let key = file
.carried_key(true)?
.ok_or("an open room's file manifest carries no key")?;
let author = record
.author
.clone()
.unwrap_or_else(|| "an unnamed member".to_string());
(file, author, Some(key))
}
_ => {
let sealed = record.sealed.as_ref().ok_or(
"the record carries no file: it has neither a sealed body nor a `cleartext.file`, \
or it has been retracted",
)?;
let sealed = SealedContent {
ciphertext: sealed.ciphertext.clone(),
nonce: sealed.nonce.clone(),
epoch: u32::try_from(sealed.epoch.get())
.map_err(|_| "the record's epoch is beyond what this client can represent")?,
version: sealed.version.map(std::num::NonZeroU64::get),
};
let verified = open_and_verify(
client,
&target,
key,
record.version.get(),
&sealed,
record.author.as_deref(),
)
.await?;
let file = verified
.body()
.file
.clone()
.ok_or("the record carries no file")?;
file.carried_key(false)?;
(file, verified.body().author.clone(), None)
}
};
let named = record
.blobs
.iter()
.flatten()
.any(|b| files::same_digest(&wire_str(b), &file.blob_ref));
if !named {
return Err(
"the host does not name the file's blob on this record; it may have been \
released"
.into(),
);
}
let (_, started) = act_and_send(
client,
&target,
blob_get::v0_2::Payload::TYPE_URI,
serde_json::json!({ "roomId": target.room_id, "blobRef": file.blob_ref, "direct": true }),
)
.await?;
let started: blob_get::v0_2::Response = parse_reply("rooms/blobs/get", started)?;
let blob: BlobManifest = serde_json::from_value(serde_json::to_value(&started.manifest)?)?;
file.check_blob(&blob)?;
let size = blob.size;
let chunk_size = blob.chunks.chunk_size;
let mut chunks = Vec::with_capacity(blob.chunks.chunk_digests.len());
for (i, expected) in blob.chunks.chunk_digests.iter().enumerate() {
let chunk = match (&started.target, &started.download_id) {
(Some(t), _) => {
let start = i as u64 * chunk_size;
vtc_client::rooms::files::get_range(t, start..(start + chunk_size).min(size))
.await?
}
(None, Some(download_id)) => {
let (_, reply) = act_and_send_as(
client,
&target,
blob_chunk::v0_1::Payload::TYPE_URI,
serde_json::json!({ "downloadId": download_id, "index": i }),
Some(&member),
)
.await?;
let c: blob_chunk::v0_1::Response = parse_reply("rooms/blobs/chunk", reply)?;
base64::Engine::decode(
&base64::engine::general_purpose::URL_SAFE_NO_PAD,
c.data.as_str(),
)?
}
(None, None) => {
return Err("the host answered with neither a target nor a download".into());
}
};
if !files::same_digest(&files::digest_multibase(&chunk), expected) {
return Err(format!("chunk {i} does not match the manifest").into());
}
chunks.push(chunk);
}
let file_key = match carried {
Some(key) => key,
None => {
let resp = client
.room_file_key(target.room_id, &file.file_id, Some(file.epoch))
.await
.map_err(|e| file_key_refusal_hint(&e, &target))?;
file_key_bytes(&resp)?
}
};
let plaintext = files::open_file(&file_key, target.room_id, &file, &blob, &chunks)?;
let out = match out {
Some(p) => p.to_path_buf(),
None => std::path::PathBuf::from(local_name(&file.name)),
};
if out.exists() {
return Err(format!("{} already exists; refusing to overwrite it", out.display()).into());
}
let tmp = out.with_extension("partial");
std::fs::write(&tmp, &plaintext)?;
std::fs::rename(&tmp, &out)?;
println!(
"Fetched {} ({} bytes) into {}, written by {}; it matches the digest {}.",
inert(&file.name).escape_debug(),
plaintext.len(),
out.display(),
inert(&author),
if carried.is_some() {
"its record states (an open room's record is not signed by its author)"
} else {
"they signed"
}
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::commands::rooms::test_support::*;
use serde_json::json;
#[test]
fn a_superseded_epoch_put_prints_keys_commit_with_the_real_values() {
let body = json!({
"type": "https://trusttasks.org/spec/rooms/records/put/0.1#error",
"payload": {
"code": "rooms/records/put:epochMismatch",
"message": "record is sealed under epoch 2, room `r` is at 3",
},
});
let err: Box<dyn std::error::Error> = Box::new(vtc_client::VtcError::Http {
status: 400,
body: body.to_string(),
});
let text = put_refused(err, &vtc_target()).to_string();
assert!(text.contains(CATCH_UP), "{text}");
assert!(text.contains("sealed under epoch 2"), "{text}");
assert!(!text.contains("HTTP 400"), "{text}");
}
#[test]
fn other_put_refusals_pass_through_and_no_host_did_is_invented() {
let t = vtc_target();
assert!(put_refusal_hint("rooms/records/put:recordTooLarge", "", &t).is_none());
let err: Box<dyn std::error::Error> = Box::new(vtc_client::VtcError::Http {
status: 500,
body: "not a document".into(),
});
assert_eq!(
put_refused(err, &t).to_string(),
"VTC returned HTTP 500: not a document"
);
let bare = RoomTarget {
host_did: None,
..vtc_target()
};
assert_eq!(
keys_commit_command(&bare),
"pnm rooms keys commit --room did:webvh:example.com:rooms:northwind \
--host https://community.example/v1"
);
}
#[test]
fn a_removed_member_is_told_it_was_removed_not_to_catch_up() {
let text = open_refusal_hint(
&open_rejected("removed", json!({ "removedAtEpoch": 3 })),
&vtc_target(),
);
assert!(
text.contains("You were removed from this room's group at epoch 3"),
"{text}"
);
assert!(!text.contains("keys commit"), "{text}");
}
#[test]
fn a_file_key_refusal_names_the_remedy() {
let removed = VtaError::Rejected {
code: "rooms/keys/file-key:unknownEpoch".into(),
message: "the file is sealed under epoch 4, and this member was removed".into(),
details: Some(json!({ "reason": "removed", "removedAtEpoch": 3 })),
};
let text = file_key_refusal_hint(&removed, &vtc_target());
assert!(
text.contains("You were removed from this room's group at epoch 3"),
"{text}"
);
assert!(text.contains("Files sealed since then"), "{text}");
assert!(!text.contains("keys commit"), "{text}");
let behind_commit = file_key_refusal_hint(
&file_key_rejected("notDelivered", "the file is sealed under epoch 4"),
&vtc_target(),
);
assert!(behind_commit.contains(CATCH_UP), "{behind_commit}");
let behind = file_key_refusal_hint(
&file_key_rejected("beyondChain", "sealed under epoch 1"),
&vtc_target(),
);
assert!(behind.contains("pnm rooms keys backfill"), "{behind}");
assert!(behind.contains("The file is older"), "{behind}");
let wrong_task = VtaError::Rejected {
code: "rooms/keys/open:unknownEpoch".into(),
message: "x".into(),
details: Some(json!({ "reason": "beyondChain" })),
};
assert!(
file_key_refusal_hint(&wrong_task, &vtc_target()).contains("did not open with any key")
);
}
#[test]
fn a_member_behind_is_told_to_catch_up() {
let text = open_refusal_hint(
&open_rejected("notDelivered", json!({ "heldEpoch": 2 })),
&vtc_target(),
);
assert!(text.contains(CATCH_UP), "{text}");
}
#[test]
fn a_file_name_cannot_leave_the_directory_or_hide() {
assert_eq!(local_name("../../etc/passwd"), "passwd");
assert_eq!(local_name("C:\\Users\\x\\evil.exe"), "evil.exe");
assert_eq!(local_name(".bashrc"), "bashrc");
assert_eq!(local_name("term sheet.pdf"), "term sheet.pdf");
assert_eq!(local_name("../"), "room-file");
assert_eq!(local_name("a\nb.txt"), "a b.txt");
}
}