1
0

Fix issue with moderator identities being revealed (#892)

* Fix issue with moderator identities being revealed

* Fix on multiple threads route

* Fix thread notifs

* Fix failing test

* fix thread messages returning nothing
This commit is contained in:
Geometrically
2024-03-19 17:25:49 -07:00
committed by GitHub
parent 730913bec4
commit decfcb6c27
20 changed files with 79 additions and 334 deletions

View File

@@ -1,14 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n UPDATE threads\n SET show_in_mod_inbox = FALSE\n WHERE id = $1\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": []
},
"hash": "10279b5a8383ba8e286f1bfb9a486e3f8b362c46cfc2647c90a83a10e5329569"
}

View File

@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT t.id, t.thread_type, t.mod_id, t.report_id, t.show_in_mod_inbox,\n ARRAY_AGG(DISTINCT tm.user_id) filter (where tm.user_id is not null) members,\n JSONB_AGG(DISTINCT jsonb_build_object('id', tmsg.id, 'author_id', tmsg.author_id, 'thread_id', tmsg.thread_id, 'body', tmsg.body, 'created', tmsg.created)) filter (where tmsg.id is not null) messages\n FROM threads t\n LEFT OUTER JOIN threads_messages tmsg ON tmsg.thread_id = t.id\n LEFT OUTER JOIN threads_members tm ON tm.thread_id = t.id\n WHERE t.id = ANY($1)\n GROUP BY t.id\n ",
"query": "\n SELECT t.id, t.thread_type, t.mod_id, t.report_id,\n ARRAY_AGG(DISTINCT tm.user_id) filter (where tm.user_id is not null) members,\n JSONB_AGG(DISTINCT jsonb_build_object('id', tmsg.id, 'author_id', tmsg.author_id, 'thread_id', tmsg.thread_id, 'body', tmsg.body, 'created', tmsg.created, 'hide_identity', tmsg.hide_identity)) filter (where tmsg.id is not null) messages\n FROM threads t\n LEFT OUTER JOIN threads_messages tmsg ON tmsg.thread_id = t.id\n LEFT OUTER JOIN threads_members tm ON tm.thread_id = t.id\n WHERE t.id = ANY($1)\n GROUP BY t.id\n ",
"describe": {
"columns": [
{
@@ -25,16 +25,11 @@
},
{
"ordinal": 4,
"name": "show_in_mod_inbox",
"type_info": "Bool"
},
{
"ordinal": 5,
"name": "members",
"type_info": "Int8Array"
},
{
"ordinal": 6,
"ordinal": 5,
"name": "messages",
"type_info": "Jsonb"
}
@@ -49,10 +44,9 @@
false,
true,
true,
false,
null,
null
]
},
"hash": "93c0fdb2bdc9c57602671d50108957654ede51e944944d4af59fe1ba1f6a336e"
"hash": "21c44c435bf9a6c138d40cd40d70ccecfd09d877e84f3fbe5cd190dd69d3b7e1"
}

View File

@@ -1,15 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n UPDATE threads\n SET show_in_mod_inbox = $1\n WHERE id = $2\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Bool",
"Int8"
]
},
"nullable": []
},
"hash": "49813a96f007216072d69468aae705d73d5b85dcdd64a22060009b12d947ed5a"
}

View File

@@ -1,14 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n UPDATE threads\n SET show_in_mod_inbox = FALSE\n WHERE id = $1\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": []
},
"hash": "8f5e2a570cf35b2d158182bac37fd40bcec277bbdeddaece5efaa88600048a70"
}

View File

@@ -0,0 +1,18 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO threads_messages (\n id, author_id, body, thread_id, hide_identity\n )\n VALUES (\n $1, $2, $3, $4, $5\n )\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Int8",
"Jsonb",
"Int8",
"Bool"
]
},
"nullable": []
},
"hash": "9d46594c3dda50dc84defee87fa98210989dd59b06941a5e71b6661f059c9692"
}

View File

@@ -1,17 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO threads_messages (\n id, author_id, body, thread_id\n )\n VALUES (\n $1, $2, $3, $4\n )\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Int8",
"Jsonb",
"Int8"
]
},
"nullable": []
},
"hash": "b0c29c51bd3ae5b93d487471a98ee9bbb43a4df468ba781852b137dd315b9608"
}

View File

