feat: reintroduce permission checks for send

This commit is contained in:
Paul Makles
2023-06-03 14:00:17 +01:00
parent 23188032ca
commit a0002d0b43
10 changed files with 82 additions and 57 deletions

6
Cargo.lock generated
View File

@@ -2837,7 +2837,7 @@ dependencies = [
[[package]] [[package]]
name = "revolt-bonfire" name = "revolt-bonfire"
version = "0.5.21" version = "0.5.22"
dependencies = [ dependencies = [
"async-std", "async-std",
"async-tungstenite", "async-tungstenite",
@@ -2882,7 +2882,7 @@ dependencies = [
[[package]] [[package]]
name = "revolt-delta" name = "revolt-delta"
version = "0.5.21" version = "0.5.22"
dependencies = [ dependencies = [
"async-channel", "async-channel",
"async-std", "async-std",
@@ -2955,7 +2955,7 @@ dependencies = [
[[package]] [[package]]
name = "revolt-quark" name = "revolt-quark"
version = "0.5.21" version = "0.5.22"
dependencies = [ dependencies = [
"async-lock", "async-lock",
"async-recursion", "async-recursion",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "revolt-bonfire" name = "revolt-bonfire"
version = "0.5.21" version = "0.5.22"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
edition = "2021" edition = "2021"

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "revolt-delta" name = "revolt-delta"
version = "0.5.21" version = "0.5.22"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
authors = ["Paul Makles <paulmakles@gmail.com>"] authors = ["Paul Makles <paulmakles@gmail.com>"]
edition = "2018" edition = "2018"

View File

@@ -44,8 +44,6 @@ pub async fn req(
.throw_permission_and_view_channel(db, Permission::SendMessage) .throw_permission_and_view_channel(db, Permission::SendMessage)
.await?; .await?;
Message::validate_sum(&edit.content, &edit.embeds)?;
let mut message = msg.as_message(db).await?; let mut message = msg.as_message(db).await?;
if message.channel != channel.id() { if message.channel != channel.id() {
return Err(Error::NotFound); return Err(Error::NotFound);
@@ -55,6 +53,12 @@ pub async fn req(
return Err(Error::CannotEditMessage); return Err(Error::CannotEditMessage);
} }
if let Some(new_embeds) = &edit.embeds {
Message::validate_sum(&edit.content, new_embeds)?;
} else {
Message::validate_sum(&edit.content, &vec![])?;
}
message.edited = Some(Timestamp::now_utc()); message.edited = Some(Timestamp::now_utc());
let mut partial = PartialMessage { let mut partial = PartialMessage {
edited: message.edited, edited: message.edited,

View File

@@ -1,11 +1,9 @@
use revolt_quark::{ use revolt_quark::{
models::{ models::{message::DataMessageSend, Message, User},
message::DataMessageSend,
Message, User,
},
perms, perms,
types::push::MessageAuthor,
web::idempotency::IdempotencyKey, web::idempotency::IdempotencyKey,
Db, Error, Permission, Ref, Result, types::push::MessageAuthor, Db, Error, Permission, Ref, Result,
}; };
use rocket::serde::json::Json; use rocket::serde::json::Json;
@@ -35,7 +33,7 @@ pub async fn message_send(
.throw_permission_and_view_channel(db, Permission::SendMessage) .throw_permission_and_view_channel(db, Permission::SendMessage)
.await?; .await?;
// Verify permissions for masquerade. // Verify permissions for masquerade
if let Some(masq) = &data.masquerade { if let Some(masq) = &data.masquerade {
permissions permissions
.throw_permission(db, Permission::Masquerade) .throw_permission(db, Permission::Masquerade)
@@ -48,8 +46,37 @@ pub async fn message_send(
} }
} }
// Check permissions for embeds
if !data.embeds.is_empty() {
permissions
.throw_permission(db, Permission::SendEmbeds)
.await?;
}
// Check permissions for files
if !data.attachments.is_empty() {
permissions
.throw_permission(db, Permission::UploadFiles)
.await?;
}
// Ensure interactions information is correct
if let Some(interactions) = &data.interactions {
interactions.validate(db, &mut permissions).await?;
}
// Create the message // Create the message
let message = channel.send_message(db, data, MessageAuthor::User(&user), idempotency).await?; let message = channel
.send_message(
db,
data,
MessageAuthor::User(&user),
idempotency,
permissions
.has_permission(db, Permission::SendEmbeds)
.await?,
)
.await?;
Ok(Json(message)) Ok(Json(message))
} }

View File

@@ -33,6 +33,9 @@ pub async fn webhook_execute(
webhook.assert_token(&token).map_err(Error::from_core)?; webhook.assert_token(&token).map_err(Error::from_core)?;
// TODO: webhooks can currently always send masquerades, files, embeds, reactions (interactions)
// TODO: they can also mention anyone
let channel = legacy_db.fetch_channel(&webhook.channel_id).await?; let channel = legacy_db.fetch_channel(&webhook.channel_id).await?;
let message = channel let message = channel
.send_message( .send_message(
@@ -40,6 +43,7 @@ pub async fn webhook_execute(
data, data,
MessageAuthor::Webhook(&webhook.into()), MessageAuthor::Webhook(&webhook.into()),
idempotency, idempotency,
true,
) )
.await?; .await?;

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "revolt-quark" name = "revolt-quark"
version = "0.5.21" version = "0.5.22"
edition = "2021" edition = "2021"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"

View File

@@ -409,6 +409,7 @@ impl Channel {
data: DataMessageSend, data: DataMessageSend,
author: MessageAuthor<'_>, author: MessageAuthor<'_>,
mut idempotency: IdempotencyKey, mut idempotency: IdempotencyKey,
generate_embeds: bool,
) -> Result<Message> { ) -> Result<Message> {
Message::validate_sum(&data.content, &data.embeds)?; Message::validate_sum(&data.content, &data.embeds)?;
@@ -416,8 +417,8 @@ impl Channel {
// Check the message is not empty // Check the message is not empty
if (data.content.as_ref().map_or(true, |v| v.is_empty())) if (data.content.as_ref().map_or(true, |v| v.is_empty()))
&& (data.attachments.as_ref().map_or(true, |v| v.is_empty())) && (data.attachments.is_empty())
&& (data.embeds.as_ref().map_or(true, |v| v.is_empty())) && (data.embeds.is_empty())
{ {
return Err(Error::EmptyMessage); return Err(Error::EmptyMessage);
} }
@@ -496,11 +497,9 @@ impl Channel {
// Process included embeds. // Process included embeds.
let mut embeds = vec![]; let mut embeds = vec![];
if let Some(sendable_embeds) = data.embeds { for sendable_embed in data.embeds {
for sendable_embed in sendable_embeds {
embeds.push(sendable_embed.into_embed(db, message_id.clone()).await?) embeds.push(sendable_embed.into_embed(db, message_id.clone()).await?)
} }
}
if !embeds.is_empty() { if !embeds.is_empty() {
message.embeds.replace(embeds); message.embeds.replace(embeds);
@@ -508,25 +507,18 @@ impl Channel {
// Add attachments to message. // Add attachments to message.
let mut attachments = vec![]; let mut attachments = vec![];
if let Some(ids) = &data.attachments { if data.attachments.len() > *MAX_ATTACHMENT_COUNT {
if ids.len() > *MAX_ATTACHMENT_COUNT {
return Err(Error::TooManyAttachments { return Err(Error::TooManyAttachments {
max: *MAX_ATTACHMENT_COUNT, max: *MAX_ATTACHMENT_COUNT,
}); });
} }
for attachment_id in ids { for attachment_id in data.attachments {
attachments.push( attachments.push(
db.find_and_use_attachment( db.find_and_use_attachment(&attachment_id, "attachments", "message", &message_id)
attachment_id,
"attachments",
"message",
&message_id,
)
.await?, .await?,
); );
} }
}
if !attachments.is_empty() { if !attachments.is_empty() {
message.attachments.replace(attachments); message.attachments.replace(attachments);
@@ -541,6 +533,7 @@ impl Channel {
message.create(db, self, Some(author)).await?; message.create(db, self, Some(author)).await?;
// Queue up a task for processing embeds // Queue up a task for processing embeds
if generate_embeds {
if let Some(content) = &message.content { if let Some(content) = &message.content {
process_embeds::queue( process_embeds::queue(
self.id().to_string(), self.id().to_string(),
@@ -549,6 +542,7 @@ impl Channel {
) )
.await; .await;
} }
}
Ok(message) Ok(message)
} }

View File

@@ -18,7 +18,7 @@ use crate::{
tasks::ack::AckEvent, tasks::ack::AckEvent,
types::{ types::{
january::{Embed, Text}, january::{Embed, Text},
push::{PushNotification, MessageAuthor}, push::{MessageAuthor, PushNotification},
}, },
Database, Error, Permission, Result, Database, Error, Permission, Result,
}; };
@@ -170,22 +170,17 @@ impl Message {
} }
/// Validate the sum of content of a message is under threshold /// Validate the sum of content of a message is under threshold
pub fn validate_sum( pub fn validate_sum(content: &Option<String>, embeds: &Vec<SendableEmbed>) -> Result<()> {
content: &Option<String>,
embeds: &Option<Vec<SendableEmbed>>,
) -> Result<()> {
let mut running_total = 0; let mut running_total = 0;
if let Some(content) = content { if let Some(content) = content {
running_total += content.len(); running_total += content.len();
} }
if let Some(embeds) = embeds {
for embed in embeds { for embed in embeds {
if let Some(desc) = &embed.description { if let Some(desc) = &embed.description {
running_total += desc.len(); running_total += desc.len();
} }
} }
}
if running_total <= 2000 { if running_total <= 2000 {
Ok(()) Ok(())

View File

@@ -268,15 +268,16 @@ pub struct DataMessageSend {
#[validate(length(min = 0, max = 2000))] #[validate(length(min = 0, max = 2000))]
pub content: Option<String>, pub content: Option<String>,
/// Attachments to include in message /// Attachments to include in message
#[validate(length(min = 1, max = 128))] #[serde(default)]
pub attachments: Option<Vec<String>>, pub attachments: Vec<String>,
/// Messages to reply to /// Messages to reply to
pub replies: Option<Vec<Reply>>, pub replies: Option<Vec<Reply>>,
/// Embeds to include in message /// Embeds to include in message
/// ///
/// Text embed content contributes to the content length cap /// Text embed content contributes to the content length cap
#[serde(default)]
#[validate(length(min = 1, max = 10))] #[validate(length(min = 1, max = 10))]
pub embeds: Option<Vec<SendableEmbed>>, pub embeds: Vec<SendableEmbed>,
/// Masquerade to apply to this message /// Masquerade to apply to this message
#[validate] #[validate]
pub masquerade: Option<Masquerade>, pub masquerade: Option<Masquerade>,