Skip to content

Commit 324bd6b

Browse files
wesbillmanCarl
andauthored
Fix shared agent avatar import profiles (#3578)
> Carl is updating this pull request on Wes's behalf. ## Summary - upload an embedded raster avatar through the existing authenticated media pipeline before minting or persisting an imported shared agent - store and publish only the resulting hosted URL so agent kind:0 profiles remain within content limits ## Root cause Snapshot import recovered raster avatar pixels as a large inline base64 data URL. That value was persisted and placed into the agent's kind:0 profile. The relay rejected the oversized profile, so other clients could not resolve the imported agent's avatar. ## Scope This is intentionally the forward fix only. It changes two Desktop files and does **not** add migration or reconciliation behavior for previously imported agents. Existing affected imports must be re-imported or fixed manually. ## Validation - successful pre-push Desktop suite: 1,863 passed, 14 ignored, 0 failed - all pre-push Rust/Desktop gates green, including all-target clippy - valid >256 KiB PNG import → production MIME detection/sanitization → bounded signed kind:0 containing only the hosted URL - upload failure, malformed data, and URL-only avatar cases covered - independent fresh review by Princess Donut: clean, no blocking findings Signed-off-by: Wes <[email protected]> Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
1 parent 7e9b77f commit 324bd6b

2 files changed

Lines changed: 159 additions & 9 deletions

File tree

‎desktop/src-tauri/src/commands/media.rs‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ fn should_retry_legacy_upload(status: reqwest::StatusCode) -> bool {
411411
}
412412

413413
async fn send_upload_attempt(
414-
state: &State<'_, AppState>,
414+
state: &AppState,
415415
url: String,
416416
auth_header: &str,
417417
mime: &str,
@@ -455,10 +455,22 @@ async fn send_upload_attempt(
455455
response.map_err(|error| classify_request_error(&error))
456456
}
457457

458+
pub(crate) async fn upload_image_bytes(
459+
body: Vec<u8>,
460+
state: &AppState,
461+
) -> Result<BlobDescriptor, String> {
462+
let mime = detect_and_validate_mime(&body)?;
463+
if !mime.starts_with("image/") {
464+
return Err("profile avatar must be an image".to_string());
465+
}
466+
let body = sanitize_image_for_upload(body, &mime)?;
467+
do_upload(body, &mime, state, None).await
468+
}
469+
458470
async fn do_upload(
459471
body: Vec<u8>,
460472
mime: &str,
461-
state: &State<'_, AppState>,
473+
state: &AppState,
462474
progress: Option<(tauri::AppHandle, String)>,
463475
) -> Result<BlobDescriptor, String> {
464476
let sha256 = hex::encode(Sha256::digest(&body));
@@ -559,7 +571,7 @@ pub async fn upload_media(
559571
/// files from ever leaving the client on image-only surfaces.
560572
async fn process_picked_path(
561573
path: std::path::PathBuf,
562-
state: &State<'_, AppState>,
574+
state: &AppState,
563575
images_only: bool,
564576
) -> Result<BlobDescriptor, String> {
565577
// Pin the inode by opening the fd BEFORE spawn_blocking. This prevents a

‎desktop/src-tauri/src/commands/personas/snapshot/import.rs‎

Lines changed: 144 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ pub(crate) fn decode_snapshot_from_bytes(
256256
Ok(snapshot)
257257
}
258258

259+
async fn materialize_import_avatar<F, Fut>(
260+
avatar_data_url: Option<&str>,
261+
avatar_url: Option<&str>,
262+
upload: F,
263+
) -> Result<Option<String>, String>
264+
where
265+
F: FnOnce(Vec<u8>) -> Fut,
266+
Fut: std::future::Future<Output = Result<String, String>>,
267+
{
268+
let Some(avatar_data_url) = avatar_data_url else {
269+
return Ok(avatar_url.map(str::to_string));
270+
};
271+
let avatar_bytes =
272+
crate::managed_agents::agent_snapshot::decode_avatar_data_url(avatar_data_url)
273+
.ok_or_else(|| "Snapshot avatar data is malformed.".to_string())?;
274+
upload(avatar_bytes).await.map(Some)
275+
}
276+
259277
// ── `preview_agent_snapshot_import` ──────────────────────────────────────────
260278

261279
/// Decode and validate a snapshot file, returning a preview for the
@@ -354,12 +372,21 @@ pub async fn confirm_agent_snapshot_import(
354372
)?;
355373
let minted_parallelism = minted.parallelism;
356374

357-
// Effective avatar: data URL wins; URL fallback when data URL is absent.
358-
let effective_avatar: Option<String> = snapshot
359-
.profile
360-
.avatar_data_url
361-
.clone()
362-
.or_else(|| snapshot.profile.avatar_url.clone());
375+
// Profile metadata must contain a hosted URL. Inline avatar data can be far
376+
// larger than the relay's kind:0 content limit, so upload imported pixels
377+
// before minting or persisting the new agent. Failing here keeps import
378+
// atomic instead of creating an agent whose profile can never publish.
379+
let effective_avatar = materialize_import_avatar(
380+
snapshot.profile.avatar_data_url.as_deref(),
381+
snapshot.profile.avatar_url.as_deref(),
382+
|avatar_bytes| async {
383+
crate::commands::media::upload_image_bytes(avatar_bytes, &state)
384+
.await
385+
.map(|descriptor| descriptor.url)
386+
.map_err(|error| format!("Could not upload the imported avatar: {error}"))
387+
},
388+
)
389+
.await?;
363390

364391
// Wire-format string for the persona definition's respond_to field.
365392
// Omit when it is the default (owner-only) to keep definitions clean.
@@ -711,3 +738,114 @@ async fn submit_engram_event(
711738
}
712739
Ok(())
713740
}
741+
742+
#[cfg(test)]
743+
mod import_avatar_tests {
744+
use super::materialize_import_avatar;
745+
use std::cell::Cell;
746+
747+
#[tokio::test]
748+
async fn inline_avatar_is_uploaded_and_replaced_with_hosted_url() {
749+
let uploaded = Cell::new(false);
750+
let result = materialize_import_avatar(
751+
Some("data:image/png;base64,iVBORw0KGgo="),
752+
Some("https://sender.invalid/avatar.png"),
753+
|bytes| {
754+
uploaded.set(true);
755+
async move {
756+
assert_eq!(bytes, b"\x89PNG\r\n\x1a\n");
757+
Ok("https://relay.example/media/avatar.png".to_string())
758+
}
759+
},
760+
)
761+
.await
762+
.unwrap();
763+
764+
assert!(uploaded.get());
765+
assert_eq!(
766+
result.as_deref(),
767+
Some("https://relay.example/media/avatar.png")
768+
);
769+
}
770+
771+
#[tokio::test]
772+
async fn hosted_avatar_skips_upload() {
773+
let result =
774+
materialize_import_avatar(None, Some("https://sender.example/avatar.png"), |_| async {
775+
panic!("hosted avatars must not be uploaded")
776+
})
777+
.await
778+
.unwrap();
779+
780+
assert_eq!(result.as_deref(), Some("https://sender.example/avatar.png"));
781+
}
782+
783+
#[tokio::test]
784+
async fn relay_sized_inline_avatar_becomes_bounded_signed_profile() {
785+
use base64::{engine::general_purpose::STANDARD, Engine};
786+
use image::ImageEncoder;
787+
use nostr::JsonUtil;
788+
789+
let mut pixels = vec![0_u8; 512 * 512 * 4];
790+
let mut seed = 0x1234_5678_u32;
791+
for byte in &mut pixels {
792+
seed ^= seed << 13;
793+
seed ^= seed >> 17;
794+
seed ^= seed << 5;
795+
*byte = seed as u8;
796+
}
797+
let mut source = Vec::new();
798+
image::codecs::png::PngEncoder::new(&mut source)
799+
.write_image(&pixels, 512, 512, image::ExtendedColorType::Rgba8)
800+
.unwrap();
801+
assert!(source.len() > 256 * 1024);
802+
let data_url = format!("data:image/png;base64,{}", STANDARD.encode(&source));
803+
assert!(data_url.len() > 256 * 1024);
804+
805+
let avatar = materialize_import_avatar(Some(&data_url), None, |bytes| async move {
806+
let mime = crate::commands::media::detect_and_validate_mime(&bytes)?;
807+
assert_eq!(mime, "image/png");
808+
let sanitized = crate::commands::media::sanitize_image_for_upload(bytes, &mime)?;
809+
image::load_from_memory(&sanitized).map_err(|error| error.to_string())?;
810+
Ok("https://relay.example/media/avatar.png".to_string())
811+
})
812+
.await
813+
.unwrap()
814+
.unwrap();
815+
816+
let event =
817+
crate::events::build_profile(Some("Imported agent"), None, Some(&avatar), None, None)
818+
.unwrap()
819+
.sign_with_keys(&nostr::Keys::generate())
820+
.unwrap();
821+
assert!(event.content.len() < 64 * 1024);
822+
assert!(!event.content.contains("data:image/"));
823+
assert!(event
824+
.content
825+
.contains("https://relay.example/media/avatar.png"));
826+
assert!(event.as_json().len() < 256 * 1024);
827+
}
828+
829+
#[tokio::test]
830+
async fn upload_failure_aborts_avatar_materialization() {
831+
let result = materialize_import_avatar(
832+
Some("data:image/png;base64,iVBORw0KGgo="),
833+
None,
834+
|_| async { Err("relay upload failed".to_string()) },
835+
)
836+
.await;
837+
838+
assert_eq!(result.unwrap_err(), "relay upload failed");
839+
}
840+
841+
#[tokio::test]
842+
async fn malformed_inline_avatar_fails_before_upload() {
843+
let result =
844+
materialize_import_avatar(Some("data:image/png;base64,not-base64!"), None, |_| async {
845+
panic!("malformed avatars must not be uploaded")
846+
})
847+
.await;
848+
849+
assert_eq!(result.unwrap_err(), "Snapshot avatar data is malformed.");
850+
}
851+
}

0 commit comments

Comments
 (0)