Skip to content

Commit 2ea9385

Browse files
tellahonpub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w
andauthored
fix(reactions): support max-length custom emoji (#3833)
**Category:** fix **User Impact:** Custom emoji with valid 64-character names can now be used as reactions without errors. **Problem:** Buzz accepted 64-character custom emoji names during registration, but rejected them as reactions after the required surrounding colons made the payload 66 characters. Validation also differed between desktop, SDK, relay, and storage boundaries. <img width="554" height="47" alt="image" src="https://github.com/user-attachments/assets/4013452f-210e-4dd3-9003-f45ff3b28dc8" /> **Solution:** Keep the product limit at 64 ASCII characters for custom emoji names, enforce it consistently when emoji sets are registered, and allow only valid matching custom reaction payloads up to 66 characters. Widen the reaction projection to preserve the wrapped payload while retaining the existing 64-character limit for ordinary reactions. <details> <summary>File changes</summary> **crates/buzz-sdk/src/builders.rs** Defines the shared custom emoji boundaries and covers accepted 64-character and rejected 65-character shortcodes. **crates/buzz-relay/src/handlers/ingest.rs** Validates emoji-set shortcodes and permits 66-character reactions only when they are valid colon-wrapped custom emoji with a matching tag. **crates/buzz-db/src/event.rs** Adds storage regression coverage for maximum-length custom emoji reactions. **crates/buzz-db/src/migration.rs** Verifies the reaction column migration is applied correctly. **desktop/src/shared/api/customEmoji.ts** Enforces the existing 64-character shortcode maximum during desktop normalization and registration/import. **desktop/src/shared/api/customEmoji.test.mjs** Covers the desktop shortcode boundary. **migrations/0027_long_reaction_payloads.sql** Widens stored reaction payloads to 66 characters for the two required surrounding colons. **schema/schema.sql** Keeps the desired schema aligned with the migration. </details> ## Reproduction Steps 1. Register or import a custom emoji whose ASCII shortcode is exactly 64 characters. 2. Select that emoji as a reaction to a message. 3. Confirm the reaction publishes, persists, and renders without an error. 4. Attempt to register a 65-character shortcode and confirm it is rejected. 5. Publish an ordinary or malformed reaction over 64 characters and confirm the relay rejects it. ## Verification - `cargo test -p buzz-sdk`: 243 passed - `cargo test -p buzz-db`: 94 passed, 152 Postgres-required tests ignored - `pnpm test` in `desktop`: 3,859 passed - `cargo test -p buzz-relay`: 795 passed, 9 existing Postgres-unavailable failures, 35 ignored; new reaction boundary tests pass directly - `cargo fmt --all -- --check` - `git diff --check` Originating Buzz channel: `f2ec9671-d78e-4cde-894c-9f4c458c7f1f` --------- Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@buzz.block.builderlab.xyz>
1 parent 719f973 commit 2ea9385

8 files changed

Lines changed: 221 additions & 18 deletions

File tree

‎crates/buzz-db/src/event.rs‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ mod tests {
15411541
use super::*;
15421542
use nostr::{EventBuilder, Keys, Kind, Tag};
15431543

1544-
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz";
1544+
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1
15451545

15461546
async fn setup_pool() -> PgPool {
15471547
let database_url = std::env::var("BUZZ_TEST_DATABASE_URL")
@@ -1943,6 +1943,42 @@ mod tests {
19431943
.expect("sign reaction event")
19441944
}
19451945

1946+
#[tokio::test]
1947+
#[ignore = "requires Postgres"]
1948+
async fn reaction_single_tx_stores_wrapped_max_shortcode() {
1949+
let pool = setup_pool().await;
1950+
let community = CommunityId::from_uuid(make_test_community(&pool).await);
1951+
let target = make_text_event("long custom emoji target");
1952+
insert_event(&pool, community, &target, None)
1953+
.await
1954+
.expect("insert target");
1955+
1956+
let actor = Keys::generate();
1957+
let emoji = format!(":{}:", "a".repeat(64));
1958+
let reaction = make_reaction_event(&actor, &target.id.to_hex(), &emoji);
1959+
let outcome = insert_reaction_event_with_thread_metadata(
1960+
&pool,
1961+
community,
1962+
&reaction,
1963+
None,
1964+
None,
1965+
target.id.as_bytes(),
1966+
&actor.public_key().to_bytes(),
1967+
&emoji,
1968+
)
1969+
.await
1970+
.expect("store wrapped 64-character shortcode");
1971+
1972+
assert!(matches!(
1973+
outcome,
1974+
ReactionEventInsertOutcome::Inserted {
1975+
was_inserted: true,
1976+
..
1977+
}
1978+
));
1979+
assert_eq!(emoji.chars().count(), 66);
1980+
}
1981+
19461982
#[tokio::test]
19471983
#[ignore = "requires Postgres"]
19481984
async fn reaction_single_tx_duplicate_short_circuit_stores_no_event() {

‎crates/buzz-db/src/migration.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ mod tests {
561561
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
562562
migrations.sort_by_key(|migration| migration.version);
563563

564-
assert_eq!(migrations.len(), 27);
564+
assert_eq!(migrations.len(), 28);
565565
assert_eq!(migrations[0].version, 1);
566566
assert_eq!(&*migrations[0].description, "initial schema");
567567
assert!(migrations[0]
@@ -919,7 +919,6 @@ mod tests {
919919
assert!(heartbeat.contains("epoch"));
920920
assert!(heartbeat.contains("INSERT INTO replica_heartbeat (id) VALUES (1)"));
921921
assert!(heartbeat.contains("_operator_global_tables"));
922-
923922
// Channel-id lookup index (0027): serves the tenant-independent
924923
// `channels` lookups that carry no community_id predicate, which no
925924
// community_id-leading index can satisfy. Covering + partial so the
@@ -940,6 +939,13 @@ mod tests {
940939
desired_schema.contains("idx_channels_id_live"),
941940
"desired-state schema must carry the channel-id lookup index",
942941
);
942+
943+
assert_eq!(migrations[27].version, 28);
944+
let long_reactions = migrations[27].sql.as_str();
945+
assert!(
946+
long_reactions.contains("ALTER TABLE reactions ALTER COLUMN emoji TYPE VARCHAR(66)")
947+
);
948+
assert!(desired_schema.contains("emoji VARCHAR(66) NOT NULL"));
943949
}
944950

945951
#[test]

‎crates/buzz-relay/src/handlers/ingest.rs‎

Lines changed: 132 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,55 @@ use crate::conformance::{
4949
state_for_request, EmitGuard, TraceAction, Verdict,
5050
};
5151

52+
fn validate_custom_emoji_tags(event: &Event) -> Result<(), IngestError> {
53+
for tag in event.tags.iter() {
54+
let parts = tag.as_slice();
55+
if parts.first().map(String::as_str) != Some("emoji") {
56+
continue;
57+
}
58+
let shortcode = parts.get(1).ok_or_else(|| {
59+
IngestError::Rejected("invalid: emoji tag must include a shortcode".into())
60+
})?;
61+
buzz_sdk::normalize_custom_emoji_shortcode(shortcode)
62+
.map_err(|err| IngestError::Rejected(format!("invalid: {err}")))?;
63+
}
64+
Ok(())
65+
}
66+
67+
fn validate_reaction_emoji(event: &Event, emoji: &str) -> Result<(), IngestError> {
68+
let emoji_char_count = emoji.chars().count();
69+
if emoji_char_count <= 64 {
70+
return Ok(());
71+
}
72+
73+
let Some(shortcode) = emoji
74+
.strip_prefix(':')
75+
.and_then(|value| value.strip_suffix(':'))
76+
else {
77+
return Err(IngestError::Rejected(format!(
78+
"invalid: reaction emoji exceeds 64 characters (got {emoji_char_count})"
79+
)));
80+
};
81+
let normalized = buzz_sdk::normalize_custom_emoji_shortcode(shortcode)
82+
.map_err(|err| IngestError::Rejected(format!("invalid: {err}")))?;
83+
if shortcode != normalized {
84+
return Err(IngestError::Rejected(
85+
"invalid: long custom emoji reaction shortcode must be canonical lowercase".into(),
86+
));
87+
}
88+
let has_matching_tag = event.tags.iter().any(|tag| {
89+
let parts = tag.as_slice();
90+
parts.first().map(String::as_str) == Some("emoji")
91+
&& parts.get(1).is_some_and(|value| value == shortcode)
92+
});
93+
if !has_matching_tag || emoji_char_count > buzz_sdk::MAX_CUSTOM_EMOJI_REACTION_LEN {
94+
return Err(IngestError::Rejected(format!(
95+
"invalid: reaction emoji exceeds 64 characters (got {emoji_char_count})"
96+
)));
97+
}
98+
Ok(())
99+
}
100+
52101
/// How the HTTP caller authenticated (for [`IngestAuth::Http`]).
53102
#[derive(Debug, Clone)]
54103
pub enum HttpAuthMethod {
@@ -2634,6 +2683,10 @@ async fn ingest_event_inner(
26342683
));
26352684
}
26362685

2686+
if kind_u32 == KIND_EMOJI_SET || kind_u32 == KIND_EMOJI_LIST {
2687+
validate_custom_emoji_tags(&event)?;
2688+
}
2689+
26372690
// Resolve the target reference, then use one DB transaction to upsert the
26382691
// reaction row (dedup via ON CONFLICT) with reaction_event_id already set and
26392692
// store the kind:7 event. This replaces the post-storage side-effect handler.
@@ -2672,17 +2725,7 @@ async fn ingest_event_inner(
26722725
&event.content
26732726
};
26742727

2675-
// Mirror the SDK's 64-character emoji limit server-side so raw clients
2676-
// cannot bypass it. Uses chars().count() (not byte len) to match the
2677-
// SDK's check_emoji_len, which also counts Unicode characters.
2678-
const MAX_REACTION_EMOJI_CHARS: usize = 64;
2679-
let emoji_char_count = emoji.chars().count();
2680-
if emoji_char_count > MAX_REACTION_EMOJI_CHARS {
2681-
return Err(IngestError::Rejected(format!(
2682-
"invalid: reaction emoji exceeds {} characters (got {})",
2683-
MAX_REACTION_EMOJI_CHARS, emoji_char_count
2684-
)));
2685-
}
2728+
validate_reaction_emoji(&event, emoji)?;
26862729

26872730
// Atomically upsert the reaction row with this kind:7 event id, then store
26882731
// the event in the same transaction. Ordering is load-bearing: active
@@ -2916,6 +2959,84 @@ mod tests {
29162959
};
29172960
use nostr::{EventBuilder, Kind};
29182961

2962+
#[test]
2963+
fn reaction_validation_accepts_wrapped_max_shortcode() {
2964+
let shortcode = "a".repeat(buzz_sdk::MAX_CUSTOM_EMOJI_SHORTCODE_LEN);
2965+
let event = EventBuilder::new(Kind::Custom(KIND_REACTION as u16), format!(":{shortcode}:"))
2966+
.tags([
2967+
nostr::Tag::parse(["emoji", &shortcode, "https://example.com/max.png"])
2968+
.expect("emoji tag"),
2969+
])
2970+
.sign_with_keys(&nostr::Keys::generate())
2971+
.expect("sign reaction");
2972+
2973+
assert!(validate_reaction_emoji(&event, &event.content).is_ok());
2974+
}
2975+
2976+
#[test]
2977+
fn reaction_validation_rejects_mixed_case_max_shortcode() {
2978+
let shortcode = "Ab".repeat(buzz_sdk::MAX_CUSTOM_EMOJI_SHORTCODE_LEN / 2);
2979+
let event = EventBuilder::new(Kind::Custom(KIND_REACTION as u16), format!(":{shortcode}:"))
2980+
.tags([
2981+
nostr::Tag::parse(["emoji", &shortcode, "https://example.com/max.png"])
2982+
.expect("emoji tag"),
2983+
])
2984+
.sign_with_keys(&nostr::Keys::generate())
2985+
.expect("sign reaction");
2986+
2987+
assert!(matches!(
2988+
validate_reaction_emoji(&event, &event.content),
2989+
Err(IngestError::Rejected(_))
2990+
));
2991+
}
2992+
2993+
#[test]
2994+
fn reaction_validation_rejects_case_mismatched_tag() {
2995+
let shortcode = "a".repeat(buzz_sdk::MAX_CUSTOM_EMOJI_SHORTCODE_LEN);
2996+
let uppercase_shortcode = shortcode.to_uppercase();
2997+
let event = EventBuilder::new(Kind::Custom(KIND_REACTION as u16), format!(":{shortcode}:"))
2998+
.tags([nostr::Tag::parse([
2999+
"emoji",
3000+
&uppercase_shortcode,
3001+
"https://example.com/max.png",
3002+
])
3003+
.expect("emoji tag")])
3004+
.sign_with_keys(&nostr::Keys::generate())
3005+
.expect("sign reaction");
3006+
3007+
assert!(matches!(
3008+
validate_reaction_emoji(&event, &event.content),
3009+
Err(IngestError::Rejected(_))
3010+
));
3011+
}
3012+
3013+
#[test]
3014+
fn emoji_set_validation_enforces_shortcode_boundary() {
3015+
let max_shortcode = "a".repeat(buzz_sdk::MAX_CUSTOM_EMOJI_SHORTCODE_LEN);
3016+
let valid_event = EventBuilder::new(Kind::Custom(KIND_EMOJI_SET as u16), "")
3017+
.tags([
3018+
nostr::Tag::parse(["emoji", &max_shortcode, "https://example.com/max.png"])
3019+
.expect("emoji tag"),
3020+
])
3021+
.sign_with_keys(&nostr::Keys::generate())
3022+
.expect("sign valid emoji set");
3023+
assert!(validate_custom_emoji_tags(&valid_event).is_ok());
3024+
3025+
let shortcode = "a".repeat(buzz_sdk::MAX_CUSTOM_EMOJI_SHORTCODE_LEN + 1);
3026+
let event = EventBuilder::new(Kind::Custom(KIND_EMOJI_SET as u16), "")
3027+
.tags([
3028+
nostr::Tag::parse(["emoji", &shortcode, "https://example.com/long.png"])
3029+
.expect("emoji tag"),
3030+
])
3031+
.sign_with_keys(&nostr::Keys::generate())
3032+
.expect("sign emoji set");
3033+
3034+
assert!(matches!(
3035+
validate_custom_emoji_tags(&event),
3036+
Err(IngestError::Rejected(message)) if message.contains("exceeds 64 bytes")
3037+
));
3038+
}
3039+
29193040
/// A banned relay admin must be refused with the same wire prefix and
29203041
/// transport status as every other durable-restriction refusal:
29213042
/// `blocked:` and (via `bridge.rs`'s `AuthFailed` arm) HTTP 403 — never

‎crates/buzz-sdk/src/builders.rs‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ fn check_repo_id(repo_id: &str) -> Result<(), SdkError> {
120120
Ok(())
121121
}
122122

123+
/// Maximum length of a custom emoji shortcode.
124+
pub const MAX_CUSTOM_EMOJI_SHORTCODE_LEN: usize = 64;
125+
/// Maximum reaction payload length for a colon-wrapped custom emoji shortcode.
126+
pub const MAX_CUSTOM_EMOJI_REACTION_LEN: usize = MAX_CUSTOM_EMOJI_SHORTCODE_LEN + 2;
127+
123128
/// Validate and normalize a NIP-30 custom emoji shortcode.
124129
///
125130
/// Shortcodes are case-insensitive in Buzz's relay-global set; lowercase
@@ -131,9 +136,9 @@ pub fn normalize_custom_emoji_shortcode(shortcode: &str) -> Result<String, SdkEr
131136
"emoji shortcode must not be empty".into(),
132137
));
133138
}
134-
if trimmed.len() > 64 {
139+
if trimmed.len() > MAX_CUSTOM_EMOJI_SHORTCODE_LEN {
135140
return Err(SdkError::InvalidInput(format!(
136-
"emoji shortcode exceeds 64 bytes (got {})",
141+
"emoji shortcode exceeds {MAX_CUSTOM_EMOJI_SHORTCODE_LEN} bytes (got {})",
137142
trimmed.len()
138143
)));
139144
}
@@ -2654,6 +2659,30 @@ mod tests {
26542659
assert!(has_tag(&ev, "emoji", "party_parrot"));
26552660
}
26562661

2662+
#[test]
2663+
fn custom_emoji_reaction_accepts_max_shortcode_length() {
2664+
let eid = event_id();
2665+
let shortcode = "a".repeat(MAX_CUSTOM_EMOJI_SHORTCODE_LEN);
2666+
let ev = sign(
2667+
build_custom_emoji_reaction(eid, &shortcode, "https://example.com/max.png").unwrap(),
2668+
);
2669+
2670+
assert_eq!(ev.content, format!(":{shortcode}:"));
2671+
assert_eq!(ev.content.chars().count(), MAX_CUSTOM_EMOJI_REACTION_LEN);
2672+
assert!(has_tag(&ev, "emoji", &shortcode));
2673+
}
2674+
2675+
#[test]
2676+
fn custom_emoji_reaction_rejects_overlong_shortcode() {
2677+
let eid = event_id();
2678+
let shortcode = "a".repeat(MAX_CUSTOM_EMOJI_SHORTCODE_LEN + 1);
2679+
2680+
assert!(matches!(
2681+
build_custom_emoji_reaction(eid, &shortcode, "https://example.com/too-long.png"),
2682+
Err(SdkError::InvalidInput(message)) if message.contains("exceeds 64 bytes")
2683+
));
2684+
}
2685+
26572686
#[test]
26582687
fn custom_emoji_set_happy_path() {
26592688
let ev = sign(

‎desktop/src/shared/api/customEmoji.test.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ test("normalizeShortcode rejects invalid chars and empties", () => {
8585
assert.equal(normalizeShortcode(""), null);
8686
});
8787

88+
test("normalizeShortcode enforces the 64-character boundary", () => {
89+
assert.equal(normalizeShortcode("a".repeat(64)), "a".repeat(64));
90+
assert.equal(normalizeShortcode("a".repeat(65)), null);
91+
});
92+
8893
test("suggestShortcodeFromFilename derives a valid name from common filenames", () => {
8994
assert.equal(
9095
suggestShortcodeFromFilename("Party Parrot.gif"),

‎desktop/src/shared/api/customEmoji.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export function reactionEmojiUrl(
4444

4545
/** NIP-30 shortcode chars. Matches the relay's `[A-Za-z0-9_-]` validation. */
4646
const SHORTCODE_RE = /^[a-z0-9_-]+$/;
47+
const MAX_SHORTCODE_LENGTH = 64;
4748

4849
/**
4950
* Normalize a shortcode the same way the relay does: strip surrounding colons
@@ -52,7 +53,9 @@ const SHORTCODE_RE = /^[a-z0-9_-]+$/;
5253
export function normalizeShortcode(raw: string): string | null {
5354
const stripped = raw.trim().replace(/^:+/, "").replace(/:+$/, "");
5455
const lower = stripped.toLowerCase();
55-
return SHORTCODE_RE.test(lower) ? lower : null;
56+
return lower.length <= MAX_SHORTCODE_LENGTH && SHORTCODE_RE.test(lower)
57+
? lower
58+
: null;
5659
}
5760

5861
/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- A valid 64-character custom emoji shortcode is wrapped as `:shortcode:` in
2+
-- NIP-25 reaction content. Preserve the wrapper in the reaction projection.
3+
ALTER TABLE reactions ALTER COLUMN emoji TYPE VARCHAR(66);

‎schema/schema.sql‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ CREATE TABLE reactions (
539539
event_created_at TIMESTAMPTZ NOT NULL,
540540
event_id BYTEA NOT NULL,
541541
pubkey BYTEA NOT NULL,
542-
emoji VARCHAR(64) NOT NULL,
542+
emoji VARCHAR(66) NOT NULL,
543543
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
544544
removed_at TIMESTAMPTZ,
545545
reaction_event_id BYTEA,

0 commit comments

Comments
 (0)