@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT tm.id, tm.author_id, tm.thread_id, tm.body, tm.created\n FROM threads_messages tm\n WHERE tm.id = ANY($1)\n ",
"query": "\n SELECT tm.id, tm.author_id, tm.thread_id, tm.body, tm.created, tm.hide_identity\n FROM threads_messages tm\n WHERE tm.id = ANY($1)\n ",
"describe": {
"columns": [
{
@@ -27,6 +27,11 @@
"ordinal": 4,
"name": "created",
"type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "hide_identity",
"type_info": "Bool"
}
],
"parameters": {
@@ -39,8 +44,9 @@
true,
false,
false,
false,
false
]
},
"hash": "320d73cd900a6e00f0e74b7a8c34a7658d16034b01a35558cb42fa9c16185eb5"
"hash": "caa4f261950f027cd34e2099e5489c02de214299004ea182f5eae93396e1d313"
}

View File

@@ -1,15 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n UPDATE threads\n SET show_in_mod_inbox = $1\n WHERE id = $2\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Bool",
"Int8"
]
},
"nullable": []
},
"hash": "e37ecb6dc1509d390bb6f68ba25899d19f693554d8969bbf8f8ee14a78adf0f9"
}

View File

@@ -1,20 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT id\n FROM threads\n WHERE show_in_mod_inbox = TRUE\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int8"
}
],
"parameters": {
"Left": []
},
"nullable": [
false
]
},
"hash": "e9d863c1793939d5ae7137d810f23d06460c28a9058b251448e3786c436f80cd"
}

View File

@@ -0,0 +1,9 @@
ALTER TABLE threads DROP COLUMN show_in_mod_inbox;
ALTER TABLE threads_messages ADD COLUMN hide_identity BOOLEAN default false NOT NULL;
UPDATE threads_messages
SET hide_identity = TRUE
FROM users
WHERE threads_messages.author_id = users.id
AND users.role IN ('moderator', 'admin');

View File

@@ -21,13 +21,13 @@ pub struct Thread {
pub messages: Vec<ThreadMessage>,
pub members: Vec<UserId>,
pub show_in_mod_inbox: bool,
}
pub struct ThreadMessageBuilder {
pub author_id: Option<UserId>,
pub body: MessageBody,
pub thread_id: ThreadId,
pub hide_identity: bool,
}
#[derive(Serialize, Deserialize, Clone)]
@@ -37,6 +37,7 @@ pub struct ThreadMessage {
pub author_id: Option<UserId>,
pub body: MessageBody,
pub created: DateTime<Utc>,
pub hide_identity: bool,
}
impl ThreadMessageBuilder {
@@ -49,16 +50,17 @@ impl ThreadMessageBuilder {
sqlx::query!(
"
INSERT INTO threads_messages (
id, author_id, body, thread_id
id, author_id, body, thread_id, hide_identity
)
VALUES (
$1, $2, $3, $4
$1, $2, $3, $4, $5
)
",
thread_message_id as ThreadMessageId,
self.author_id.map(|x| x.0),
serde_json::value::to_value(self.body.clone())?,
self.thread_id as ThreadId,
self.hide_identity
)
.execute(&mut **transaction)
.await?;
@@ -131,9 +133,9 @@ impl Thread {
let thread_ids_parsed: Vec<i64> = thread_ids.iter().map(|x| x.0).collect();
let threads = sqlx::query!(
"
SELECT t.id, t.thread_type, t.mod_id, t.report_id, t.show_in_mod_inbox,
SELECT t.id, t.thread_type, t.mod_id, t.report_id,
ARRAY_AGG(DISTINCT tm.user_id) filter (where tm.user_id is not null) members,
JSONB_AGG(DISTINCT jsonb_build_object('id', tmsg.id, 'author_id', tmsg.author_id, 'thread_id', tmsg.thread_id, 'body', tmsg.body, 'created', tmsg.created)) filter (where tmsg.id is not null) messages
JSONB_AGG(DISTINCT jsonb_build_object('id', tmsg.id, 'author_id', tmsg.author_id, 'thread_id', tmsg.thread_id, 'body', tmsg.body, 'created', tmsg.created, 'hide_identity', tmsg.hide_identity)) filter (where tmsg.id is not null) messages
FROM threads t
LEFT OUTER JOIN threads_messages tmsg ON tmsg.thread_id = t.id
LEFT OUTER JOIN threads_members tm ON tm.thread_id = t.id
@@ -159,7 +161,6 @@ impl Thread {
messages
},
members: x.members.unwrap_or_default().into_iter().map(UserId).collect(),
show_in_mod_inbox: x.show_in_mod_inbox,
}))
})
.try_collect::<Vec<Thread>>()
@@ -229,7 +230,7 @@ impl ThreadMessage {
let message_ids_parsed: Vec<i64> = message_ids.iter().map(|x| x.0).collect();
let messages = sqlx::query!(
"
SELECT tm.id, tm.author_id, tm.thread_id, tm.body, tm.created
SELECT tm.id, tm.author_id, tm.thread_id, tm.body, tm.created, tm.hide_identity
FROM threads_messages tm
WHERE tm.id = ANY($1)
",
@@ -244,6 +245,7 @@ impl ThreadMessage {
body: serde_json::from_value(x.body)
.unwrap_or(MessageBody::Deleted { private: false }),
created: x.created,
hide_identity: x.hide_identity,
}))
})
.try_collect::<Vec<ThreadMessage>>()

