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.

✨ feat(share): let signed-out visitors read an agent share profile (#23719)

YuTengjing2cd5111081e0
diff --git a/apps/server/src/routers/lambda/__tests__/share.test.ts b/apps/server/src/routers/lambda/__tests__/share.test.ts
index d73536c62f6..68cb9fffa25 100644
--- a/apps/server/src/routers/lambda/__tests__/share.test.ts
+++ b/apps/server/src/routers/lambda/__tests__/share.test.ts
@@ -357,16 +357,55 @@ describe('shareRouter', () => {
expect(result.featuredWorks).toEqual([]);
});
- it('requires authentication without resolving or counting the share', async () => {
+ it('serves a signed-out visitor the landing data without counting the view', async () => {
const caller = shareRouter.createCaller(await createContextInner());
- await expect(caller.getSharedAgent({ slugOrId: 'shared-agent' })).rejects.toMatchObject({
- code: 'UNAUTHORIZED',
+ await expect(caller.getSharedAgent({ slugOrId: 'shared-agent' })).resolves.toMatchObject({
+ isOwner: false,
+ ownerWorkspaceSlug: null,
+ shareId: 'agent-share-1',
});
- expect(AgentShareModel.findBySlugOrId).not.toHaveBeenCalled();
+ expect(AgentShareModel.assertShareAccess).toHaveBeenCalledWith(
+ expect.objectContaining({ shareId: 'agent-share-1' }),
+ undefined,
+ );
expect(AgentShareModel.incrementUserViewCount).not.toHaveBeenCalled();
+ expect(listFeaturedWorks).toHaveBeenCalled();
+ });
+
+ it('hides a private share from a signed-out visitor', async () => {
+ vi.mocked(AgentShareModel.findBySlugOrId).mockResolvedValue({
+ ...agentShare,
+ visibility: 'private',
+ } as any);
+ vi.mocked(AgentShareModel.assertShareAccess).mockImplementationOnce(() => {
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Share not found' });
+ });
+ const caller = shareRouter.createCaller(await createContextInner());
+
+ await expect(caller.getSharedAgent({ slugOrId: 'shared-agent' })).rejects.toMatchObject({
+ code: 'NOT_FOUND',
+ });
expect(listFeaturedWorks).not.toHaveBeenCalled();
- expect(getDeliveryStats).not.toHaveBeenCalled();
+ });
+
+ it('reads a sandbox operation token as signed out, even for the owner', async () => {
+ const caller = shareRouter.createCaller(
+ await createContextInner({
+ oidcAuth: { purpose: 'hetero-operation', sub: 'owner-user' } as any,
+ userId: 'owner-user',
+ }),
+ );
+
+ await expect(caller.getSharedAgent({ slugOrId: 'shared-agent' })).resolves.toMatchObject({
+ isOwner: false,
+ ownerWorkspaceSlug: null,
+ });
+ expect(AgentShareModel.assertShareAccess).toHaveBeenCalledWith(
+ expect.objectContaining({ shareId: 'agent-share-1' }),
+ null,
+ );
+ expect(AgentShareModel.incrementUserViewCount).not.toHaveBeenCalled();
});
it('resolves by slug, returns only visitor-safe metadata, and counts the view', async () => {
diff --git a/apps/server/src/routers/lambda/share.ts b/apps/server/src/routers/lambda/share.ts
index e29173effc3..512502bbc59 100644
--- a/apps/server/src/routers/lambda/share.ts
+++ b/apps/server/src/routers/lambda/share.ts
@@ -24,7 +24,7 @@ import { MessageModel } from '@/database/models/message';
import { TopicModel } from '@/database/models/topic';
import { TopicShareModel } from '@/database/models/topicShare';
import type { LobeChatDatabase } from '@/database/type';
-import { authedProcedure, publicProcedure, router } from '@/libs/trpc/lambda';
+import { publicProcedure, router } from '@/libs/trpc/lambda';
import { serverDatabase } from '@/libs/trpc/lambda/middleware';
import { resolveModelMediaCapabilities } from '@/server/modules/AgentRuntime/resolveModelMediaCapabilities';
import { AgentService } from '@/server/services/agent';
@@ -146,7 +146,13 @@ export const shareRouter = router({
}),
/**
* Resolve the visitor-facing metadata for an agent share, by its custom
- * slug or its raw share id, after enforcing signed-in access.
+ * slug or its raw share id.
+ *
+ * Public so a signed-out visitor can read the share's landing page before
+ * deciding to sign in; starting a conversation still requires a session
+ * (`channel.openShared` is authed). Everything returned is computed as the
+ * share OWNER, never as the caller, so an anonymous caller sees exactly what
+ * a signed-in stranger sees.
*
* `findBySlugOrId` intentionally does NOT enforce visibility — it resolves
* whatever share matches, of ANY visibility; the (private → owner only,
@@ -156,7 +162,7 @@ export const shareRouter = router({
* Deployment support applies to every viewer, including owner previews.
* The rollout flag only gates publishing, never access to an existing share.
*/
- getSharedAgent: authedProcedure
+ getSharedAgent: publicProcedure
.use(serverDatabase)
.input(z.object({ slugOrId: z.string().trim().min(1) }))
.query(async ({ input, ctx }): Promise<SharedAgentData> => {
@@ -168,13 +174,20 @@ export const shareRouter = router({
throw new TRPCError({ code: 'NOT_FOUND', message: 'Share not found' });
}
- AgentShareModel.assertShareAccess(share, ctx.userId);
+ // A public procedure skips `oidcAuth`, which confines a sandbox
+ // `hetero-operation` token to its own routes. Read such a caller as
+ // signed out so the token cannot unlock the owner's private shares.
+ const viewerId = ctx.oidcAuth?.purpose === 'hetero-operation' ? null : ctx.userId;
+
+ AgentShareModel.assertShareAccess(share, viewerId);
- const isOwner = share.ownerId === ctx.userId;
+ const isOwner = !!viewerId && share.ownerId === viewerId;
- if (!isOwner) {
- // Owner previews are not counted: userViewCount tracks visitor page
- // views (PV, not deduplicated visitors). The counter is analytics
+ if (!isOwner && viewerId) {
+ // Owner previews are not counted: userViewCount tracks signed-in
+ // visitor page views (PV, not deduplicated visitors). Anonymous loads
+ // are not counted either, so an unauthenticated client cannot inflate
+ // the counter. The counter is analytics
// only, so it is best-effort: a failed increment must never turn an
// otherwise valid share page into an error for the visitor.
try {
diff --git a/packages/database/src/models/__tests__/agentShare.test.ts b/packages/database/src/models/__tests__/agentShare.test.ts
index e0b18653574..d67c5c8ca5f 100644
--- a/packages/database/src/models/__tests__/agentShare.test.ts
+++ b/packages/database/src/models/__tests__/agentShare.test.ts
@@ -800,6 +800,19 @@ describe('AgentShareModel', () => {
expect(share.shareId).toBe(created!.id);
});
+ it('lets a signed-out viewer read a link share and nothing else', async () => {
+ const link = await agentShareModel.create(agentId, 'link');
+ const linkShare = await AgentShareModel.findByShareId(serverDB, link!.id);
+
+ expect(() => AgentShareModel.assertShareAccess(linkShare!, undefined)).not.toThrow();
+ // An empty viewer must never match the owner, even a row with no owner id.
+ for (const visibility of ['private', 'card'] as const) {
+ expect(() =>
+ AgentShareModel.assertShareAccess({ ownerId: '', visibility }, undefined),
+ ).toThrow(expect.objectContaining({ code: 'NOT_FOUND' }));
+ }
+ });
+
it('throws NOT_FOUND for an unknown UUID', async () => {
await expect(
AgentShareModel.findByShareIdWithAccessCheck(
diff --git a/packages/database/src/models/agentShare.ts b/packages/database/src/models/agentShare.ts
index ccbe9478c5e..53ab365204e 100644
--- a/packages/database/src/models/agentShare.ts
+++ b/packages/database/src/models/agentShare.ts
@@ -847,12 +847,14 @@ export class AgentShareModel {
* Kept as the single access-check implementation so callers that already
* hold a resolved row (e.g. via `findBySlugOrId`) don't need a second
* lookup just to reuse the gate.
+ *
+ * `viewerId` is empty for a signed-out caller, which is never the owner.
*/
static assertShareAccess = (
share: Pick<AgentShareData, 'ownerId' | 'visibility'>,
- viewerId: string,
+ viewerId: string | null | undefined,
): void => {
- const isOwner = viewerId === share.ownerId;
+ const isOwner = !!viewerId && viewerId === share.ownerId;
// NOT_FOUND, not FORBIDDEN: a paused (private) share must be
// indistinguishable from a share that never existed, otherwise a
// stranger can probe custom slugs / ids to learn which private shares
diff --git a/packages/types/src/agent/share.ts b/packages/types/src/agent/share.ts
index 53917973a46..f8d037e51f0 100644
--- a/packages/types/src/agent/share.ts
+++ b/packages/types/src/agent/share.ts
@@ -94,7 +94,7 @@ export interface SharedAgentStats extends SharedAgentDeliveryStats {
visitors: number;
}
-/** Agent metadata exposed to signed-in visitors of an agent share. */
+/** Agent metadata exposed to visitors of an agent share, including signed-out ones. */
export interface SharedAgentUploadAbility {
audio: boolean;
image: boolean;