Prevent fetching messages from other channels. Change channel tag to channel_type.

This commit is contained in:
Paul Makles
2021-01-19 19:54:37 +00:00
parent 3a63d502d9
commit 5ab329dfdd
5 changed files with 11 additions and 6 deletions

View File

@@ -6,7 +6,7 @@ use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")] #[serde(tag = "channel_type")]
pub enum Channel { pub enum Channel {
SavedMessages { SavedMessages {
#[serde(rename = "_id")] #[serde(rename = "_id")]

View File

@@ -47,8 +47,13 @@ impl Ref {
self.fetch("channels").await self.fetch("channels").await
} }
pub async fn fetch_message(&self) -> Result<Message> { pub async fn fetch_message(&self, channel: &Channel) -> Result<Message> {
self.fetch("messages").await let message: Message = self.fetch("messages").await?;
if &message.channel != channel.id() {
Err(Error::InvalidOperation)
} else {
Ok(message)
}
} }
} }

View File

@@ -12,7 +12,7 @@ pub async fn req(user: User, target: Ref, msg: Ref) -> Result<()> {
Err(Error::LabelMe)? Err(Error::LabelMe)?
} }
let message = msg.fetch_message().await?; let message = msg.fetch_message(&channel).await?;
if message.author != user.id && !perm.get_manage_messages() { if message.author != user.id && !perm.get_manage_messages() {
match channel { match channel {
Channel::SavedMessages { .. } => unreachable!(), Channel::SavedMessages { .. } => unreachable!(),

View File

@@ -25,7 +25,7 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
Err(Error::LabelMe)? Err(Error::LabelMe)?
} }
let message = msg.fetch_message().await?; let message = msg.fetch_message(&channel).await?;
if message.author != user.id { if message.author != user.id {
Err(Error::CannotEditMessage)? Err(Error::CannotEditMessage)?
} }

View File

@@ -12,6 +12,6 @@ pub async fn req(user: User, target: Ref, msg: Ref) -> Result<JsonValue> {
Err(Error::LabelMe)? Err(Error::LabelMe)?
} }
let message = msg.fetch_message().await?; let message = msg.fetch_message(&channel).await?;
Ok(json!(message)) Ok(json!(message))
} }