View File

@@ -30,8 +30,6 @@ pub enum LegacyMessageBody {
body: String,
#[serde(default)]
private: bool,
#[serde(default)]
hide_identity: bool,
replying_to: Option<ThreadMessageId>,
#[serde(default)]
associated_images: Vec<ImageId>,
@@ -76,13 +74,11 @@ impl From<crate::models::v3::threads::MessageBody> for LegacyMessageBody {
private,
replying_to,
associated_images,
hide_identity,
} => LegacyMessageBody::Text {
body,
private,
replying_to,
associated_images,
hide_identity,
},
crate::models::v3::threads::MessageBody::StatusChange {
new_status,

View File

@@ -32,6 +32,7 @@ pub struct ThreadMessage {
pub author_id: Option<UserId>,
pub body: MessageBody,
pub created: DateTime<Utc>,
pub hide_identity: bool,
}
#[derive(Serialize, Deserialize, Clone)]
@@ -41,8 +42,6 @@ pub enum MessageBody {
body: String,
#[serde(default)]
private: bool,
#[serde(default)]
hide_identity: bool,
replying_to: Option<ThreadMessageId>,
#[serde(default)]
associated_images: Vec<ImageId>,
@@ -116,24 +115,17 @@ impl Thread {
})
.map(|x| ThreadMessage {
id: x.id.into(),
author_id: if users
.iter()
.find(|y| x.author_id == Some(y.id.into()))
.map(|x| x.role.is_mod() && !user.role.is_mod())
.unwrap_or(false)
{
author_id: if x.hide_identity && !user.role.is_mod() {
None
} else {
x.author_id.map(|x| x.into())
},
body: x.body,
created: x.created,
hide_identity: x.hide_identity,
})
.collect(),
members: users
.into_iter()
.filter(|x| !x.role.is_mod() || user.role.is_mod())
.collect(),
members: users,
}
}
}

View File

