Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(channel): let an external contact attach only files they uploaded (#23336)

Arvin Xu1fba3b0935e1
diff --git a/packages/database/src/repositories/channel/__tests__/peer.test.ts b/packages/database/src/repositories/channel/__tests__/peer.test.ts
index 03cc7a9fbb8..0a4cacaf83d 100644
--- a/packages/database/src/repositories/channel/__tests__/peer.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/peer.test.ts
@@ -4,7 +4,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { getTestDB } from '../../../core/getTestDB';
import { ContactModel } from '../../../models/contact';
-import { agents, userContacts, users, workspaceMembers } from '../../../schemas';
+import { agents, files, userContacts, users, workspaceMembers } from '../../../schemas';
import { ChannelRepository } from '..';
import { createChannelFixture, jobsOf } from './testUtils';
@@ -191,6 +191,40 @@ describe('a conversation with an external contact of a workspace', () => {
expect(await inTeam(carol).openPeer(bob, 'Mia')).toBeDefined();
});
+ it('lets them attach only files they uploaded, never one of the workspace’s', async () => {
+ await inviteBob();
+ const upload = async (userId: string, workspaceId: string | null) => {
+ const [file] = await db
+ .insert(files)
+ .values({
+ fileType: 'text/plain',
+ name: 'f.txt',
+ size: 1,
+ url: 'files/f',
+ userId,
+ workspaceId,
+ })
+ .returning();
+ return file.id;
+ };
+ // A public file of the workspace, which any member may attach.
+ const teamFile = await upload(carol, fixture.workspaceId);
+ const { channel } = await inTeam(bob).openPeer(alice, 'Olivia');
+ const send = (userId: string, fileId: string) =>
+ inTeam(userId).send(channel.id, {
+ content: 'See this',
+ fileIds: [fileId],
+ requestKey: `${userId}-${fileId}`,
+ routing: 'fallback',
+ });
+
+ await expect(send(bob, teamFile)).rejects.toThrow('One or more attachments are unavailable');
+ // His own upload, which the file router stores in his personal space, goes through.
+ expect(await send(bob, await upload(bob, null))).toBeDefined();
+ // A member still attaches the workspace's file.
+ expect(await send(alice, teamFile)).toBeDefined();
+ });
+
it('goes silent once the member closes themselves to them', async () => {
await inviteBob();
const { channel } = await inTeam(alice).openPeer(bob, 'Mia');
diff --git a/packages/database/src/repositories/channel/peer.ts b/packages/database/src/repositories/channel/peer.ts
index 8d2b7123604..7e87ed8a64c 100644
--- a/packages/database/src/repositories/channel/peer.ts
+++ b/packages/database/src/repositories/channel/peer.ts
@@ -32,7 +32,8 @@ const areMutualContacts = async (
return rows.length >= 2;
};
-const isWorkspaceMember = async (tx: Transaction, workspaceId: string, userId: string) => {
+/** Whether `userId` is an active member of the workspace. */
+export const isWorkspaceMember = async (tx: Transaction, workspaceId: string, userId: string) => {
const [member] = await tx
.select({ userId: workspaceMembers.userId })
.from(workspaceMembers)
diff --git a/packages/database/src/repositories/channel/send.ts b/packages/database/src/repositories/channel/send.ts
index b1a617a37fb..df7bf7b43ca 100644
--- a/packages/database/src/repositories/channel/send.ts
+++ b/packages/database/src/repositories/channel/send.ts
@@ -18,7 +18,7 @@ import {
pairedAgent,
} from './delivery';
import { ChannelError } from './error';
-import { peerOf, requirePeerRelation } from './peer';
+import { isWorkspaceMember, peerOf, requirePeerRelation } from './peer';
import { audit } from './transaction';
import type { ChannelAccess } from './types';
import { createChannelThread } from './view';
@@ -187,6 +187,10 @@ export const sendMessage = async (
}
if (fileIds.length) {
+ // A workspace's public files are its members'. An external contact talking in it attaches
+ // only files they uploaded themselves, never one of the workspace's by its id.
+ const external =
+ !!channel.workspaceId && !(await isWorkspaceMember(tx, channel.workspaceId, member.userId!));
// Lock in a stable order so concurrent sends and file cleanup cannot deadlock.
const accessible = await tx
.select({ id: files.id })
@@ -194,10 +198,12 @@ export const sendMessage = async (
.where(
and(
inArray(files.id, fileIds),
- buildWorkspaceWhere(
- { userId: member.userId!, workspaceId: channel.workspaceId ?? undefined },
- files,
- ),
+ external
+ ? eq(files.userId, member.userId!)
+ : buildWorkspaceWhere(
+ { userId: member.userId!, workspaceId: channel.workspaceId ?? undefined },
+ files,
+ ),
),
)
.orderBy(asc(files.id))