mirror of
https://github.com/block/buzz.git
synced 2026-11-25 18:52:52 +00:00
## Summary Editing attachment messages through `buzz messages edit` can remove their files from the effective message. Preserve the latest live imeta set across successive edits and add `--clear-attachments` for explicit removal. Add an SDK builder for complete replacement attachment sets while preserving the existing API. The relay provides a versioned effective-edit lookup on `/query` for kinds 9/40002/40008/45001/45003; media reads fold the same set. It uses the existing normalized-target index, fresh channel authorization, and a primary snapshot. Generic `#e` matching is unchanged. Mixed-case historical edits cannot restore removed files, including when another reference would allow republication. ### Related issue Fixes #3022. Follows #9077. Related open attempts: #3062 and #3065. ### Testing Published head: `c630c4ebf0b20a9618f4844629eec1e666699cbe`, through all mandatory push hooks. The governed-five snapshot passed all four PostgreSQL/HTTP acceptance tests with `RUST_MIN_STACK` unset and 3,333 affected-package tests. The committed test-only array correction passed clippy and the preflight rerun. Independent mutations detected dropped read/lookup kinds, the CLI two-kind split, and preflight ordering, channel and live-edit errors. The published follow-up adds forum preview retention/suppression and a deleted-original preflight fixture; both surviving mutations now fail as intended. Independent review verified the fixes. All four acceptance tests pass; the full DB/relay run had two NIP-FI failures that passed in isolation. The actual standalone MCP workflow passed against this relay and clean local app `5b583b8c69596353bc37e9f3938eca1aabcd081e`: 15 MCP edits, 10 direct publications and 60 member GET/HEAD checks across five isolated blob pairs. Both tested heads are now published. Relay hosted checks report 80 successful and 31 skipped, with none pending or failed; app hosted checks and human upload → edit → clear → edit → GET/HEAD confirmation remain pending. No human acceptance is claimed. ### Rollout The lookup, CLI preservation/clear and media reads use kinds 9/40002/40008/45001/45003. Deploy the relay before releasing the CLI and paired [MCP producer](https://github.com/block/buzz-app/pull/2937). New clients abort without publishing against an unsupported relay. Old CLI edits emit empty imeta and now revoke diff/forum attachments that previously remained readable. Other channel-tagged kinds retain the historical plain-edit path. No additional schema change is needed beyond migration 0086. Readiness remains blocked by mobile edits that omit imeta, the app UI fold that can restore older files, unaudited SDK/direct callers and unmeasured historical exposure. Run `docs/sql/private-media-edit-preflight.sql` for each community; producer updates do not repair signed historical edits. No enforce activation is authorized. `off` skips policy checks, `shadow` changes policy metrics while allowing policy failures, and `enforce` denies inaccessible references. Observe `buzz_media_publication_access_total{mode="shadow",reason="denied"}`. Prefer rolling back the client release while retaining the additive lookup. A relay rollback makes new CLI/MCP edits fail closed; already-published imeta remains valid under the older reference rules. Concurrent edits still use `(created_at,id)` without compare-and-swap. Generated with Codex --------- Signed-off-by: Brad Seiler <seiler@squareup.com> Co-authored-by: Honor <e0bf479eeebe47dde1e35705212a4df70616a7bc834d907ddfb18cf0405f8b1b@seiler-test.communities.public.builderlabstaging.xyz>
348 lines
12 KiB
Rust
348 lines
12 KiB
Rust
//! A shared vocabulary for local media references on reads and publications.
|
|
use nostr::Event;
|
|
use std::collections::BTreeSet;
|
|
|
|
/// Original event kinds whose latest live edit replaces attachment references.
|
|
/// Shared by clients, the effective-edit lookup and media-read authorization.
|
|
pub const EDIT_GOVERNED_KINDS: [u16; 5] = [9, 40002, 40008, 45001, 45003];
|
|
|
|
/// Parse a local media URL, returning its parent hash (also for thumbnails).
|
|
/// Only the tenant's canonical base and root-relative paths count.
|
|
pub fn local_hash(value: &str, base: &str) -> Option<String> {
|
|
let alternate = if let Some(rest) = base.strip_prefix("https://") {
|
|
format!("http://{rest}")
|
|
} else if let Some(rest) = base.strip_prefix("http://") {
|
|
format!("https://{rest}")
|
|
} else {
|
|
base.to_owned()
|
|
};
|
|
let prefix = format!("{}/", base.trim_end_matches('/'));
|
|
let alternate_prefix = format!("{}/", alternate.trim_end_matches('/'));
|
|
let rest = value
|
|
.strip_prefix("/media/")
|
|
.or_else(|| value.strip_prefix(&prefix))
|
|
.or_else(|| value.strip_prefix(&alternate_prefix))?;
|
|
let mut parts = rest.split('.');
|
|
let hash = parts.next()?;
|
|
if hash.len() != 64
|
|
|| !hash
|
|
.bytes()
|
|
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
|
{
|
|
return None;
|
|
}
|
|
let safe_ext = |s: &str| {
|
|
!s.is_empty()
|
|
&& s.len() <= 8
|
|
&& s.bytes()
|
|
.all(|b| b.is_ascii_digit() || b.is_ascii_lowercase())
|
|
};
|
|
match (parts.next(), parts.next(), parts.next()) {
|
|
(None, None, None) => {}
|
|
(Some(ext), None, None) if safe_ext(ext) => {}
|
|
(Some("thumb"), Some("jpg" | "webp"), None) => {}
|
|
_ => return None,
|
|
}
|
|
Some(hash.to_owned())
|
|
}
|
|
|
|
/// Decode the desktop's animated-avatar descriptor into its poster and animation.
|
|
/// Each URL still needs its own tenant check. Malformed escapes never become URLs.
|
|
pub fn animated_avatar_urls(value: &str) -> Option<(String, String)> {
|
|
let (poster, encoded) = value.split_once("#buzz-anim=")?;
|
|
if encoded.is_empty() {
|
|
return None;
|
|
}
|
|
let bytes = encoded.as_bytes();
|
|
for (i, byte) in bytes.iter().enumerate() {
|
|
if *byte == b'%'
|
|
&& (i + 2 >= bytes.len()
|
|
|| !bytes[i + 1].is_ascii_hexdigit()
|
|
|| !bytes[i + 2].is_ascii_hexdigit())
|
|
{
|
|
return None;
|
|
}
|
|
}
|
|
let animation = percent_encoding::percent_decode_str(encoded)
|
|
.decode_utf8()
|
|
.ok()?
|
|
.into_owned();
|
|
for candidate in [poster, animation.as_str()] {
|
|
let parsed = url::Url::parse(candidate).ok()?;
|
|
if !matches!(parsed.scheme(), "http" | "https") || parsed.host_str().is_none() {
|
|
return None;
|
|
}
|
|
}
|
|
Some((poster.to_owned(), animation))
|
|
}
|
|
|
|
fn avatar_hashes(value: &str, base: &str, out: &mut BTreeSet<String>) {
|
|
if let Some((poster, animation)) = animated_avatar_urls(value) {
|
|
out.extend(local_hash(&poster, base));
|
|
out.extend(local_hash(&animation, base));
|
|
} else {
|
|
out.extend(local_hash(value, base));
|
|
}
|
|
}
|
|
|
|
/// Extract counted references. imeta URL/image fields cover attachments,
|
|
/// previews and posters. Profile pictures/banners, group pictures and emoji
|
|
/// are the explicitly supported non-message references. Free text grants nothing.
|
|
pub fn reference_hashes(event: &Event, base: &str) -> BTreeSet<String> {
|
|
let mut out = BTreeSet::new();
|
|
let preview_suppressed = event
|
|
.tags
|
|
.iter()
|
|
.any(|tag| matches!(tag.as_slice(),[name,value] if name=="link-preview" && value=="none"));
|
|
for tag in event.tags.iter() {
|
|
let parts = tag.as_slice();
|
|
match parts.first().map(String::as_str) {
|
|
Some("imeta") => {
|
|
for part in &parts[1..] {
|
|
if let Some(("url" | "image", value)) = part.split_once(' ') {
|
|
out.extend(local_hash(value, base));
|
|
}
|
|
}
|
|
}
|
|
Some("link-preview")
|
|
if !preview_suppressed
|
|
&& parts.len() == 11
|
|
&& parts[1] == "snapshot"
|
|
&& parts[2] == "1" =>
|
|
{
|
|
for value in [&parts[7], &parts[9]] {
|
|
out.extend(local_hash(value, base));
|
|
}
|
|
}
|
|
Some("picture") if event.kind.as_u16() == 39000 => {
|
|
if let Some(value) = parts.get(1) {
|
|
out.extend(local_hash(value, base));
|
|
}
|
|
}
|
|
Some("emoji") if matches!(event.kind.as_u16(), 10030 | 30030 | 30180) => {
|
|
if let Some(value) = parts.get(2) {
|
|
out.extend(local_hash(value, base));
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
if matches!(event.kind.as_u16(), 0 | 30175) {
|
|
if let Ok(profile) = serde_json::from_str::<serde_json::Value>(&event.content) {
|
|
let fields = if event.kind.as_u16() == 30175 {
|
|
[profile.get("avatar_url"), None]
|
|
} else {
|
|
[
|
|
profile.get("picture").or_else(|| profile.get("image")),
|
|
profile.get("banner"),
|
|
]
|
|
};
|
|
for value in fields.into_iter().flatten() {
|
|
if let Some(value) = value.as_str() {
|
|
avatar_hashes(value, base, &mut out);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// All local URLs in structured command payloads as well as counted event
|
|
/// references must be authorized before dispatch. This also covers metadata
|
|
/// commands whose accepted payload later becomes relay-signed state.
|
|
pub fn publication_hashes(event: &Event, base: &str) -> BTreeSet<String> {
|
|
fn visit(value: &serde_json::Value, base: &str, out: &mut BTreeSet<String>) {
|
|
match value {
|
|
serde_json::Value::String(value) => {
|
|
avatar_hashes(value, base, out);
|
|
}
|
|
serde_json::Value::Array(values) => {
|
|
for value in values {
|
|
visit(value, base, out);
|
|
}
|
|
}
|
|
serde_json::Value::Object(values) => {
|
|
for value in values.values() {
|
|
visit(value, base, out);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
let mut out = reference_hashes(event, base);
|
|
if let Ok(value) = serde_json::from_str(&event.content) {
|
|
visit(&value, base, &mut out);
|
|
}
|
|
for tag in event.tags.iter() {
|
|
for part in tag.as_slice().iter().skip(1) {
|
|
out.extend(local_hash(part, base));
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// First valid edit target in signed tag order, accepting all ASCII hex casing.
|
|
pub fn first_edit_target(event: &nostr::Event) -> Option<Vec<u8>> {
|
|
event.tags.iter().find_map(|tag| {
|
|
let parts = tag.as_slice();
|
|
let value = parts.get(1)?;
|
|
if parts.first().map(String::as_str) == Some("e")
|
|
&& value.len() == 64
|
|
&& value.bytes().all(|c| c.is_ascii_hexdigit())
|
|
{
|
|
hex::decode(value).ok()
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use nostr::{EventBuilder, Keys, Kind, Tag};
|
|
#[test]
|
|
fn aliases_and_foreign_hosts() {
|
|
let hash = "a".repeat(64);
|
|
for suffix in ["", ".png", ".thumb.jpg", ".thumb.webp"] {
|
|
assert_eq!(
|
|
local_hash(&format!("/media/{hash}{suffix}"), "https://local/media"),
|
|
Some(hash.clone())
|
|
);
|
|
}
|
|
for value in [
|
|
format!("https://foreign/media/{hash}.jpg"),
|
|
format!("/media/{hash}0"),
|
|
format!("/media/{hash}.png?x=1"),
|
|
] {
|
|
assert_eq!(local_hash(&value, "https://local/media"), None);
|
|
}
|
|
}
|
|
#[test]
|
|
fn animated_profiles_check_each_url_and_reject_malformed_descriptors() {
|
|
let a = "a".repeat(64);
|
|
let b = "b".repeat(64);
|
|
for (poster, animation, expected) in [
|
|
(
|
|
format!("https://local/media/{a}.png"),
|
|
format!("https://local/media/{b}.png"),
|
|
BTreeSet::from([a.clone(), b.clone()]),
|
|
),
|
|
(
|
|
format!("https://foreign/media/{a}.png"),
|
|
format!("https://local/media/{b}.png"),
|
|
BTreeSet::from([b]),
|
|
),
|
|
(
|
|
format!("https://local/media/{a}.png"),
|
|
format!("https://foreign/media/{}.png", "c".repeat(64)),
|
|
BTreeSet::from([a.clone()]),
|
|
),
|
|
] {
|
|
let descriptor = format!(
|
|
"{poster}#buzz-anim={}",
|
|
percent_encoding::utf8_percent_encode(
|
|
&animation,
|
|
percent_encoding::NON_ALPHANUMERIC
|
|
)
|
|
);
|
|
let event = EventBuilder::new(
|
|
Kind::Metadata,
|
|
serde_json::json!({"picture":descriptor}).to_string(),
|
|
)
|
|
.sign_with_keys(&Keys::generate())
|
|
.unwrap();
|
|
assert_eq!(reference_hashes(&event, "https://local/media"), expected);
|
|
assert_eq!(publication_hashes(&event, "https://local/media"), expected);
|
|
}
|
|
for value in [
|
|
"https://local/a#buzz-anim=%xx",
|
|
"https://local/a#buzz-anim=%ff",
|
|
"https://local/a#buzz-anim=",
|
|
"https://local/a#buzz-anim=javascript:foo",
|
|
] {
|
|
assert!(animated_avatar_urls(value).is_none(), "{value}");
|
|
}
|
|
}
|
|
#[test]
|
|
fn snapshot_media_counts_only_image_and_favicon_urls() {
|
|
let a = "a".repeat(64);
|
|
let b = "b".repeat(64);
|
|
let c = "c".repeat(64);
|
|
for (image, favicon, expected) in [
|
|
(
|
|
format!("/media/{a}.png"),
|
|
format!("https://local/media/{b}.png"),
|
|
BTreeSet::from([a.clone(), b.clone()]),
|
|
),
|
|
(
|
|
format!("https://foreign/media/{a}.png"),
|
|
String::new(),
|
|
BTreeSet::new(),
|
|
),
|
|
] {
|
|
let event = EventBuilder::new(Kind::Custom(9), "")
|
|
.tags([Tag::parse([
|
|
"link-preview",
|
|
"snapshot",
|
|
"1",
|
|
&format!("https://local/media/{c}.png"),
|
|
"title",
|
|
"site",
|
|
"description",
|
|
&image,
|
|
&a,
|
|
&favicon,
|
|
&b,
|
|
])
|
|
.unwrap()])
|
|
.sign_with_keys(&Keys::generate())
|
|
.unwrap();
|
|
assert_eq!(reference_hashes(&event, "https://local/media"), expected);
|
|
}
|
|
}
|
|
#[test]
|
|
fn profile_image_fallback_matches_materialization() {
|
|
let a = "a".repeat(64);
|
|
let b = "b".repeat(64);
|
|
for (content, expected) in [
|
|
(
|
|
serde_json::json!({"image":format!("/media/{a}.png")}),
|
|
BTreeSet::from([a.clone()]),
|
|
),
|
|
(
|
|
serde_json::json!({"picture":format!("/media/{b}.png"),"image":format!("/media/{a}.png")}),
|
|
BTreeSet::from([b]),
|
|
),
|
|
(
|
|
serde_json::json!({"picture":null,"image":format!("/media/{a}.png")}),
|
|
BTreeSet::new(),
|
|
),
|
|
] {
|
|
let event = EventBuilder::new(Kind::Metadata, content.to_string())
|
|
.sign_with_keys(&Keys::generate())
|
|
.unwrap();
|
|
assert_eq!(reference_hashes(&event, "https://local/media"), expected);
|
|
}
|
|
}
|
|
#[test]
|
|
fn profile_and_imeta_share_the_publication_extractor() {
|
|
let hash = "b".repeat(64);
|
|
let event = EventBuilder::new(
|
|
Kind::Metadata,
|
|
format!(r#"{{"picture":"https:\/\/local\/media\/{hash}.jpg"}}"#),
|
|
)
|
|
.tags([Tag::parse(["imeta", &format!("url /media/{hash}.jpg")]).unwrap()])
|
|
.sign_with_keys(&Keys::generate())
|
|
.unwrap();
|
|
assert_eq!(
|
|
reference_hashes(&event, "https://local/media"),
|
|
BTreeSet::from([hash])
|
|
);
|
|
assert_eq!(
|
|
publication_hashes(&event, "https://local/media"),
|
|
reference_hashes(&event, "https://local/media")
|
|
);
|
|
}
|
|
}
|