@@ -612,7 +612,7 @@ impl AutomatedModerationQueue {
if !mod_messages.is_empty() {
let first_time = database::models::Thread::get(project.thread_id, &pool).await?
.map(|x| x.messages.iter().all(|x| match x.body { MessageBody::Text { hide_identity, .. } => x.author_id == Some(database::models::UserId(AUTOMOD_ID)) || hide_identity, _ => true}))
.map(|x| x.messages.iter().all(|x| x.author_id == Some(database::models::UserId(AUTOMOD_ID)) || x.hide_identity))
.unwrap_or(true);
let mut transaction = pool.begin().await?;
@@ -621,11 +621,11 @@ impl AutomatedModerationQueue {
body: MessageBody::Text {
body: mod_messages.markdown(true),
private: false,
hide_identity: false,
replying_to: None,
associated_images: vec![],
},
thread_id: project.thread_id,
hide_identity: false,
}
.insert(&mut transaction)
.await?;
@@ -645,6 +645,7 @@ impl AutomatedModerationQueue {
old_status: project.inner.status,
},
thread_id: project.thread_id,
hide_identity: false,
}
.insert(&mut transaction)
.await?;
@@ -733,11 +734,11 @@ impl AutomatedModerationQueue {
body: MessageBody::Text {
body: str,
private: true,
hide_identity: false,
replying_to: None,
associated_images: vec![],
},
thread_id: project.thread_id,
hide_identity: false,
}
.insert(&mut transaction)
.await?;

View File

@@ -14,10 +14,8 @@ use sqlx::PgPool;
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("thread")
.service(moderation_inbox)
.service(thread_get)
.service(thread_send_message)
.service(thread_read),
.service(thread_send_message),
);
cfg.service(web::scope("message").service(message_delete));
cfg.service(threads_get);
@@ -102,44 +100,6 @@ pub async fn thread_send_message(
.or_else(v2_reroute::flatten_404_error)
}
#[get("inbox")]
pub async fn moderation_inbox(
req: HttpRequest,
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let response = v3::threads::moderation_inbox(req, pool, redis, session_queue)
.await
.or_else(v2_reroute::flatten_404_error)?;
// Convert response to V2 format
match v2_reroute::extract_ok_json::<Vec<Thread>>(response).await {
Ok(threads) => {
let threads = threads
.into_iter()
.map(LegacyThread::from)
.collect::<Vec<_>>();
Ok(HttpResponse::Ok().json(threads))
}
Err(response) => Ok(response),
}
}
#[post("{id}/read")]
pub async fn thread_read(
req: HttpRequest,
info: web::Path<(ThreadId,)>,
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
// Returns NoContent, so we don't need to convert the response
v3::threads::thread_read(req, info, pool, redis, session_queue)
.await
.or_else(v2_reroute::flatten_404_error)
}
#[delete("{id}")]
pub async fn message_delete(
req: HttpRequest,

View File

@@ -615,14 +615,14 @@ async fn project_create_inner(
let mut members = vec![];
if let Some(organization_id) = project_create_data.organization_id {
let org = models::Organization::get_id(organization_id.into(), &*pool, &redis)
let org = models::Organization::get_id(organization_id.into(), pool, redis)
.await?
.ok_or_else(|| {
CreateError::InvalidInput("Invalid organization ID specified!".to_string())
})?;
let team_member =
models::TeamMember::get_from_user_id(org.team_id, current_user.id.into(), &*pool)
models::TeamMember::get_from_user_id(org.team_id, current_user.id.into(), pool)
.await?;
let perms =

View File

@@ -355,17 +355,6 @@ pub async fn project_edit(
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
UPDATE threads
SET show_in_mod_inbox = FALSE
WHERE id = $1
",
project_item.thread_id as db_ids::ThreadId,
)
.execute(&mut *transaction)
.await?;
moderation_queue
.projects
.insert(project_item.inner.id.into());
@@ -464,6 +453,7 @@ pub async fn project_edit(
old_status: project_item.inner.status,
},
thread_id: project_item.thread_id,
hide_identity: true,
}
.insert(&mut transaction)
.await?;

View File

@@ -435,6 +435,7 @@ pub async fn report_edit(
MessageBody::ThreadClosure
},
thread_id: report.thread_id,
hide_identity: true,
}
.insert(&mut transaction)
.await?;
@@ -450,18 +451,6 @@ pub async fn report_edit(
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
UPDATE threads
SET show_in_mod_inbox = $1
WHERE id = $2
",
!(edit_closed || report.closed),
report.thread_id.0,
)
.execute(&mut *transaction)
.await?;
}
// delete any images no longer in the body

