fix: harden channel access and refresh server dependencies
This commit is contained in:
parent
84df32a9de
commit
b14f036bdb
23 changed files with 1242 additions and 720 deletions
|
|
@ -98,14 +98,26 @@ pub async fn handle_channel_create(
|
|||
|
||||
let guild_id = req.guild_id.clone();
|
||||
|
||||
let created = channels::create(&state.channels, &channel_id, &channel_name, kind.clone(), guild_id.clone()).await;
|
||||
let created = channels::create(
|
||||
&state.channels,
|
||||
&channel_id,
|
||||
&channel_name,
|
||||
kind.clone(),
|
||||
guild_id.clone(),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Persist to DB (best-effort, log and continue on failure).
|
||||
#[allow(clippy::collapsible_if)]
|
||||
if created {
|
||||
if let Err(e) = state
|
||||
.storage
|
||||
.create_channel(&channel_id, &channel_name, kind.as_str(), guild_id.as_deref())
|
||||
.create_channel(
|
||||
&channel_id,
|
||||
&channel_name,
|
||||
kind.as_str(),
|
||||
guild_id.as_deref(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
warn!("failed to persist channel to storage: {e}");
|
||||
|
|
|
|||
|
|
@ -29,18 +29,10 @@ pub async fn join(
|
|||
state: &State,
|
||||
addr: SocketAddr,
|
||||
) -> Result<()> {
|
||||
let prev_channel = session::get(&state.sessions, session_id)
|
||||
.await
|
||||
.and_then(|s| s.channel_id);
|
||||
|
||||
if prev_channel.as_deref() == Some(channel_id) {
|
||||
let Some(sess) = session::get(&state.sessions, session_id).await else {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(prev) = prev_channel {
|
||||
channels::leave(&state.channels, &prev, session_id).await;
|
||||
broadcast_leave(state, &prev, session_id).await;
|
||||
}
|
||||
};
|
||||
let prev_channel = sess.channel_id;
|
||||
|
||||
let ch = match channels::get_channel(&state.channels, channel_id).await {
|
||||
Some(c) => c,
|
||||
|
|
@ -60,6 +52,40 @@ pub async fn join(
|
|||
}
|
||||
};
|
||||
|
||||
// Validate the destination before changing membership or exposing its history.
|
||||
// Role assignments alone are insufficient: a kicked user can retain stale roles.
|
||||
if let Some(guild_id) = ch.guild_id.as_deref() {
|
||||
let is_member = state
|
||||
.storage
|
||||
.list_user_guilds(&sess.user_id)
|
||||
.await?
|
||||
.iter()
|
||||
.any(|guild| guild.id == guild_id);
|
||||
if !is_member {
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::Error,
|
||||
seq,
|
||||
&to_payload(&proto::ErrorPayload {
|
||||
code: proto::ErrorCode::PermissionDenied as u32,
|
||||
message: "guild membership required".into(),
|
||||
}),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
if prev_channel.as_deref() == Some(channel_id) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(prev) = prev_channel {
|
||||
channels::leave(&state.channels, &prev, session_id).await;
|
||||
broadcast_leave(state, &prev, session_id).await;
|
||||
}
|
||||
|
||||
channels::join(&state.channels, channel_id, session_id).await;
|
||||
set_channel(state, session_id, Some(channel_id.into())).await;
|
||||
|
||||
|
|
@ -130,3 +156,6 @@ pub async fn join(
|
|||
info!("session {} joined {channel_id}", &session_id[..8]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
|
|||
145
gateway/src/handler/channel/join/tests.rs
Normal file
145
gateway/src/handler/channel/join/tests.rs
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
use super::*;
|
||||
use std::sync::{Arc, atomic::AtomicUsize};
|
||||
|
||||
async fn fixture() -> (State, String) {
|
||||
let storage = crate::domain::storage::Storage::connect_sqlite(":memory:")
|
||||
.await
|
||||
.unwrap();
|
||||
let config = toml::from_str(include_str!("../../../../../dev/config.toml")).unwrap();
|
||||
let identity_dir =
|
||||
std::env::temp_dir().join(format!("vnox-join-test-{}", uuid::Uuid::new_v4()));
|
||||
let identity =
|
||||
crate::bootstrap::server_identity::ServerIdentity::load_or_generate(&identity_dir).unwrap();
|
||||
std::fs::remove_file(identity_dir.join("server_identity.json")).unwrap();
|
||||
std::fs::remove_dir(identity_dir).unwrap();
|
||||
let (broadcast, _) = tokio::sync::broadcast::channel(16);
|
||||
let state = State::new(
|
||||
session::new_store(),
|
||||
channels::new_store(),
|
||||
Arc::new(storage),
|
||||
Arc::new(config),
|
||||
Arc::new(identity),
|
||||
broadcast,
|
||||
crate::admin::metrics::Metrics::new(),
|
||||
Arc::new(AtomicUsize::new(0)),
|
||||
Arc::new(AtomicUsize::new(0)),
|
||||
None,
|
||||
);
|
||||
let sess = session::create(&state.sessions, "visitor".into(), "Visitor".into()).await;
|
||||
channels::join(&state.channels, "general", &sess.session_id).await;
|
||||
set_channel(&state, &sess.session_id, Some("general".into())).await;
|
||||
(state, sess.session_id)
|
||||
}
|
||||
|
||||
async fn attempt_join(state: &State, session_id: &str, channel_id: &str) {
|
||||
let (mut stream, _peer) = tokio::io::duplex(65536);
|
||||
join(
|
||||
&mut stream,
|
||||
&mut 0,
|
||||
session_id,
|
||||
channel_id,
|
||||
&SessionCrypto::derive(&[7; 32], session_id),
|
||||
state,
|
||||
"127.0.0.1:12345".parse().unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn missing_channel_keeps_previous_membership() {
|
||||
let (state, sid) = fixture().await;
|
||||
attempt_join(&state, &sid, "missing").await;
|
||||
assert!(
|
||||
channels::members(&state.channels, "general")
|
||||
.await
|
||||
.contains(&sid)
|
||||
);
|
||||
assert_eq!(
|
||||
session::get(&state.sessions, &sid)
|
||||
.await
|
||||
.unwrap()
|
||||
.channel_id
|
||||
.as_deref(),
|
||||
Some("general")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn outsider_cannot_join_guild_channel_or_lose_previous_membership() {
|
||||
let (state, sid) = fixture().await;
|
||||
let guild_id = state
|
||||
.storage
|
||||
.create_guild("owner", "Private guild")
|
||||
.await
|
||||
.unwrap();
|
||||
channels::create(
|
||||
&state.channels,
|
||||
"private",
|
||||
"Private",
|
||||
channels::ChannelKind::Text,
|
||||
Some(guild_id),
|
||||
)
|
||||
.await;
|
||||
attempt_join(&state, &sid, "private").await;
|
||||
assert!(
|
||||
!channels::members(&state.channels, "private")
|
||||
.await
|
||||
.contains(&sid)
|
||||
);
|
||||
assert!(
|
||||
channels::members(&state.channels, "general")
|
||||
.await
|
||||
.contains(&sid)
|
||||
);
|
||||
assert_eq!(
|
||||
session::get(&state.sessions, &sid)
|
||||
.await
|
||||
.unwrap()
|
||||
.channel_id
|
||||
.as_deref(),
|
||||
Some("general")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guild_member_can_switch_channels() {
|
||||
let (state, sid) = fixture().await;
|
||||
let guild_id = state
|
||||
.storage
|
||||
.create_guild("owner", "Private guild")
|
||||
.await
|
||||
.unwrap();
|
||||
state
|
||||
.storage
|
||||
.add_guild_member(&guild_id, "visitor")
|
||||
.await
|
||||
.unwrap();
|
||||
channels::create(
|
||||
&state.channels,
|
||||
"private",
|
||||
"Private",
|
||||
channels::ChannelKind::Text,
|
||||
Some(guild_id),
|
||||
)
|
||||
.await;
|
||||
attempt_join(&state, &sid, "private").await;
|
||||
assert!(
|
||||
channels::members(&state.channels, "private")
|
||||
.await
|
||||
.contains(&sid)
|
||||
);
|
||||
assert!(
|
||||
!channels::members(&state.channels, "general")
|
||||
.await
|
||||
.contains(&sid)
|
||||
);
|
||||
assert_eq!(
|
||||
session::get(&state.sessions, &sid)
|
||||
.await
|
||||
.unwrap()
|
||||
.channel_id
|
||||
.as_deref(),
|
||||
Some("private")
|
||||
);
|
||||
}
|
||||
|
|
@ -79,12 +79,7 @@ pub async fn dispatch<S: AsyncRead + AsyncWrite + Unpin>(
|
|||
}
|
||||
PacketId::ChannelList => {
|
||||
channel::handle_channel_list(
|
||||
ctx.stream,
|
||||
ctx.seq,
|
||||
session_id,
|
||||
payload,
|
||||
ctx.crypto,
|
||||
ctx.state,
|
||||
ctx.stream, ctx.seq, session_id, payload, ctx.crypto, ctx.state,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue