|
10 | 10 | //! | 9030 | Add member | admin or owner | |
11 | 11 | //! | 9031 | Remove member | admin or owner | |
12 | 12 | //! | 9032 | Change role | owner only | |
13 | | -//! | 9033 | Set workspace profile (icon) | admin or owner | |
| 13 | +//! | 9033 | Set workspace profile (icon) | admin or owner; on an open relay whose community has no admin/owner row at all, any authenticated sender (see [`may_set_workspace_profile`]) | |
14 | 14 |
|
15 | 15 | use std::sync::Arc; |
16 | 16 |
|
@@ -94,6 +94,35 @@ fn validate_workspace_icon(icon: &str) -> Result<(), String> { |
94 | 94 | Ok(()) |
95 | 95 | } |
96 | 96 |
|
| 97 | +/// Whether `sender_role` may set the workspace profile (kind:9033). |
| 98 | +/// |
| 99 | +/// Closed relays (`membership_enforced == true`) require an `admin`/`owner` |
| 100 | +/// row in `relay_members` — the enforced roster is the authority. Open relays |
| 101 | +/// don't *enforce* the roster, but the data can still exist: startup |
| 102 | +/// bootstraps `RELAY_OWNER_PUBKEY` as `owner` regardless of the flag |
| 103 | +/// (`main.rs`), as does operator provisioning. So the rule is steward-wins: |
| 104 | +/// |
| 105 | +/// - a steward (any admin/owner row) exists → admin/owner only, exactly like |
| 106 | +/// a closed relay. An open relay with a configured owner keeps its icon |
| 107 | +/// owner-controlled instead of last-write-wins for every authenticated key. |
| 108 | +/// - genuinely rosterless (e.g. a community created by |
| 109 | +/// `ensure_configured_community`, which writes no owner row) → any |
| 110 | +/// NIP-42-authenticated sender may set the icon, mirroring how open relays |
| 111 | +/// gate every other write. Without this the icon is permanently unsettable: |
| 112 | +/// the desktop deliberately shows the icon editor on open relays (see |
| 113 | +/// `canEditIcon` in `EditCommunityDialog.tsx`, #2640) and defers to this |
| 114 | +/// relay-side check, which used to always say no. |
| 115 | +fn may_set_workspace_profile( |
| 116 | + sender_role: &str, |
| 117 | + membership_enforced: bool, |
| 118 | + community_has_steward: bool, |
| 119 | +) -> bool { |
| 120 | + if !membership_enforced && !community_has_steward { |
| 121 | + return true; |
| 122 | + } |
| 123 | + sender_role == "admin" || sender_role == "owner" |
| 124 | +} |
| 125 | + |
97 | 126 | /// A relay-admin command failure, carrying the *category* of the failure so |
98 | 127 | /// the ingest seam can map it to the right NIP-01 prefix and HTTP status. |
99 | 128 | /// |
@@ -230,9 +259,33 @@ async fn execute_relay_admin_command( |
230 | 259 | // kind:9033 — Set workspace profile (icon). Handled before p-tag |
231 | 260 | // extraction: it targets the relay itself, not a member pubkey. |
232 | 261 | if kind == RELAY_ADMIN_SET_WORKSPACE_PROFILE { |
233 | | - if sender_role != "admin" && sender_role != "owner" { |
| 262 | + // Steward detection only matters on open relays (closed relays gate on |
| 263 | + // the sender's own role either way), so skip the extra query there. |
| 264 | + let community_has_steward = if state.config.require_relay_membership { |
| 265 | + true |
| 266 | + } else { |
| 267 | + state |
| 268 | + .db |
| 269 | + .has_admin_or_owner(tenant.community()) |
| 270 | + .await |
| 271 | + .map_err(|e| format!("database error: {e}"))? |
| 272 | + }; |
| 273 | + if !may_set_workspace_profile( |
| 274 | + sender_role, |
| 275 | + state.config.require_relay_membership, |
| 276 | + community_has_steward, |
| 277 | + ) { |
234 | 278 | return Err("actor not authorized: must be admin or owner".to_string()); |
235 | 279 | } |
| 280 | + if sender_role != "admin" && sender_role != "owner" { |
| 281 | + // Rosterless-open-relay admit: 9033 writes no audit row and |
| 282 | + // publishes no announcement event (unlike 9030/9031), so this warn |
| 283 | + // is the only durable attribution of who changed the icon. |
| 284 | + warn!( |
| 285 | + sender = %sender_hex, |
| 286 | + "workspace profile change admitted without a roster role (open relay, no steward)" |
| 287 | + ); |
| 288 | + } |
236 | 289 |
|
237 | 290 | // Empty or missing icon tag clears the workspace icon. |
238 | 291 | let icon = extract_tag_value(event, "icon").unwrap_or_default(); |
@@ -562,6 +615,46 @@ mod tests { |
562 | 615 | assert!(validate_workspace_icon("").is_ok()); |
563 | 616 | } |
564 | 617 |
|
| 618 | + /// Closed relay (membership enforced): only an admin/owner row in |
| 619 | + /// `relay_members` may set the workspace profile — a plain member, or a |
| 620 | + /// pubkey with no row at all (empty role), must be refused. The steward |
| 621 | + /// flag is irrelevant when membership is enforced (call sites pass `true`, |
| 622 | + /// but the rule must not depend on it). |
| 623 | + #[test] |
| 624 | + fn closed_relay_requires_admin_or_owner_for_workspace_profile() { |
| 625 | + for steward in [true, false] { |
| 626 | + assert!(may_set_workspace_profile("owner", true, steward)); |
| 627 | + assert!(may_set_workspace_profile("admin", true, steward)); |
| 628 | + assert!(!may_set_workspace_profile("member", true, steward)); |
| 629 | + assert!(!may_set_workspace_profile("", true, steward)); |
| 630 | + } |
| 631 | + } |
| 632 | + |
| 633 | + /// Open relay with a steward: startup bootstraps `RELAY_OWNER_PUBKEY` as |
| 634 | + /// `owner` regardless of `require_relay_membership`, so an open relay's |
| 635 | + /// community can hold admin/owner rows. When one exists, the icon stays |
| 636 | + /// steward-only — the fix must not widen an owner-controlled icon to |
| 637 | + /// every authenticated key. |
| 638 | + #[test] |
| 639 | + fn open_relay_with_steward_keeps_workspace_profile_steward_only() { |
| 640 | + assert!(may_set_workspace_profile("owner", false, true)); |
| 641 | + assert!(may_set_workspace_profile("admin", false, true)); |
| 642 | + assert!(!may_set_workspace_profile("member", false, true)); |
| 643 | + assert!(!may_set_workspace_profile("", false, true)); |
| 644 | + } |
| 645 | + |
| 646 | + /// Open relay, genuinely rosterless (no admin/owner row anywhere): any |
| 647 | + /// authenticated sender may set the icon — including the roleless (empty |
| 648 | + /// role) case, which is *every* sender there. This is the bug being |
| 649 | + /// fixed: the desktop shows the icon editor on open relays (#2640) but |
| 650 | + /// the relay refused every 9033. |
| 651 | + #[test] |
| 652 | + fn rosterless_open_relay_admits_any_authenticated_sender_for_workspace_profile() { |
| 653 | + assert!(may_set_workspace_profile("", false, false)); |
| 654 | + assert!(may_set_workspace_profile("member", false, false)); |
| 655 | + assert!(may_set_workspace_profile("owner", false, false)); |
| 656 | + } |
| 657 | + |
565 | 658 | #[test] |
566 | 659 | fn workspace_icon_https_ok() { |
567 | 660 | assert!(validate_workspace_icon("https://example.com/icon.png").is_ok()); |
@@ -591,4 +684,216 @@ mod tests { |
591 | 684 | let long_data = format!("data:image/png;base64,{}", "A".repeat(98_304)); |
592 | 685 | assert!(validate_workspace_icon(&long_data).is_err()); |
593 | 686 | } |
| 687 | + |
| 688 | + // ─── Call-site integration: the 9033 gate wired to real config + DB ──── |
| 689 | + // |
| 690 | + // The unit tests above pin `may_set_workspace_profile`'s truth table, but |
| 691 | + // not its wiring: mutation-testing showed that inverting |
| 692 | + // `state.config.require_relay_membership` at the call site — an exact |
| 693 | + // inversion of the security contract — survives the default suite. These |
| 694 | + // tests drive `handle_relay_admin_event` with a real `AppState` against |
| 695 | + // Postgres, on both relay modes, so the wiring itself is pinned. Selected |
| 696 | + // explicitly in CI's Backend Integration job; requires local Postgres |
| 697 | + // (and hard-fails rather than skipping when it is unreachable). |
| 698 | + |
| 699 | + const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1 |
| 700 | + |
| 701 | + /// Build a real `AppState` + tenant for a fresh community on `host`, with |
| 702 | + /// `require_relay_membership` set as given. Mirrors |
| 703 | + /// `api::invites::tests::invite_test_state`. |
| 704 | + async fn workspace_profile_test_state( |
| 705 | + host: &str, |
| 706 | + require_relay_membership: bool, |
| 707 | + ) -> (Arc<AppState>, TenantContext) { |
| 708 | + let mut config = crate::config::Config::from_env().expect("config from env"); |
| 709 | + let database_url = std::env::var("BUZZ_TEST_DATABASE_URL") |
| 710 | + .or_else(|_| std::env::var("DATABASE_URL")) |
| 711 | + .unwrap_or_else(|_| TEST_DB_URL.to_string()); |
| 712 | + config.database_url = database_url.clone(); |
| 713 | + config.redis_url = "redis://127.0.0.1:1".to_string(); |
| 714 | + config.relay_url = format!("wss://{host}"); |
| 715 | + config.require_relay_membership = require_relay_membership; |
| 716 | + |
| 717 | + let pool = sqlx::PgPool::connect(&database_url) |
| 718 | + .await |
| 719 | + .expect("requires reachable Postgres"); |
| 720 | + let db = buzz_db::Db::from_pool(pool.clone()); |
| 721 | + let record = db |
| 722 | + .ensure_configured_community(host) |
| 723 | + .await |
| 724 | + .expect("ensure community"); |
| 725 | + let tenant = TenantContext::resolved(record.id, host); |
| 726 | + |
| 727 | + let redis_pool = deadpool_redis::Config::from_url(&config.redis_url) |
| 728 | + .create_pool(Some(deadpool_redis::Runtime::Tokio1)) |
| 729 | + .expect("redis pool config"); |
| 730 | + let pubsub = Arc::new( |
| 731 | + buzz_pubsub::PubSubManager::new(&config.redis_url, redis_pool.clone()) |
| 732 | + .await |
| 733 | + .expect("pubsub manager"), |
| 734 | + ); |
| 735 | + let audit = buzz_audit::AuditService::new(pool.clone()); |
| 736 | + let auth = buzz_auth::AuthService::new(config.auth.clone()); |
| 737 | + let search = buzz_search::SearchService::new(pool.clone()); |
| 738 | + let workflow_engine = Arc::new(buzz_workflow::WorkflowEngine::new( |
| 739 | + db.clone(), |
| 740 | + buzz_workflow::WorkflowConfig::default(), |
| 741 | + )); |
| 742 | + let media_storage = buzz_media::MediaStorage::new(&config.media).expect("media storage"); |
| 743 | + let (state, _audit_shutdown) = AppState::new( |
| 744 | + config, |
| 745 | + db, |
| 746 | + redis_pool, |
| 747 | + audit, |
| 748 | + pubsub, |
| 749 | + auth, |
| 750 | + search, |
| 751 | + workflow_engine, |
| 752 | + Keys::generate(), |
| 753 | + media_storage, |
| 754 | + ); |
| 755 | + (Arc::new(state), tenant) |
| 756 | + } |
| 757 | + |
| 758 | + /// Sign a fresh kind:9033 with `icon` and run it through the real |
| 759 | + /// admission + command path. |
| 760 | + async fn submit_9033( |
| 761 | + state: &Arc<AppState>, |
| 762 | + tenant: &TenantContext, |
| 763 | + keys: &Keys, |
| 764 | + icon: &str, |
| 765 | + ) -> Result<(), RelayAdminError> { |
| 766 | + let event = EventBuilder::new(Kind::Custom(9033), "") |
| 767 | + .tags(vec![Tag::parse(["icon", icon]).expect("icon tag")]) |
| 768 | + .sign_with_keys(keys) |
| 769 | + .expect("sign 9033"); |
| 770 | + handle_relay_admin_event(tenant, state, &event).await |
| 771 | + } |
| 772 | + |
| 773 | + async fn stored_icon(state: &Arc<AppState>, tenant: &TenantContext) -> Option<String> { |
| 774 | + state |
| 775 | + .db |
| 776 | + .get_community_icon(tenant.community()) |
| 777 | + .await |
| 778 | + .expect("read icon") |
| 779 | + } |
| 780 | + |
| 781 | + /// Open relay (`require_relay_membership = false`): a rosterless |
| 782 | + /// community admits any authenticated sender, but the moment a steward |
| 783 | + /// (admin/owner row) exists the gate reverts to steward-only. |
| 784 | + /// |
| 785 | + /// Discriminating: fails if the call site inverts or drops |
| 786 | + /// `require_relay_membership`, or stops consulting `has_admin_or_owner`. |
| 787 | + #[tokio::test] |
| 788 | + #[ignore = "requires Postgres"] |
| 789 | + async fn open_relay_9033_admits_roleless_only_until_a_steward_exists() { |
| 790 | + let host = format!("icon-gate-open-{}.example", uuid::Uuid::new_v4().simple()); |
| 791 | + let (state, tenant) = workspace_profile_test_state(&host, false).await; |
| 792 | + let roleless = Keys::generate(); |
| 793 | + let owner = Keys::generate(); |
| 794 | + |
| 795 | + // Rosterless: the roleless sender may set the icon. |
| 796 | + submit_9033(&state, &tenant, &roleless, "https://example.com/open.png") |
| 797 | + .await |
| 798 | + .expect("rosterless open relay must admit an authenticated sender"); |
| 799 | + assert_eq!( |
| 800 | + stored_icon(&state, &tenant).await.as_deref(), |
| 801 | + Some("https://example.com/open.png"), |
| 802 | + "icon must actually be stored" |
| 803 | + ); |
| 804 | + |
| 805 | + // Seed a steward — the same roleless sender must now be refused, and |
| 806 | + // the previously stored icon must survive the refused attempt. |
| 807 | + state |
| 808 | + .db |
| 809 | + .add_relay_member( |
| 810 | + tenant.community(), |
| 811 | + &owner.public_key().to_hex(), |
| 812 | + "owner", |
| 813 | + None, |
| 814 | + ) |
| 815 | + .await |
| 816 | + .expect("seed owner"); |
| 817 | + let refused = submit_9033(&state, &tenant, &roleless, "https://evil.example/pwn.png").await; |
| 818 | + assert_eq!( |
| 819 | + refused, |
| 820 | + Err(RelayAdminError::Rejected( |
| 821 | + "actor not authorized: must be admin or owner".to_string() |
| 822 | + )), |
| 823 | + "an open relay with a steward must refuse a roleless sender" |
| 824 | + ); |
| 825 | + assert_eq!( |
| 826 | + stored_icon(&state, &tenant).await.as_deref(), |
| 827 | + Some("https://example.com/open.png"), |
| 828 | + "refused attempt must not mutate the icon" |
| 829 | + ); |
| 830 | + |
| 831 | + // The steward still can. |
| 832 | + submit_9033(&state, &tenant, &owner, "https://example.com/owner.png") |
| 833 | + .await |
| 834 | + .expect("the steward must retain icon control"); |
| 835 | + assert_eq!( |
| 836 | + stored_icon(&state, &tenant).await.as_deref(), |
| 837 | + Some("https://example.com/owner.png") |
| 838 | + ); |
| 839 | + } |
| 840 | + |
| 841 | + /// Closed relay (`require_relay_membership = true`): admin/owner only — |
| 842 | + /// a plain member and a roleless key are refused even though the |
| 843 | + /// community also *looks* rosterless-then-stewarded to the open-relay |
| 844 | + /// branch. Together with the open-relay test this kills the inverted-flag |
| 845 | + /// mutant: no assignment of the flag satisfies both. |
| 846 | + #[tokio::test] |
| 847 | + #[ignore = "requires Postgres"] |
| 848 | + async fn closed_relay_9033_still_requires_admin_or_owner() { |
| 849 | + let host = format!("icon-gate-closed-{}.example", uuid::Uuid::new_v4().simple()); |
| 850 | + let (state, tenant) = workspace_profile_test_state(&host, true).await; |
| 851 | + let roleless = Keys::generate(); |
| 852 | + let member = Keys::generate(); |
| 853 | + let admin = Keys::generate(); |
| 854 | + state |
| 855 | + .db |
| 856 | + .add_relay_member( |
| 857 | + tenant.community(), |
| 858 | + &member.public_key().to_hex(), |
| 859 | + "member", |
| 860 | + None, |
| 861 | + ) |
| 862 | + .await |
| 863 | + .expect("seed member"); |
| 864 | + state |
| 865 | + .db |
| 866 | + .add_relay_member( |
| 867 | + tenant.community(), |
| 868 | + &admin.public_key().to_hex(), |
| 869 | + "admin", |
| 870 | + None, |
| 871 | + ) |
| 872 | + .await |
| 873 | + .expect("seed admin"); |
| 874 | + |
| 875 | + for (keys, label) in [(&roleless, "roleless"), (&member, "member")] { |
| 876 | + let refused = submit_9033(&state, &tenant, keys, "https://evil.example/pwn.png").await; |
| 877 | + assert_eq!( |
| 878 | + refused, |
| 879 | + Err(RelayAdminError::Rejected( |
| 880 | + "actor not authorized: must be admin or owner".to_string() |
| 881 | + )), |
| 882 | + "closed relay must refuse a {label} sender" |
| 883 | + ); |
| 884 | + } |
| 885 | + assert_eq!( |
| 886 | + stored_icon(&state, &tenant).await, |
| 887 | + None, |
| 888 | + "refused attempts must not set an icon" |
| 889 | + ); |
| 890 | + |
| 891 | + submit_9033(&state, &tenant, &admin, "https://example.com/closed.png") |
| 892 | + .await |
| 893 | + .expect("closed-relay admin must set the icon"); |
| 894 | + assert_eq!( |
| 895 | + stored_icon(&state, &tenant).await.as_deref(), |
| 896 | + Some("https://example.com/closed.png") |
| 897 | + ); |
| 898 | + } |
594 | 899 | } |
0 commit comments