View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use crate::auth::{check_is_moderator_from_headers, get_user_from_headers};
use crate::auth::get_user_from_headers;
use crate::database;
use crate::database::models::image_item;
use crate::database::models::notification_item::NotificationBuilder;
@@ -24,10 +24,8 @@ use sqlx::PgPool;
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("thread")
.route("inbox", web::get().to(moderation_inbox))
.route("{id}", web::get().to(thread_get))
.route("{id}", web::post().to(thread_send_message))
.route("{id}/read", web::post().to(thread_read)),
.route("{id}", web::post().to(thread_send_message)),
);
cfg.service(web::scope("message").route("{id}", web::delete().to(message_delete)));
cfg.route("threads", web::get().to(threads_get));
@@ -252,7 +250,13 @@ pub async fn filter_authorized_threads(
&mut thread
.messages
.iter()
.filter_map(|x| x.author_id)
.filter_map(|x| {
if x.hide_identity && !user.role.is_mod() {
None
} else {
x.author_id
}
})
.collect::<Vec<_>>(),
);
@@ -299,7 +303,13 @@ pub async fn thread_get(
&mut data
.messages
.iter()
.filter_map(|x| x.author_id)
.filter_map(|x| {
if x.hide_identity && !user.role.is_mod() {
None
} else {
x.author_id
}
})
.collect::<Vec<_>>(),
);
@@ -379,7 +389,6 @@ pub async fn thread_send_message(
body,
replying_to,
private,
hide_identity,
..
} = &new_message.body
{
@@ -395,12 +404,6 @@ pub async fn thread_send_message(
));
}
if *hide_identity && !user.role.is_mod() {
return Err(ApiError::InvalidInput(
"You are not allowed to send masked messages!".to_string(),
));
}
if let Some(replying_to) = replying_to {
let thread_message =
database::models::ThreadMessage::get((*replying_to).into(), &**pool).await?;
@@ -436,11 +439,12 @@ pub async fn thread_send_message(
author_id: Some(user.id.into()),
body: new_message.body.clone(),
thread_id: thread.id,
hide_identity: user.role.is_mod(),
}
.insert(&mut transaction)
.await?;
let mod_notif = if let Some(project_id) = thread.project_id {
if let Some(project_id) = thread.project_id {
let project = database::models::Project::get_id(project_id, &**pool, &redis).await?;
if let Some(project) = project {
@@ -468,8 +472,6 @@ pub async fn thread_send_message(
.await?;
}
}
!user.role.is_mod()
} else if let Some(report_id) = thread.report_id {
let report = database::models::report_item::Report::get(report_id, &**pool).await?;
@@ -493,23 +495,7 @@ pub async fn thread_send_message(
.await?;
}
}
!user.role.is_mod()
} else {
false
};
sqlx::query!(
"
UPDATE threads
SET show_in_mod_inbox = $1
WHERE id = $2
",
mod_notif,
thread.id.0,
)
.execute(&mut *transaction)
.await?;
}
if let MessageBody::Text {
associated_images, ..
@@ -559,72 +545,6 @@ pub async fn thread_send_message(
}
}
pub async fn moderation_inbox(
req: HttpRequest,
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
let user = check_is_moderator_from_headers(
&req,
&**pool,
&redis,
&session_queue,
Some(&[Scopes::THREAD_READ]),
)
.await?;
let ids = sqlx::query!(
"
SELECT id
FROM threads
WHERE show_in_mod_inbox = TRUE
"
)
.fetch_many(&**pool)
.try_filter_map(|e| async { Ok(e.right().map(|m| database::models::ThreadId(m.id))) })
.try_collect::<Vec<database::models::ThreadId>>()
.await?;
let threads_data = database::models::Thread::get_many(&ids, &**pool).await?;
let threads = filter_authorized_threads(threads_data, &user, &pool, &redis).await?;
Ok(HttpResponse::Ok().json(threads))
}
pub async fn thread_read(
req: HttpRequest,
info: web::Path<(ThreadId,)>,
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
check_is_moderator_from_headers(
&req,
&**pool,
&redis,
&session_queue,
Some(&[Scopes::THREAD_READ]),
)
.await?;
let id = info.into_inner().0;
let mut transaction = pool.begin().await?;
sqlx::query!(
"
UPDATE threads
SET show_in_mod_inbox = FALSE
WHERE id = $1
",
id.0 as i64,
)
.execute(&mut *transaction)
.await?;
transaction.commit().await?;
Ok(HttpResponse::NoContent().body(""))
}
pub async fn message_delete(
req: HttpRequest,
info: web::Path<(ThreadMessageId,)>,

View File

@@ -836,43 +836,6 @@ pub async fn thread_scopes() {
.test(req_gen, thread_write)
.await
.unwrap();
// Check moderation inbox
// Uses moderator PAT, as only moderators can see the moderation inbox
let req_gen =
|pat: Option<String>| async move { api.get_moderation_inbox(pat.as_deref()).await };
let (_, success) = ScopeTest::new(&test_env)
.with_user_id(MOD_USER_ID_PARSED)
.test(req_gen, thread_read)
.await
.unwrap();
let thread_id: &str = success[0]["id"].as_str().unwrap();
// Moderator 'read' thread
// Uses moderator PAT, as only moderators can see the moderation inbox
let req_gen =
|pat: Option<String>| async move { api.read_thread(thread_id, pat.as_deref()).await };
ScopeTest::new(&test_env)
.with_user_id(MOD_USER_ID_PARSED)
.test(req_gen, thread_read)
.await
.unwrap();
// Delete that message
// First, get message id
let resp = api.get_thread(thread_id, USER_USER_PAT).await;
let success: serde_json::Value = test::read_body_json(resp).await;
let thread_message_id = success["messages"][0]["id"].as_str().unwrap();
let req_gen = |pat: Option<String>| async move {
api.delete_thread_message(thread_message_id, pat.as_deref())
.await
};
ScopeTest::new(&test_env)
.with_user_id(MOD_USER_ID_PARSED)
.test(req_gen, thread_write)
.await
.unwrap();
})
.await;
}